Fix potentially illegal shift and assign operation in CSKY disassembler.
[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"
4de283e4
TT
44#include "filenames.h" /* for DOSish file names */
45#include "macrotab.h"
46#include "language.h"
47#include "complaints.h"
d55e5aa6
TT
48#include "dwarf2expr.h"
49#include "dwarf2loc.h"
4de283e4
TT
50#include "cp-support.h"
51#include "hashtab.h"
52#include "command.h"
d55e5aa6 53#include "gdbcmd.h"
4de283e4
TT
54#include "block.h"
55#include "addrmap.h"
56#include "typeprint.h"
57#include "psympriv.h"
4de283e4 58#include "c-lang.h"
d55e5aa6 59#include "go-lang.h"
4de283e4
TT
60#include "valprint.h"
61#include "gdbcore.h" /* for gnutarget */
62#include "gdb/gdb-index.h"
4de283e4
TT
63#include "gdb_bfd.h"
64#include "f-lang.h"
65#include "source.h"
4de283e4 66#include "build-id.h"
d55e5aa6 67#include "namespace.h"
268a13a5
TT
68#include "gdbsupport/function-view.h"
69#include "gdbsupport/gdb_optional.h"
70#include "gdbsupport/underlying.h"
268a13a5 71#include "gdbsupport/hash_enum.h"
4de283e4 72#include "filename-seen-cache.h"
b32b108a 73#include "producer.h"
4de283e4 74#include <fcntl.h>
4de283e4 75#include <algorithm>
4de283e4 76#include <unordered_map>
268a13a5 77#include "gdbsupport/selftest.h"
c9317f21 78#include "rust-lang.h"
268a13a5 79#include "gdbsupport/pathstuff.h"
437afbb8 80
73be47f5
DE
81/* When == 1, print basic high level tracing messages.
82 When > 1, be more verbose.
b4f54984
DE
83 This is in contrast to the low level DIE reading of dwarf_die_debug. */
84static unsigned int dwarf_read_debug = 0;
45cfd468 85
d97bc12b 86/* When non-zero, dump DIEs after they are read in. */
b4f54984 87static unsigned int dwarf_die_debug = 0;
d97bc12b 88
27e0867f
DE
89/* When non-zero, dump line number entries as they are read in. */
90static unsigned int dwarf_line_debug = 0;
91
491144b5
CB
92/* When true, cross-check physname against demangler. */
93static bool check_physname = false;
900e11f9 94
491144b5
CB
95/* When true, do not reject deprecated .gdb_index sections. */
96static bool use_deprecated_index_sections = false;
481860b3 97
5bfd760d 98static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
6502dd73 99
f1e6e072
TT
100/* The "aclass" indices for various kinds of computed DWARF symbols. */
101
102static int dwarf2_locexpr_index;
103static int dwarf2_loclist_index;
104static int dwarf2_locexpr_block_index;
105static int dwarf2_loclist_block_index;
106
3f563c84
PA
107/* An index into a (C++) symbol name component in a symbol name as
108 recorded in the mapped_index's symbol table. For each C++ symbol
109 in the symbol table, we record one entry for the start of each
110 component in the symbol in a table of name components, and then
111 sort the table, in order to be able to binary search symbol names,
112 ignoring leading namespaces, both completion and regular look up.
113 For example, for symbol "A::B::C", we'll have an entry that points
114 to "A::B::C", another that points to "B::C", and another for "C".
115 Note that function symbols in GDB index have no parameter
116 information, just the function/method names. You can convert a
117 name_component to a "const char *" using the
118 'mapped_index::symbol_name_at(offset_type)' method. */
119
120struct name_component
121{
122 /* Offset in the symbol name where the component starts. Stored as
123 a (32-bit) offset instead of a pointer to save memory and improve
124 locality on 64-bit architectures. */
125 offset_type name_offset;
126
127 /* The symbol's index in the symbol and constant pool tables of a
128 mapped_index. */
129 offset_type idx;
130};
131
44ed8f3e
PA
132/* Base class containing bits shared by both .gdb_index and
133 .debug_name indexes. */
134
135struct mapped_index_base
136{
22ca247e
TT
137 mapped_index_base () = default;
138 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
139
44ed8f3e
PA
140 /* The name_component table (a sorted vector). See name_component's
141 description above. */
142 std::vector<name_component> name_components;
143
144 /* How NAME_COMPONENTS is sorted. */
145 enum case_sensitivity name_components_casing;
146
147 /* Return the number of names in the symbol table. */
148 virtual size_t symbol_name_count () const = 0;
149
150 /* Get the name of the symbol at IDX in the symbol table. */
151 virtual const char *symbol_name_at (offset_type idx) const = 0;
152
153 /* Return whether the name at IDX in the symbol table should be
154 ignored. */
155 virtual bool symbol_name_slot_invalid (offset_type idx) const
156 {
157 return false;
158 }
159
160 /* Build the symbol name component sorted vector, if we haven't
161 yet. */
162 void build_name_components ();
163
164 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
165 possible matches for LN_NO_PARAMS in the name component
166 vector. */
167 std::pair<std::vector<name_component>::const_iterator,
168 std::vector<name_component>::const_iterator>
3b00ef10
TT
169 find_name_components_bounds (const lookup_name_info &ln_no_params,
170 enum language lang) const;
44ed8f3e
PA
171
172 /* Prevent deleting/destroying via a base class pointer. */
173protected:
174 ~mapped_index_base() = default;
175};
176
9291a0cd
TT
177/* A description of the mapped index. The file format is described in
178 a comment by the code that writes the index. */
fc898b42 179struct mapped_index final : public mapped_index_base
9291a0cd 180{
f00a2de2
PA
181 /* A slot/bucket in the symbol table hash. */
182 struct symbol_table_slot
183 {
184 const offset_type name;
185 const offset_type vec;
186 };
187
559a7a62 188 /* Index data format version. */
3063847f 189 int version = 0;
559a7a62 190
f00a2de2
PA
191 /* The address table data. */
192 gdb::array_view<const gdb_byte> address_table;
b11b1f88 193
3876f04e 194 /* The symbol table, implemented as a hash table. */
f00a2de2 195 gdb::array_view<symbol_table_slot> symbol_table;
b11b1f88 196
9291a0cd 197 /* A pointer to the constant pool. */
3063847f 198 const char *constant_pool = nullptr;
3f563c84 199
44ed8f3e
PA
200 bool symbol_name_slot_invalid (offset_type idx) const override
201 {
202 const auto &bucket = this->symbol_table[idx];
9ab08412 203 return bucket.name == 0 && bucket.vec == 0;
44ed8f3e 204 }
5c58de74 205
3f563c84
PA
206 /* Convenience method to get at the name of the symbol at IDX in the
207 symbol table. */
44ed8f3e 208 const char *symbol_name_at (offset_type idx) const override
f00a2de2 209 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
5c58de74 210
44ed8f3e
PA
211 size_t symbol_name_count () const override
212 { return this->symbol_table.size (); }
9291a0cd
TT
213};
214
927aa2e7
JK
215/* A description of the mapped .debug_names.
216 Uninitialized map has CU_COUNT 0. */
fc898b42 217struct mapped_debug_names final : public mapped_index_base
927aa2e7 218{
ed2dc618
SM
219 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
220 : dwarf2_per_objfile (dwarf2_per_objfile_)
221 {}
222
223 struct dwarf2_per_objfile *dwarf2_per_objfile;
927aa2e7
JK
224 bfd_endian dwarf5_byte_order;
225 bool dwarf5_is_dwarf64;
226 bool augmentation_is_gdb;
227 uint8_t offset_size;
228 uint32_t cu_count = 0;
229 uint32_t tu_count, bucket_count, name_count;
230 const gdb_byte *cu_table_reordered, *tu_table_reordered;
231 const uint32_t *bucket_table_reordered, *hash_table_reordered;
232 const gdb_byte *name_table_string_offs_reordered;
233 const gdb_byte *name_table_entry_offs_reordered;
234 const gdb_byte *entry_pool;
235
236 struct index_val
237 {
238 ULONGEST dwarf_tag;
239 struct attr
240 {
241 /* Attribute name DW_IDX_*. */
242 ULONGEST dw_idx;
243
244 /* Attribute form DW_FORM_*. */
245 ULONGEST form;
246
247 /* Value if FORM is DW_FORM_implicit_const. */
248 LONGEST implicit_const;
249 };
250 std::vector<attr> attr_vec;
251 };
252
253 std::unordered_map<ULONGEST, index_val> abbrev_map;
254
255 const char *namei_to_name (uint32_t namei) const;
44ed8f3e
PA
256
257 /* Implementation of the mapped_index_base virtual interface, for
258 the name_components cache. */
259
260 const char *symbol_name_at (offset_type idx) const override
261 { return namei_to_name (idx); }
262
263 size_t symbol_name_count () const override
264 { return this->name_count; }
927aa2e7
JK
265};
266
cd4fb1b2 267/* See dwarf2read.h. */
ed2dc618 268
cd4fb1b2 269dwarf2_per_objfile *
ed2dc618
SM
270get_dwarf2_per_objfile (struct objfile *objfile)
271{
5bfd760d 272 return dwarf2_objfile_data_key.get (objfile);
ed2dc618 273}
c906108c 274
251d32d9 275/* Default names of the debugging sections. */
c906108c 276
233a11ab
CS
277/* Note that if the debugging section has been compressed, it might
278 have a name like .zdebug_info. */
279
9cdd5dbd
DE
280static const struct dwarf2_debug_sections dwarf2_elf_names =
281{
251d32d9
TG
282 { ".debug_info", ".zdebug_info" },
283 { ".debug_abbrev", ".zdebug_abbrev" },
284 { ".debug_line", ".zdebug_line" },
285 { ".debug_loc", ".zdebug_loc" },
43988095 286 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 287 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 288 { ".debug_macro", ".zdebug_macro" },
251d32d9 289 { ".debug_str", ".zdebug_str" },
43988095 290 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 291 { ".debug_ranges", ".zdebug_ranges" },
43988095 292 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 293 { ".debug_types", ".zdebug_types" },
3019eac3 294 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
295 { ".debug_frame", ".zdebug_frame" },
296 { ".eh_frame", NULL },
24d3216f 297 { ".gdb_index", ".zgdb_index" },
927aa2e7
JK
298 { ".debug_names", ".zdebug_names" },
299 { ".debug_aranges", ".zdebug_aranges" },
24d3216f 300 23
251d32d9 301};
c906108c 302
80626a55 303/* List of DWO/DWP sections. */
3019eac3 304
80626a55 305static const struct dwop_section_names
3019eac3
DE
306{
307 struct dwarf2_section_names abbrev_dwo;
308 struct dwarf2_section_names info_dwo;
309 struct dwarf2_section_names line_dwo;
310 struct dwarf2_section_names loc_dwo;
43988095 311 struct dwarf2_section_names loclists_dwo;
09262596
DE
312 struct dwarf2_section_names macinfo_dwo;
313 struct dwarf2_section_names macro_dwo;
3019eac3
DE
314 struct dwarf2_section_names str_dwo;
315 struct dwarf2_section_names str_offsets_dwo;
316 struct dwarf2_section_names types_dwo;
80626a55
DE
317 struct dwarf2_section_names cu_index;
318 struct dwarf2_section_names tu_index;
3019eac3 319}
80626a55 320dwop_section_names =
3019eac3
DE
321{
322 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
323 { ".debug_info.dwo", ".zdebug_info.dwo" },
324 { ".debug_line.dwo", ".zdebug_line.dwo" },
325 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 326 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
327 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
328 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
329 { ".debug_str.dwo", ".zdebug_str.dwo" },
330 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
331 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
332 { ".debug_cu_index", ".zdebug_cu_index" },
333 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
334};
335
c906108c
SS
336/* local data types */
337
107d2387
AC
338/* The data in a compilation unit header, after target2host
339 translation, looks like this. */
c906108c 340struct comp_unit_head
a738430d 341{
c764a876 342 unsigned int length;
a738430d 343 short version;
a738430d
MK
344 unsigned char addr_size;
345 unsigned char signed_addr_p;
9c541725 346 sect_offset abbrev_sect_off;
57349743 347
a738430d
MK
348 /* Size of file offsets; either 4 or 8. */
349 unsigned int offset_size;
57349743 350
a738430d
MK
351 /* Size of the length field; either 4 or 12. */
352 unsigned int initial_length_size;
57349743 353
43988095
JK
354 enum dwarf_unit_type unit_type;
355
a738430d
MK
356 /* Offset to the first byte of this compilation unit header in the
357 .debug_info section, for resolving relative reference dies. */
9c541725 358 sect_offset sect_off;
57349743 359
d00adf39
DE
360 /* Offset to first die in this cu from the start of the cu.
361 This will be the first byte following the compilation unit header. */
9c541725 362 cu_offset first_die_cu_offset;
43988095 363
a084a2a6
AT
364
365 /* 64-bit signature of this unit. For type units, it denotes the signature of
366 the type (DW_UT_type in DWARF 4, additionally DW_UT_split_type in DWARF 5).
367 Also used in DWARF 5, to denote the dwo id when the unit type is
368 DW_UT_skeleton or DW_UT_split_compile. */
43988095
JK
369 ULONGEST signature;
370
371 /* For types, offset in the type's DIE of the type defined by this TU. */
9c541725 372 cu_offset type_cu_offset_in_tu;
a738430d 373};
c906108c 374
3da10d80
KS
375/* Type used for delaying computation of method physnames.
376 See comments for compute_delayed_physnames. */
377struct delayed_method_info
378{
379 /* The type to which the method is attached, i.e., its parent class. */
380 struct type *type;
381
382 /* The index of the method in the type's function fieldlists. */
383 int fnfield_index;
384
385 /* The index of the method in the fieldlist. */
386 int index;
387
388 /* The name of the DIE. */
389 const char *name;
390
391 /* The DIE associated with this method. */
392 struct die_info *die;
393};
394
e7c27a73
DJ
395/* Internal state when decoding a particular compilation unit. */
396struct dwarf2_cu
397{
fcd3b13d
SM
398 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
399 ~dwarf2_cu ();
400
401 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
402
c24bdb02
KS
403 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
404 Create the set of symtabs used by this TU, or if this TU is sharing
405 symtabs with another TU and the symtabs have already been created
406 then restore those symtabs in the line header.
407 We don't need the pc/line-number mapping for type units. */
408 void setup_type_unit_groups (struct die_info *die);
409
410 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
411 buildsym_compunit constructor. */
412 struct compunit_symtab *start_symtab (const char *name,
413 const char *comp_dir,
414 CORE_ADDR low_pc);
415
416 /* Reset the builder. */
417 void reset_builder () { m_builder.reset (); }
418
d00adf39 419 /* The header of the compilation unit. */
fcd3b13d 420 struct comp_unit_head header {};
e142c38c 421
d00adf39 422 /* Base address of this compilation unit. */
fcd3b13d 423 CORE_ADDR base_address = 0;
d00adf39
DE
424
425 /* Non-zero if base_address has been set. */
fcd3b13d 426 int base_known = 0;
d00adf39 427
e142c38c 428 /* The language we are debugging. */
fcd3b13d
SM
429 enum language language = language_unknown;
430 const struct language_defn *language_defn = nullptr;
e142c38c 431
fcd3b13d 432 const char *producer = nullptr;
b0f35d58 433
c24bdb02 434private:
804d2729
TT
435 /* The symtab builder for this CU. This is only non-NULL when full
436 symbols are being read. */
c24bdb02 437 std::unique_ptr<buildsym_compunit> m_builder;
804d2729 438
c24bdb02 439public:
e142c38c
DJ
440 /* The generic symbol table building routines have separate lists for
441 file scope symbols and all all other scopes (local scopes). So
442 we need to select the right one to pass to add_symbol_to_list().
443 We do it by keeping a pointer to the correct list in list_in_scope.
444
445 FIXME: The original dwarf code just treated the file scope as the
446 first local scope, and all other local scopes as nested local
447 scopes, and worked fine. Check to see if we really need to
448 distinguish these in buildsym.c. */
fcd3b13d 449 struct pending **list_in_scope = nullptr;
e142c38c 450
b64f50a1
JK
451 /* Hash table holding all the loaded partial DIEs
452 with partial_die->offset.SECT_OFF as hash. */
fcd3b13d 453 htab_t partial_dies = nullptr;
72bf9492
DJ
454
455 /* Storage for things with the same lifetime as this read-in compilation
456 unit, including partial DIEs. */
fcd3b13d 457 auto_obstack comp_unit_obstack;
72bf9492 458
ae038cb0
DJ
459 /* When multiple dwarf2_cu structures are living in memory, this field
460 chains them all together, so that they can be released efficiently.
461 We will probably also want a generation counter so that most-recently-used
462 compilation units are cached... */
fcd3b13d 463 struct dwarf2_per_cu_data *read_in_chain = nullptr;
ae038cb0 464
69d751e3 465 /* Backlink to our per_cu entry. */
ae038cb0
DJ
466 struct dwarf2_per_cu_data *per_cu;
467
468 /* How many compilation units ago was this CU last referenced? */
fcd3b13d 469 int last_used = 0;
ae038cb0 470
b64f50a1
JK
471 /* A hash table of DIE cu_offset for following references with
472 die_info->offset.sect_off as hash. */
fcd3b13d 473 htab_t die_hash = nullptr;
10b3939b
DJ
474
475 /* Full DIEs if read in. */
fcd3b13d 476 struct die_info *dies = nullptr;
10b3939b
DJ
477
478 /* A set of pointers to dwarf2_per_cu_data objects for compilation
479 units referenced by this one. Only set during full symbol processing;
480 partial symbol tables do not have dependencies. */
fcd3b13d 481 htab_t dependencies = nullptr;
10b3939b 482
cb1df416 483 /* Header data from the line table, during full symbol processing. */
fcd3b13d 484 struct line_header *line_header = nullptr;
4c8aa72d
PA
485 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
486 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
487 this is the DW_TAG_compile_unit die for this CU. We'll hold on
488 to the line header as long as this DIE is being processed. See
489 process_die_scope. */
fcd3b13d 490 die_info *line_header_die_owner = nullptr;
cb1df416 491
3da10d80
KS
492 /* A list of methods which need to have physnames computed
493 after all type information has been read. */
c89b44cd 494 std::vector<delayed_method_info> method_list;
3da10d80 495
96408a79 496 /* To be copied to symtab->call_site_htab. */
fcd3b13d 497 htab_t call_site_htab = nullptr;
96408a79 498
034e5797
DE
499 /* Non-NULL if this CU came from a DWO file.
500 There is an invariant here that is important to remember:
501 Except for attributes copied from the top level DIE in the "main"
502 (or "stub") file in preparation for reading the DWO file
503 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
504 Either there isn't a DWO file (in which case this is NULL and the point
505 is moot), or there is and either we're not going to read it (in which
506 case this is NULL) or there is and we are reading it (in which case this
507 is non-NULL). */
fcd3b13d 508 struct dwo_unit *dwo_unit = nullptr;
3019eac3
DE
509
510 /* The DW_AT_addr_base attribute if present, zero otherwise
511 (zero is a valid value though).
1dbab08b 512 Note this value comes from the Fission stub CU/TU's DIE. */
fcd3b13d 513 ULONGEST addr_base = 0;
3019eac3 514
2e3cf129
DE
515 /* The DW_AT_ranges_base attribute if present, zero otherwise
516 (zero is a valid value though).
1dbab08b 517 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 518 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
519 be used without needing to know whether DWO files are in use or not.
520 N.B. This does not apply to DW_AT_ranges appearing in
521 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
522 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
523 DW_AT_ranges_base *would* have to be applied, and we'd have to care
524 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
fcd3b13d 525 ULONGEST ranges_base = 0;
2e3cf129 526
c9317f21
TT
527 /* When reading debug info generated by older versions of rustc, we
528 have to rewrite some union types to be struct types with a
529 variant part. This rewriting must be done after the CU is fully
530 read in, because otherwise at the point of rewriting some struct
531 type might not have been fully processed. So, we keep a list of
532 all such types here and process them after expansion. */
533 std::vector<struct type *> rust_unions;
534
ae038cb0 535 /* Mark used when releasing cached dies. */
9068261f 536 bool mark : 1;
ae038cb0 537
8be455d7
JK
538 /* This CU references .debug_loc. See the symtab->locations_valid field.
539 This test is imperfect as there may exist optimized debug code not using
540 any location list and still facing inlining issues if handled as
541 unoptimized code. For a future better test see GCC PR other/32998. */
9068261f 542 bool has_loclist : 1;
ba919b58 543
9068261f 544 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
1b80a9fa
JK
545 if all the producer_is_* fields are valid. This information is cached
546 because profiling CU expansion showed excessive time spent in
547 producer_is_gxx_lt_4_6. */
9068261f
AB
548 bool checked_producer : 1;
549 bool producer_is_gxx_lt_4_6 : 1;
550 bool producer_is_gcc_lt_4_3 : 1;
eb77c9df 551 bool producer_is_icc : 1;
9068261f 552 bool producer_is_icc_lt_14 : 1;
c258c396 553 bool producer_is_codewarrior : 1;
4d4ec4e5 554
9068261f 555 /* When true, the file that we're processing is known to have
4d4ec4e5
TT
556 debugging info for C++ namespaces. GCC 3.3.x did not produce
557 this information, but later versions do. */
558
9068261f 559 bool processing_has_namespace_info : 1;
d590ff25
YQ
560
561 struct partial_die_info *find_partial_die (sect_offset sect_off);
c24bdb02
KS
562
563 /* If this CU was inherited by another CU (via specification,
564 abstract_origin, etc), this is the ancestor CU. */
565 dwarf2_cu *ancestor;
566
567 /* Get the buildsym_compunit for this CU. */
568 buildsym_compunit *get_builder ()
569 {
570 /* If this CU has a builder associated with it, use that. */
571 if (m_builder != nullptr)
572 return m_builder.get ();
573
574 /* Otherwise, search ancestors for a valid builder. */
575 if (ancestor != nullptr)
576 return ancestor->get_builder ();
577
578 return nullptr;
579 }
e7c27a73
DJ
580};
581
094b34ac
DE
582/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
583 This includes type_unit_group and quick_file_names. */
584
585struct stmt_list_hash
586{
587 /* The DWO unit this table is from or NULL if there is none. */
588 struct dwo_unit *dwo_unit;
589
590 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 591 sect_offset line_sect_off;
094b34ac
DE
592};
593
f4dc4d17
DE
594/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
595 an object of this type. */
596
597struct type_unit_group
598{
0186c6a7 599 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
600 To simplify things we create an artificial CU that "includes" all the
601 type units using this stmt_list so that the rest of the code still has
602 a "per_cu" handle on the symtab.
603 This PER_CU is recognized by having no section. */
8a0459fd 604#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
605 struct dwarf2_per_cu_data per_cu;
606
0186c6a7
DE
607 /* The TUs that share this DW_AT_stmt_list entry.
608 This is added to while parsing type units to build partial symtabs,
609 and is deleted afterwards and not used again. */
a8b3b8e9 610 std::vector<signatured_type *> *tus;
f4dc4d17 611
43f3e411 612 /* The compunit symtab.
094b34ac 613 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
614 so we create an essentially anonymous symtab as the compunit symtab. */
615 struct compunit_symtab *compunit_symtab;
f4dc4d17 616
094b34ac
DE
617 /* The data used to construct the hash key. */
618 struct stmt_list_hash hash;
f4dc4d17
DE
619
620 /* The number of symtabs from the line header.
621 The value here must match line_header.num_file_names. */
622 unsigned int num_symtabs;
623
624 /* The symbol tables for this TU (obtained from the files listed in
625 DW_AT_stmt_list).
626 WARNING: The order of entries here must match the order of entries
627 in the line header. After the first TU using this type_unit_group, the
628 line header for the subsequent TUs is recreated from this. This is done
629 because we need to use the same symtabs for each TU using the same
630 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
631 there's no guarantee the line header doesn't have duplicate entries. */
632 struct symtab **symtabs;
633};
634
73869dc2 635/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
636
637struct dwo_sections
638{
639 struct dwarf2_section_info abbrev;
3019eac3
DE
640 struct dwarf2_section_info line;
641 struct dwarf2_section_info loc;
43988095 642 struct dwarf2_section_info loclists;
09262596
DE
643 struct dwarf2_section_info macinfo;
644 struct dwarf2_section_info macro;
3019eac3
DE
645 struct dwarf2_section_info str;
646 struct dwarf2_section_info str_offsets;
80626a55
DE
647 /* In the case of a virtual DWO file, these two are unused. */
648 struct dwarf2_section_info info;
fd5866f6 649 std::vector<dwarf2_section_info> types;
3019eac3
DE
650};
651
c88ee1f0 652/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
653
654struct dwo_unit
655{
656 /* Backlink to the containing struct dwo_file. */
657 struct dwo_file *dwo_file;
658
659 /* The "id" that distinguishes this CU/TU.
660 .debug_info calls this "dwo_id", .debug_types calls this "signature".
661 Since signatures came first, we stick with it for consistency. */
662 ULONGEST signature;
663
664 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 665 struct dwarf2_section_info *section;
3019eac3 666
9c541725
PA
667 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
668 sect_offset sect_off;
3019eac3
DE
669 unsigned int length;
670
671 /* For types, offset in the type's DIE of the type defined by this TU. */
672 cu_offset type_offset_in_tu;
673};
674
73869dc2
DE
675/* include/dwarf2.h defines the DWP section codes.
676 It defines a max value but it doesn't define a min value, which we
677 use for error checking, so provide one. */
678
679enum dwp_v2_section_ids
680{
681 DW_SECT_MIN = 1
682};
683
80626a55 684/* Data for one DWO file.
57d63ce2
DE
685
686 This includes virtual DWO files (a virtual DWO file is a DWO file as it
687 appears in a DWP file). DWP files don't really have DWO files per se -
688 comdat folding of types "loses" the DWO file they came from, and from
689 a high level view DWP files appear to contain a mass of random types.
690 However, to maintain consistency with the non-DWP case we pretend DWP
691 files contain virtual DWO files, and we assign each TU with one virtual
692 DWO file (generally based on the line and abbrev section offsets -
693 a heuristic that seems to work in practice). */
3019eac3
DE
694
695struct dwo_file
696{
51ac9db5
SM
697 dwo_file () = default;
698 DISABLE_COPY_AND_ASSIGN (dwo_file);
699
0ac5b59e 700 /* The DW_AT_GNU_dwo_name attribute.
80626a55
DE
701 For virtual DWO files the name is constructed from the section offsets
702 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
703 from related CU+TUs. */
51ac9db5 704 const char *dwo_name = nullptr;
0ac5b59e
DE
705
706 /* The DW_AT_comp_dir attribute. */
51ac9db5 707 const char *comp_dir = nullptr;
3019eac3 708
80626a55
DE
709 /* The bfd, when the file is open. Otherwise this is NULL.
710 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
fb1eb2f9 711 gdb_bfd_ref_ptr dbfd;
3019eac3 712
73869dc2
DE
713 /* The sections that make up this DWO file.
714 Remember that for virtual DWO files in DWP V2, these are virtual
715 sections (for lack of a better name). */
51ac9db5 716 struct dwo_sections sections {};
3019eac3 717
33c5cd75
DB
718 /* The CUs in the file.
719 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
720 an extension to handle LLVM's Link Time Optimization output (where
721 multiple source files may be compiled into a single object/dwo pair). */
51ac9db5 722 htab_t cus {};
3019eac3
DE
723
724 /* Table of TUs in the file.
725 Each element is a struct dwo_unit. */
51ac9db5 726 htab_t tus {};
3019eac3
DE
727};
728
80626a55
DE
729/* These sections are what may appear in a DWP file. */
730
731struct dwp_sections
732{
73869dc2 733 /* These are used by both DWP version 1 and 2. */
80626a55
DE
734 struct dwarf2_section_info str;
735 struct dwarf2_section_info cu_index;
736 struct dwarf2_section_info tu_index;
73869dc2
DE
737
738 /* These are only used by DWP version 2 files.
739 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
740 sections are referenced by section number, and are not recorded here.
741 In DWP version 2 there is at most one copy of all these sections, each
742 section being (effectively) comprised of the concatenation of all of the
743 individual sections that exist in the version 1 format.
744 To keep the code simple we treat each of these concatenated pieces as a
745 section itself (a virtual section?). */
746 struct dwarf2_section_info abbrev;
747 struct dwarf2_section_info info;
748 struct dwarf2_section_info line;
749 struct dwarf2_section_info loc;
750 struct dwarf2_section_info macinfo;
751 struct dwarf2_section_info macro;
752 struct dwarf2_section_info str_offsets;
753 struct dwarf2_section_info types;
80626a55
DE
754};
755
73869dc2
DE
756/* These sections are what may appear in a virtual DWO file in DWP version 1.
757 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 758
73869dc2 759struct virtual_v1_dwo_sections
80626a55
DE
760{
761 struct dwarf2_section_info abbrev;
762 struct dwarf2_section_info line;
763 struct dwarf2_section_info loc;
764 struct dwarf2_section_info macinfo;
765 struct dwarf2_section_info macro;
766 struct dwarf2_section_info str_offsets;
767 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 768 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
769 struct dwarf2_section_info info_or_types;
770};
771
73869dc2
DE
772/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
773 In version 2, the sections of the DWO files are concatenated together
774 and stored in one section of that name. Thus each ELF section contains
775 several "virtual" sections. */
776
777struct virtual_v2_dwo_sections
778{
779 bfd_size_type abbrev_offset;
780 bfd_size_type abbrev_size;
781
782 bfd_size_type line_offset;
783 bfd_size_type line_size;
784
785 bfd_size_type loc_offset;
786 bfd_size_type loc_size;
787
788 bfd_size_type macinfo_offset;
789 bfd_size_type macinfo_size;
790
791 bfd_size_type macro_offset;
792 bfd_size_type macro_size;
793
794 bfd_size_type str_offsets_offset;
795 bfd_size_type str_offsets_size;
796
797 /* Each DWP hash table entry records one CU or one TU.
798 That is recorded here, and copied to dwo_unit.section. */
799 bfd_size_type info_or_types_offset;
800 bfd_size_type info_or_types_size;
801};
802
80626a55
DE
803/* Contents of DWP hash tables. */
804
805struct dwp_hash_table
806{
73869dc2 807 uint32_t version, nr_columns;
80626a55 808 uint32_t nr_units, nr_slots;
73869dc2
DE
809 const gdb_byte *hash_table, *unit_table;
810 union
811 {
812 struct
813 {
814 const gdb_byte *indices;
815 } v1;
816 struct
817 {
818 /* This is indexed by column number and gives the id of the section
819 in that column. */
820#define MAX_NR_V2_DWO_SECTIONS \
821 (1 /* .debug_info or .debug_types */ \
822 + 1 /* .debug_abbrev */ \
823 + 1 /* .debug_line */ \
824 + 1 /* .debug_loc */ \
825 + 1 /* .debug_str_offsets */ \
826 + 1 /* .debug_macro or .debug_macinfo */)
827 int section_ids[MAX_NR_V2_DWO_SECTIONS];
828 const gdb_byte *offsets;
829 const gdb_byte *sizes;
830 } v2;
831 } section_pool;
80626a55
DE
832};
833
834/* Data for one DWP file. */
835
836struct dwp_file
837{
400174b1
TT
838 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
839 : name (name_),
840 dbfd (std::move (abfd))
841 {
842 }
843
80626a55
DE
844 /* Name of the file. */
845 const char *name;
846
73869dc2 847 /* File format version. */
400174b1 848 int version = 0;
73869dc2 849
93417882 850 /* The bfd. */
400174b1 851 gdb_bfd_ref_ptr dbfd;
80626a55
DE
852
853 /* Section info for this file. */
400174b1 854 struct dwp_sections sections {};
80626a55 855
57d63ce2 856 /* Table of CUs in the file. */
400174b1 857 const struct dwp_hash_table *cus = nullptr;
80626a55
DE
858
859 /* Table of TUs in the file. */
400174b1 860 const struct dwp_hash_table *tus = nullptr;
80626a55 861
19ac8c2e 862 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
400174b1
TT
863 htab_t loaded_cus {};
864 htab_t loaded_tus {};
80626a55 865
73869dc2
DE
866 /* Table to map ELF section numbers to their sections.
867 This is only needed for the DWP V1 file format. */
400174b1
TT
868 unsigned int num_sections = 0;
869 asection **elf_sections = nullptr;
80626a55
DE
870};
871
0963b4bd
MS
872/* Struct used to pass misc. parameters to read_die_and_children, et
873 al. which are used for both .debug_info and .debug_types dies.
874 All parameters here are unchanging for the life of the call. This
dee91e82 875 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
876
877struct die_reader_specs
878{
a32a8923 879 /* The bfd of die_section. */
93311388
DE
880 bfd* abfd;
881
882 /* The CU of the DIE we are parsing. */
883 struct dwarf2_cu *cu;
884
80626a55 885 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
886 struct dwo_file *dwo_file;
887
dee91e82 888 /* The section the die comes from.
3019eac3 889 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
890 struct dwarf2_section_info *die_section;
891
892 /* die_section->buffer. */
d521ce57 893 const gdb_byte *buffer;
f664829e
DE
894
895 /* The end of the buffer. */
896 const gdb_byte *buffer_end;
a2ce51a0
DE
897
898 /* The value of the DW_AT_comp_dir attribute. */
899 const char *comp_dir;
685af9cd
TT
900
901 /* The abbreviation table to use when reading the DIEs. */
902 struct abbrev_table *abbrev_table;
93311388
DE
903};
904
fd820528 905/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82 906typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
d521ce57 907 const gdb_byte *info_ptr,
dee91e82
DE
908 struct die_info *comp_unit_die,
909 int has_children,
910 void *data);
911
7ba99d21
AT
912/* dir_index is 1-based in DWARF 4 and before, and is 0-based in DWARF 5 and
913 later. */
914typedef int dir_index;
ecfb656c 915
7ba99d21
AT
916/* file_name_index is 1-based in DWARF 4 and before, and is 0-based in DWARF 5
917 and later. */
918typedef int file_name_index;
ecfb656c 919
52059ffd
TT
920struct file_entry
921{
fff8551c
PA
922 file_entry () = default;
923
ecfb656c 924 file_entry (const char *name_, dir_index d_index_,
fff8551c
PA
925 unsigned int mod_time_, unsigned int length_)
926 : name (name_),
ecfb656c 927 d_index (d_index_),
fff8551c
PA
928 mod_time (mod_time_),
929 length (length_)
930 {}
931
ecfb656c
PA
932 /* Return the include directory at D_INDEX stored in LH. Returns
933 NULL if D_INDEX is out of bounds. */
8c43009f
PA
934 const char *include_dir (const line_header *lh) const;
935
fff8551c
PA
936 /* The file name. Note this is an observing pointer. The memory is
937 owned by debug_line_buffer. */
938 const char *name {};
939
8c43009f 940 /* The directory index (1-based). */
ecfb656c 941 dir_index d_index {};
fff8551c
PA
942
943 unsigned int mod_time {};
944
945 unsigned int length {};
946
947 /* True if referenced by the Line Number Program. */
948 bool included_p {};
949
83769d0b 950 /* The associated symbol table, if any. */
fff8551c 951 struct symtab *symtab {};
52059ffd
TT
952};
953
debd256d
JB
954/* The line number information for a compilation unit (found in the
955 .debug_line section) begins with a "statement program header",
956 which contains the following information. */
957struct line_header
958{
fff8551c
PA
959 line_header ()
960 : offset_in_dwz {}
961 {}
962
963 /* Add an entry to the include directory table. */
964 void add_include_dir (const char *include_dir);
965
966 /* Add an entry to the file name table. */
ecfb656c 967 void add_file_name (const char *name, dir_index d_index,
fff8551c
PA
968 unsigned int mod_time, unsigned int length);
969
7ba99d21
AT
970 /* Return the include dir at INDEX (0-based in DWARF 5 and 1-based before).
971 Returns NULL if INDEX is out of bounds. */
ecfb656c 972 const char *include_dir_at (dir_index index) const
8c43009f 973 {
7ba99d21
AT
974 int vec_index;
975 if (version >= 5)
976 vec_index = index;
977 else
978 vec_index = index - 1;
979 if (vec_index < 0 || vec_index >= m_include_dirs.size ())
8c43009f 980 return NULL;
7ba99d21 981 return m_include_dirs[vec_index];
8c43009f
PA
982 }
983
7ba99d21 984 bool is_valid_file_index (int file_index)
8c43009f 985 {
7ba99d21
AT
986 if (version >= 5)
987 return 0 <= file_index && file_index < file_names_size ();
988 return 1 <= file_index && file_index <= file_names_size ();
989 }
ecfb656c 990
7ba99d21
AT
991 /* Return the file name at INDEX (0-based in DWARF 5 and 1-based before).
992 Returns NULL if INDEX is out of bounds. */
993 file_entry *file_name_at (file_name_index index)
994 {
995 int vec_index;
996 if (version >= 5)
997 vec_index = index;
998 else
999 vec_index = index - 1;
1000 if (vec_index < 0 || vec_index >= m_file_names.size ())
fff8551c 1001 return NULL;
7ba99d21 1002 return &m_file_names[vec_index];
fff8551c
PA
1003 }
1004
7ba99d21
AT
1005 /* The indexes are 0-based in DWARF 5 and 1-based in DWARF 4. Therefore,
1006 this method should only be used to iterate through all file entries in an
1007 index-agnostic manner. */
1008 std::vector<file_entry> &file_names ()
1009 { return m_file_names; }
1010
527f3840 1011 /* Offset of line number information in .debug_line section. */
9c541725 1012 sect_offset sect_off {};
527f3840
JK
1013
1014 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
fff8551c
PA
1015 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1016
1017 unsigned int total_length {};
1018 unsigned short version {};
1019 unsigned int header_length {};
1020 unsigned char minimum_instruction_length {};
1021 unsigned char maximum_ops_per_instruction {};
1022 unsigned char default_is_stmt {};
1023 int line_base {};
1024 unsigned char line_range {};
1025 unsigned char opcode_base {};
debd256d
JB
1026
1027 /* standard_opcode_lengths[i] is the number of operands for the
1028 standard opcode whose value is i. This means that
1029 standard_opcode_lengths[0] is unused, and the last meaningful
1030 element is standard_opcode_lengths[opcode_base - 1]. */
fff8551c 1031 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
debd256d 1032
7ba99d21
AT
1033 int file_names_size ()
1034 { return m_file_names.size(); }
debd256d
JB
1035
1036 /* The start and end of the statement program following this
6502dd73 1037 header. These point into dwarf2_per_objfile->line_buffer. */
fff8551c 1038 const gdb_byte *statement_program_start {}, *statement_program_end {};
7ba99d21
AT
1039
1040 private:
1041 /* The include_directories table. Note these are observing
1042 pointers. The memory is owned by debug_line_buffer. */
1043 std::vector<const char *> m_include_dirs;
1044
1045 /* The file_names table. This is private because the meaning of indexes
1046 differs among DWARF versions (The first valid index is 1 in DWARF 4 and
1047 before, and is 0 in DWARF 5 and later). So the client should use
1048 file_name_at method for access. */
1049 std::vector<file_entry> m_file_names;
debd256d 1050};
c906108c 1051
fff8551c
PA
1052typedef std::unique_ptr<line_header> line_header_up;
1053
8c43009f
PA
1054const char *
1055file_entry::include_dir (const line_header *lh) const
1056{
ecfb656c 1057 return lh->include_dir_at (d_index);
8c43009f
PA
1058}
1059
c906108c 1060/* When we construct a partial symbol table entry we only
0963b4bd 1061 need this much information. */
6f06d47b 1062struct partial_die_info : public allocate_on_obstack
c906108c 1063 {
6f06d47b
YQ
1064 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1065
1066 /* Disable assign but still keep copy ctor, which is needed
1067 load_partial_dies. */
1068 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1069
52356b79
YQ
1070 /* Adjust the partial die before generating a symbol for it. This
1071 function may set the is_external flag or change the DIE's
1072 name. */
1073 void fixup (struct dwarf2_cu *cu);
1074
48fbe735
YQ
1075 /* Read a minimal amount of information into the minimal die
1076 structure. */
1077 const gdb_byte *read (const struct die_reader_specs *reader,
1078 const struct abbrev_info &abbrev,
1079 const gdb_byte *info_ptr);
1080
72bf9492 1081 /* Offset of this DIE. */
6f06d47b 1082 const sect_offset sect_off;
72bf9492
DJ
1083
1084 /* DWARF-2 tag for this DIE. */
6f06d47b 1085 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 1086
72bf9492 1087 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
1088 const unsigned int has_children : 1;
1089
72bf9492
DJ
1090 unsigned int is_external : 1;
1091 unsigned int is_declaration : 1;
1092 unsigned int has_type : 1;
1093 unsigned int has_specification : 1;
1094 unsigned int has_pc_info : 1;
481860b3 1095 unsigned int may_be_inlined : 1;
72bf9492 1096
0c1b455e
TT
1097 /* This DIE has been marked DW_AT_main_subprogram. */
1098 unsigned int main_subprogram : 1;
1099
72bf9492
DJ
1100 /* Flag set if the SCOPE field of this structure has been
1101 computed. */
1102 unsigned int scope_set : 1;
1103
fa4028e9
JB
1104 /* Flag set if the DIE has a byte_size attribute. */
1105 unsigned int has_byte_size : 1;
1106
ff908ebf
AW
1107 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1108 unsigned int has_const_value : 1;
1109
98bfdba5
PA
1110 /* Flag set if any of the DIE's children are template arguments. */
1111 unsigned int has_template_arguments : 1;
1112
52356b79 1113 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
1114 unsigned int fixup_called : 1;
1115
36586728
TT
1116 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1117 unsigned int is_dwz : 1;
1118
1119 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1120 unsigned int spec_is_dwz : 1;
1121
72bf9492 1122 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1123 sometimes a default name for unnamed DIEs. */
6f06d47b 1124 const char *name = nullptr;
72bf9492 1125
abc72ce4 1126 /* The linkage name, if present. */
6f06d47b 1127 const char *linkage_name = nullptr;
abc72ce4 1128
72bf9492
DJ
1129 /* The scope to prepend to our children. This is generally
1130 allocated on the comp_unit_obstack, so will disappear
1131 when this compilation unit leaves the cache. */
6f06d47b 1132 const char *scope = nullptr;
72bf9492 1133
95554aad
TT
1134 /* Some data associated with the partial DIE. The tag determines
1135 which field is live. */
1136 union
1137 {
1138 /* The location description associated with this DIE, if any. */
1139 struct dwarf_block *locdesc;
1140 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1141 sect_offset sect_off;
6f06d47b 1142 } d {};
72bf9492
DJ
1143
1144 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
1145 CORE_ADDR lowpc = 0;
1146 CORE_ADDR highpc = 0;
72bf9492 1147
93311388 1148 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1149 DW_AT_sibling, if any. */
48fbe735
YQ
1150 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1151 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1152 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1153
1154 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1155 DW_AT_specification (or DW_AT_abstract_origin or
1156 DW_AT_extension). */
6f06d47b 1157 sect_offset spec_offset {};
72bf9492
DJ
1158
1159 /* Pointers to this DIE's parent, first child, and next sibling,
1160 if any. */
6f06d47b
YQ
1161 struct partial_die_info *die_parent = nullptr;
1162 struct partial_die_info *die_child = nullptr;
1163 struct partial_die_info *die_sibling = nullptr;
1164
1165 friend struct partial_die_info *
1166 dwarf2_cu::find_partial_die (sect_offset sect_off);
1167
1168 private:
1169 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1170 partial_die_info (sect_offset sect_off)
1171 : partial_die_info (sect_off, DW_TAG_padding, 0)
1172 {
1173 }
1174
1175 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1176 int has_children_)
1177 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1178 {
1179 is_external = 0;
1180 is_declaration = 0;
1181 has_type = 0;
1182 has_specification = 0;
1183 has_pc_info = 0;
1184 may_be_inlined = 0;
1185 main_subprogram = 0;
1186 scope_set = 0;
1187 has_byte_size = 0;
1188 has_const_value = 0;
1189 has_template_arguments = 0;
1190 fixup_called = 0;
1191 is_dwz = 0;
1192 spec_is_dwz = 0;
1193 }
c906108c
SS
1194 };
1195
0963b4bd 1196/* This data structure holds the information of an abbrev. */
c906108c
SS
1197struct abbrev_info
1198 {
1199 unsigned int number; /* number identifying abbrev */
1200 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
1201 unsigned short has_children; /* boolean */
1202 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1203 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1204 struct abbrev_info *next; /* next in chain */
1205 };
1206
1207struct attr_abbrev
1208 {
9d25dd43
DE
1209 ENUM_BITFIELD(dwarf_attribute) name : 16;
1210 ENUM_BITFIELD(dwarf_form) form : 16;
43988095
JK
1211
1212 /* It is valid only if FORM is DW_FORM_implicit_const. */
1213 LONGEST implicit_const;
c906108c
SS
1214 };
1215
433df2d4
DE
1216/* Size of abbrev_table.abbrev_hash_table. */
1217#define ABBREV_HASH_SIZE 121
1218
1219/* Top level data structure to contain an abbreviation table. */
1220
1221struct abbrev_table
1222{
685af9cd
TT
1223 explicit abbrev_table (sect_offset off)
1224 : sect_off (off)
1225 {
4a17f768 1226 m_abbrevs =
685af9cd 1227 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
4a17f768 1228 memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
685af9cd
TT
1229 }
1230
1231 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1232
1233 /* Allocate space for a struct abbrev_info object in
1234 ABBREV_TABLE. */
1235 struct abbrev_info *alloc_abbrev ();
1236
1237 /* Add an abbreviation to the table. */
1238 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1239
1240 /* Look up an abbrev in the table.
1241 Returns NULL if the abbrev is not found. */
1242
1243 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1244
1245
f4dc4d17
DE
1246 /* Where the abbrev table came from.
1247 This is used as a sanity check when the table is used. */
685af9cd 1248 const sect_offset sect_off;
433df2d4
DE
1249
1250 /* Storage for the abbrev table. */
685af9cd 1251 auto_obstack abbrev_obstack;
433df2d4 1252
4a17f768
YQ
1253private:
1254
433df2d4
DE
1255 /* Hash table of abbrevs.
1256 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1257 It could be statically allocated, but the previous code didn't so we
1258 don't either. */
4a17f768 1259 struct abbrev_info **m_abbrevs;
433df2d4
DE
1260};
1261
685af9cd
TT
1262typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
1263
0963b4bd 1264/* Attributes have a name and a value. */
b60c80d6
DJ
1265struct attribute
1266 {
9d25dd43 1267 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1268 ENUM_BITFIELD(dwarf_form) form : 15;
1269
1270 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1271 field should be in u.str (existing only for DW_STRING) but it is kept
1272 here for better struct attribute alignment. */
1273 unsigned int string_is_canonical : 1;
1274
b60c80d6
DJ
1275 union
1276 {
15d034d0 1277 const char *str;
b60c80d6 1278 struct dwarf_block *blk;
43bbcdc2
PH
1279 ULONGEST unsnd;
1280 LONGEST snd;
b60c80d6 1281 CORE_ADDR addr;
ac9ec31b 1282 ULONGEST signature;
b60c80d6
DJ
1283 }
1284 u;
1285 };
1286
0963b4bd 1287/* This data structure holds a complete die structure. */
c906108c
SS
1288struct die_info
1289 {
76815b17
DE
1290 /* DWARF-2 tag for this DIE. */
1291 ENUM_BITFIELD(dwarf_tag) tag : 16;
1292
1293 /* Number of attributes */
98bfdba5
PA
1294 unsigned char num_attrs;
1295
1296 /* True if we're presently building the full type name for the
1297 type derived from this DIE. */
1298 unsigned char building_fullname : 1;
76815b17 1299
adde2bff
DE
1300 /* True if this die is in process. PR 16581. */
1301 unsigned char in_process : 1;
1302
76815b17
DE
1303 /* Abbrev number */
1304 unsigned int abbrev;
1305
93311388 1306 /* Offset in .debug_info or .debug_types section. */
9c541725 1307 sect_offset sect_off;
78ba4af6
JB
1308
1309 /* The dies in a compilation unit form an n-ary tree. PARENT
1310 points to this die's parent; CHILD points to the first child of
1311 this node; and all the children of a given node are chained
4950bc1c 1312 together via their SIBLING fields. */
639d11d3
DC
1313 struct die_info *child; /* Its first child, if any. */
1314 struct die_info *sibling; /* Its next sibling, if any. */
1315 struct die_info *parent; /* Its parent, if any. */
c906108c 1316
b60c80d6
DJ
1317 /* An array of attributes, with NUM_ATTRS elements. There may be
1318 zero, but it's not common and zero-sized arrays are not
1319 sufficiently portable C. */
1320 struct attribute attrs[1];
c906108c
SS
1321 };
1322
0963b4bd 1323/* Get at parts of an attribute structure. */
c906108c
SS
1324
1325#define DW_STRING(attr) ((attr)->u.str)
8285870a 1326#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1327#define DW_UNSND(attr) ((attr)->u.unsnd)
1328#define DW_BLOCK(attr) ((attr)->u.blk)
1329#define DW_SND(attr) ((attr)->u.snd)
1330#define DW_ADDR(attr) ((attr)->u.addr)
ac9ec31b 1331#define DW_SIGNATURE(attr) ((attr)->u.signature)
c906108c 1332
0963b4bd 1333/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1334struct dwarf_block
1335 {
56eb65bd 1336 size_t size;
1d6edc3c
JK
1337
1338 /* Valid only if SIZE is not zero. */
d521ce57 1339 const gdb_byte *data;
c906108c
SS
1340 };
1341
c906108c
SS
1342#ifndef ATTR_ALLOC_CHUNK
1343#define ATTR_ALLOC_CHUNK 4
1344#endif
1345
c906108c
SS
1346/* Allocate fields for structs, unions and enums in this size. */
1347#ifndef DW_FIELD_ALLOC_CHUNK
1348#define DW_FIELD_ALLOC_CHUNK 4
1349#endif
1350
c906108c
SS
1351/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1352 but this would require a corresponding change in unpack_field_as_long
1353 and friends. */
1354static int bits_per_byte = 8;
1355
2ddeaf8a
TT
1356/* When reading a variant or variant part, we track a bit more
1357 information about the field, and store it in an object of this
1358 type. */
1359
1360struct variant_field
1361{
1362 /* If we see a DW_TAG_variant, then this will be the discriminant
1363 value. */
1364 ULONGEST discriminant_value;
1365 /* If we see a DW_TAG_variant, then this will be set if this is the
1366 default branch. */
1367 bool default_branch;
1368 /* While reading a DW_TAG_variant_part, this will be set if this
1369 field is the discriminant. */
1370 bool is_discriminant;
1371};
1372
52059ffd
TT
1373struct nextfield
1374{
be2daae6
TT
1375 int accessibility = 0;
1376 int virtuality = 0;
2ddeaf8a 1377 /* Extra information to describe a variant or variant part. */
be2daae6
TT
1378 struct variant_field variant {};
1379 struct field field {};
52059ffd
TT
1380};
1381
1382struct fnfieldlist
1383{
be2daae6
TT
1384 const char *name = nullptr;
1385 std::vector<struct fn_field> fnfields;
52059ffd
TT
1386};
1387
c906108c
SS
1388/* The routines that read and process dies for a C struct or C++ class
1389 pass lists of data member fields and lists of member function fields
1390 in an instance of a field_info structure, as defined below. */
1391struct field_info
c5aa993b 1392 {
0963b4bd 1393 /* List of data member and baseclasses fields. */
be2daae6
TT
1394 std::vector<struct nextfield> fields;
1395 std::vector<struct nextfield> baseclasses;
c906108c 1396
7d0ccb61 1397 /* Number of fields (including baseclasses). */
be2daae6 1398 int nfields = 0;
c906108c 1399
85102364 1400 /* Set if the accessibility of one of the fields is not public. */
be2daae6 1401 int non_public_fields = 0;
c906108c 1402
c5aa993b
JM
1403 /* Member function fieldlist array, contains name of possibly overloaded
1404 member function, number of overloaded member functions and a pointer
1405 to the head of the member function field chain. */
be2daae6 1406 std::vector<struct fnfieldlist> fnfieldlists;
98751a41
JK
1407
1408 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1409 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
be2daae6 1410 std::vector<struct decl_field> typedef_field_list;
883fd55a
KS
1411
1412 /* Nested types defined by this class and the number of elements in this
1413 list. */
be2daae6 1414 std::vector<struct decl_field> nested_types_list;
c5aa993b 1415 };
c906108c 1416
10b3939b
DJ
1417/* One item on the queue of compilation units to read in full symbols
1418 for. */
1419struct dwarf2_queue_item
1420{
1421 struct dwarf2_per_cu_data *per_cu;
95554aad 1422 enum language pretend_language;
10b3939b
DJ
1423 struct dwarf2_queue_item *next;
1424};
1425
1426/* The current queue. */
1427static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1428
ae038cb0
DJ
1429/* Loaded secondary compilation units are kept in memory until they
1430 have not been referenced for the processing of this many
1431 compilation units. Set this to zero to disable caching. Cache
1432 sizes of up to at least twenty will improve startup time for
1433 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1434static int dwarf_max_cache_age = 5;
920d2a44 1435static void
b4f54984
DE
1436show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1437 struct cmd_list_element *c, const char *value)
920d2a44 1438{
3e43a32a 1439 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1440 "DWARF compilation units is %s.\n"),
920d2a44
AC
1441 value);
1442}
4390d890 1443\f
c906108c
SS
1444/* local function prototypes */
1445
a32a8923
DE
1446static const char *get_section_name (const struct dwarf2_section_info *);
1447
1448static const char *get_section_file_name (const struct dwarf2_section_info *);
1449
918dd910
JK
1450static void dwarf2_find_base_address (struct die_info *die,
1451 struct dwarf2_cu *cu);
1452
0018ea6f
DE
1453static struct partial_symtab *create_partial_symtab
1454 (struct dwarf2_per_cu_data *per_cu, const char *name);
1455
f1902523
JK
1456static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1457 const gdb_byte *info_ptr,
1458 struct die_info *type_unit_die,
1459 int has_children, void *data);
1460
ed2dc618
SM
1461static void dwarf2_build_psymtabs_hard
1462 (struct dwarf2_per_objfile *dwarf2_per_objfile);
c906108c 1463
72bf9492
DJ
1464static void scan_partial_symbols (struct partial_die_info *,
1465 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1466 int, struct dwarf2_cu *);
c906108c 1467
72bf9492
DJ
1468static void add_partial_symbol (struct partial_die_info *,
1469 struct dwarf2_cu *);
63d06c5c 1470
72bf9492
DJ
1471static void add_partial_namespace (struct partial_die_info *pdi,
1472 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1473 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1474
5d7cb8df 1475static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1476 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1477 struct dwarf2_cu *cu);
1478
72bf9492
DJ
1479static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1480 struct dwarf2_cu *cu);
91c24f0a 1481
bc30ff58
JB
1482static void add_partial_subprogram (struct partial_die_info *pdi,
1483 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1484 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1485
257e7a09
YQ
1486static void dwarf2_read_symtab (struct partial_symtab *,
1487 struct objfile *);
c906108c 1488
a14ed312 1489static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1490
685af9cd 1491static abbrev_table_up abbrev_table_read_table
ed2dc618
SM
1492 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1493 sect_offset);
433df2d4 1494
d521ce57 1495static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1496
dee91e82 1497static struct partial_die_info *load_partial_dies
d521ce57 1498 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1499
fb816e8b
TV
1500/* A pair of partial_die_info and compilation unit. */
1501struct cu_partial_die_info
1502{
1503 /* The compilation unit of the partial_die_info. */
1504 struct dwarf2_cu *cu;
1505 /* A partial_die_info. */
1506 struct partial_die_info *pdi;
122cf0f2
AB
1507
1508 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1509 : cu (cu),
1510 pdi (pdi)
405feb71 1511 { /* Nothing. */ }
122cf0f2
AB
1512
1513private:
1514 cu_partial_die_info () = delete;
fb816e8b
TV
1515};
1516
122cf0f2
AB
1517static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1518 struct dwarf2_cu *);
72bf9492 1519
d521ce57
TT
1520static const gdb_byte *read_attribute (const struct die_reader_specs *,
1521 struct attribute *, struct attr_abbrev *,
1522 const gdb_byte *);
a8329558 1523
a1855c1d 1524static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1525
a1855c1d 1526static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1527
a1855c1d 1528static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1529
15f18d14
AT
1530/* Read the next three bytes (little-endian order) as an unsigned integer. */
1531static unsigned int read_3_bytes (bfd *, const gdb_byte *);
1532
a1855c1d 1533static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1534
a1855c1d 1535static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1536
d521ce57 1537static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1538 unsigned int *);
c906108c 1539
d521ce57 1540static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1541
1542static LONGEST read_checked_initial_length_and_offset
d521ce57 1543 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1544 unsigned int *, unsigned int *);
613e1657 1545
d521ce57
TT
1546static LONGEST read_offset (bfd *, const gdb_byte *,
1547 const struct comp_unit_head *,
c764a876
DE
1548 unsigned int *);
1549
d521ce57 1550static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1551
ed2dc618
SM
1552static sect_offset read_abbrev_offset
1553 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1554 struct dwarf2_section_info *, sect_offset);
f4dc4d17 1555
d521ce57 1556static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1557
d521ce57 1558static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1559
ed2dc618
SM
1560static const char *read_indirect_string
1561 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1562 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1563
ed2dc618
SM
1564static const char *read_indirect_line_string
1565 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1566 const struct comp_unit_head *, unsigned int *);
36586728 1567
ed2dc618
SM
1568static const char *read_indirect_string_at_offset
1569 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1570 LONGEST str_offset);
927aa2e7 1571
ed2dc618
SM
1572static const char *read_indirect_string_from_dwz
1573 (struct objfile *objfile, struct dwz_file *, LONGEST);
c906108c 1574
d521ce57 1575static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1576
d521ce57
TT
1577static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1578 const gdb_byte *,
3019eac3
DE
1579 unsigned int *);
1580
d521ce57 1581static const char *read_str_index (const struct die_reader_specs *reader,
342587c4 1582 ULONGEST str_index);
3019eac3 1583
e142c38c 1584static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1585
e142c38c
DJ
1586static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1587 struct dwarf2_cu *);
c906108c 1588
348e048f 1589static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1590 unsigned int);
348e048f 1591
7d45c7c3
KB
1592static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1593 struct dwarf2_cu *cu);
1594
a084a2a6
AT
1595static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1596
05cf31d1
JB
1597static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1598 struct dwarf2_cu *cu);
1599
e142c38c 1600static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1601
e142c38c 1602static struct die_info *die_specification (struct die_info *die,
f2f0e013 1603 struct dwarf2_cu **);
63d06c5c 1604
9c541725 1605static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1606 struct dwarf2_cu *cu);
debd256d 1607
f3f5162e 1608static void dwarf_decode_lines (struct line_header *, const char *,
c3b7b696 1609 struct dwarf2_cu *, struct partial_symtab *,
527f3840 1610 CORE_ADDR, int decode_mapping);
c906108c 1611
804d2729
TT
1612static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1613 const char *);
c906108c 1614
a14ed312 1615static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1616 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1617
ff39bb5e 1618static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1619 struct dwarf2_cu *);
c906108c 1620
ff39bb5e 1621static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1622 struct type *type,
1623 const char *name,
1624 struct obstack *obstack,
12df843f 1625 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1626 const gdb_byte **bytes,
98bfdba5 1627 struct dwarf2_locexpr_baton **baton);
2df3850c 1628
e7c27a73 1629static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1630
b4ba55a1
JB
1631static int need_gnat_info (struct dwarf2_cu *);
1632
3e43a32a
MS
1633static struct type *die_descriptive_type (struct die_info *,
1634 struct dwarf2_cu *);
b4ba55a1
JB
1635
1636static void set_descriptive_type (struct type *, struct die_info *,
1637 struct dwarf2_cu *);
1638
e7c27a73
DJ
1639static struct type *die_containing_type (struct die_info *,
1640 struct dwarf2_cu *);
c906108c 1641
ff39bb5e 1642static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1643 struct dwarf2_cu *);
c906108c 1644
f792889a 1645static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1646
673bfd45
DE
1647static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1648
0d5cff50 1649static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1650
6e70227d 1651static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1652 const char *suffix, int physname,
1653 struct dwarf2_cu *cu);
63d06c5c 1654
e7c27a73 1655static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1656
348e048f
DE
1657static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1658
e7c27a73 1659static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1660
e7c27a73 1661static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1662
96408a79
SA
1663static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1664
71a3c369
TT
1665static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1666
ff013f42
JK
1667static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1668 struct dwarf2_cu *, struct partial_symtab *);
1669
3a2b436a 1670/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1671 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1672enum pc_bounds_kind
1673{
e385593e 1674 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1675 PC_BOUNDS_NOT_PRESENT,
1676
e385593e
JK
1677 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1678 were present but they do not form a valid range of PC addresses. */
1679 PC_BOUNDS_INVALID,
1680
3a2b436a
JK
1681 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1682 PC_BOUNDS_RANGES,
1683
1684 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1685 PC_BOUNDS_HIGH_LOW,
1686};
1687
1688static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1689 CORE_ADDR *, CORE_ADDR *,
1690 struct dwarf2_cu *,
1691 struct partial_symtab *);
c906108c 1692
fae299cd
DC
1693static void get_scope_pc_bounds (struct die_info *,
1694 CORE_ADDR *, CORE_ADDR *,
1695 struct dwarf2_cu *);
1696
801e3a5b
JB
1697static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1698 CORE_ADDR, struct dwarf2_cu *);
1699
a14ed312 1700static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1701 struct dwarf2_cu *);
c906108c 1702
a14ed312 1703static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1704 struct type *, struct dwarf2_cu *);
c906108c 1705
a14ed312 1706static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1707 struct die_info *, struct type *,
e7c27a73 1708 struct dwarf2_cu *);
c906108c 1709
a14ed312 1710static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1711 struct type *,
1712 struct dwarf2_cu *);
c906108c 1713
134d01f1 1714static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1715
e7c27a73 1716static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1717
e7c27a73 1718static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1719
5d7cb8df
JK
1720static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1721
804d2729 1722static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1723
27aa8d6a
SW
1724static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1725
74921315
KS
1726static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1727
f55ee35c
JK
1728static struct type *read_module_type (struct die_info *die,
1729 struct dwarf2_cu *cu);
1730
38d518c9 1731static const char *namespace_name (struct die_info *die,
e142c38c 1732 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1733
134d01f1 1734static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1735
e7c27a73 1736static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1737
6e70227d 1738static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1739 struct dwarf2_cu *);
1740
bf6af496 1741static struct die_info *read_die_and_siblings_1
d521ce57 1742 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1743 struct die_info *);
639d11d3 1744
dee91e82 1745static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1746 const gdb_byte *info_ptr,
1747 const gdb_byte **new_info_ptr,
639d11d3
DC
1748 struct die_info *parent);
1749
d521ce57
TT
1750static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1751 struct die_info **, const gdb_byte *,
1752 int *, int);
3019eac3 1753
d521ce57
TT
1754static const gdb_byte *read_full_die (const struct die_reader_specs *,
1755 struct die_info **, const gdb_byte *,
1756 int *);
93311388 1757
e7c27a73 1758static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1759
15d034d0
TT
1760static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1761 struct obstack *);
71c25dea 1762
15d034d0 1763static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1764
15d034d0 1765static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1766 struct die_info *die,
1767 struct dwarf2_cu *cu);
1768
ca69b9e6
DE
1769static const char *dwarf2_physname (const char *name, struct die_info *die,
1770 struct dwarf2_cu *cu);
1771
e142c38c 1772static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1773 struct dwarf2_cu **);
9219021c 1774
f39c6ffd 1775static const char *dwarf_tag_name (unsigned int);
c906108c 1776
f39c6ffd 1777static const char *dwarf_attr_name (unsigned int);
c906108c 1778
a084a2a6
AT
1779static const char *dwarf_unit_type_name (int unit_type);
1780
f39c6ffd 1781static const char *dwarf_form_name (unsigned int);
c906108c 1782
a121b7c1 1783static const char *dwarf_bool_name (unsigned int);
c906108c 1784
f39c6ffd 1785static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1786
f9aca02d 1787static struct die_info *sibling_die (struct die_info *);
c906108c 1788
d97bc12b
DE
1789static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1790
1791static void dump_die_for_error (struct die_info *);
1792
1793static void dump_die_1 (struct ui_file *, int level, int max_level,
1794 struct die_info *);
c906108c 1795
d97bc12b 1796/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1797
51545339 1798static void store_in_ref_table (struct die_info *,
10b3939b 1799 struct dwarf2_cu *);
c906108c 1800
ff39bb5e 1801static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1802
ff39bb5e 1803static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1804
348e048f 1805static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1806 const struct attribute *,
348e048f
DE
1807 struct dwarf2_cu **);
1808
10b3939b 1809static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1810 const struct attribute *,
f2f0e013 1811 struct dwarf2_cu **);
c906108c 1812
348e048f 1813static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1814 const struct attribute *,
348e048f
DE
1815 struct dwarf2_cu **);
1816
ac9ec31b
DE
1817static struct type *get_signatured_type (struct die_info *, ULONGEST,
1818 struct dwarf2_cu *);
1819
1820static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1821 const struct attribute *,
ac9ec31b
DE
1822 struct dwarf2_cu *);
1823
e5fe5e75 1824static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1825
52dc124a 1826static void read_signatured_type (struct signatured_type *);
348e048f 1827
63e43d3a
PMR
1828static int attr_to_dynamic_prop (const struct attribute *attr,
1829 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1830 struct dynamic_prop *prop, struct type *type);
63e43d3a 1831
c906108c
SS
1832/* memory allocation interface */
1833
7b5a2f43 1834static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1835
b60c80d6 1836static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1837
43f3e411 1838static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1839
6e5a29e1 1840static int attr_form_is_block (const struct attribute *);
8e19ed76 1841
6e5a29e1 1842static int attr_form_is_section_offset (const struct attribute *);
3690dd37 1843
6e5a29e1 1844static int attr_form_is_constant (const struct attribute *);
3690dd37 1845
6e5a29e1 1846static int attr_form_is_ref (const struct attribute *);
7771576e 1847
8cf6f0b1
TT
1848static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1849 struct dwarf2_loclist_baton *baton,
ff39bb5e 1850 const struct attribute *attr);
8cf6f0b1 1851
ff39bb5e 1852static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1853 struct symbol *sym,
f1e6e072
TT
1854 struct dwarf2_cu *cu,
1855 int is_block);
4c2df51b 1856
d521ce57
TT
1857static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1858 const gdb_byte *info_ptr,
1859 struct abbrev_info *abbrev);
4bb7a0a7 1860
72bf9492
DJ
1861static hashval_t partial_die_hash (const void *item);
1862
1863static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1864
ae038cb0 1865static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618
SM
1866 (sect_offset sect_off, unsigned int offset_in_dwz,
1867 struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1868
9816fde3 1869static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1870 struct die_info *comp_unit_die,
1871 enum language pretend_language);
93311388 1872
ed2dc618 1873static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1874
dee91e82 1875static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1876
f792889a
DJ
1877static struct type *set_die_type (struct die_info *, struct type *,
1878 struct dwarf2_cu *);
1c379e20 1879
ed2dc618 1880static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1881
ed2dc618 1882static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 1883
58f0c718 1884static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
95554aad 1885 enum language);
10b3939b 1886
95554aad
TT
1887static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1888 enum language);
10b3939b 1889
f4dc4d17
DE
1890static void process_full_type_unit (struct dwarf2_per_cu_data *,
1891 enum language);
1892
10b3939b
DJ
1893static void dwarf2_add_dependence (struct dwarf2_cu *,
1894 struct dwarf2_per_cu_data *);
1895
ae038cb0
DJ
1896static void dwarf2_mark (struct dwarf2_cu *);
1897
1898static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1899
b64f50a1 1900static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1901 struct dwarf2_per_cu_data *);
673bfd45 1902
f792889a 1903static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1904
95554aad
TT
1905static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1906 enum language pretend_language);
1907
ed2dc618 1908static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 1909
9a49df9d
AB
1910static struct type *dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu);
1911static struct type *dwarf2_per_cu_addr_sized_int_type
1912 (struct dwarf2_per_cu_data *per_cu, bool unsigned_p);
1913
b303c6f6
AB
1914/* Class, the destructor of which frees all allocated queue entries. This
1915 will only have work to do if an error was thrown while processing the
1916 dwarf. If no error was thrown then the queue entries should have all
1917 been processed, and freed, as we went along. */
1918
1919class dwarf2_queue_guard
1920{
1921public:
1922 dwarf2_queue_guard () = default;
1923
1924 /* Free any entries remaining on the queue. There should only be
1925 entries left if we hit an error while processing the dwarf. */
1926 ~dwarf2_queue_guard ()
1927 {
1928 struct dwarf2_queue_item *item, *last;
1929
1930 item = dwarf2_queue;
1931 while (item)
1932 {
1933 /* Anything still marked queued is likely to be in an
1934 inconsistent state, so discard it. */
1935 if (item->per_cu->queued)
1936 {
1937 if (item->per_cu->cu != NULL)
1938 free_one_cached_comp_unit (item->per_cu);
1939 item->per_cu->queued = 0;
1940 }
1941
1942 last = item;
1943 item = item->next;
1944 xfree (last);
1945 }
1946
1947 dwarf2_queue = dwarf2_queue_tail = NULL;
1948 }
1949};
1950
d721ba37
PA
1951/* The return type of find_file_and_directory. Note, the enclosed
1952 string pointers are only valid while this object is valid. */
1953
1954struct file_and_directory
1955{
1956 /* The filename. This is never NULL. */
1957 const char *name;
1958
1959 /* The compilation directory. NULL if not known. If we needed to
1960 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1961 points directly to the DW_AT_comp_dir string attribute owned by
1962 the obstack that owns the DIE. */
1963 const char *comp_dir;
1964
1965 /* If we needed to build a new string for comp_dir, this is what
1966 owns the storage. */
1967 std::string comp_dir_storage;
1968};
1969
1970static file_and_directory find_file_and_directory (struct die_info *die,
1971 struct dwarf2_cu *cu);
9291a0cd
TT
1972
1973static char *file_full_name (int file, struct line_header *lh,
1974 const char *comp_dir);
1975
43988095
JK
1976/* Expected enum dwarf_unit_type for read_comp_unit_head. */
1977enum class rcuh_kind { COMPILE, TYPE };
1978
d521ce57 1979static const gdb_byte *read_and_check_comp_unit_head
ed2dc618
SM
1980 (struct dwarf2_per_objfile* dwarf2_per_objfile,
1981 struct comp_unit_head *header,
36586728 1982 struct dwarf2_section_info *section,
d521ce57 1983 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
43988095 1984 rcuh_kind section_kind);
36586728 1985
fd820528 1986static void init_cutu_and_read_dies
f4dc4d17 1987 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
58f0c718 1988 int use_existing_cu, int keep, bool skip_partial,
3019eac3
DE
1989 die_reader_func_ftype *die_reader_func, void *data);
1990
dee91e82
DE
1991static void init_cutu_and_read_dies_simple
1992 (struct dwarf2_per_cu_data *this_cu,
1993 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1994
673bfd45 1995static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1996
3019eac3
DE
1997static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1998
57d63ce2 1999static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
2000 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2001 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 2002 ULONGEST signature, int is_debug_types);
a2ce51a0 2003
ed2dc618
SM
2004static struct dwp_file *get_dwp_file
2005 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 2006
3019eac3 2007static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 2008 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
2009
2010static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 2011 (struct signatured_type *, const char *, const char *);
3019eac3 2012
89e63ee4
DE
2013static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2014
263db9a1
TT
2015/* A unique pointer to a dwo_file. */
2016
51ac9db5 2017typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 2018
ed2dc618 2019static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 2020
1b80a9fa 2021static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
2022
2023static void free_line_header_voidp (void *arg);
4390d890
DE
2024\f
2025/* Various complaints about symbol reading that don't abort the process. */
2026
2027static void
2028dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2029{
b98664d3 2030 complaint (_("statement list doesn't fit in .debug_line section"));
4390d890
DE
2031}
2032
2033static void
2034dwarf2_debug_line_missing_file_complaint (void)
2035{
b98664d3 2036 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
2037}
2038
2039static void
2040dwarf2_debug_line_missing_end_sequence_complaint (void)
2041{
b98664d3 2042 complaint (_(".debug_line section has line "
4390d890
DE
2043 "program sequence without an end"));
2044}
2045
2046static void
2047dwarf2_complex_location_expr_complaint (void)
2048{
b98664d3 2049 complaint (_("location expression too complex"));
4390d890
DE
2050}
2051
2052static void
2053dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2054 int arg3)
2055{
b98664d3 2056 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
2057 arg1, arg2, arg3);
2058}
2059
2060static void
2061dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2062{
b98664d3 2063 complaint (_("debug info runs off end of %s section"
4390d890 2064 " [in module %s]"),
a32a8923
DE
2065 get_section_name (section),
2066 get_section_file_name (section));
4390d890 2067}
1b80a9fa 2068
4390d890
DE
2069static void
2070dwarf2_macro_malformed_definition_complaint (const char *arg1)
2071{
b98664d3 2072 complaint (_("macro debug info contains a "
4390d890
DE
2073 "malformed macro definition:\n`%s'"),
2074 arg1);
2075}
2076
2077static void
2078dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2079{
b98664d3 2080 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
2081 arg1, arg2);
2082}
527f3840
JK
2083
2084/* Hash function for line_header_hash. */
2085
2086static hashval_t
2087line_header_hash (const struct line_header *ofs)
2088{
9c541725 2089 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
2090}
2091
2092/* Hash function for htab_create_alloc_ex for line_header_hash. */
2093
2094static hashval_t
2095line_header_hash_voidp (const void *item)
2096{
9a3c8263 2097 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
2098
2099 return line_header_hash (ofs);
2100}
2101
2102/* Equality function for line_header_hash. */
2103
2104static int
2105line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2106{
9a3c8263
SM
2107 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2108 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 2109
9c541725 2110 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
2111 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2112}
2113
4390d890 2114\f
9291a0cd 2115
31aa7e4e
JB
2116/* Read the given attribute value as an address, taking the attribute's
2117 form into account. */
2118
2119static CORE_ADDR
2120attr_value_as_address (struct attribute *attr)
2121{
2122 CORE_ADDR addr;
2123
336d760d
AT
2124 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_addrx
2125 && attr->form != DW_FORM_GNU_addr_index)
31aa7e4e
JB
2126 {
2127 /* Aside from a few clearly defined exceptions, attributes that
2128 contain an address must always be in DW_FORM_addr form.
2129 Unfortunately, some compilers happen to be violating this
2130 requirement by encoding addresses using other forms, such
2131 as DW_FORM_data4 for example. For those broken compilers,
2132 we try to do our best, without any guarantee of success,
2133 to interpret the address correctly. It would also be nice
2134 to generate a complaint, but that would require us to maintain
2135 a list of legitimate cases where a non-address form is allowed,
2136 as well as update callers to pass in at least the CU's DWARF
2137 version. This is more overhead than what we're willing to
2138 expand for a pretty rare case. */
2139 addr = DW_UNSND (attr);
2140 }
2141 else
2142 addr = DW_ADDR (attr);
2143
2144 return addr;
2145}
2146
330cdd98
PA
2147/* See declaration. */
2148
2149dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
4b610737
TT
2150 const dwarf2_debug_sections *names,
2151 bool can_copy_)
2152 : objfile (objfile_),
2153 can_copy (can_copy_)
330cdd98
PA
2154{
2155 if (names == NULL)
2156 names = &dwarf2_elf_names;
2157
2158 bfd *obfd = objfile->obfd;
2159
2160 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2161 locate_sections (obfd, sec, *names);
2162}
2163
2164dwarf2_per_objfile::~dwarf2_per_objfile ()
2165{
2166 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2167 free_cached_comp_units ();
2168
2169 if (quick_file_names_table)
2170 htab_delete (quick_file_names_table);
2171
2172 if (line_header_hash)
2173 htab_delete (line_header_hash);
2174
b76e467d 2175 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 2176 per_cu->imported_symtabs_free ();
fc8e7e75 2177
b2bdb8cf 2178 for (signatured_type *sig_type : all_type_units)
ae640021 2179 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 2180
330cdd98
PA
2181 /* Everything else should be on the objfile obstack. */
2182}
2183
2184/* See declaration. */
2185
2186void
2187dwarf2_per_objfile::free_cached_comp_units ()
2188{
2189 dwarf2_per_cu_data *per_cu = read_in_chain;
2190 dwarf2_per_cu_data **last_chain = &read_in_chain;
2191 while (per_cu != NULL)
2192 {
2193 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2194
fcd3b13d 2195 delete per_cu->cu;
330cdd98
PA
2196 *last_chain = next_cu;
2197 per_cu = next_cu;
2198 }
2199}
2200
11ed8cad
TT
2201/* A helper class that calls free_cached_comp_units on
2202 destruction. */
2203
2204class free_cached_comp_units
2205{
2206public:
2207
2208 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2209 : m_per_objfile (per_objfile)
2210 {
2211 }
2212
2213 ~free_cached_comp_units ()
2214 {
2215 m_per_objfile->free_cached_comp_units ();
2216 }
2217
2218 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2219
2220private:
2221
2222 dwarf2_per_objfile *m_per_objfile;
2223};
2224
c906108c 2225/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
2226 information and return true if we have enough to do something.
2227 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
2228 ELF names are used. CAN_COPY is true for formats where symbol
2229 interposition is possible and so symbol values must follow copy
2230 relocation rules. */
c906108c
SS
2231
2232int
251d32d9 2233dwarf2_has_info (struct objfile *objfile,
4b610737
TT
2234 const struct dwarf2_debug_sections *names,
2235 bool can_copy)
c906108c 2236{
97cbe998
SDJ
2237 if (objfile->flags & OBJF_READNEVER)
2238 return 0;
2239
ed2dc618
SM
2240 struct dwarf2_per_objfile *dwarf2_per_objfile
2241 = get_dwarf2_per_objfile (objfile);
2242
2243 if (dwarf2_per_objfile == NULL)
5bfd760d 2244 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
4b610737
TT
2245 names,
2246 can_copy);
5bfd760d 2247
73869dc2 2248 return (!dwarf2_per_objfile->info.is_virtual
049412e3 2249 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 2250 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 2251 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
2252}
2253
2254/* Return the containing section of virtual section SECTION. */
2255
2256static struct dwarf2_section_info *
2257get_containing_section (const struct dwarf2_section_info *section)
2258{
2259 gdb_assert (section->is_virtual);
2260 return section->s.containing_section;
c906108c
SS
2261}
2262
a32a8923
DE
2263/* Return the bfd owner of SECTION. */
2264
2265static struct bfd *
2266get_section_bfd_owner (const struct dwarf2_section_info *section)
2267{
73869dc2
DE
2268 if (section->is_virtual)
2269 {
2270 section = get_containing_section (section);
2271 gdb_assert (!section->is_virtual);
2272 }
049412e3 2273 return section->s.section->owner;
a32a8923
DE
2274}
2275
2276/* Return the bfd section of SECTION.
2277 Returns NULL if the section is not present. */
2278
2279static asection *
2280get_section_bfd_section (const struct dwarf2_section_info *section)
2281{
73869dc2
DE
2282 if (section->is_virtual)
2283 {
2284 section = get_containing_section (section);
2285 gdb_assert (!section->is_virtual);
2286 }
049412e3 2287 return section->s.section;
a32a8923
DE
2288}
2289
2290/* Return the name of SECTION. */
2291
2292static const char *
2293get_section_name (const struct dwarf2_section_info *section)
2294{
2295 asection *sectp = get_section_bfd_section (section);
2296
2297 gdb_assert (sectp != NULL);
fd361982 2298 return bfd_section_name (sectp);
a32a8923
DE
2299}
2300
2301/* Return the name of the file SECTION is in. */
2302
2303static const char *
2304get_section_file_name (const struct dwarf2_section_info *section)
2305{
2306 bfd *abfd = get_section_bfd_owner (section);
2307
2308 return bfd_get_filename (abfd);
2309}
2310
2311/* Return the id of SECTION.
2312 Returns 0 if SECTION doesn't exist. */
2313
2314static int
2315get_section_id (const struct dwarf2_section_info *section)
2316{
2317 asection *sectp = get_section_bfd_section (section);
2318
2319 if (sectp == NULL)
2320 return 0;
2321 return sectp->id;
2322}
2323
2324/* Return the flags of SECTION.
73869dc2 2325 SECTION (or containing section if this is a virtual section) must exist. */
a32a8923
DE
2326
2327static int
2328get_section_flags (const struct dwarf2_section_info *section)
2329{
2330 asection *sectp = get_section_bfd_section (section);
2331
2332 gdb_assert (sectp != NULL);
fd361982 2333 return bfd_section_flags (sectp);
a32a8923
DE
2334}
2335
251d32d9
TG
2336/* When loading sections, we look either for uncompressed section or for
2337 compressed section names. */
233a11ab
CS
2338
2339static int
251d32d9
TG
2340section_is_p (const char *section_name,
2341 const struct dwarf2_section_names *names)
233a11ab 2342{
251d32d9
TG
2343 if (names->normal != NULL
2344 && strcmp (section_name, names->normal) == 0)
2345 return 1;
2346 if (names->compressed != NULL
2347 && strcmp (section_name, names->compressed) == 0)
2348 return 1;
2349 return 0;
233a11ab
CS
2350}
2351
330cdd98 2352/* See declaration. */
c906108c 2353
330cdd98
PA
2354void
2355dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2356 const dwarf2_debug_sections &names)
c906108c 2357{
fd361982 2358 flagword aflag = bfd_section_flags (sectp);
251d32d9 2359
dc7650b8
JK
2360 if ((aflag & SEC_HAS_CONTENTS) == 0)
2361 {
2362 }
950b7495
KS
2363 else if (elf_section_data (sectp)->this_hdr.sh_size
2364 > bfd_get_file_size (abfd))
2365 {
2366 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
2367 warning (_("Discarding section %s which has a section size (%s"
2368 ") larger than the file size [in module %s]"),
2369 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
2370 bfd_get_filename (abfd));
2371 }
330cdd98 2372 else if (section_is_p (sectp->name, &names.info))
c906108c 2373 {
330cdd98 2374 this->info.s.section = sectp;
fd361982 2375 this->info.size = bfd_section_size (sectp);
c906108c 2376 }
330cdd98 2377 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 2378 {
330cdd98 2379 this->abbrev.s.section = sectp;
fd361982 2380 this->abbrev.size = bfd_section_size (sectp);
c906108c 2381 }
330cdd98 2382 else if (section_is_p (sectp->name, &names.line))
c906108c 2383 {
330cdd98 2384 this->line.s.section = sectp;
fd361982 2385 this->line.size = bfd_section_size (sectp);
c906108c 2386 }
330cdd98 2387 else if (section_is_p (sectp->name, &names.loc))
c906108c 2388 {
330cdd98 2389 this->loc.s.section = sectp;
fd361982 2390 this->loc.size = bfd_section_size (sectp);
c906108c 2391 }
330cdd98 2392 else if (section_is_p (sectp->name, &names.loclists))
43988095 2393 {
330cdd98 2394 this->loclists.s.section = sectp;
fd361982 2395 this->loclists.size = bfd_section_size (sectp);
43988095 2396 }
330cdd98 2397 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 2398 {
330cdd98 2399 this->macinfo.s.section = sectp;
fd361982 2400 this->macinfo.size = bfd_section_size (sectp);
c906108c 2401 }
330cdd98 2402 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 2403 {
330cdd98 2404 this->macro.s.section = sectp;
fd361982 2405 this->macro.size = bfd_section_size (sectp);
cf2c3c16 2406 }
330cdd98 2407 else if (section_is_p (sectp->name, &names.str))
c906108c 2408 {
330cdd98 2409 this->str.s.section = sectp;
fd361982 2410 this->str.size = bfd_section_size (sectp);
c906108c 2411 }
330cdd98 2412 else if (section_is_p (sectp->name, &names.line_str))
43988095 2413 {
330cdd98 2414 this->line_str.s.section = sectp;
fd361982 2415 this->line_str.size = bfd_section_size (sectp);
43988095 2416 }
330cdd98 2417 else if (section_is_p (sectp->name, &names.addr))
3019eac3 2418 {
330cdd98 2419 this->addr.s.section = sectp;
fd361982 2420 this->addr.size = bfd_section_size (sectp);
3019eac3 2421 }
330cdd98 2422 else if (section_is_p (sectp->name, &names.frame))
b6af0555 2423 {
330cdd98 2424 this->frame.s.section = sectp;
fd361982 2425 this->frame.size = bfd_section_size (sectp);
b6af0555 2426 }
330cdd98 2427 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2428 {
330cdd98 2429 this->eh_frame.s.section = sectp;
fd361982 2430 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 2431 }
330cdd98 2432 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2433 {
330cdd98 2434 this->ranges.s.section = sectp;
fd361982 2435 this->ranges.size = bfd_section_size (sectp);
af34e669 2436 }
330cdd98 2437 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2438 {
330cdd98 2439 this->rnglists.s.section = sectp;
fd361982 2440 this->rnglists.size = bfd_section_size (sectp);
43988095 2441 }
330cdd98 2442 else if (section_is_p (sectp->name, &names.types))
348e048f 2443 {
8b70b953
TT
2444 struct dwarf2_section_info type_section;
2445
2446 memset (&type_section, 0, sizeof (type_section));
049412e3 2447 type_section.s.section = sectp;
fd361982 2448 type_section.size = bfd_section_size (sectp);
8b70b953 2449
fd5866f6 2450 this->types.push_back (type_section);
348e048f 2451 }
330cdd98 2452 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2453 {
330cdd98 2454 this->gdb_index.s.section = sectp;
fd361982 2455 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 2456 }
927aa2e7
JK
2457 else if (section_is_p (sectp->name, &names.debug_names))
2458 {
2459 this->debug_names.s.section = sectp;
fd361982 2460 this->debug_names.size = bfd_section_size (sectp);
927aa2e7
JK
2461 }
2462 else if (section_is_p (sectp->name, &names.debug_aranges))
2463 {
2464 this->debug_aranges.s.section = sectp;
fd361982 2465 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 2466 }
dce234bc 2467
fd361982
AM
2468 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2469 && bfd_section_vma (sectp) == 0)
330cdd98 2470 this->has_section_at_zero = true;
c906108c
SS
2471}
2472
fceca515
DE
2473/* A helper function that decides whether a section is empty,
2474 or not present. */
9e0ac564
TT
2475
2476static int
19ac8c2e 2477dwarf2_section_empty_p (const struct dwarf2_section_info *section)
9e0ac564 2478{
73869dc2
DE
2479 if (section->is_virtual)
2480 return section->size == 0;
049412e3 2481 return section->s.section == NULL || section->size == 0;
9e0ac564
TT
2482}
2483
cd4fb1b2 2484/* See dwarf2read.h. */
c906108c 2485
cd4fb1b2
SM
2486void
2487dwarf2_read_section (struct objfile *objfile, dwarf2_section_info *info)
c906108c 2488{
a32a8923 2489 asection *sectp;
3019eac3 2490 bfd *abfd;
dce234bc 2491 gdb_byte *buf, *retbuf;
c906108c 2492
be391dca
TT
2493 if (info->readin)
2494 return;
dce234bc 2495 info->buffer = NULL;
dc4ccb6f 2496 info->readin = true;
188dd5d6 2497
9e0ac564 2498 if (dwarf2_section_empty_p (info))
dce234bc 2499 return;
c906108c 2500
a32a8923 2501 sectp = get_section_bfd_section (info);
3019eac3 2502
73869dc2
DE
2503 /* If this is a virtual section we need to read in the real one first. */
2504 if (info->is_virtual)
2505 {
2506 struct dwarf2_section_info *containing_section =
2507 get_containing_section (info);
2508
2509 gdb_assert (sectp != NULL);
2510 if ((sectp->flags & SEC_RELOC) != 0)
2511 {
2512 error (_("Dwarf Error: DWP format V2 with relocations is not"
2513 " supported in section %s [in module %s]"),
2514 get_section_name (info), get_section_file_name (info));
2515 }
2516 dwarf2_read_section (objfile, containing_section);
2517 /* Other code should have already caught virtual sections that don't
2518 fit. */
2519 gdb_assert (info->virtual_offset + info->size
2520 <= containing_section->size);
2521 /* If the real section is empty or there was a problem reading the
2522 section we shouldn't get here. */
2523 gdb_assert (containing_section->buffer != NULL);
2524 info->buffer = containing_section->buffer + info->virtual_offset;
2525 return;
2526 }
2527
4bf44c1c
TT
2528 /* If the section has relocations, we must read it ourselves.
2529 Otherwise we attach it to the BFD. */
2530 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 2531 {
d521ce57 2532 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 2533 return;
dce234bc 2534 }
dce234bc 2535
224c3ddb 2536 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
4bf44c1c 2537 info->buffer = buf;
dce234bc
PP
2538
2539 /* When debugging .o files, we may need to apply relocations; see
2540 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2541 We never compress sections in .o files, so we only need to
2542 try this when the section is not compressed. */
ac8035ab 2543 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2544 if (retbuf != NULL)
2545 {
2546 info->buffer = retbuf;
2547 return;
2548 }
2549
a32a8923
DE
2550 abfd = get_section_bfd_owner (info);
2551 gdb_assert (abfd != NULL);
2552
dce234bc
PP
2553 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2554 || bfd_bread (buf, info->size, abfd) != info->size)
19ac8c2e
DE
2555 {
2556 error (_("Dwarf Error: Can't read DWARF data"
2557 " in section %s [in module %s]"),
fd361982 2558 bfd_section_name (sectp), bfd_get_filename (abfd));
19ac8c2e 2559 }
dce234bc
PP
2560}
2561
9e0ac564
TT
2562/* A helper function that returns the size of a section in a safe way.
2563 If you are positive that the section has been read before using the
2564 size, then it is safe to refer to the dwarf2_section_info object's
2565 "size" field directly. In other cases, you must call this
2566 function, because for compressed sections the size field is not set
2567 correctly until the section has been read. */
2568
2569static bfd_size_type
2570dwarf2_section_size (struct objfile *objfile,
2571 struct dwarf2_section_info *info)
2572{
2573 if (!info->readin)
2574 dwarf2_read_section (objfile, info);
2575 return info->size;
2576}
2577
dce234bc 2578/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2579 SECTION_NAME. */
af34e669 2580
dce234bc 2581void
3017a003
TG
2582dwarf2_get_section_info (struct objfile *objfile,
2583 enum dwarf2_section_enum sect,
d521ce57 2584 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2585 bfd_size_type *sizep)
2586{
5bfd760d 2587 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
dce234bc 2588 struct dwarf2_section_info *info;
a3b2a86b
TT
2589
2590 /* We may see an objfile without any DWARF, in which case we just
2591 return nothing. */
2592 if (data == NULL)
2593 {
2594 *sectp = NULL;
2595 *bufp = NULL;
2596 *sizep = 0;
2597 return;
2598 }
3017a003
TG
2599 switch (sect)
2600 {
2601 case DWARF2_DEBUG_FRAME:
2602 info = &data->frame;
2603 break;
2604 case DWARF2_EH_FRAME:
2605 info = &data->eh_frame;
2606 break;
2607 default:
2608 gdb_assert_not_reached ("unexpected section");
2609 }
dce234bc 2610
9e0ac564 2611 dwarf2_read_section (objfile, info);
dce234bc 2612
a32a8923 2613 *sectp = get_section_bfd_section (info);
dce234bc
PP
2614 *bufp = info->buffer;
2615 *sizep = info->size;
2616}
2617
36586728
TT
2618/* A helper function to find the sections for a .dwz file. */
2619
2620static void
2621locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2622{
9a3c8263 2623 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2624
2625 /* Note that we only support the standard ELF names, because .dwz
2626 is ELF-only (at the time of writing). */
2627 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2628 {
049412e3 2629 dwz_file->abbrev.s.section = sectp;
fd361982 2630 dwz_file->abbrev.size = bfd_section_size (sectp);
36586728
TT
2631 }
2632 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2633 {
049412e3 2634 dwz_file->info.s.section = sectp;
fd361982 2635 dwz_file->info.size = bfd_section_size (sectp);
36586728
TT
2636 }
2637 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2638 {
049412e3 2639 dwz_file->str.s.section = sectp;
fd361982 2640 dwz_file->str.size = bfd_section_size (sectp);
36586728
TT
2641 }
2642 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2643 {
049412e3 2644 dwz_file->line.s.section = sectp;
fd361982 2645 dwz_file->line.size = bfd_section_size (sectp);
36586728
TT
2646 }
2647 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2648 {
049412e3 2649 dwz_file->macro.s.section = sectp;
fd361982 2650 dwz_file->macro.size = bfd_section_size (sectp);
36586728 2651 }
2ec9a5e0
TT
2652 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2653 {
049412e3 2654 dwz_file->gdb_index.s.section = sectp;
fd361982 2655 dwz_file->gdb_index.size = bfd_section_size (sectp);
2ec9a5e0 2656 }
927aa2e7
JK
2657 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2658 {
2659 dwz_file->debug_names.s.section = sectp;
fd361982 2660 dwz_file->debug_names.size = bfd_section_size (sectp);
927aa2e7 2661 }
36586728
TT
2662}
2663
c4973306 2664/* See dwarf2read.h. */
36586728 2665
c4973306 2666struct dwz_file *
ed2dc618 2667dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 2668{
36586728 2669 const char *filename;
acd13123 2670 bfd_size_type buildid_len_arg;
dc294be5
TT
2671 size_t buildid_len;
2672 bfd_byte *buildid;
36586728
TT
2673
2674 if (dwarf2_per_objfile->dwz_file != NULL)
7ff8cb8c 2675 return dwarf2_per_objfile->dwz_file.get ();
36586728 2676
4db1a1dc 2677 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2678 gdb::unique_xmalloc_ptr<char> data
2679 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2680 &buildid_len_arg, &buildid));
4db1a1dc
TT
2681 if (data == NULL)
2682 {
2683 if (bfd_get_error () == bfd_error_no_error)
2684 return NULL;
2685 error (_("could not read '.gnu_debugaltlink' section: %s"),
2686 bfd_errmsg (bfd_get_error ()));
2687 }
791afaa2
TT
2688
2689 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2690
acd13123
TT
2691 buildid_len = (size_t) buildid_len_arg;
2692
791afaa2 2693 filename = data.get ();
d721ba37
PA
2694
2695 std::string abs_storage;
36586728
TT
2696 if (!IS_ABSOLUTE_PATH (filename))
2697 {
14278e1f
TT
2698 gdb::unique_xmalloc_ptr<char> abs
2699 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2700
14278e1f 2701 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2702 filename = abs_storage.c_str ();
36586728
TT
2703 }
2704
dc294be5
TT
2705 /* First try the file name given in the section. If that doesn't
2706 work, try to use the build-id instead. */
192b62ce 2707 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2708 if (dwz_bfd != NULL)
36586728 2709 {
192b62ce 2710 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2711 dwz_bfd.reset (nullptr);
36586728
TT
2712 }
2713
dc294be5
TT
2714 if (dwz_bfd == NULL)
2715 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2716
2717 if (dwz_bfd == NULL)
2718 error (_("could not find '.gnu_debugaltlink' file for %s"),
2719 objfile_name (dwarf2_per_objfile->objfile));
2720
7ff8cb8c
TT
2721 std::unique_ptr<struct dwz_file> result
2722 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2723
7ff8cb8c
TT
2724 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2725 result.get ());
36586728 2726
7ff8cb8c
TT
2727 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2728 result->dwz_bfd.get ());
2729 dwarf2_per_objfile->dwz_file = std::move (result);
2730 return dwarf2_per_objfile->dwz_file.get ();
36586728 2731}
9291a0cd 2732\f
7b9f3c50
DE
2733/* DWARF quick_symbols_functions support. */
2734
2735/* TUs can share .debug_line entries, and there can be a lot more TUs than
2736 unique line tables, so we maintain a separate table of all .debug_line
2737 derived entries to support the sharing.
2738 All the quick functions need is the list of file names. We discard the
2739 line_header when we're done and don't need to record it here. */
2740struct quick_file_names
2741{
094b34ac
DE
2742 /* The data used to construct the hash key. */
2743 struct stmt_list_hash hash;
7b9f3c50
DE
2744
2745 /* The number of entries in file_names, real_names. */
2746 unsigned int num_file_names;
2747
2748 /* The file names from the line table, after being run through
2749 file_full_name. */
2750 const char **file_names;
2751
2752 /* The file names from the line table after being run through
2753 gdb_realpath. These are computed lazily. */
2754 const char **real_names;
2755};
2756
2757/* When using the index (and thus not using psymtabs), each CU has an
2758 object of this type. This is used to hold information needed by
2759 the various "quick" methods. */
2760struct dwarf2_per_cu_quick_data
2761{
2762 /* The file table. This can be NULL if there was no file table
2763 or it's currently not read in.
2764 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2765 struct quick_file_names *file_names;
2766
2767 /* The corresponding symbol table. This is NULL if symbols for this
2768 CU have not yet been read. */
43f3e411 2769 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2770
2771 /* A temporary mark bit used when iterating over all CUs in
2772 expand_symtabs_matching. */
2773 unsigned int mark : 1;
2774
2775 /* True if we've tried to read the file table and found there isn't one.
2776 There will be no point in trying to read it again next time. */
2777 unsigned int no_file_data : 1;
2778};
2779
094b34ac
DE
2780/* Utility hash function for a stmt_list_hash. */
2781
2782static hashval_t
2783hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2784{
2785 hashval_t v = 0;
2786
2787 if (stmt_list_hash->dwo_unit != NULL)
2788 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2789 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2790 return v;
2791}
2792
2793/* Utility equality function for a stmt_list_hash. */
2794
2795static int
2796eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2797 const struct stmt_list_hash *rhs)
2798{
2799 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2800 return 0;
2801 if (lhs->dwo_unit != NULL
2802 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2803 return 0;
2804
9c541725 2805 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2806}
2807
7b9f3c50
DE
2808/* Hash function for a quick_file_names. */
2809
2810static hashval_t
2811hash_file_name_entry (const void *e)
2812{
9a3c8263
SM
2813 const struct quick_file_names *file_data
2814 = (const struct quick_file_names *) e;
7b9f3c50 2815
094b34ac 2816 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2817}
2818
2819/* Equality function for a quick_file_names. */
2820
2821static int
2822eq_file_name_entry (const void *a, const void *b)
2823{
9a3c8263
SM
2824 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2825 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2826
094b34ac 2827 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2828}
2829
2830/* Delete function for a quick_file_names. */
2831
2832static void
2833delete_file_name_entry (void *e)
2834{
9a3c8263 2835 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2836 int i;
2837
2838 for (i = 0; i < file_data->num_file_names; ++i)
2839 {
2840 xfree ((void*) file_data->file_names[i]);
2841 if (file_data->real_names)
2842 xfree ((void*) file_data->real_names[i]);
2843 }
2844
2845 /* The space for the struct itself lives on objfile_obstack,
2846 so we don't free it here. */
2847}
2848
2849/* Create a quick_file_names hash table. */
2850
2851static htab_t
2852create_quick_file_names_table (unsigned int nr_initial_entries)
2853{
2854 return htab_create_alloc (nr_initial_entries,
2855 hash_file_name_entry, eq_file_name_entry,
2856 delete_file_name_entry, xcalloc, xfree);
2857}
9291a0cd 2858
918dd910
JK
2859/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2860 have to be created afterwards. You should call age_cached_comp_units after
2861 processing PER_CU->CU. dw2_setup must have been already called. */
2862
2863static void
58f0c718 2864load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
918dd910 2865{
3019eac3 2866 if (per_cu->is_debug_types)
e5fe5e75 2867 load_full_type_unit (per_cu);
918dd910 2868 else
58f0c718 2869 load_full_comp_unit (per_cu, skip_partial, language_minimal);
918dd910 2870
cc12ce38
DE
2871 if (per_cu->cu == NULL)
2872 return; /* Dummy CU. */
2dc860c0
DE
2873
2874 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2875}
2876
a0f42c21 2877/* Read in the symbols for PER_CU. */
2fdf6df6 2878
9291a0cd 2879static void
58f0c718 2880dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2881{
ed2dc618 2882 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9291a0cd 2883
f4dc4d17
DE
2884 /* Skip type_unit_groups, reading the type units they contain
2885 is handled elsewhere. */
2886 if (IS_TYPE_UNIT_GROUP (per_cu))
2887 return;
2888
b303c6f6
AB
2889 /* The destructor of dwarf2_queue_guard frees any entries left on
2890 the queue. After this point we're guaranteed to leave this function
2891 with the dwarf queue empty. */
2892 dwarf2_queue_guard q_guard;
9291a0cd 2893
95554aad 2894 if (dwarf2_per_objfile->using_index
43f3e411 2895 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2896 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2897 {
2898 queue_comp_unit (per_cu, language_minimal);
58f0c718 2899 load_cu (per_cu, skip_partial);
89e63ee4
DE
2900
2901 /* If we just loaded a CU from a DWO, and we're working with an index
2902 that may badly handle TUs, load all the TUs in that DWO as well.
2903 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2904 if (!per_cu->is_debug_types
cc12ce38 2905 && per_cu->cu != NULL
89e63ee4
DE
2906 && per_cu->cu->dwo_unit != NULL
2907 && dwarf2_per_objfile->index_table != NULL
2908 && dwarf2_per_objfile->index_table->version <= 7
2909 /* DWP files aren't supported yet. */
ed2dc618 2910 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 2911 queue_and_load_all_dwo_tus (per_cu);
95554aad 2912 }
9291a0cd 2913
ed2dc618 2914 process_queue (dwarf2_per_objfile);
9291a0cd
TT
2915
2916 /* Age the cache, releasing compilation units that have not
2917 been used recently. */
ed2dc618 2918 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
2919}
2920
2921/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2922 the objfile from which this CU came. Returns the resulting symbol
2923 table. */
2fdf6df6 2924
43f3e411 2925static struct compunit_symtab *
58f0c718 2926dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2927{
ed2dc618
SM
2928 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2929
95554aad 2930 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2931 if (!per_cu->v.quick->compunit_symtab)
9291a0cd 2932 {
11ed8cad 2933 free_cached_comp_units freer (dwarf2_per_objfile);
c83dd867 2934 scoped_restore decrementer = increment_reading_symtab ();
58f0c718 2935 dw2_do_instantiate_symtab (per_cu, skip_partial);
ed2dc618 2936 process_cu_includes (dwarf2_per_objfile);
9291a0cd 2937 }
f194fefb 2938
43f3e411 2939 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2940}
2941
ff4c9fec 2942/* See declaration. */
f4dc4d17 2943
ff4c9fec
SM
2944dwarf2_per_cu_data *
2945dwarf2_per_objfile::get_cutu (int index)
2946{
b76e467d 2947 if (index >= this->all_comp_units.size ())
ff4c9fec 2948 {
b76e467d 2949 index -= this->all_comp_units.size ();
b2bdb8cf 2950 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2951 return &this->all_type_units[index]->per_cu;
2952 }
f4dc4d17 2953
ff4c9fec
SM
2954 return this->all_comp_units[index];
2955}
f4dc4d17 2956
ff4c9fec 2957/* See declaration. */
2fdf6df6 2958
ff4c9fec
SM
2959dwarf2_per_cu_data *
2960dwarf2_per_objfile::get_cu (int index)
1fd400ff 2961{
b76e467d 2962 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2963
ff4c9fec 2964 return this->all_comp_units[index];
f4dc4d17
DE
2965}
2966
ff4c9fec 2967/* See declaration. */
f4dc4d17 2968
ff4c9fec
SM
2969signatured_type *
2970dwarf2_per_objfile::get_tu (int index)
f4dc4d17 2971{
b2bdb8cf 2972 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2973
ff4c9fec 2974 return this->all_type_units[index];
1fd400ff
TT
2975}
2976
4b514bc8
JK
2977/* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2978 objfile_obstack, and constructed with the specified field
2979 values. */
2980
2981static dwarf2_per_cu_data *
ed2dc618 2982create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
2983 struct dwarf2_section_info *section,
2984 int is_dwz,
2985 sect_offset sect_off, ULONGEST length)
2986{
ed2dc618 2987 struct objfile *objfile = dwarf2_per_objfile->objfile;
4b514bc8
JK
2988 dwarf2_per_cu_data *the_cu
2989 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2990 struct dwarf2_per_cu_data);
2991 the_cu->sect_off = sect_off;
2992 the_cu->length = length;
e3b94546 2993 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
4b514bc8
JK
2994 the_cu->section = section;
2995 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2996 struct dwarf2_per_cu_quick_data);
2997 the_cu->is_dwz = is_dwz;
2998 return the_cu;
2999}
3000
2ec9a5e0
TT
3001/* A helper for create_cus_from_index that handles a given list of
3002 CUs. */
2fdf6df6 3003
74a0d9f6 3004static void
12359b5e 3005create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
3006 const gdb_byte *cu_list, offset_type n_elements,
3007 struct dwarf2_section_info *section,
b76e467d 3008 int is_dwz)
9291a0cd 3009{
12359b5e 3010 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 3011 {
74a0d9f6 3012 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3013
3014 sect_offset sect_off
3015 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
3016 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
3017 cu_list += 2 * 8;
3018
b76e467d 3019 dwarf2_per_cu_data *per_cu
ed2dc618
SM
3020 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
3021 sect_off, length);
b76e467d 3022 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
9291a0cd 3023 }
9291a0cd
TT
3024}
3025
2ec9a5e0 3026/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 3027 the CU objects for this objfile. */
2ec9a5e0 3028
74a0d9f6 3029static void
12359b5e 3030create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
3031 const gdb_byte *cu_list, offset_type cu_list_elements,
3032 const gdb_byte *dwz_list, offset_type dwz_elements)
3033{
b76e467d
SM
3034 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
3035 dwarf2_per_objfile->all_comp_units.reserve
3036 ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 3037
12359b5e 3038 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
b76e467d 3039 &dwarf2_per_objfile->info, 0);
2ec9a5e0
TT
3040
3041 if (dwz_elements == 0)
74a0d9f6 3042 return;
2ec9a5e0 3043
12359b5e
SM
3044 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3045 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
b76e467d 3046 &dwz->info, 1);
2ec9a5e0
TT
3047}
3048
1fd400ff 3049/* Create the signatured type hash table from the index. */
673bfd45 3050
74a0d9f6 3051static void
12359b5e
SM
3052create_signatured_type_table_from_index
3053 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3054 struct dwarf2_section_info *section,
3055 const gdb_byte *bytes,
3056 offset_type elements)
1fd400ff 3057{
12359b5e 3058 struct objfile *objfile = dwarf2_per_objfile->objfile;
1fd400ff 3059
b2bdb8cf
SM
3060 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3061 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
1fd400ff 3062
12359b5e 3063 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff 3064
12359b5e 3065 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 3066 {
52dc124a 3067 struct signatured_type *sig_type;
9c541725 3068 ULONGEST signature;
1fd400ff 3069 void **slot;
9c541725 3070 cu_offset type_offset_in_tu;
1fd400ff 3071
74a0d9f6 3072 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3073 sect_offset sect_off
3074 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3075 type_offset_in_tu
3076 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3077 BFD_ENDIAN_LITTLE);
1fd400ff
TT
3078 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3079 bytes += 3 * 8;
3080
52dc124a 3081 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 3082 struct signatured_type);
52dc124a 3083 sig_type->signature = signature;
9c541725 3084 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 3085 sig_type->per_cu.is_debug_types = 1;
8a0459fd 3086 sig_type->per_cu.section = section;
9c541725 3087 sig_type->per_cu.sect_off = sect_off;
e3b94546 3088 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
52dc124a 3089 sig_type->per_cu.v.quick
1fd400ff
TT
3090 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3091 struct dwarf2_per_cu_quick_data);
3092
52dc124a
DE
3093 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3094 *slot = sig_type;
1fd400ff 3095
b2bdb8cf 3096 dwarf2_per_objfile->all_type_units.push_back (sig_type);
1fd400ff
TT
3097 }
3098
673bfd45 3099 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
3100}
3101
927aa2e7
JK
3102/* Create the signatured type hash table from .debug_names. */
3103
3104static void
3105create_signatured_type_table_from_debug_names
ed2dc618 3106 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3107 const mapped_debug_names &map,
3108 struct dwarf2_section_info *section,
3109 struct dwarf2_section_info *abbrev_section)
3110{
ed2dc618
SM
3111 struct objfile *objfile = dwarf2_per_objfile->objfile;
3112
927aa2e7
JK
3113 dwarf2_read_section (objfile, section);
3114 dwarf2_read_section (objfile, abbrev_section);
3115
b2bdb8cf
SM
3116 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3117 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
927aa2e7
JK
3118
3119 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3120
3121 for (uint32_t i = 0; i < map.tu_count; ++i)
3122 {
3123 struct signatured_type *sig_type;
927aa2e7 3124 void **slot;
927aa2e7
JK
3125
3126 sect_offset sect_off
3127 = (sect_offset) (extract_unsigned_integer
3128 (map.tu_table_reordered + i * map.offset_size,
3129 map.offset_size,
3130 map.dwarf5_byte_order));
3131
3132 comp_unit_head cu_header;
ed2dc618
SM
3133 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3134 abbrev_section,
927aa2e7
JK
3135 section->buffer + to_underlying (sect_off),
3136 rcuh_kind::TYPE);
3137
3138 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3139 struct signatured_type);
3140 sig_type->signature = cu_header.signature;
3141 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3142 sig_type->per_cu.is_debug_types = 1;
3143 sig_type->per_cu.section = section;
3144 sig_type->per_cu.sect_off = sect_off;
e3b94546 3145 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
927aa2e7
JK
3146 sig_type->per_cu.v.quick
3147 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3148 struct dwarf2_per_cu_quick_data);
3149
3150 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3151 *slot = sig_type;
3152
b2bdb8cf 3153 dwarf2_per_objfile->all_type_units.push_back (sig_type);
927aa2e7
JK
3154 }
3155
3156 dwarf2_per_objfile->signatured_types = sig_types_hash;
3157}
3158
9291a0cd
TT
3159/* Read the address map data from the mapped index, and use it to
3160 populate the objfile's psymtabs_addrmap. */
2fdf6df6 3161
9291a0cd 3162static void
ed2dc618
SM
3163create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3164 struct mapped_index *index)
9291a0cd 3165{
ed2dc618 3166 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 3167 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 3168 const gdb_byte *iter, *end;
9291a0cd 3169 struct addrmap *mutable_map;
9291a0cd
TT
3170 CORE_ADDR baseaddr;
3171
8268c778
PA
3172 auto_obstack temp_obstack;
3173
9291a0cd
TT
3174 mutable_map = addrmap_create_mutable (&temp_obstack);
3175
f00a2de2
PA
3176 iter = index->address_table.data ();
3177 end = iter + index->address_table.size ();
9291a0cd
TT
3178
3179 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3180
3181 while (iter < end)
3182 {
3183 ULONGEST hi, lo, cu_index;
3184 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3185 iter += 8;
3186 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3187 iter += 8;
3188 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3189 iter += 4;
f652bce2 3190
24a55014 3191 if (lo > hi)
f652bce2 3192 {
b98664d3 3193 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 3194 hex_string (lo), hex_string (hi));
24a55014 3195 continue;
f652bce2 3196 }
24a55014 3197
b76e467d 3198 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
f652bce2 3199 {
b98664d3 3200 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 3201 (unsigned) cu_index);
24a55014 3202 continue;
f652bce2 3203 }
24a55014 3204
79748972
TT
3205 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
3206 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 3207 addrmap_set_empty (mutable_map, lo, hi - 1,
ff4c9fec 3208 dwarf2_per_objfile->get_cu (cu_index));
9291a0cd
TT
3209 }
3210
d320c2b5 3211 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 3212 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
3213}
3214
927aa2e7
JK
3215/* Read the address map data from DWARF-5 .debug_aranges, and use it to
3216 populate the objfile's psymtabs_addrmap. */
3217
3218static void
ed2dc618 3219create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3220 struct dwarf2_section_info *section)
3221{
ed2dc618 3222 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
3223 bfd *abfd = objfile->obfd;
3224 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3225 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3226 SECT_OFF_TEXT (objfile));
3227
3228 auto_obstack temp_obstack;
3229 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3230
3231 std::unordered_map<sect_offset,
3232 dwarf2_per_cu_data *,
3233 gdb::hash_enum<sect_offset>>
3234 debug_info_offset_to_per_cu;
b76e467d 3235 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 3236 {
927aa2e7
JK
3237 const auto insertpair
3238 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3239 if (!insertpair.second)
3240 {
3241 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
3242 "debug_info_offset %s, ignoring .debug_aranges."),
3243 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
3244 return;
3245 }
3246 }
3247
3248 dwarf2_read_section (objfile, section);
3249
3250 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3251
3252 const gdb_byte *addr = section->buffer;
3253
3254 while (addr < section->buffer + section->size)
3255 {
3256 const gdb_byte *const entry_addr = addr;
3257 unsigned int bytes_read;
3258
3259 const LONGEST entry_length = read_initial_length (abfd, addr,
3260 &bytes_read);
3261 addr += bytes_read;
3262
3263 const gdb_byte *const entry_end = addr + entry_length;
3264 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3265 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3266 if (addr + entry_length > section->buffer + section->size)
3267 {
47e3f474 3268 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3269 "length %s exceeds section length %s, "
3270 "ignoring .debug_aranges."),
47e3f474
TV
3271 objfile_name (objfile),
3272 plongest (entry_addr - section->buffer),
927aa2e7
JK
3273 plongest (bytes_read + entry_length),
3274 pulongest (section->size));
3275 return;
3276 }
3277
3278 /* The version number. */
3279 const uint16_t version = read_2_bytes (abfd, addr);
3280 addr += 2;
3281 if (version != 2)
3282 {
47e3f474 3283 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 3284 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
3285 objfile_name (objfile),
3286 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
3287 return;
3288 }
3289
3290 const uint64_t debug_info_offset
3291 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3292 addr += offset_size;
3293 const auto per_cu_it
3294 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3295 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3296 {
47e3f474 3297 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3298 "debug_info_offset %s does not exists, "
3299 "ignoring .debug_aranges."),
47e3f474
TV
3300 objfile_name (objfile),
3301 plongest (entry_addr - section->buffer),
927aa2e7
JK
3302 pulongest (debug_info_offset));
3303 return;
3304 }
3305 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3306
3307 const uint8_t address_size = *addr++;
3308 if (address_size < 1 || address_size > 8)
3309 {
47e3f474 3310 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 3311 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
3312 objfile_name (objfile),
3313 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
3314 return;
3315 }
3316
3317 const uint8_t segment_selector_size = *addr++;
3318 if (segment_selector_size != 0)
3319 {
47e3f474 3320 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3321 "segment_selector_size %u is not supported, "
3322 "ignoring .debug_aranges."),
47e3f474
TV
3323 objfile_name (objfile),
3324 plongest (entry_addr - section->buffer),
927aa2e7
JK
3325 segment_selector_size);
3326 return;
3327 }
3328
3329 /* Must pad to an alignment boundary that is twice the address
3330 size. It is undocumented by the DWARF standard but GCC does
3331 use it. */
3332 for (size_t padding = ((-(addr - section->buffer))
3333 & (2 * address_size - 1));
3334 padding > 0; padding--)
3335 if (*addr++ != 0)
3336 {
47e3f474 3337 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 3338 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
3339 objfile_name (objfile),
3340 plongest (entry_addr - section->buffer));
927aa2e7
JK
3341 return;
3342 }
3343
3344 for (;;)
3345 {
3346 if (addr + 2 * address_size > entry_end)
3347 {
47e3f474 3348 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3349 "address list is not properly terminated, "
3350 "ignoring .debug_aranges."),
47e3f474
TV
3351 objfile_name (objfile),
3352 plongest (entry_addr - section->buffer));
927aa2e7
JK
3353 return;
3354 }
3355 ULONGEST start = extract_unsigned_integer (addr, address_size,
3356 dwarf5_byte_order);
3357 addr += address_size;
3358 ULONGEST length = extract_unsigned_integer (addr, address_size,
3359 dwarf5_byte_order);
3360 addr += address_size;
3361 if (start == 0 && length == 0)
3362 break;
3363 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3364 {
3365 /* Symbol was eliminated due to a COMDAT group. */
3366 continue;
3367 }
3368 ULONGEST end = start + length;
79748972
TT
3369 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3370 - baseaddr);
3371 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3372 - baseaddr);
927aa2e7
JK
3373 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3374 }
3375 }
3376
d320c2b5 3377 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 3378 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
3379}
3380
9291a0cd
TT
3381/* Find a slot in the mapped index INDEX for the object named NAME.
3382 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
3383 constant pool and return true. If NAME cannot be found, return
3384 false. */
2fdf6df6 3385
109483d9 3386static bool
9291a0cd
TT
3387find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3388 offset_type **vec_out)
3389{
0cf03b49 3390 offset_type hash;
9291a0cd 3391 offset_type slot, step;
559a7a62 3392 int (*cmp) (const char *, const char *);
9291a0cd 3393
791afaa2 3394 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 3395 if (current_language->la_language == language_cplus
45280282
IB
3396 || current_language->la_language == language_fortran
3397 || current_language->la_language == language_d)
0cf03b49
JK
3398 {
3399 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3400 not contain any. */
a8719064 3401
72998fb3 3402 if (strchr (name, '(') != NULL)
0cf03b49 3403 {
109483d9 3404 without_params = cp_remove_params (name);
0cf03b49 3405
72998fb3 3406 if (without_params != NULL)
791afaa2 3407 name = without_params.get ();
0cf03b49
JK
3408 }
3409 }
3410
559a7a62 3411 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3412 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3413 simulate our NAME being searched is also lowercased. */
3414 hash = mapped_index_string_hash ((index->version == 4
3415 && case_sensitivity == case_sensitive_off
3416 ? 5 : index->version),
3417 name);
3418
f00a2de2
PA
3419 slot = hash & (index->symbol_table.size () - 1);
3420 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 3421 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3422
3423 for (;;)
3424 {
9291a0cd 3425 const char *str;
f00a2de2
PA
3426
3427 const auto &bucket = index->symbol_table[slot];
3428 if (bucket.name == 0 && bucket.vec == 0)
109483d9 3429 return false;
9291a0cd 3430
f00a2de2 3431 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 3432 if (!cmp (name, str))
9291a0cd
TT
3433 {
3434 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 3435 + MAYBE_SWAP (bucket.vec));
109483d9 3436 return true;
9291a0cd
TT
3437 }
3438
f00a2de2 3439 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
3440 }
3441}
3442
4485a1c1
SM
3443/* A helper function that reads the .gdb_index from BUFFER and fills
3444 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 3445 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
3446 ok to use deprecated sections.
3447
3448 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3449 out parameters that are filled in with information about the CU and
3450 TU lists in the section.
3451
4485a1c1 3452 Returns true if all went well, false otherwise. */
2fdf6df6 3453
d33bc52e 3454static bool
4485a1c1
SM
3455read_gdb_index_from_buffer (struct objfile *objfile,
3456 const char *filename,
3457 bool deprecated_ok,
3458 gdb::array_view<const gdb_byte> buffer,
3459 struct mapped_index *map,
3460 const gdb_byte **cu_list,
3461 offset_type *cu_list_elements,
3462 const gdb_byte **types_list,
3463 offset_type *types_list_elements)
3464{
3465 const gdb_byte *addr = &buffer[0];
82430852 3466
9291a0cd 3467 /* Version check. */
4485a1c1 3468 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3469 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3470 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3471 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3472 indices. */
831adc1f 3473 if (version < 4)
481860b3
GB
3474 {
3475 static int warning_printed = 0;
3476 if (!warning_printed)
3477 {
3478 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3479 filename);
481860b3
GB
3480 warning_printed = 1;
3481 }
3482 return 0;
3483 }
3484 /* Index version 4 uses a different hash function than index version
3485 5 and later.
3486
3487 Versions earlier than 6 did not emit psymbols for inlined
3488 functions. Using these files will cause GDB not to be able to
3489 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3490 indices unless the user has done
3491 "set use-deprecated-index-sections on". */
2ec9a5e0 3492 if (version < 6 && !deprecated_ok)
481860b3
GB
3493 {
3494 static int warning_printed = 0;
3495 if (!warning_printed)
3496 {
e615022a
DE
3497 warning (_("\
3498Skipping deprecated .gdb_index section in %s.\n\
3499Do \"set use-deprecated-index-sections on\" before the file is read\n\
3500to use the section anyway."),
2ec9a5e0 3501 filename);
481860b3
GB
3502 warning_printed = 1;
3503 }
3504 return 0;
3505 }
796a7ff8 3506 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3507 of the TU (for symbols coming from TUs),
3508 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3509 Plus gold-generated indices can have duplicate entries for global symbols,
3510 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3511 These are just performance bugs, and we can't distinguish gdb-generated
3512 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3513
481860b3 3514 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3515 longer backward compatible. */
796a7ff8 3516 if (version > 8)
594e8718 3517 return 0;
9291a0cd 3518
559a7a62 3519 map->version = version;
9291a0cd 3520
4485a1c1 3521 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 3522
4485a1c1 3523 int i = 0;
2ec9a5e0
TT
3524 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3525 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3526 / 8);
1fd400ff
TT
3527 ++i;
3528
2ec9a5e0
TT
3529 *types_list = addr + MAYBE_SWAP (metadata[i]);
3530 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3531 - MAYBE_SWAP (metadata[i]))
3532 / 8);
987d643c 3533 ++i;
1fd400ff 3534
f00a2de2
PA
3535 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3536 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3537 map->address_table
3538 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3539 ++i;
3540
f00a2de2
PA
3541 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3542 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3543 map->symbol_table
3544 = gdb::array_view<mapped_index::symbol_table_slot>
3545 ((mapped_index::symbol_table_slot *) symbol_table,
3546 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3547
f00a2de2 3548 ++i;
f9d83a0b 3549 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3550
2ec9a5e0
TT
3551 return 1;
3552}
3553
4485a1c1
SM
3554/* Callback types for dwarf2_read_gdb_index. */
3555
3556typedef gdb::function_view
3557 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
3558 get_gdb_index_contents_ftype;
3559typedef gdb::function_view
3560 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3561 get_gdb_index_contents_dwz_ftype;
3562
927aa2e7 3563/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3564 elements of all the CUs and return 1. Otherwise, return 0. */
3565
3566static int
4485a1c1
SM
3567dwarf2_read_gdb_index
3568 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3569 get_gdb_index_contents_ftype get_gdb_index_contents,
3570 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3571{
2ec9a5e0
TT
3572 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3573 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3574 struct dwz_file *dwz;
12359b5e 3575 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ec9a5e0 3576
4485a1c1
SM
3577 gdb::array_view<const gdb_byte> main_index_contents
3578 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3579
3580 if (main_index_contents.empty ())
3581 return 0;
3582
3063847f 3583 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
4485a1c1
SM
3584 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3585 use_deprecated_index_sections,
3586 main_index_contents, map.get (), &cu_list,
3587 &cu_list_elements, &types_list,
3588 &types_list_elements))
2ec9a5e0
TT
3589 return 0;
3590
0fefef59 3591 /* Don't use the index if it's empty. */
3063847f 3592 if (map->symbol_table.empty ())
0fefef59
DE
3593 return 0;
3594
2ec9a5e0
TT
3595 /* If there is a .dwz file, read it so we can get its CU list as
3596 well. */
ed2dc618 3597 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 3598 if (dwz != NULL)
2ec9a5e0 3599 {
2ec9a5e0
TT
3600 struct mapped_index dwz_map;
3601 const gdb_byte *dwz_types_ignore;
3602 offset_type dwz_types_elements_ignore;
3603
4485a1c1
SM
3604 gdb::array_view<const gdb_byte> dwz_index_content
3605 = get_gdb_index_contents_dwz (objfile, dwz);
3606
3607 if (dwz_index_content.empty ())
3608 return 0;
3609
3610 if (!read_gdb_index_from_buffer (objfile,
00f93c44
AM
3611 bfd_get_filename (dwz->dwz_bfd.get ()),
3612 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3613 &dwz_list, &dwz_list_elements,
3614 &dwz_types_ignore,
3615 &dwz_types_elements_ignore))
2ec9a5e0
TT
3616 {
3617 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3618 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3619 return 0;
3620 }
3621 }
3622
12359b5e
SM
3623 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3624 dwz_list, dwz_list_elements);
1fd400ff 3625
8b70b953
TT
3626 if (types_list_elements)
3627 {
8b70b953
TT
3628 /* We can only handle a single .debug_types when we have an
3629 index. */
fd5866f6 3630 if (dwarf2_per_objfile->types.size () != 1)
8b70b953
TT
3631 return 0;
3632
fd5866f6 3633 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
8b70b953 3634
12359b5e
SM
3635 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3636 types_list, types_list_elements);
8b70b953 3637 }
9291a0cd 3638
3063847f 3639 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
9291a0cd 3640
3063847f 3641 dwarf2_per_objfile->index_table = std::move (map);
9291a0cd 3642 dwarf2_per_objfile->using_index = 1;
7b9f3c50 3643 dwarf2_per_objfile->quick_file_names_table =
b76e467d 3644 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd
TT
3645
3646 return 1;
3647}
3648
dee91e82 3649/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3650
dee91e82
DE
3651static void
3652dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3653 const gdb_byte *info_ptr,
dee91e82
DE
3654 struct die_info *comp_unit_die,
3655 int has_children,
3656 void *data)
9291a0cd 3657{
dee91e82 3658 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3659 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
518817b3
SM
3660 struct dwarf2_per_objfile *dwarf2_per_objfile
3661 = cu->per_cu->dwarf2_per_objfile;
dee91e82 3662 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3663 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3664 struct attribute *attr;
7b9f3c50
DE
3665 void **slot;
3666 struct quick_file_names *qfn;
9291a0cd 3667
0186c6a7
DE
3668 gdb_assert (! this_cu->is_debug_types);
3669
07261596
TT
3670 /* Our callers never want to match partial units -- instead they
3671 will match the enclosing full CU. */
3672 if (comp_unit_die->tag == DW_TAG_partial_unit)
3673 {
3674 this_cu->v.quick->no_file_data = 1;
3675 return;
3676 }
3677
0186c6a7 3678 lh_cu = this_cu;
7b9f3c50 3679 slot = NULL;
dee91e82 3680
fff8551c 3681 line_header_up lh;
9c541725 3682 sect_offset line_offset {};
fff8551c 3683
dee91e82 3684 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
3685 if (attr)
3686 {
7b9f3c50
DE
3687 struct quick_file_names find_entry;
3688
9c541725 3689 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3690
3691 /* We may have already read in this line header (TU line header sharing).
3692 If we have we're done. */
094b34ac 3693 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3694 find_entry.hash.line_sect_off = line_offset;
7b9f3c50
DE
3695 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3696 &find_entry, INSERT);
3697 if (*slot != NULL)
3698 {
9a3c8263 3699 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3700 return;
7b9f3c50
DE
3701 }
3702
3019eac3 3703 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3704 }
3705 if (lh == NULL)
3706 {
094b34ac 3707 lh_cu->v.quick->no_file_data = 1;
dee91e82 3708 return;
9291a0cd
TT
3709 }
3710
8d749320 3711 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3712 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3713 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3714 gdb_assert (slot != NULL);
3715 *slot = qfn;
9291a0cd 3716
d721ba37 3717 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3718
aa391654
TT
3719 int offset = 0;
3720 if (strcmp (fnd.name, "<unknown>") != 0)
3721 ++offset;
3722
7ba99d21 3723 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3724 qfn->file_names =
aa391654
TT
3725 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3726 if (offset != 0)
3727 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3728 for (int i = 0; i < lh->file_names_size (); ++i)
aa391654 3729 qfn->file_names[i + offset] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
7b9f3c50 3730 qfn->real_names = NULL;
9291a0cd 3731
094b34ac 3732 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3733}
3734
3735/* A helper for the "quick" functions which attempts to read the line
3736 table for THIS_CU. */
3737
3738static struct quick_file_names *
e4a48d9d 3739dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3740{
0186c6a7
DE
3741 /* This should never be called for TUs. */
3742 gdb_assert (! this_cu->is_debug_types);
3743 /* Nor type unit groups. */
3744 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 3745
dee91e82
DE
3746 if (this_cu->v.quick->file_names != NULL)
3747 return this_cu->v.quick->file_names;
3748 /* If we know there is no line data, no point in looking again. */
3749 if (this_cu->v.quick->no_file_data)
3750 return NULL;
3751
0186c6a7 3752 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
3753
3754 if (this_cu->v.quick->no_file_data)
3755 return NULL;
3756 return this_cu->v.quick->file_names;
9291a0cd
TT
3757}
3758
3759/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3760 real path for a given file name from the line table. */
2fdf6df6 3761
9291a0cd 3762static const char *
7b9f3c50
DE
3763dw2_get_real_path (struct objfile *objfile,
3764 struct quick_file_names *qfn, int index)
9291a0cd 3765{
7b9f3c50
DE
3766 if (qfn->real_names == NULL)
3767 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3768 qfn->num_file_names, const char *);
9291a0cd 3769
7b9f3c50 3770 if (qfn->real_names[index] == NULL)
14278e1f 3771 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3772
7b9f3c50 3773 return qfn->real_names[index];
9291a0cd
TT
3774}
3775
3776static struct symtab *
3777dw2_find_last_source_symtab (struct objfile *objfile)
3778{
ed2dc618
SM
3779 struct dwarf2_per_objfile *dwarf2_per_objfile
3780 = get_dwarf2_per_objfile (objfile);
b76e467d 3781 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
58f0c718 3782 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
ae2de4f8 3783
43f3e411
DE
3784 if (cust == NULL)
3785 return NULL;
ed2dc618 3786
43f3e411 3787 return compunit_primary_filetab (cust);
9291a0cd
TT
3788}
3789
7b9f3c50
DE
3790/* Traversal function for dw2_forget_cached_source_info. */
3791
3792static int
3793dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3794{
7b9f3c50 3795 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3796
7b9f3c50 3797 if (file_data->real_names)
9291a0cd 3798 {
7b9f3c50 3799 int i;
9291a0cd 3800
7b9f3c50 3801 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3802 {
7b9f3c50
DE
3803 xfree ((void*) file_data->real_names[i]);
3804 file_data->real_names[i] = NULL;
9291a0cd
TT
3805 }
3806 }
7b9f3c50
DE
3807
3808 return 1;
3809}
3810
3811static void
3812dw2_forget_cached_source_info (struct objfile *objfile)
3813{
ed2dc618
SM
3814 struct dwarf2_per_objfile *dwarf2_per_objfile
3815 = get_dwarf2_per_objfile (objfile);
7b9f3c50
DE
3816
3817 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3818 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3819}
3820
f8eba3c6
TT
3821/* Helper function for dw2_map_symtabs_matching_filename that expands
3822 the symtabs and calls the iterator. */
3823
3824static int
3825dw2_map_expand_apply (struct objfile *objfile,
3826 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3827 const char *name, const char *real_path,
14bc53a8 3828 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3829{
43f3e411 3830 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3831
3832 /* Don't visit already-expanded CUs. */
43f3e411 3833 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3834 return 0;
3835
3836 /* This may expand more than one symtab, and we want to iterate over
3837 all of them. */
58f0c718 3838 dw2_instantiate_symtab (per_cu, false);
f8eba3c6 3839
14bc53a8
PA
3840 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3841 last_made, callback);
f8eba3c6
TT
3842}
3843
3844/* Implementation of the map_symtabs_matching_filename method. */
3845
14bc53a8
PA
3846static bool
3847dw2_map_symtabs_matching_filename
3848 (struct objfile *objfile, const char *name, const char *real_path,
3849 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3850{
c011a4f4 3851 const char *name_basename = lbasename (name);
ed2dc618
SM
3852 struct dwarf2_per_objfile *dwarf2_per_objfile
3853 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3854
848e3e78
DE
3855 /* The rule is CUs specify all the files, including those used by
3856 any TU, so there's no need to scan TUs here. */
f4dc4d17 3857
b76e467d 3858 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3859 {
3d7bb9d9 3860 /* We only need to look at symtabs not already expanded. */
43f3e411 3861 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3862 continue;
3863
b76e467d 3864 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3865 if (file_data == NULL)
9291a0cd
TT
3866 continue;
3867
b76e467d 3868 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3869 {
7b9f3c50 3870 const char *this_name = file_data->file_names[j];
da235a7c 3871 const char *this_real_name;
9291a0cd 3872
af529f8f 3873 if (compare_filenames_for_search (this_name, name))
9291a0cd 3874 {
f5b95b50 3875 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3876 callback))
3877 return true;
288e77a7 3878 continue;
4aac40c8 3879 }
9291a0cd 3880
c011a4f4
DE
3881 /* Before we invoke realpath, which can get expensive when many
3882 files are involved, do a quick comparison of the basenames. */
3883 if (! basenames_may_differ
3884 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3885 continue;
3886
da235a7c
JK
3887 this_real_name = dw2_get_real_path (objfile, file_data, j);
3888 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3889 {
da235a7c 3890 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3891 callback))
3892 return true;
288e77a7 3893 continue;
da235a7c 3894 }
9291a0cd 3895
da235a7c
JK
3896 if (real_path != NULL)
3897 {
af529f8f
JK
3898 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3899 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3900 if (this_real_name != NULL
af529f8f 3901 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3902 {
f5b95b50 3903 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3904 callback))
3905 return true;
288e77a7 3906 continue;
9291a0cd
TT
3907 }
3908 }
3909 }
3910 }
3911
14bc53a8 3912 return false;
9291a0cd
TT
3913}
3914
da51c347
DE
3915/* Struct used to manage iterating over all CUs looking for a symbol. */
3916
3917struct dw2_symtab_iterator
9291a0cd 3918{
ed2dc618
SM
3919 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3920 struct dwarf2_per_objfile *dwarf2_per_objfile;
2b79f376
SM
3921 /* If set, only look for symbols that match that block. Valid values are
3922 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3923 gdb::optional<block_enum> block_index;
da51c347
DE
3924 /* The kind of symbol we're looking for. */
3925 domain_enum domain;
3926 /* The list of CUs from the index entry of the symbol,
3927 or NULL if not found. */
3928 offset_type *vec;
3929 /* The next element in VEC to look at. */
3930 int next;
3931 /* The number of elements in VEC, or zero if there is no match. */
3932 int length;
8943b874
DE
3933 /* Have we seen a global version of the symbol?
3934 If so we can ignore all further global instances.
3935 This is to work around gold/15646, inefficient gold-generated
3936 indices. */
3937 int global_seen;
da51c347 3938};
9291a0cd 3939
2b79f376 3940/* Initialize the index symtab iterator ITER. */
2fdf6df6 3941
9291a0cd 3942static void
da51c347 3943dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3944 struct dwarf2_per_objfile *dwarf2_per_objfile,
c7f839cb 3945 gdb::optional<block_enum> block_index,
da51c347
DE
3946 domain_enum domain,
3947 const char *name)
3948{
ed2dc618 3949 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3950 iter->block_index = block_index;
3951 iter->domain = domain;
3952 iter->next = 0;
8943b874 3953 iter->global_seen = 0;
da51c347 3954
3063847f 3955 mapped_index *index = dwarf2_per_objfile->index_table.get ();
ed2dc618
SM
3956
3957 /* index is NULL if OBJF_READNOW. */
3958 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3959 iter->length = MAYBE_SWAP (*iter->vec);
3960 else
3961 {
3962 iter->vec = NULL;
3963 iter->length = 0;
3964 }
3965}
3966
3967/* Return the next matching CU or NULL if there are no more. */
3968
3969static struct dwarf2_per_cu_data *
3970dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3971{
ed2dc618
SM
3972 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3973
da51c347
DE
3974 for ( ; iter->next < iter->length; ++iter->next)
3975 {
3976 offset_type cu_index_and_attrs =
3977 MAYBE_SWAP (iter->vec[iter->next + 1]);
3978 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3979 gdb_index_symbol_kind symbol_kind =
3980 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3981 /* Only check the symbol attributes if they're present.
3982 Indices prior to version 7 don't record them,
3983 and indices >= 7 may elide them for certain symbols
3984 (gold does this). */
3985 int attrs_valid =
ed2dc618 3986 (dwarf2_per_objfile->index_table->version >= 7
da51c347
DE
3987 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3988
3190f0c6 3989 /* Don't crash on bad data. */
b76e467d 3990 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3991 + dwarf2_per_objfile->all_type_units.size ()))
3190f0c6 3992 {
b98664d3 3993 complaint (_(".gdb_index entry has bad CU index"
4262abfb
JK
3994 " [in module %s]"),
3995 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3996 continue;
3997 }
3998
ff4c9fec 3999 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3190f0c6 4000
da51c347 4001 /* Skip if already read in. */
43f3e411 4002 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
4003 continue;
4004
8943b874
DE
4005 /* Check static vs global. */
4006 if (attrs_valid)
4007 {
2b79f376
SM
4008 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4009
4010 if (iter->block_index.has_value ())
4011 {
4012 bool want_static = *iter->block_index == STATIC_BLOCK;
4013
4014 if (is_static != want_static)
4015 continue;
4016 }
4017
8943b874
DE
4018 /* Work around gold/15646. */
4019 if (!is_static && iter->global_seen)
4020 continue;
4021 if (!is_static)
4022 iter->global_seen = 1;
4023 }
da51c347
DE
4024
4025 /* Only check the symbol's kind if it has one. */
4026 if (attrs_valid)
4027 {
4028 switch (iter->domain)
4029 {
4030 case VAR_DOMAIN:
4031 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4032 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4033 /* Some types are also in VAR_DOMAIN. */
4034 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4035 continue;
4036 break;
4037 case STRUCT_DOMAIN:
4038 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4039 continue;
4040 break;
4041 case LABEL_DOMAIN:
4042 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4043 continue;
4044 break;
4045 default:
4046 break;
4047 }
4048 }
4049
4050 ++iter->next;
4051 return per_cu;
4052 }
4053
4054 return NULL;
4055}
4056
43f3e411 4057static struct compunit_symtab *
c7f839cb 4058dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 4059 const char *name, domain_enum domain)
9291a0cd 4060{
43f3e411 4061 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
4062 struct dwarf2_per_objfile *dwarf2_per_objfile
4063 = get_dwarf2_per_objfile (objfile);
9291a0cd 4064
b5ec771e
PA
4065 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4066
ed2dc618
SM
4067 struct dw2_symtab_iterator iter;
4068 struct dwarf2_per_cu_data *per_cu;
da51c347 4069
2b79f376 4070 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
9291a0cd 4071
ed2dc618
SM
4072 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4073 {
4074 struct symbol *sym, *with_opaque = NULL;
58f0c718 4075 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
ed2dc618 4076 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 4077 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 4078
ed2dc618
SM
4079 sym = block_find_symbol (block, name, domain,
4080 block_find_non_opaque_type_preferred,
4081 &with_opaque);
b2e2f908 4082
ed2dc618
SM
4083 /* Some caution must be observed with overloaded functions
4084 and methods, since the index will not contain any overload
4085 information (but NAME might contain it). */
da51c347 4086
ed2dc618
SM
4087 if (sym != NULL
4088 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4089 return stab;
4090 if (with_opaque != NULL
4091 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4092 stab_best = stab;
da51c347 4093
ed2dc618 4094 /* Keep looking through other CUs. */
9291a0cd 4095 }
9291a0cd 4096
da51c347 4097 return stab_best;
9291a0cd
TT
4098}
4099
4100static void
4101dw2_print_stats (struct objfile *objfile)
4102{
ed2dc618
SM
4103 struct dwarf2_per_objfile *dwarf2_per_objfile
4104 = get_dwarf2_per_objfile (objfile);
b76e467d 4105 int total = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4106 + dwarf2_per_objfile->all_type_units.size ());
ed2dc618 4107 int count = 0;
9291a0cd 4108
ed2dc618 4109 for (int i = 0; i < total; ++i)
9291a0cd 4110 {
ff4c9fec 4111 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 4112
43f3e411 4113 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4114 ++count;
4115 }
e4a48d9d 4116 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
4117 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4118}
4119
779bd270
DE
4120/* This dumps minimal information about the index.
4121 It is called via "mt print objfiles".
4122 One use is to verify .gdb_index has been loaded by the
4123 gdb.dwarf2/gdb-index.exp testcase. */
4124
9291a0cd
TT
4125static void
4126dw2_dump (struct objfile *objfile)
4127{
ed2dc618
SM
4128 struct dwarf2_per_objfile *dwarf2_per_objfile
4129 = get_dwarf2_per_objfile (objfile);
4130
779bd270
DE
4131 gdb_assert (dwarf2_per_objfile->using_index);
4132 printf_filtered (".gdb_index:");
4133 if (dwarf2_per_objfile->index_table != NULL)
4134 {
4135 printf_filtered (" version %d\n",
4136 dwarf2_per_objfile->index_table->version);
4137 }
4138 else
4139 printf_filtered (" faked for \"readnow\"\n");
4140 printf_filtered ("\n");
9291a0cd
TT
4141}
4142
9291a0cd
TT
4143static void
4144dw2_expand_symtabs_for_function (struct objfile *objfile,
4145 const char *func_name)
4146{
ed2dc618
SM
4147 struct dwarf2_per_objfile *dwarf2_per_objfile
4148 = get_dwarf2_per_objfile (objfile);
da51c347 4149
ed2dc618
SM
4150 struct dw2_symtab_iterator iter;
4151 struct dwarf2_per_cu_data *per_cu;
da51c347 4152
2b79f376 4153 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 4154
ed2dc618 4155 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
58f0c718 4156 dw2_instantiate_symtab (per_cu, false);
da51c347 4157
9291a0cd
TT
4158}
4159
4160static void
4161dw2_expand_all_symtabs (struct objfile *objfile)
4162{
ed2dc618
SM
4163 struct dwarf2_per_objfile *dwarf2_per_objfile
4164 = get_dwarf2_per_objfile (objfile);
b76e467d 4165 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4166 + dwarf2_per_objfile->all_type_units.size ());
9291a0cd 4167
ed2dc618 4168 for (int i = 0; i < total_units; ++i)
9291a0cd 4169 {
ff4c9fec 4170 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 4171
58f0c718
TT
4172 /* We don't want to directly expand a partial CU, because if we
4173 read it with the wrong language, then assertion failures can
4174 be triggered later on. See PR symtab/23010. So, tell
4175 dw2_instantiate_symtab to skip partial CUs -- any important
4176 partial CU will be read via DW_TAG_imported_unit anyway. */
4177 dw2_instantiate_symtab (per_cu, true);
9291a0cd
TT
4178 }
4179}
4180
4181static void
652a8996
JK
4182dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4183 const char *fullname)
9291a0cd 4184{
ed2dc618
SM
4185 struct dwarf2_per_objfile *dwarf2_per_objfile
4186 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
4187
4188 /* We don't need to consider type units here.
4189 This is only called for examining code, e.g. expand_line_sal.
4190 There can be an order of magnitude (or more) more type units
4191 than comp units, and we avoid them if we can. */
4192
b76e467d 4193 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 4194 {
3d7bb9d9 4195 /* We only need to look at symtabs not already expanded. */
43f3e411 4196 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4197 continue;
4198
b76e467d 4199 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 4200 if (file_data == NULL)
9291a0cd
TT
4201 continue;
4202
b76e467d 4203 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4204 {
652a8996
JK
4205 const char *this_fullname = file_data->file_names[j];
4206
4207 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 4208 {
58f0c718 4209 dw2_instantiate_symtab (per_cu, false);
9291a0cd
TT
4210 break;
4211 }
4212 }
4213 }
4214}
4215
9291a0cd 4216static void
199b4314
TT
4217dw2_map_matching_symbols
4218 (struct objfile *objfile,
b054970d 4219 const lookup_name_info &name, domain_enum domain,
199b4314
TT
4220 int global,
4221 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 4222 symbol_compare_ftype *ordered_compare)
9291a0cd 4223{
40658b94 4224 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
4225 current language is Ada for a non-Ada objfile using GNU index. As Ada
4226 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
4227}
4228
e1ef7d7a
PA
4229/* Starting from a search name, return the string that finds the upper
4230 bound of all strings that start with SEARCH_NAME in a sorted name
4231 list. Returns the empty string to indicate that the upper bound is
4232 the end of the list. */
4233
4234static std::string
4235make_sort_after_prefix_name (const char *search_name)
4236{
4237 /* When looking to complete "func", we find the upper bound of all
4238 symbols that start with "func" by looking for where we'd insert
4239 the closest string that would follow "func" in lexicographical
4240 order. Usually, that's "func"-with-last-character-incremented,
4241 i.e. "fund". Mind non-ASCII characters, though. Usually those
4242 will be UTF-8 multi-byte sequences, but we can't be certain.
4243 Especially mind the 0xff character, which is a valid character in
4244 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4245 rule out compilers allowing it in identifiers. Note that
4246 conveniently, strcmp/strcasecmp are specified to compare
4247 characters interpreted as unsigned char. So what we do is treat
4248 the whole string as a base 256 number composed of a sequence of
4249 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4250 to 0, and carries 1 to the following more-significant position.
4251 If the very first character in SEARCH_NAME ends up incremented
4252 and carries/overflows, then the upper bound is the end of the
4253 list. The string after the empty string is also the empty
4254 string.
4255
4256 Some examples of this operation:
4257
4258 SEARCH_NAME => "+1" RESULT
4259
4260 "abc" => "abd"
4261 "ab\xff" => "ac"
4262 "\xff" "a" "\xff" => "\xff" "b"
4263 "\xff" => ""
4264 "\xff\xff" => ""
4265 "" => ""
4266
4267 Then, with these symbols for example:
4268
4269 func
4270 func1
4271 fund
4272
4273 completing "func" looks for symbols between "func" and
4274 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4275 which finds "func" and "func1", but not "fund".
4276
4277 And with:
4278
4279 funcÿ (Latin1 'ÿ' [0xff])
4280 funcÿ1
4281 fund
4282
4283 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4284 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4285
4286 And with:
4287
4288 ÿÿ (Latin1 'ÿ' [0xff])
4289 ÿÿ1
4290
4291 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4292 the end of the list.
4293 */
4294 std::string after = search_name;
4295 while (!after.empty () && (unsigned char) after.back () == 0xff)
4296 after.pop_back ();
4297 if (!after.empty ())
4298 after.back () = (unsigned char) after.back () + 1;
4299 return after;
4300}
4301
5c58de74 4302/* See declaration. */
61d96d7e 4303
5c58de74
PA
4304std::pair<std::vector<name_component>::const_iterator,
4305 std::vector<name_component>::const_iterator>
44ed8f3e 4306mapped_index_base::find_name_components_bounds
3b00ef10 4307 (const lookup_name_info &lookup_name_without_params, language lang) const
3f563c84 4308{
5c58de74
PA
4309 auto *name_cmp
4310 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 4311
3b00ef10
TT
4312 const char *lang_name
4313 = lookup_name_without_params.language_lookup_name (lang).c_str ();
9291a0cd 4314
3f563c84
PA
4315 /* Comparison function object for lower_bound that matches against a
4316 given symbol name. */
4317 auto lookup_compare_lower = [&] (const name_component &elem,
4318 const char *name)
4319 {
5c58de74 4320 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4321 const char *elem_name = elem_qualified + elem.name_offset;
4322 return name_cmp (elem_name, name) < 0;
4323 };
4324
4325 /* Comparison function object for upper_bound that matches against a
4326 given symbol name. */
4327 auto lookup_compare_upper = [&] (const char *name,
4328 const name_component &elem)
4329 {
5c58de74 4330 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4331 const char *elem_name = elem_qualified + elem.name_offset;
4332 return name_cmp (name, elem_name) < 0;
4333 };
4334
5c58de74
PA
4335 auto begin = this->name_components.begin ();
4336 auto end = this->name_components.end ();
3f563c84
PA
4337
4338 /* Find the lower bound. */
4339 auto lower = [&] ()
4340 {
3b00ef10 4341 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
4342 return begin;
4343 else
3b00ef10 4344 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
4345 } ();
4346
4347 /* Find the upper bound. */
4348 auto upper = [&] ()
4349 {
5c58de74 4350 if (lookup_name_without_params.completion_mode ())
3f563c84 4351 {
e1ef7d7a
PA
4352 /* In completion mode, we want UPPER to point past all
4353 symbols names that have the same prefix. I.e., with
4354 these symbols, and completing "func":
4355
4356 function << lower bound
4357 function1
4358 other_function << upper bound
4359
4360 We find the upper bound by looking for the insertion
4361 point of "func"-with-last-character-incremented,
4362 i.e. "fund". */
3b00ef10 4363 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 4364 if (after.empty ())
3f563c84 4365 return end;
e6b2f5ef
PA
4366 return std::lower_bound (lower, end, after.c_str (),
4367 lookup_compare_lower);
3f563c84
PA
4368 }
4369 else
3b00ef10 4370 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
4371 } ();
4372
5c58de74
PA
4373 return {lower, upper};
4374}
4375
4376/* See declaration. */
4377
4378void
44ed8f3e 4379mapped_index_base::build_name_components ()
5c58de74
PA
4380{
4381 if (!this->name_components.empty ())
4382 return;
4383
4384 this->name_components_casing = case_sensitivity;
4385 auto *name_cmp
4386 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4387
4388 /* The code below only knows how to break apart components of C++
4389 symbol names (and other languages that use '::' as
3b00ef10 4390 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
4391 auto count = this->symbol_name_count ();
4392 for (offset_type idx = 0; idx < count; idx++)
5c58de74 4393 {
44ed8f3e 4394 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
4395 continue;
4396
4397 const char *name = this->symbol_name_at (idx);
4398
4399 /* Add each name component to the name component table. */
4400 unsigned int previous_len = 0;
3b00ef10
TT
4401
4402 if (strstr (name, "::") != nullptr)
4403 {
4404 for (unsigned int current_len = cp_find_first_component (name);
4405 name[current_len] != '\0';
4406 current_len += cp_find_first_component (name + current_len))
4407 {
4408 gdb_assert (name[current_len] == ':');
4409 this->name_components.push_back ({previous_len, idx});
4410 /* Skip the '::'. */
4411 current_len += 2;
4412 previous_len = current_len;
4413 }
4414 }
4415 else
5c58de74 4416 {
3b00ef10
TT
4417 /* Handle the Ada encoded (aka mangled) form here. */
4418 for (const char *iter = strstr (name, "__");
4419 iter != nullptr;
4420 iter = strstr (iter, "__"))
4421 {
4422 this->name_components.push_back ({previous_len, idx});
4423 iter += 2;
4424 previous_len = iter - name;
4425 }
5c58de74 4426 }
3b00ef10 4427
5c58de74
PA
4428 this->name_components.push_back ({previous_len, idx});
4429 }
4430
4431 /* Sort name_components elements by name. */
4432 auto name_comp_compare = [&] (const name_component &left,
4433 const name_component &right)
4434 {
4435 const char *left_qualified = this->symbol_name_at (left.idx);
4436 const char *right_qualified = this->symbol_name_at (right.idx);
4437
4438 const char *left_name = left_qualified + left.name_offset;
4439 const char *right_name = right_qualified + right.name_offset;
4440
4441 return name_cmp (left_name, right_name) < 0;
4442 };
4443
4444 std::sort (this->name_components.begin (),
4445 this->name_components.end (),
4446 name_comp_compare);
4447}
4448
4449/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4450 mapped_index_base instead of the containing objfile. This is split
4451 to a separate function in order to be able to unit test the
4452 name_components matching using a mock mapped_index_base. For each
5c58de74 4453 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4454 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4455
4456static void
4457dw2_expand_symtabs_matching_symbol
44ed8f3e 4458 (mapped_index_base &index,
5c58de74
PA
4459 const lookup_name_info &lookup_name_in,
4460 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4461 enum search_domain kind,
3b00ef10 4462 gdb::function_view<bool (offset_type)> match_callback)
5c58de74
PA
4463{
4464 lookup_name_info lookup_name_without_params
4465 = lookup_name_in.make_ignore_params ();
5c58de74
PA
4466
4467 /* Build the symbol name component sorted vector, if we haven't
4468 yet. */
4469 index.build_name_components ();
4470
3f563c84
PA
4471 /* The same symbol may appear more than once in the range though.
4472 E.g., if we're looking for symbols that complete "w", and we have
4473 a symbol named "w1::w2", we'll find the two name components for
4474 that same symbol in the range. To be sure we only call the
4475 callback once per symbol, we first collect the symbol name
4476 indexes that matched in a temporary vector and ignore
4477 duplicates. */
4478 std::vector<offset_type> matches;
3f563c84 4479
3b00ef10
TT
4480 struct name_and_matcher
4481 {
4482 symbol_name_matcher_ftype *matcher;
4483 const std::string &name;
4484
4485 bool operator== (const name_and_matcher &other) const
3f563c84 4486 {
3b00ef10
TT
4487 return matcher == other.matcher && name == other.name;
4488 }
4489 };
4490
4491 /* A vector holding all the different symbol name matchers, for all
4492 languages. */
4493 std::vector<name_and_matcher> matchers;
4494
4495 for (int i = 0; i < nr_languages; i++)
4496 {
4497 enum language lang_e = (enum language) i;
4498
4499 const language_defn *lang = language_def (lang_e);
4500 symbol_name_matcher_ftype *name_matcher
4501 = get_symbol_name_matcher (lang, lookup_name_without_params);
3f563c84 4502
3b00ef10
TT
4503 name_and_matcher key {
4504 name_matcher,
4505 lookup_name_without_params.language_lookup_name (lang_e)
4506 };
4507
4508 /* Don't insert the same comparison routine more than once.
4509 Note that we do this linear walk. This is not a problem in
4510 practice because the number of supported languages is
4511 low. */
4512 if (std::find (matchers.begin (), matchers.end (), key)
4513 != matchers.end ())
9291a0cd 4514 continue;
3b00ef10
TT
4515 matchers.push_back (std::move (key));
4516
4517 auto bounds
4518 = index.find_name_components_bounds (lookup_name_without_params,
4519 lang_e);
4520
4521 /* Now for each symbol name in range, check to see if we have a name
4522 match, and if so, call the MATCH_CALLBACK callback. */
4523
4524 for (; bounds.first != bounds.second; ++bounds.first)
4525 {
4526 const char *qualified = index.symbol_name_at (bounds.first->idx);
4527
4528 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4529 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4530 continue;
9291a0cd 4531
3b00ef10
TT
4532 matches.push_back (bounds.first->idx);
4533 }
3f563c84
PA
4534 }
4535
4536 std::sort (matches.begin (), matches.end ());
4537
4538 /* Finally call the callback, once per match. */
4539 ULONGEST prev = -1;
4540 for (offset_type idx : matches)
4541 {
4542 if (prev != idx)
4543 {
3b00ef10
TT
4544 if (!match_callback (idx))
4545 break;
3f563c84
PA
4546 prev = idx;
4547 }
4548 }
4549
4550 /* Above we use a type wider than idx's for 'prev', since 0 and
4551 (offset_type)-1 are both possible values. */
4552 static_assert (sizeof (prev) > sizeof (offset_type), "");
4553}
4554
c62446b1
PA
4555#if GDB_SELF_TEST
4556
4557namespace selftests { namespace dw2_expand_symtabs_matching {
4558
a3c5fafd
PA
4559/* A mock .gdb_index/.debug_names-like name index table, enough to
4560 exercise dw2_expand_symtabs_matching_symbol, which works with the
4561 mapped_index_base interface. Builds an index from the symbol list
4562 passed as parameter to the constructor. */
4563class mock_mapped_index : public mapped_index_base
c62446b1
PA
4564{
4565public:
a3c5fafd
PA
4566 mock_mapped_index (gdb::array_view<const char *> symbols)
4567 : m_symbol_table (symbols)
c62446b1
PA
4568 {}
4569
a3c5fafd 4570 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4571
a3c5fafd 4572 /* Return the number of names in the symbol table. */
632e107b 4573 size_t symbol_name_count () const override
c62446b1 4574 {
a3c5fafd 4575 return m_symbol_table.size ();
c62446b1
PA
4576 }
4577
a3c5fafd 4578 /* Get the name of the symbol at IDX in the symbol table. */
632e107b 4579 const char *symbol_name_at (offset_type idx) const override
a3c5fafd
PA
4580 {
4581 return m_symbol_table[idx];
4582 }
c62446b1 4583
a3c5fafd
PA
4584private:
4585 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4586};
4587
4588/* Convenience function that converts a NULL pointer to a "<null>"
4589 string, to pass to print routines. */
4590
4591static const char *
4592string_or_null (const char *str)
4593{
4594 return str != NULL ? str : "<null>";
4595}
4596
4597/* Check if a lookup_name_info built from
4598 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4599 index. EXPECTED_LIST is the list of expected matches, in expected
4600 matching order. If no match expected, then an empty list is
4601 specified. Returns true on success. On failure prints a warning
4602 indicating the file:line that failed, and returns false. */
4603
4604static bool
4605check_match (const char *file, int line,
4606 mock_mapped_index &mock_index,
4607 const char *name, symbol_name_match_type match_type,
4608 bool completion_mode,
4609 std::initializer_list<const char *> expected_list)
4610{
4611 lookup_name_info lookup_name (name, match_type, completion_mode);
4612
4613 bool matched = true;
4614
4615 auto mismatch = [&] (const char *expected_str,
4616 const char *got)
4617 {
4618 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4619 "expected=\"%s\", got=\"%s\"\n"),
4620 file, line,
4621 (match_type == symbol_name_match_type::FULL
4622 ? "FULL" : "WILD"),
4623 name, string_or_null (expected_str), string_or_null (got));
4624 matched = false;
4625 };
4626
4627 auto expected_it = expected_list.begin ();
4628 auto expected_end = expected_list.end ();
4629
a3c5fafd 4630 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4631 NULL, ALL_DOMAIN,
4632 [&] (offset_type idx)
4633 {
a3c5fafd 4634 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4635 const char *expected_str
4636 = expected_it == expected_end ? NULL : *expected_it++;
4637
4638 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4639 mismatch (expected_str, matched_name);
3b00ef10 4640 return true;
c62446b1
PA
4641 });
4642
4643 const char *expected_str
4644 = expected_it == expected_end ? NULL : *expected_it++;
4645 if (expected_str != NULL)
4646 mismatch (expected_str, NULL);
4647
4648 return matched;
4649}
4650
4651/* The symbols added to the mock mapped_index for testing (in
4652 canonical form). */
4653static const char *test_symbols[] = {
4654 "function",
4655 "std::bar",
4656 "std::zfunction",
4657 "std::zfunction2",
4658 "w1::w2",
4659 "ns::foo<char*>",
4660 "ns::foo<int>",
4661 "ns::foo<long>",
a20714ff
PA
4662 "ns2::tmpl<int>::foo2",
4663 "(anonymous namespace)::A::B::C",
c62446b1 4664
e1ef7d7a
PA
4665 /* These are used to check that the increment-last-char in the
4666 matching algorithm for completion doesn't match "t1_fund" when
4667 completing "t1_func". */
4668 "t1_func",
4669 "t1_func1",
4670 "t1_fund",
4671 "t1_fund1",
4672
4673 /* A UTF-8 name with multi-byte sequences to make sure that
4674 cp-name-parser understands this as a single identifier ("função"
4675 is "function" in PT). */
4676 u8"u8função",
4677
4678 /* \377 (0xff) is Latin1 'ÿ'. */
4679 "yfunc\377",
4680
4681 /* \377 (0xff) is Latin1 'ÿ'. */
4682 "\377",
4683 "\377\377123",
4684
c62446b1
PA
4685 /* A name with all sorts of complications. Starts with "z" to make
4686 it easier for the completion tests below. */
4687#define Z_SYM_NAME \
4688 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4689 "::tuple<(anonymous namespace)::ui*, " \
4690 "std::default_delete<(anonymous namespace)::ui>, void>"
4691
4692 Z_SYM_NAME
4693};
4694
a3c5fafd
PA
4695/* Returns true if the mapped_index_base::find_name_component_bounds
4696 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4697 in completion mode. */
5c58de74
PA
4698
4699static bool
a3c5fafd 4700check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4701 const char *search_name,
4702 gdb::array_view<const char *> expected_syms)
4703{
4704 lookup_name_info lookup_name (search_name,
4705 symbol_name_match_type::FULL, true);
4706
3b00ef10
TT
4707 auto bounds = index.find_name_components_bounds (lookup_name,
4708 language_cplus);
5c58de74
PA
4709
4710 size_t distance = std::distance (bounds.first, bounds.second);
4711 if (distance != expected_syms.size ())
4712 return false;
4713
4714 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4715 {
4716 auto nc_elem = bounds.first + exp_elem;
4717 const char *qualified = index.symbol_name_at (nc_elem->idx);
4718 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4719 return false;
4720 }
4721
4722 return true;
4723}
4724
4725/* Test the lower-level mapped_index::find_name_component_bounds
4726 method. */
4727
c62446b1 4728static void
5c58de74
PA
4729test_mapped_index_find_name_component_bounds ()
4730{
4731 mock_mapped_index mock_index (test_symbols);
4732
a3c5fafd 4733 mock_index.build_name_components ();
5c58de74
PA
4734
4735 /* Test the lower-level mapped_index::find_name_component_bounds
4736 method in completion mode. */
4737 {
4738 static const char *expected_syms[] = {
4739 "t1_func",
4740 "t1_func1",
5c58de74
PA
4741 };
4742
a3c5fafd 4743 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4744 "t1_func", expected_syms));
4745 }
4746
4747 /* Check that the increment-last-char in the name matching algorithm
4748 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4749 {
4750 static const char *expected_syms1[] = {
4751 "\377",
4752 "\377\377123",
4753 };
a3c5fafd 4754 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4755 "\377", expected_syms1));
4756
4757 static const char *expected_syms2[] = {
4758 "\377\377123",
4759 };
a3c5fafd 4760 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4761 "\377\377", expected_syms2));
4762 }
4763}
4764
4765/* Test dw2_expand_symtabs_matching_symbol. */
4766
4767static void
4768test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4769{
4770 mock_mapped_index mock_index (test_symbols);
4771
4772 /* We let all tests run until the end even if some fails, for debug
4773 convenience. */
4774 bool any_mismatch = false;
4775
4776 /* Create the expected symbols list (an initializer_list). Needed
4777 because lists have commas, and we need to pass them to CHECK,
4778 which is a macro. */
4779#define EXPECT(...) { __VA_ARGS__ }
4780
4781 /* Wrapper for check_match that passes down the current
4782 __FILE__/__LINE__. */
4783#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4784 any_mismatch |= !check_match (__FILE__, __LINE__, \
4785 mock_index, \
4786 NAME, MATCH_TYPE, COMPLETION_MODE, \
4787 EXPECTED_LIST)
4788
4789 /* Identity checks. */
4790 for (const char *sym : test_symbols)
4791 {
4792 /* Should be able to match all existing symbols. */
4793 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4794 EXPECT (sym));
4795
4796 /* Should be able to match all existing symbols with
4797 parameters. */
4798 std::string with_params = std::string (sym) + "(int)";
4799 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4800 EXPECT (sym));
4801
4802 /* Should be able to match all existing symbols with
4803 parameters and qualifiers. */
4804 with_params = std::string (sym) + " ( int ) const";
4805 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4806 EXPECT (sym));
4807
4808 /* This should really find sym, but cp-name-parser.y doesn't
4809 know about lvalue/rvalue qualifiers yet. */
4810 with_params = std::string (sym) + " ( int ) &&";
4811 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4812 {});
4813 }
4814
e1ef7d7a
PA
4815 /* Check that the name matching algorithm for completion doesn't get
4816 confused with Latin1 'ÿ' / 0xff. */
4817 {
4818 static const char str[] = "\377";
4819 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4820 EXPECT ("\377", "\377\377123"));
4821 }
4822
4823 /* Check that the increment-last-char in the matching algorithm for
4824 completion doesn't match "t1_fund" when completing "t1_func". */
4825 {
4826 static const char str[] = "t1_func";
4827 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4828 EXPECT ("t1_func", "t1_func1"));
4829 }
4830
c62446b1
PA
4831 /* Check that completion mode works at each prefix of the expected
4832 symbol name. */
4833 {
4834 static const char str[] = "function(int)";
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 ("function"));
4843 }
4844 }
4845
4846 /* While "w" is a prefix of both components, the match function
4847 should still only be called once. */
4848 {
4849 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4850 EXPECT ("w1::w2"));
a20714ff
PA
4851 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4852 EXPECT ("w1::w2"));
c62446b1
PA
4853 }
4854
4855 /* Same, with a "complicated" symbol. */
4856 {
4857 static const char str[] = Z_SYM_NAME;
4858 size_t len = strlen (str);
4859 std::string lookup;
4860
4861 for (size_t i = 1; i < len; i++)
4862 {
4863 lookup.assign (str, i);
4864 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4865 EXPECT (Z_SYM_NAME));
4866 }
4867 }
4868
4869 /* In FULL mode, an incomplete symbol doesn't match. */
4870 {
4871 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4872 {});
4873 }
4874
4875 /* A complete symbol with parameters matches any overload, since the
4876 index has no overload info. */
4877 {
4878 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4879 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4880 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4881 EXPECT ("std::zfunction", "std::zfunction2"));
4882 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4883 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4884 }
4885
4886 /* Check that whitespace is ignored appropriately. A symbol with a
4887 template argument list. */
4888 {
4889 static const char expected[] = "ns::foo<int>";
4890 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4891 EXPECT (expected));
a20714ff
PA
4892 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4893 EXPECT (expected));
c62446b1
PA
4894 }
4895
4896 /* Check that whitespace is ignored appropriately. A symbol with a
4897 template argument list that includes a pointer. */
4898 {
4899 static const char expected[] = "ns::foo<char*>";
4900 /* Try both completion and non-completion modes. */
4901 static const bool completion_mode[2] = {false, true};
4902 for (size_t i = 0; i < 2; i++)
4903 {
4904 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4905 completion_mode[i], EXPECT (expected));
a20714ff
PA
4906 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4907 completion_mode[i], EXPECT (expected));
c62446b1
PA
4908
4909 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4910 completion_mode[i], EXPECT (expected));
a20714ff
PA
4911 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4912 completion_mode[i], EXPECT (expected));
c62446b1
PA
4913 }
4914 }
4915
4916 {
4917 /* Check method qualifiers are ignored. */
4918 static const char expected[] = "ns::foo<char*>";
4919 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4920 symbol_name_match_type::FULL, true, EXPECT (expected));
4921 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4922 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4923 CHECK_MATCH ("foo < char * > ( int ) const",
4924 symbol_name_match_type::WILD, true, EXPECT (expected));
4925 CHECK_MATCH ("foo < char * > ( int ) &&",
4926 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4927 }
4928
4929 /* Test lookup names that don't match anything. */
4930 {
a20714ff
PA
4931 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4932 {});
4933
c62446b1
PA
4934 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4935 {});
4936 }
4937
a20714ff
PA
4938 /* Some wild matching tests, exercising "(anonymous namespace)",
4939 which should not be confused with a parameter list. */
4940 {
4941 static const char *syms[] = {
4942 "A::B::C",
4943 "B::C",
4944 "C",
4945 "A :: B :: C ( int )",
4946 "B :: C ( int )",
4947 "C ( int )",
4948 };
4949
4950 for (const char *s : syms)
4951 {
4952 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4953 EXPECT ("(anonymous namespace)::A::B::C"));
4954 }
4955 }
4956
4957 {
4958 static const char expected[] = "ns2::tmpl<int>::foo2";
4959 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4960 EXPECT (expected));
4961 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4962 EXPECT (expected));
4963 }
4964
c62446b1
PA
4965 SELF_CHECK (!any_mismatch);
4966
4967#undef EXPECT
4968#undef CHECK_MATCH
4969}
4970
5c58de74
PA
4971static void
4972run_test ()
4973{
4974 test_mapped_index_find_name_component_bounds ();
4975 test_dw2_expand_symtabs_matching_symbol ();
4976}
4977
c62446b1
PA
4978}} // namespace selftests::dw2_expand_symtabs_matching
4979
4980#endif /* GDB_SELF_TEST */
4981
4b514bc8
JK
4982/* If FILE_MATCHER is NULL or if PER_CU has
4983 dwarf2_per_cu_quick_data::MARK set (see
4984 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4985 EXPANSION_NOTIFY on it. */
4986
4987static void
4988dw2_expand_symtabs_matching_one
4989 (struct dwarf2_per_cu_data *per_cu,
4990 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4991 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4992{
4993 if (file_matcher == NULL || per_cu->v.quick->mark)
4994 {
4995 bool symtab_was_null
4996 = (per_cu->v.quick->compunit_symtab == NULL);
4997
58f0c718 4998 dw2_instantiate_symtab (per_cu, false);
4b514bc8
JK
4999
5000 if (expansion_notify != NULL
5001 && symtab_was_null
5002 && per_cu->v.quick->compunit_symtab != NULL)
5003 expansion_notify (per_cu->v.quick->compunit_symtab);
5004 }
5005}
5006
3f563c84
PA
5007/* Helper for dw2_expand_matching symtabs. Called on each symbol
5008 matched, to expand corresponding CUs that were marked. IDX is the
5009 index of the symbol name that matched. */
5010
5011static void
5012dw2_expand_marked_cus
ed2dc618 5013 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
5014 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5015 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5016 search_domain kind)
5017{
3f563c84
PA
5018 offset_type *vec, vec_len, vec_idx;
5019 bool global_seen = false;
ed2dc618 5020 mapped_index &index = *dwarf2_per_objfile->index_table;
3f563c84 5021
61920122 5022 vec = (offset_type *) (index.constant_pool
f00a2de2 5023 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
5024 vec_len = MAYBE_SWAP (vec[0]);
5025 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5026 {
61920122
PA
5027 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5028 /* This value is only valid for index versions >= 7. */
5029 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5030 gdb_index_symbol_kind symbol_kind =
5031 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5032 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5033 /* Only check the symbol attributes if they're present.
5034 Indices prior to version 7 don't record them,
5035 and indices >= 7 may elide them for certain symbols
5036 (gold does this). */
5037 int attrs_valid =
5038 (index.version >= 7
5039 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5040
5041 /* Work around gold/15646. */
5042 if (attrs_valid)
9291a0cd 5043 {
61920122
PA
5044 if (!is_static && global_seen)
5045 continue;
5046 if (!is_static)
5047 global_seen = true;
5048 }
3190f0c6 5049
61920122
PA
5050 /* Only check the symbol's kind if it has one. */
5051 if (attrs_valid)
5052 {
5053 switch (kind)
8943b874 5054 {
61920122
PA
5055 case VARIABLES_DOMAIN:
5056 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5057 continue;
5058 break;
5059 case FUNCTIONS_DOMAIN:
5060 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 5061 continue;
61920122
PA
5062 break;
5063 case TYPES_DOMAIN:
5064 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5065 continue;
5066 break;
5067 default:
5068 break;
8943b874 5069 }
61920122 5070 }
8943b874 5071
61920122 5072 /* Don't crash on bad data. */
b76e467d 5073 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 5074 + dwarf2_per_objfile->all_type_units.size ()))
61920122 5075 {
b98664d3 5076 complaint (_(".gdb_index entry has bad CU index"
ed2dc618
SM
5077 " [in module %s]"),
5078 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
5079 continue;
5080 }
5081
ff4c9fec 5082 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4b514bc8
JK
5083 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5084 expansion_notify);
61920122
PA
5085 }
5086}
5087
4b514bc8
JK
5088/* If FILE_MATCHER is non-NULL, set all the
5089 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5090 that match FILE_MATCHER. */
5091
61920122 5092static void
4b514bc8 5093dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
5094 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5095 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 5096{
4b514bc8 5097 if (file_matcher == NULL)
61920122
PA
5098 return;
5099
4b514bc8
JK
5100 objfile *const objfile = dwarf2_per_objfile->objfile;
5101
5102 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5103 htab_eq_pointer,
5104 NULL, xcalloc, xfree));
5105 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
5106 htab_eq_pointer,
5107 NULL, xcalloc, xfree));
61920122 5108
4b514bc8
JK
5109 /* The rule is CUs specify all the files, including those used by
5110 any TU, so there's no need to scan TUs here. */
61920122 5111
b76e467d 5112 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5113 {
927aa2e7
JK
5114 QUIT;
5115
5116 per_cu->v.quick->mark = 0;
5117
5118 /* We only need to look at symtabs not already expanded. */
5119 if (per_cu->v.quick->compunit_symtab)
5120 continue;
5121
b76e467d 5122 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
5123 if (file_data == NULL)
5124 continue;
5125
5126 if (htab_find (visited_not_found.get (), file_data) != NULL)
5127 continue;
5128 else if (htab_find (visited_found.get (), file_data) != NULL)
5129 {
5130 per_cu->v.quick->mark = 1;
5131 continue;
5132 }
5133
b76e467d 5134 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
5135 {
5136 const char *this_real_name;
5137
5138 if (file_matcher (file_data->file_names[j], false))
5139 {
5140 per_cu->v.quick->mark = 1;
5141 break;
5142 }
5143
5144 /* Before we invoke realpath, which can get expensive when many
5145 files are involved, do a quick comparison of the basenames. */
5146 if (!basenames_may_differ
5147 && !file_matcher (lbasename (file_data->file_names[j]),
5148 true))
5149 continue;
5150
5151 this_real_name = dw2_get_real_path (objfile, file_data, j);
5152 if (file_matcher (this_real_name, false))
5153 {
5154 per_cu->v.quick->mark = 1;
5155 break;
5156 }
5157 }
5158
b76e467d
SM
5159 void **slot = htab_find_slot (per_cu->v.quick->mark
5160 ? visited_found.get ()
5161 : visited_not_found.get (),
5162 file_data, INSERT);
927aa2e7
JK
5163 *slot = file_data;
5164 }
5165}
5166
5167static void
5168dw2_expand_symtabs_matching
5169 (struct objfile *objfile,
5170 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5171 const lookup_name_info &lookup_name,
5172 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5173 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5174 enum search_domain kind)
5175{
ed2dc618
SM
5176 struct dwarf2_per_objfile *dwarf2_per_objfile
5177 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5178
5179 /* index_table is NULL if OBJF_READNOW. */
5180 if (!dwarf2_per_objfile->index_table)
5181 return;
5182
ed2dc618 5183 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7
JK
5184
5185 mapped_index &index = *dwarf2_per_objfile->index_table;
5186
5187 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5188 symbol_matcher,
5189 kind, [&] (offset_type idx)
5190 {
ed2dc618 5191 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7 5192 expansion_notify, kind);
3b00ef10 5193 return true;
927aa2e7
JK
5194 });
5195}
5196
5197/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5198 symtab. */
5199
5200static struct compunit_symtab *
5201recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5202 CORE_ADDR pc)
5203{
5204 int i;
5205
5206 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5207 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5208 return cust;
5209
5210 if (cust->includes == NULL)
5211 return NULL;
5212
5213 for (i = 0; cust->includes[i]; ++i)
5214 {
5215 struct compunit_symtab *s = cust->includes[i];
5216
5217 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5218 if (s != NULL)
5219 return s;
5220 }
5221
5222 return NULL;
5223}
5224
5225static struct compunit_symtab *
5226dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5227 struct bound_minimal_symbol msymbol,
5228 CORE_ADDR pc,
5229 struct obj_section *section,
5230 int warn_if_readin)
5231{
5232 struct dwarf2_per_cu_data *data;
5233 struct compunit_symtab *result;
5234
d320c2b5 5235 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
5236 return NULL;
5237
79748972
TT
5238 CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
5239 SECT_OFF_TEXT (objfile));
d320c2b5
TT
5240 data = (struct dwarf2_per_cu_data *) addrmap_find
5241 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
5242 if (!data)
5243 return NULL;
5244
5245 if (warn_if_readin && data->v.quick->compunit_symtab)
5246 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5247 paddress (get_objfile_arch (objfile), pc));
5248
5249 result
58f0c718
TT
5250 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
5251 false),
927aa2e7
JK
5252 pc);
5253 gdb_assert (result != NULL);
5254 return result;
5255}
5256
5257static void
5258dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5259 void *data, int need_fullname)
5260{
ed2dc618
SM
5261 struct dwarf2_per_objfile *dwarf2_per_objfile
5262 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5263
5264 if (!dwarf2_per_objfile->filenames_cache)
5265 {
5266 dwarf2_per_objfile->filenames_cache.emplace ();
5267
5268 htab_up visited (htab_create_alloc (10,
5269 htab_hash_pointer, htab_eq_pointer,
5270 NULL, xcalloc, xfree));
5271
5272 /* The rule is CUs specify all the files, including those used
5273 by any TU, so there's no need to scan TUs here. We can
5274 ignore file names coming from already-expanded CUs. */
5275
b76e467d 5276 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5277 {
927aa2e7
JK
5278 if (per_cu->v.quick->compunit_symtab)
5279 {
5280 void **slot = htab_find_slot (visited.get (),
5281 per_cu->v.quick->file_names,
5282 INSERT);
5283
5284 *slot = per_cu->v.quick->file_names;
5285 }
5286 }
5287
b76e467d 5288 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5289 {
927aa2e7
JK
5290 /* We only need to look at symtabs not already expanded. */
5291 if (per_cu->v.quick->compunit_symtab)
5292 continue;
5293
b76e467d 5294 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
5295 if (file_data == NULL)
5296 continue;
5297
b76e467d 5298 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
5299 if (*slot)
5300 {
5301 /* Already visited. */
5302 continue;
5303 }
5304 *slot = file_data;
5305
5306 for (int j = 0; j < file_data->num_file_names; ++j)
5307 {
5308 const char *filename = file_data->file_names[j];
5309 dwarf2_per_objfile->filenames_cache->seen (filename);
5310 }
5311 }
5312 }
5313
5314 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5315 {
5316 gdb::unique_xmalloc_ptr<char> this_real_name;
5317
5318 if (need_fullname)
5319 this_real_name = gdb_realpath (filename);
5320 (*fun) (filename, this_real_name.get (), data);
5321 });
5322}
5323
5324static int
5325dw2_has_symbols (struct objfile *objfile)
5326{
5327 return 1;
5328}
5329
5330const struct quick_symbol_functions dwarf2_gdb_index_functions =
5331{
5332 dw2_has_symbols,
5333 dw2_find_last_source_symtab,
5334 dw2_forget_cached_source_info,
5335 dw2_map_symtabs_matching_filename,
5336 dw2_lookup_symbol,
5337 dw2_print_stats,
5338 dw2_dump,
927aa2e7
JK
5339 dw2_expand_symtabs_for_function,
5340 dw2_expand_all_symtabs,
5341 dw2_expand_symtabs_with_fullname,
5342 dw2_map_matching_symbols,
5343 dw2_expand_symtabs_matching,
5344 dw2_find_pc_sect_compunit_symtab,
5345 NULL,
5346 dw2_map_symbol_filenames
5347};
5348
5349/* DWARF-5 debug_names reader. */
5350
5351/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5352static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5353
5354/* A helper function that reads the .debug_names section in SECTION
5355 and fills in MAP. FILENAME is the name of the file containing the
5356 section; it is used for error reporting.
5357
5358 Returns true if all went well, false otherwise. */
5359
5360static bool
5361read_debug_names_from_section (struct objfile *objfile,
5362 const char *filename,
5363 struct dwarf2_section_info *section,
5364 mapped_debug_names &map)
5365{
5366 if (dwarf2_section_empty_p (section))
5367 return false;
5368
5369 /* Older elfutils strip versions could keep the section in the main
5370 executable while splitting it for the separate debug info file. */
5371 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5372 return false;
5373
5374 dwarf2_read_section (objfile, section);
5375
5376 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5377
5378 const gdb_byte *addr = section->buffer;
5379
5380 bfd *const abfd = get_section_bfd_owner (section);
5381
5382 unsigned int bytes_read;
5383 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5384 addr += bytes_read;
5385
5386 map.dwarf5_is_dwarf64 = bytes_read != 4;
5387 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5388 if (bytes_read + length != section->size)
5389 {
5390 /* There may be multiple per-CU indices. */
5391 warning (_("Section .debug_names in %s length %s does not match "
5392 "section length %s, ignoring .debug_names."),
5393 filename, plongest (bytes_read + length),
5394 pulongest (section->size));
5395 return false;
5396 }
5397
5398 /* The version number. */
5399 uint16_t version = read_2_bytes (abfd, addr);
5400 addr += 2;
5401 if (version != 5)
5402 {
5403 warning (_("Section .debug_names in %s has unsupported version %d, "
5404 "ignoring .debug_names."),
5405 filename, version);
5406 return false;
5407 }
5408
5409 /* Padding. */
5410 uint16_t padding = read_2_bytes (abfd, addr);
5411 addr += 2;
5412 if (padding != 0)
5413 {
5414 warning (_("Section .debug_names in %s has unsupported padding %d, "
5415 "ignoring .debug_names."),
5416 filename, padding);
5417 return false;
5418 }
5419
5420 /* comp_unit_count - The number of CUs in the CU list. */
5421 map.cu_count = read_4_bytes (abfd, addr);
5422 addr += 4;
5423
5424 /* local_type_unit_count - The number of TUs in the local TU
5425 list. */
5426 map.tu_count = read_4_bytes (abfd, addr);
5427 addr += 4;
5428
5429 /* foreign_type_unit_count - The number of TUs in the foreign TU
5430 list. */
5431 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5432 addr += 4;
5433 if (foreign_tu_count != 0)
5434 {
5435 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5436 "ignoring .debug_names."),
5437 filename, static_cast<unsigned long> (foreign_tu_count));
5438 return false;
5439 }
5440
5441 /* bucket_count - The number of hash buckets in the hash lookup
5442 table. */
5443 map.bucket_count = read_4_bytes (abfd, addr);
5444 addr += 4;
5445
5446 /* name_count - The number of unique names in the index. */
5447 map.name_count = read_4_bytes (abfd, addr);
5448 addr += 4;
5449
5450 /* abbrev_table_size - The size in bytes of the abbreviations
5451 table. */
5452 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5453 addr += 4;
5454
5455 /* augmentation_string_size - The size in bytes of the augmentation
5456 string. This value is rounded up to a multiple of 4. */
5457 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5458 addr += 4;
5459 map.augmentation_is_gdb = ((augmentation_string_size
5460 == sizeof (dwarf5_augmentation))
5461 && memcmp (addr, dwarf5_augmentation,
5462 sizeof (dwarf5_augmentation)) == 0);
5463 augmentation_string_size += (-augmentation_string_size) & 3;
5464 addr += augmentation_string_size;
5465
5466 /* List of CUs */
5467 map.cu_table_reordered = addr;
5468 addr += map.cu_count * map.offset_size;
5469
5470 /* List of Local TUs */
5471 map.tu_table_reordered = addr;
5472 addr += map.tu_count * map.offset_size;
5473
5474 /* Hash Lookup Table */
5475 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5476 addr += map.bucket_count * 4;
5477 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5478 addr += map.name_count * 4;
5479
5480 /* Name Table */
5481 map.name_table_string_offs_reordered = addr;
5482 addr += map.name_count * map.offset_size;
5483 map.name_table_entry_offs_reordered = addr;
5484 addr += map.name_count * map.offset_size;
5485
5486 const gdb_byte *abbrev_table_start = addr;
5487 for (;;)
5488 {
927aa2e7
JK
5489 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5490 addr += bytes_read;
5491 if (index_num == 0)
5492 break;
5493
5494 const auto insertpair
5495 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5496 if (!insertpair.second)
5497 {
5498 warning (_("Section .debug_names in %s has duplicate index %s, "
5499 "ignoring .debug_names."),
5500 filename, pulongest (index_num));
5501 return false;
5502 }
5503 mapped_debug_names::index_val &indexval = insertpair.first->second;
5504 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5505 addr += bytes_read;
5506
5507 for (;;)
5508 {
5509 mapped_debug_names::index_val::attr attr;
5510 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5511 addr += bytes_read;
5512 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5513 addr += bytes_read;
5514 if (attr.form == DW_FORM_implicit_const)
5515 {
5516 attr.implicit_const = read_signed_leb128 (abfd, addr,
5517 &bytes_read);
5518 addr += bytes_read;
5519 }
5520 if (attr.dw_idx == 0 && attr.form == 0)
5521 break;
5522 indexval.attr_vec.push_back (std::move (attr));
5523 }
5524 }
5525 if (addr != abbrev_table_start + abbrev_table_size)
5526 {
5527 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
5528 "of size %s vs. written as %u, ignoring .debug_names."),
5529 filename, plongest (addr - abbrev_table_start),
5530 abbrev_table_size);
927aa2e7
JK
5531 return false;
5532 }
5533 map.entry_pool = addr;
5534
5535 return true;
5536}
5537
5538/* A helper for create_cus_from_debug_names that handles the MAP's CU
5539 list. */
5540
5541static void
ed2dc618 5542create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5543 const mapped_debug_names &map,
5544 dwarf2_section_info &section,
b76e467d 5545 bool is_dwz)
927aa2e7
JK
5546{
5547 sect_offset sect_off_prev;
5548 for (uint32_t i = 0; i <= map.cu_count; ++i)
5549 {
5550 sect_offset sect_off_next;
5551 if (i < map.cu_count)
5552 {
5553 sect_off_next
5554 = (sect_offset) (extract_unsigned_integer
5555 (map.cu_table_reordered + i * map.offset_size,
5556 map.offset_size,
5557 map.dwarf5_byte_order));
5558 }
5559 else
5560 sect_off_next = (sect_offset) section.size;
5561 if (i >= 1)
5562 {
5563 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5564 dwarf2_per_cu_data *per_cu
ed2dc618 5565 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7 5566 sect_off_prev, length);
b76e467d 5567 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
927aa2e7
JK
5568 }
5569 sect_off_prev = sect_off_next;
5570 }
5571}
5572
5573/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5574 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5575
5576static void
ed2dc618 5577create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5578 const mapped_debug_names &map,
5579 const mapped_debug_names &dwz_map)
5580{
b76e467d
SM
5581 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5582 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5583
ed2dc618
SM
5584 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5585 dwarf2_per_objfile->info,
b76e467d 5586 false /* is_dwz */);
927aa2e7
JK
5587
5588 if (dwz_map.cu_count == 0)
5589 return;
5590
ed2dc618
SM
5591 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5592 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
b76e467d 5593 true /* is_dwz */);
927aa2e7
JK
5594}
5595
5596/* Read .debug_names. If everything went ok, initialize the "quick"
5597 elements of all the CUs and return true. Otherwise, return false. */
5598
5599static bool
ed2dc618 5600dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5601{
22ca247e
TT
5602 std::unique_ptr<mapped_debug_names> map
5603 (new mapped_debug_names (dwarf2_per_objfile));
ed2dc618
SM
5604 mapped_debug_names dwz_map (dwarf2_per_objfile);
5605 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5606
5607 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5608 &dwarf2_per_objfile->debug_names,
22ca247e 5609 *map))
927aa2e7
JK
5610 return false;
5611
5612 /* Don't use the index if it's empty. */
22ca247e 5613 if (map->name_count == 0)
927aa2e7
JK
5614 return false;
5615
5616 /* If there is a .dwz file, read it so we can get its CU list as
5617 well. */
ed2dc618 5618 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
927aa2e7
JK
5619 if (dwz != NULL)
5620 {
5621 if (!read_debug_names_from_section (objfile,
00f93c44 5622 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5623 &dwz->debug_names, dwz_map))
5624 {
5625 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5626 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5627 return false;
5628 }
5629 }
5630
22ca247e 5631 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
927aa2e7 5632
22ca247e 5633 if (map->tu_count != 0)
927aa2e7
JK
5634 {
5635 /* We can only handle a single .debug_types when we have an
5636 index. */
fd5866f6 5637 if (dwarf2_per_objfile->types.size () != 1)
927aa2e7
JK
5638 return false;
5639
fd5866f6 5640 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
927aa2e7
JK
5641
5642 create_signatured_type_table_from_debug_names
22ca247e 5643 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
927aa2e7
JK
5644 }
5645
ed2dc618
SM
5646 create_addrmap_from_aranges (dwarf2_per_objfile,
5647 &dwarf2_per_objfile->debug_aranges);
927aa2e7 5648
22ca247e 5649 dwarf2_per_objfile->debug_names_table = std::move (map);
927aa2e7
JK
5650 dwarf2_per_objfile->using_index = 1;
5651 dwarf2_per_objfile->quick_file_names_table =
b76e467d 5652 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
927aa2e7
JK
5653
5654 return true;
5655}
5656
927aa2e7
JK
5657/* Type used to manage iterating over all CUs looking for a symbol for
5658 .debug_names. */
5659
5660class dw2_debug_names_iterator
5661{
5662public:
927aa2e7 5663 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5664 gdb::optional<block_enum> block_index,
5665 domain_enum domain,
927aa2e7 5666 const char *name)
2b79f376 5667 : m_map (map), m_block_index (block_index), m_domain (domain),
927aa2e7
JK
5668 m_addr (find_vec_in_debug_names (map, name))
5669 {}
5670
5671 dw2_debug_names_iterator (const mapped_debug_names &map,
5672 search_domain search, uint32_t namei)
5673 : m_map (map),
5674 m_search (search),
5675 m_addr (find_vec_in_debug_names (map, namei))
5676 {}
5677
3b00ef10
TT
5678 dw2_debug_names_iterator (const mapped_debug_names &map,
5679 block_enum block_index, domain_enum domain,
5680 uint32_t namei)
5681 : m_map (map), m_block_index (block_index), m_domain (domain),
5682 m_addr (find_vec_in_debug_names (map, namei))
5683 {}
5684
927aa2e7
JK
5685 /* Return the next matching CU or NULL if there are no more. */
5686 dwarf2_per_cu_data *next ();
5687
5688private:
5689 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5690 const char *name);
5691 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5692 uint32_t namei);
5693
5694 /* The internalized form of .debug_names. */
5695 const mapped_debug_names &m_map;
5696
2b79f376
SM
5697 /* If set, only look for symbols that match that block. Valid values are
5698 GLOBAL_BLOCK and STATIC_BLOCK. */
5699 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5700
5701 /* The kind of symbol we're looking for. */
5702 const domain_enum m_domain = UNDEF_DOMAIN;
5703 const search_domain m_search = ALL_DOMAIN;
5704
5705 /* The list of CUs from the index entry of the symbol, or NULL if
5706 not found. */
5707 const gdb_byte *m_addr;
5708};
5709
5710const char *
5711mapped_debug_names::namei_to_name (uint32_t namei) const
5712{
5713 const ULONGEST namei_string_offs
5714 = extract_unsigned_integer ((name_table_string_offs_reordered
5715 + namei * offset_size),
5716 offset_size,
5717 dwarf5_byte_order);
5718 return read_indirect_string_at_offset
ed2dc618 5719 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
927aa2e7
JK
5720}
5721
5722/* Find a slot in .debug_names for the object named NAME. If NAME is
5723 found, return pointer to its pool data. If NAME cannot be found,
5724 return NULL. */
5725
5726const gdb_byte *
5727dw2_debug_names_iterator::find_vec_in_debug_names
5728 (const mapped_debug_names &map, const char *name)
5729{
5730 int (*cmp) (const char *, const char *);
5731
54ee4252 5732 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5733 if (current_language->la_language == language_cplus
5734 || current_language->la_language == language_fortran
5735 || current_language->la_language == language_d)
5736 {
5737 /* NAME is already canonical. Drop any qualifiers as
5738 .debug_names does not contain any. */
5739
5740 if (strchr (name, '(') != NULL)
5741 {
54ee4252 5742 without_params = cp_remove_params (name);
927aa2e7 5743 if (without_params != NULL)
54ee4252 5744 name = without_params.get ();
927aa2e7
JK
5745 }
5746 }
5747
5748 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5749
5750 const uint32_t full_hash = dwarf5_djb_hash (name);
5751 uint32_t namei
5752 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5753 (map.bucket_table_reordered
5754 + (full_hash % map.bucket_count)), 4,
5755 map.dwarf5_byte_order);
5756 if (namei == 0)
5757 return NULL;
5758 --namei;
5759 if (namei >= map.name_count)
5760 {
b98664d3 5761 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5762 "[in module %s]"),
5763 namei, map.name_count,
ed2dc618 5764 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5765 return NULL;
5766 }
5767
5768 for (;;)
5769 {
5770 const uint32_t namei_full_hash
5771 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5772 (map.hash_table_reordered + namei), 4,
5773 map.dwarf5_byte_order);
5774 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5775 return NULL;
5776
5777 if (full_hash == namei_full_hash)
5778 {
5779 const char *const namei_string = map.namei_to_name (namei);
5780
5781#if 0 /* An expensive sanity check. */
5782 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5783 {
b98664d3 5784 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5785 "[in module %s]"),
5786 namei, objfile_name (dwarf2_per_objfile->objfile));
5787 return NULL;
5788 }
5789#endif
5790
5791 if (cmp (namei_string, name) == 0)
5792 {
5793 const ULONGEST namei_entry_offs
5794 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5795 + namei * map.offset_size),
5796 map.offset_size, map.dwarf5_byte_order);
5797 return map.entry_pool + namei_entry_offs;
5798 }
5799 }
5800
5801 ++namei;
5802 if (namei >= map.name_count)
5803 return NULL;
5804 }
5805}
5806
5807const gdb_byte *
5808dw2_debug_names_iterator::find_vec_in_debug_names
5809 (const mapped_debug_names &map, uint32_t namei)
5810{
5811 if (namei >= map.name_count)
5812 {
b98664d3 5813 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5814 "[in module %s]"),
5815 namei, map.name_count,
ed2dc618 5816 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5817 return NULL;
5818 }
5819
5820 const ULONGEST namei_entry_offs
5821 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5822 + namei * map.offset_size),
5823 map.offset_size, map.dwarf5_byte_order);
5824 return map.entry_pool + namei_entry_offs;
5825}
5826
5827/* See dw2_debug_names_iterator. */
5828
5829dwarf2_per_cu_data *
5830dw2_debug_names_iterator::next ()
5831{
5832 if (m_addr == NULL)
5833 return NULL;
5834
ed2dc618
SM
5835 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5836 struct objfile *objfile = dwarf2_per_objfile->objfile;
5837 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5838
5839 again:
5840
5841 unsigned int bytes_read;
5842 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5843 m_addr += bytes_read;
5844 if (abbrev == 0)
5845 return NULL;
5846
5847 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5848 if (indexval_it == m_map.abbrev_map.cend ())
5849 {
b98664d3 5850 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5851 "[in module %s]"),
ed2dc618 5852 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5853 return NULL;
5854 }
5855 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5856 enum class symbol_linkage {
5857 unknown,
5858 static_,
5859 extern_,
23c13d42 5860 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5861 dwarf2_per_cu_data *per_cu = NULL;
5862 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5863 {
5864 ULONGEST ull;
5865 switch (attr.form)
5866 {
5867 case DW_FORM_implicit_const:
5868 ull = attr.implicit_const;
5869 break;
5870 case DW_FORM_flag_present:
5871 ull = 1;
5872 break;
5873 case DW_FORM_udata:
5874 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5875 m_addr += bytes_read;
5876 break;
5877 default:
b98664d3 5878 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5879 dwarf_form_name (attr.form),
ed2dc618 5880 objfile_name (objfile));
927aa2e7
JK
5881 return NULL;
5882 }
5883 switch (attr.dw_idx)
5884 {
5885 case DW_IDX_compile_unit:
5886 /* Don't crash on bad data. */
b76e467d 5887 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
927aa2e7 5888 {
b98664d3 5889 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5890 " [in module %s]"),
5891 pulongest (ull),
5892 objfile_name (dwarf2_per_objfile->objfile));
5893 continue;
5894 }
ff4c9fec 5895 per_cu = dwarf2_per_objfile->get_cutu (ull);
927aa2e7 5896 break;
8af5c486
JK
5897 case DW_IDX_type_unit:
5898 /* Don't crash on bad data. */
b2bdb8cf 5899 if (ull >= dwarf2_per_objfile->all_type_units.size ())
8af5c486 5900 {
b98664d3 5901 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5902 " [in module %s]"),
5903 pulongest (ull),
5904 objfile_name (dwarf2_per_objfile->objfile));
5905 continue;
5906 }
ff4c9fec 5907 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
8af5c486 5908 break;
927aa2e7
JK
5909 case DW_IDX_GNU_internal:
5910 if (!m_map.augmentation_is_gdb)
5911 break;
23c13d42 5912 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5913 break;
5914 case DW_IDX_GNU_external:
5915 if (!m_map.augmentation_is_gdb)
5916 break;
23c13d42 5917 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5918 break;
5919 }
5920 }
5921
5922 /* Skip if already read in. */
5923 if (per_cu->v.quick->compunit_symtab)
5924 goto again;
5925
5926 /* Check static vs global. */
23c13d42 5927 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5928 {
2b79f376 5929 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5930 const bool symbol_is_static =
5931 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5932 if (want_static != symbol_is_static)
2b79f376 5933 goto again;
927aa2e7
JK
5934 }
5935
5936 /* Match dw2_symtab_iter_next, symbol_kind
5937 and debug_names::psymbol_tag. */
5938 switch (m_domain)
5939 {
5940 case VAR_DOMAIN:
5941 switch (indexval.dwarf_tag)
5942 {
5943 case DW_TAG_variable:
5944 case DW_TAG_subprogram:
5945 /* Some types are also in VAR_DOMAIN. */
5946 case DW_TAG_typedef:
5947 case DW_TAG_structure_type:
5948 break;
5949 default:
5950 goto again;
5951 }
5952 break;
5953 case STRUCT_DOMAIN:
5954 switch (indexval.dwarf_tag)
5955 {
5956 case DW_TAG_typedef:
5957 case DW_TAG_structure_type:
5958 break;
5959 default:
5960 goto again;
5961 }
5962 break;
5963 case LABEL_DOMAIN:
5964 switch (indexval.dwarf_tag)
5965 {
5966 case 0:
5967 case DW_TAG_variable:
5968 break;
5969 default:
5970 goto again;
5971 }
5972 break;
5973 default:
5974 break;
5975 }
5976
5977 /* Match dw2_expand_symtabs_matching, symbol_kind and
5978 debug_names::psymbol_tag. */
5979 switch (m_search)
4b514bc8 5980 {
927aa2e7
JK
5981 case VARIABLES_DOMAIN:
5982 switch (indexval.dwarf_tag)
4b514bc8 5983 {
927aa2e7
JK
5984 case DW_TAG_variable:
5985 break;
5986 default:
5987 goto again;
4b514bc8 5988 }
927aa2e7
JK
5989 break;
5990 case FUNCTIONS_DOMAIN:
5991 switch (indexval.dwarf_tag)
4b514bc8 5992 {
927aa2e7
JK
5993 case DW_TAG_subprogram:
5994 break;
5995 default:
5996 goto again;
4b514bc8 5997 }
927aa2e7
JK
5998 break;
5999 case TYPES_DOMAIN:
6000 switch (indexval.dwarf_tag)
6001 {
6002 case DW_TAG_typedef:
6003 case DW_TAG_structure_type:
6004 break;
6005 default:
6006 goto again;
6007 }
6008 break;
6009 default:
6010 break;
4b514bc8 6011 }
927aa2e7
JK
6012
6013 return per_cu;
4b514bc8 6014}
61920122 6015
927aa2e7 6016static struct compunit_symtab *
c7f839cb 6017dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 6018 const char *name, domain_enum domain)
4b514bc8 6019{
ed2dc618
SM
6020 struct dwarf2_per_objfile *dwarf2_per_objfile
6021 = get_dwarf2_per_objfile (objfile);
61920122 6022
927aa2e7
JK
6023 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6024 if (!mapp)
61920122 6025 {
927aa2e7
JK
6026 /* index is NULL if OBJF_READNOW. */
6027 return NULL;
6028 }
6029 const auto &map = *mapp;
9291a0cd 6030
2b79f376 6031 dw2_debug_names_iterator iter (map, block_index, domain, name);
9703b513 6032
927aa2e7
JK
6033 struct compunit_symtab *stab_best = NULL;
6034 struct dwarf2_per_cu_data *per_cu;
6035 while ((per_cu = iter.next ()) != NULL)
6036 {
6037 struct symbol *sym, *with_opaque = NULL;
58f0c718 6038 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
927aa2e7 6039 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 6040 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 6041
927aa2e7
JK
6042 sym = block_find_symbol (block, name, domain,
6043 block_find_non_opaque_type_preferred,
6044 &with_opaque);
9703b513 6045
927aa2e7
JK
6046 /* Some caution must be observed with overloaded functions and
6047 methods, since the index will not contain any overload
6048 information (but NAME might contain it). */
a3ec0bb1 6049
927aa2e7
JK
6050 if (sym != NULL
6051 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6052 return stab;
6053 if (with_opaque != NULL
6054 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6055 stab_best = stab;
9703b513 6056
927aa2e7 6057 /* Keep looking through other CUs. */
9703b513
TT
6058 }
6059
927aa2e7 6060 return stab_best;
9703b513
TT
6061}
6062
927aa2e7
JK
6063/* This dumps minimal information about .debug_names. It is called
6064 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6065 uses this to verify that .debug_names has been loaded. */
9291a0cd 6066
927aa2e7
JK
6067static void
6068dw2_debug_names_dump (struct objfile *objfile)
6069{
ed2dc618
SM
6070 struct dwarf2_per_objfile *dwarf2_per_objfile
6071 = get_dwarf2_per_objfile (objfile);
6072
927aa2e7
JK
6073 gdb_assert (dwarf2_per_objfile->using_index);
6074 printf_filtered (".debug_names:");
6075 if (dwarf2_per_objfile->debug_names_table)
6076 printf_filtered (" exists\n");
6077 else
6078 printf_filtered (" faked for \"readnow\"\n");
6079 printf_filtered ("\n");
9291a0cd
TT
6080}
6081
9291a0cd 6082static void
927aa2e7
JK
6083dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6084 const char *func_name)
9291a0cd 6085{
ed2dc618
SM
6086 struct dwarf2_per_objfile *dwarf2_per_objfile
6087 = get_dwarf2_per_objfile (objfile);
ae2de4f8 6088
927aa2e7
JK
6089 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6090 if (dwarf2_per_objfile->debug_names_table)
24c79950 6091 {
927aa2e7 6092 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
24c79950 6093
2b79f376 6094 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
24c79950 6095
927aa2e7
JK
6096 struct dwarf2_per_cu_data *per_cu;
6097 while ((per_cu = iter.next ()) != NULL)
58f0c718 6098 dw2_instantiate_symtab (per_cu, false);
927aa2e7
JK
6099 }
6100}
24c79950 6101
3b00ef10
TT
6102static void
6103dw2_debug_names_map_matching_symbols
6104 (struct objfile *objfile,
6105 const lookup_name_info &name, domain_enum domain,
6106 int global,
6107 gdb::function_view<symbol_found_callback_ftype> callback,
6108 symbol_compare_ftype *ordered_compare)
6109{
6110 struct dwarf2_per_objfile *dwarf2_per_objfile
6111 = get_dwarf2_per_objfile (objfile);
6112
6113 /* debug_names_table is NULL if OBJF_READNOW. */
6114 if (!dwarf2_per_objfile->debug_names_table)
6115 return;
6116
6117 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
6118 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
6119
6120 const char *match_name = name.ada ().lookup_name ().c_str ();
6121 auto matcher = [&] (const char *symname)
6122 {
6123 if (ordered_compare == nullptr)
6124 return true;
6125 return ordered_compare (symname, match_name) == 0;
6126 };
6127
6128 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
6129 [&] (offset_type namei)
6130 {
6131 /* The name was matched, now expand corresponding CUs that were
6132 marked. */
6133 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
6134
6135 struct dwarf2_per_cu_data *per_cu;
6136 while ((per_cu = iter.next ()) != NULL)
6137 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
6138 return true;
6139 });
6140
6141 /* It's a shame we couldn't do this inside the
6142 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
6143 that have already been expanded. Instead, this loop matches what
6144 the psymtab code does. */
6145 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
6146 {
6147 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
6148 if (cust != nullptr)
6149 {
6150 const struct block *block
6151 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
6152 if (!iterate_over_symbols_terminated (block, name,
6153 domain, callback))
6154 break;
6155 }
6156 }
6157}
6158
927aa2e7
JK
6159static void
6160dw2_debug_names_expand_symtabs_matching
6161 (struct objfile *objfile,
6162 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6163 const lookup_name_info &lookup_name,
6164 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6165 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6166 enum search_domain kind)
6167{
ed2dc618
SM
6168 struct dwarf2_per_objfile *dwarf2_per_objfile
6169 = get_dwarf2_per_objfile (objfile);
9291a0cd 6170
927aa2e7
JK
6171 /* debug_names_table is NULL if OBJF_READNOW. */
6172 if (!dwarf2_per_objfile->debug_names_table)
6173 return;
9291a0cd 6174
ed2dc618 6175 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 6176
44ed8f3e 6177 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
bbf2f4df 6178
44ed8f3e
PA
6179 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6180 symbol_matcher,
6181 kind, [&] (offset_type namei)
927aa2e7 6182 {
927aa2e7
JK
6183 /* The name was matched, now expand corresponding CUs that were
6184 marked. */
6185 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 6186
927aa2e7
JK
6187 struct dwarf2_per_cu_data *per_cu;
6188 while ((per_cu = iter.next ()) != NULL)
6189 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6190 expansion_notify);
3b00ef10 6191 return true;
44ed8f3e 6192 });
9291a0cd
TT
6193}
6194
927aa2e7 6195const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
6196{
6197 dw2_has_symbols,
6198 dw2_find_last_source_symtab,
6199 dw2_forget_cached_source_info,
f8eba3c6 6200 dw2_map_symtabs_matching_filename,
927aa2e7 6201 dw2_debug_names_lookup_symbol,
9291a0cd 6202 dw2_print_stats,
927aa2e7 6203 dw2_debug_names_dump,
927aa2e7 6204 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 6205 dw2_expand_all_symtabs,
652a8996 6206 dw2_expand_symtabs_with_fullname,
3b00ef10 6207 dw2_debug_names_map_matching_symbols,
927aa2e7 6208 dw2_debug_names_expand_symtabs_matching,
43f3e411 6209 dw2_find_pc_sect_compunit_symtab,
71a3c369 6210 NULL,
9291a0cd
TT
6211 dw2_map_symbol_filenames
6212};
6213
4485a1c1
SM
6214/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
6215 to either a dwarf2_per_objfile or dwz_file object. */
6216
6217template <typename T>
6218static gdb::array_view<const gdb_byte>
6219get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
6220{
6221 dwarf2_section_info *section = &section_owner->gdb_index;
6222
6223 if (dwarf2_section_empty_p (section))
6224 return {};
6225
6226 /* Older elfutils strip versions could keep the section in the main
6227 executable while splitting it for the separate debug info file. */
6228 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
6229 return {};
6230
6231 dwarf2_read_section (obj, section);
6232
8bebfcda
PA
6233 /* dwarf2_section_info::size is a bfd_size_type, while
6234 gdb::array_view works with size_t. On 32-bit hosts, with
6235 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
6236 is 32-bit. So we need an explicit narrowing conversion here.
6237 This is fine, because it's impossible to allocate or mmap an
6238 array/buffer larger than what size_t can represent. */
6239 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
6240}
6241
87d6a7aa
SM
6242/* Lookup the index cache for the contents of the index associated to
6243 DWARF2_OBJ. */
6244
6245static gdb::array_view<const gdb_byte>
6246get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
6247{
6248 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
6249 if (build_id == nullptr)
6250 return {};
6251
6252 return global_index_cache.lookup_gdb_index (build_id,
6253 &dwarf2_obj->index_cache_res);
6254}
6255
6256/* Same as the above, but for DWZ. */
6257
6258static gdb::array_view<const gdb_byte>
6259get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
6260{
6261 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
6262 if (build_id == nullptr)
6263 return {};
6264
6265 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6266}
6267
3c0aa29a 6268/* See symfile.h. */
9291a0cd 6269
3c0aa29a
PA
6270bool
6271dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 6272{
ed2dc618
SM
6273 struct dwarf2_per_objfile *dwarf2_per_objfile
6274 = get_dwarf2_per_objfile (objfile);
6275
9291a0cd
TT
6276 /* If we're about to read full symbols, don't bother with the
6277 indices. In this case we also don't care if some other debug
6278 format is making psymtabs, because they are all about to be
6279 expanded anyway. */
6280 if ((objfile->flags & OBJF_READNOW))
6281 {
9291a0cd 6282 dwarf2_per_objfile->using_index = 1;
ed2dc618
SM
6283 create_all_comp_units (dwarf2_per_objfile);
6284 create_all_type_units (dwarf2_per_objfile);
b76e467d
SM
6285 dwarf2_per_objfile->quick_file_names_table
6286 = create_quick_file_names_table
6287 (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd 6288
b76e467d 6289 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 6290 + dwarf2_per_objfile->all_type_units.size ()); ++i)
9291a0cd 6291 {
ff4c9fec 6292 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 6293
e254ef6a
DE
6294 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6295 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
6296 }
6297
6298 /* Return 1 so that gdb sees the "quick" functions. However,
6299 these functions will be no-ops because we will have expanded
6300 all symtabs. */
3c0aa29a
PA
6301 *index_kind = dw_index_kind::GDB_INDEX;
6302 return true;
9291a0cd
TT
6303 }
6304
ed2dc618 6305 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
6306 {
6307 *index_kind = dw_index_kind::DEBUG_NAMES;
6308 return true;
6309 }
927aa2e7 6310
4485a1c1
SM
6311 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6312 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
6313 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
6314 {
6315 *index_kind = dw_index_kind::GDB_INDEX;
6316 return true;
6317 }
9291a0cd 6318
87d6a7aa
SM
6319 /* ... otherwise, try to find the index in the index cache. */
6320 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6321 get_gdb_index_contents_from_cache,
6322 get_gdb_index_contents_from_cache_dwz))
6323 {
6324 global_index_cache.hit ();
6325 *index_kind = dw_index_kind::GDB_INDEX;
6326 return true;
6327 }
6328
6329 global_index_cache.miss ();
3c0aa29a 6330 return false;
9291a0cd
TT
6331}
6332
6333\f
6334
dce234bc
PP
6335/* Build a partial symbol table. */
6336
6337void
f29dff0a 6338dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 6339{
ed2dc618
SM
6340 struct dwarf2_per_objfile *dwarf2_per_objfile
6341 = get_dwarf2_per_objfile (objfile);
c9bf0622 6342
6eee24ce 6343 init_psymbol_list (objfile, 1024);
c906108c 6344
a70b8144 6345 try
c9bf0622
TT
6346 {
6347 /* This isn't really ideal: all the data we allocate on the
6348 objfile's obstack is still uselessly kept around. However,
6349 freeing it seems unsafe. */
906768f9 6350 psymtab_discarder psymtabs (objfile);
ed2dc618 6351 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 6352 psymtabs.keep ();
87d6a7aa
SM
6353
6354 /* (maybe) store an index in the cache. */
6355 global_index_cache.store (dwarf2_per_objfile);
c9bf0622 6356 }
230d2906 6357 catch (const gdb_exception_error &except)
492d29ea
PA
6358 {
6359 exception_print (gdb_stderr, except);
6360 }
c906108c 6361}
c906108c 6362
1ce1cefd
DE
6363/* Return the total length of the CU described by HEADER. */
6364
6365static unsigned int
6366get_cu_length (const struct comp_unit_head *header)
6367{
6368 return header->initial_length_size + header->length;
6369}
6370
9c541725 6371/* Return TRUE if SECT_OFF is within CU_HEADER. */
45452591 6372
9c541725
PA
6373static inline bool
6374offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
45452591 6375{
9c541725
PA
6376 sect_offset bottom = cu_header->sect_off;
6377 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
9a619af0 6378
9c541725 6379 return sect_off >= bottom && sect_off < top;
45452591
DE
6380}
6381
3b80fe9b
DE
6382/* Find the base address of the compilation unit for range lists and
6383 location lists. It will normally be specified by DW_AT_low_pc.
6384 In DWARF-3 draft 4, the base address could be overridden by
6385 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6386 compilation units with discontinuous ranges. */
6387
6388static void
6389dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6390{
6391 struct attribute *attr;
6392
6393 cu->base_known = 0;
6394 cu->base_address = 0;
6395
6396 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6397 if (attr)
6398 {
31aa7e4e 6399 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6400 cu->base_known = 1;
6401 }
6402 else
6403 {
6404 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6405 if (attr)
6406 {
31aa7e4e 6407 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6408 cu->base_known = 1;
6409 }
6410 }
6411}
6412
93311388 6413/* Read in the comp unit header information from the debug_info at info_ptr.
43988095 6414 Use rcuh_kind::COMPILE as the default type if not known by the caller.
93311388
DE
6415 NOTE: This leaves members offset, first_die_offset to be filled in
6416 by the caller. */
107d2387 6417
d521ce57 6418static const gdb_byte *
107d2387 6419read_comp_unit_head (struct comp_unit_head *cu_header,
43988095
JK
6420 const gdb_byte *info_ptr,
6421 struct dwarf2_section_info *section,
6422 rcuh_kind section_kind)
107d2387
AC
6423{
6424 int signed_addr;
891d2f0b 6425 unsigned int bytes_read;
43988095
JK
6426 const char *filename = get_section_file_name (section);
6427 bfd *abfd = get_section_bfd_owner (section);
c764a876
DE
6428
6429 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6430 cu_header->initial_length_size = bytes_read;
6431 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 6432 info_ptr += bytes_read;
107d2387 6433 cu_header->version = read_2_bytes (abfd, info_ptr);
1ea5da02
TV
6434 if (cu_header->version < 2 || cu_header->version > 5)
6435 error (_("Dwarf Error: wrong version in compilation unit header "
6436 "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
6437 cu_header->version, filename);
107d2387 6438 info_ptr += 2;
43988095
JK
6439 if (cu_header->version < 5)
6440 switch (section_kind)
6441 {
6442 case rcuh_kind::COMPILE:
6443 cu_header->unit_type = DW_UT_compile;
6444 break;
6445 case rcuh_kind::TYPE:
6446 cu_header->unit_type = DW_UT_type;
6447 break;
6448 default:
6449 internal_error (__FILE__, __LINE__,
6450 _("read_comp_unit_head: invalid section_kind"));
6451 }
6452 else
6453 {
6454 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6455 (read_1_byte (abfd, info_ptr));
6456 info_ptr += 1;
6457 switch (cu_header->unit_type)
6458 {
6459 case DW_UT_compile:
a084a2a6
AT
6460 case DW_UT_partial:
6461 case DW_UT_skeleton:
6462 case DW_UT_split_compile:
43988095
JK
6463 if (section_kind != rcuh_kind::COMPILE)
6464 error (_("Dwarf Error: wrong unit_type in compilation unit header "
a084a2a6
AT
6465 "(is %s, should be %s) [in module %s]"),
6466 dwarf_unit_type_name (cu_header->unit_type),
6467 dwarf_unit_type_name (DW_UT_type), filename);
43988095
JK
6468 break;
6469 case DW_UT_type:
a084a2a6 6470 case DW_UT_split_type:
43988095
JK
6471 section_kind = rcuh_kind::TYPE;
6472 break;
6473 default:
6474 error (_("Dwarf Error: wrong unit_type in compilation unit header "
a084a2a6
AT
6475 "(is %#04x, should be one of: %s, %s, %s, %s or %s) "
6476 "[in module %s]"), cu_header->unit_type,
6477 dwarf_unit_type_name (DW_UT_compile),
6478 dwarf_unit_type_name (DW_UT_skeleton),
6479 dwarf_unit_type_name (DW_UT_split_compile),
6480 dwarf_unit_type_name (DW_UT_type),
6481 dwarf_unit_type_name (DW_UT_split_type), filename);
43988095
JK
6482 }
6483
6484 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6485 info_ptr += 1;
6486 }
9c541725
PA
6487 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6488 cu_header,
6489 &bytes_read);
613e1657 6490 info_ptr += bytes_read;
43988095
JK
6491 if (cu_header->version < 5)
6492 {
6493 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6494 info_ptr += 1;
6495 }
107d2387
AC
6496 signed_addr = bfd_get_sign_extend_vma (abfd);
6497 if (signed_addr < 0)
8e65ff28 6498 internal_error (__FILE__, __LINE__,
e2e0b3e5 6499 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 6500 cu_header->signed_addr_p = signed_addr;
c764a876 6501
a084a2a6
AT
6502 bool header_has_signature = section_kind == rcuh_kind::TYPE
6503 || cu_header->unit_type == DW_UT_skeleton
6504 || cu_header->unit_type == DW_UT_split_compile;
43988095 6505
a084a2a6
AT
6506 if (header_has_signature)
6507 {
43988095
JK
6508 cu_header->signature = read_8_bytes (abfd, info_ptr);
6509 info_ptr += 8;
a084a2a6 6510 }
43988095 6511
a084a2a6
AT
6512 if (section_kind == rcuh_kind::TYPE)
6513 {
6514 LONGEST type_offset;
43988095
JK
6515 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6516 info_ptr += bytes_read;
9c541725
PA
6517 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6518 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
43988095
JK
6519 error (_("Dwarf Error: Too big type_offset in compilation unit "
6520 "header (is %s) [in module %s]"), plongest (type_offset),
6521 filename);
6522 }
6523
107d2387
AC
6524 return info_ptr;
6525}
6526
36586728
TT
6527/* Helper function that returns the proper abbrev section for
6528 THIS_CU. */
6529
6530static struct dwarf2_section_info *
6531get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6532{
6533 struct dwarf2_section_info *abbrev;
ed2dc618 6534 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
36586728
TT
6535
6536 if (this_cu->is_dwz)
ed2dc618 6537 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
36586728
TT
6538 else
6539 abbrev = &dwarf2_per_objfile->abbrev;
6540
6541 return abbrev;
6542}
6543
9ff913ba
DE
6544/* Subroutine of read_and_check_comp_unit_head and
6545 read_and_check_type_unit_head to simplify them.
6546 Perform various error checking on the header. */
6547
6548static void
ed2dc618
SM
6549error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6550 struct comp_unit_head *header,
4bdcc0c1
DE
6551 struct dwarf2_section_info *section,
6552 struct dwarf2_section_info *abbrev_section)
9ff913ba 6553{
a32a8923 6554 const char *filename = get_section_file_name (section);
9ff913ba 6555
9c541725 6556 if (to_underlying (header->abbrev_sect_off)
36586728 6557 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9d8780f0
SM
6558 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6559 "(offset %s + 6) [in module %s]"),
6560 sect_offset_str (header->abbrev_sect_off),
6561 sect_offset_str (header->sect_off),
9ff913ba
DE
6562 filename);
6563
9c541725 6564 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
9ff913ba 6565 avoid potential 32-bit overflow. */
9c541725 6566 if (((ULONGEST) header->sect_off + get_cu_length (header))
9ff913ba 6567 > section->size)
9c541725 6568 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
9d8780f0
SM
6569 "(offset %s + 0) [in module %s]"),
6570 header->length, sect_offset_str (header->sect_off),
9ff913ba
DE
6571 filename);
6572}
6573
6574/* Read in a CU/TU header and perform some basic error checking.
6575 The contents of the header are stored in HEADER.
6576 The result is a pointer to the start of the first DIE. */
adabb602 6577
d521ce57 6578static const gdb_byte *
ed2dc618
SM
6579read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6580 struct comp_unit_head *header,
9ff913ba 6581 struct dwarf2_section_info *section,
4bdcc0c1 6582 struct dwarf2_section_info *abbrev_section,
d521ce57 6583 const gdb_byte *info_ptr,
43988095 6584 rcuh_kind section_kind)
72bf9492 6585{
d521ce57 6586 const gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492 6587
9c541725 6588 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
adabb602 6589
43988095 6590 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
9ff913ba 6591
9c541725 6592 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
348e048f 6593
ed2dc618
SM
6594 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6595 abbrev_section);
9ff913ba
DE
6596
6597 return info_ptr;
348e048f
DE
6598}
6599
f4dc4d17
DE
6600/* Fetch the abbreviation table offset from a comp or type unit header. */
6601
6602static sect_offset
ed2dc618
SM
6603read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6604 struct dwarf2_section_info *section,
9c541725 6605 sect_offset sect_off)
f4dc4d17 6606{
a32a8923 6607 bfd *abfd = get_section_bfd_owner (section);
d521ce57 6608 const gdb_byte *info_ptr;
ac298888 6609 unsigned int initial_length_size, offset_size;
43988095 6610 uint16_t version;
f4dc4d17
DE
6611
6612 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
9c541725 6613 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6614 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6615 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6616 info_ptr += initial_length_size;
6617
6618 version = read_2_bytes (abfd, info_ptr);
6619 info_ptr += 2;
6620 if (version >= 5)
6621 {
6622 /* Skip unit type and address size. */
6623 info_ptr += 2;
6624 }
6625
9c541725 6626 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
f4dc4d17
DE
6627}
6628
aaa75496
JB
6629/* Allocate a new partial symtab for file named NAME and mark this new
6630 partial symtab as being an include of PST. */
6631
6632static void
d521ce57 6633dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
6634 struct objfile *objfile)
6635{
6636 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6637
fbd9ab74
JK
6638 if (!IS_ABSOLUTE_PATH (subpst->filename))
6639 {
6640 /* It shares objfile->objfile_obstack. */
6641 subpst->dirname = pst->dirname;
6642 }
6643
a9342b62 6644 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6645 subpst->dependencies[0] = pst;
6646 subpst->number_of_dependencies = 1;
6647
aaa75496 6648 subpst->read_symtab = pst->read_symtab;
aaa75496
JB
6649
6650 /* No private part is necessary for include psymtabs. This property
6651 can be used to differentiate between such include psymtabs and
10b3939b 6652 the regular ones. */
58a9656e 6653 subpst->read_symtab_private = NULL;
aaa75496
JB
6654}
6655
6656/* Read the Line Number Program data and extract the list of files
6657 included by the source file represented by PST. Build an include
d85a05f0 6658 partial symtab for each of these included files. */
aaa75496
JB
6659
6660static void
6661dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
6662 struct die_info *die,
6663 struct partial_symtab *pst)
aaa75496 6664{
fff8551c 6665 line_header_up lh;
d85a05f0 6666 struct attribute *attr;
aaa75496 6667
d85a05f0
DJ
6668 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6669 if (attr)
9c541725 6670 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6671 if (lh == NULL)
6672 return; /* No linetable, so no includes. */
6673
79748972
TT
6674 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6675 that we pass in the raw text_low here; that is ok because we're
6676 only decoding the line table to make include partial symtabs, and
6677 so the addresses aren't really used. */
4ae976d1 6678 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6679 pst->raw_text_low (), 1);
aaa75496
JB
6680}
6681
348e048f 6682static hashval_t
52dc124a 6683hash_signatured_type (const void *item)
348e048f 6684{
9a3c8263
SM
6685 const struct signatured_type *sig_type
6686 = (const struct signatured_type *) item;
9a619af0 6687
348e048f 6688 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6689 return sig_type->signature;
348e048f
DE
6690}
6691
6692static int
52dc124a 6693eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6694{
9a3c8263
SM
6695 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6696 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6697
348e048f
DE
6698 return lhs->signature == rhs->signature;
6699}
6700
1fd400ff
TT
6701/* Allocate a hash table for signatured types. */
6702
6703static htab_t
673bfd45 6704allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
6705{
6706 return htab_create_alloc_ex (41,
52dc124a
DE
6707 hash_signatured_type,
6708 eq_signatured_type,
1fd400ff
TT
6709 NULL,
6710 &objfile->objfile_obstack,
6711 hashtab_obstack_allocate,
6712 dummy_obstack_deallocate);
6713}
6714
d467dd73 6715/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6716
6717static int
d467dd73 6718add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6719{
9a3c8263 6720 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6721 std::vector<signatured_type *> *all_type_units
6722 = (std::vector<signatured_type *> *) datum;
1fd400ff 6723
b2bdb8cf 6724 all_type_units->push_back (sigt);
1fd400ff
TT
6725
6726 return 1;
6727}
6728
78d4d2c5 6729/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6730 and fill them into TYPES_HTAB. It will process only type units,
6731 therefore DW_UT_type. */
c88ee1f0 6732
78d4d2c5 6733static void
ed2dc618
SM
6734create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6735 struct dwo_file *dwo_file,
43988095
JK
6736 dwarf2_section_info *section, htab_t &types_htab,
6737 rcuh_kind section_kind)
348e048f 6738{
3019eac3 6739 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6740 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6741 bfd *abfd;
6742 const gdb_byte *info_ptr, *end_ptr;
348e048f 6743
4bdcc0c1
DE
6744 abbrev_section = (dwo_file != NULL
6745 ? &dwo_file->sections.abbrev
6746 : &dwarf2_per_objfile->abbrev);
6747
b4f54984 6748 if (dwarf_read_debug)
43988095
JK
6749 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6750 get_section_name (section),
a32a8923 6751 get_section_file_name (abbrev_section));
09406207 6752
78d4d2c5
JK
6753 dwarf2_read_section (objfile, section);
6754 info_ptr = section->buffer;
348e048f 6755
78d4d2c5
JK
6756 if (info_ptr == NULL)
6757 return;
348e048f 6758
78d4d2c5
JK
6759 /* We can't set abfd until now because the section may be empty or
6760 not present, in which case the bfd is unknown. */
6761 abfd = get_section_bfd_owner (section);
348e048f 6762
78d4d2c5
JK
6763 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6764 because we don't need to read any dies: the signature is in the
6765 header. */
3019eac3 6766
78d4d2c5
JK
6767 end_ptr = info_ptr + section->size;
6768 while (info_ptr < end_ptr)
6769 {
78d4d2c5
JK
6770 struct signatured_type *sig_type;
6771 struct dwo_unit *dwo_tu;
6772 void **slot;
6773 const gdb_byte *ptr = info_ptr;
6774 struct comp_unit_head header;
6775 unsigned int length;
8b70b953 6776
9c541725 6777 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6778
a49dd8dd
JK
6779 /* Initialize it due to a false compiler warning. */
6780 header.signature = -1;
9c541725 6781 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6782
78d4d2c5
JK
6783 /* We need to read the type's signature in order to build the hash
6784 table, but we don't need anything else just yet. */
348e048f 6785
ed2dc618 6786 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6787 abbrev_section, ptr, section_kind);
348e048f 6788
78d4d2c5 6789 length = get_cu_length (&header);
6caca83c 6790
78d4d2c5
JK
6791 /* Skip dummy type units. */
6792 if (ptr >= info_ptr + length
43988095
JK
6793 || peek_abbrev_code (abfd, ptr) == 0
6794 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6795 {
6796 info_ptr += length;
6797 continue;
6798 }
dee91e82 6799
78d4d2c5
JK
6800 if (types_htab == NULL)
6801 {
6802 if (dwo_file)
6803 types_htab = allocate_dwo_unit_table (objfile);
6804 else
6805 types_htab = allocate_signatured_type_table (objfile);
6806 }
8b70b953 6807
78d4d2c5
JK
6808 if (dwo_file)
6809 {
6810 sig_type = NULL;
6811 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6812 struct dwo_unit);
6813 dwo_tu->dwo_file = dwo_file;
43988095 6814 dwo_tu->signature = header.signature;
9c541725 6815 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6816 dwo_tu->section = section;
9c541725 6817 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6818 dwo_tu->length = length;
6819 }
6820 else
6821 {
6822 /* N.B.: type_offset is not usable if this type uses a DWO file.
6823 The real type_offset is in the DWO file. */
6824 dwo_tu = NULL;
6825 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6826 struct signatured_type);
43988095 6827 sig_type->signature = header.signature;
9c541725 6828 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 6829 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
6830 sig_type->per_cu.is_debug_types = 1;
6831 sig_type->per_cu.section = section;
9c541725 6832 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6833 sig_type->per_cu.length = length;
6834 }
6835
6836 slot = htab_find_slot (types_htab,
6837 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6838 INSERT);
6839 gdb_assert (slot != NULL);
6840 if (*slot != NULL)
6841 {
9c541725 6842 sect_offset dup_sect_off;
0349ea22 6843
3019eac3
DE
6844 if (dwo_file)
6845 {
78d4d2c5
JK
6846 const struct dwo_unit *dup_tu
6847 = (const struct dwo_unit *) *slot;
6848
9c541725 6849 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6850 }
6851 else
6852 {
78d4d2c5
JK
6853 const struct signatured_type *dup_tu
6854 = (const struct signatured_type *) *slot;
6855
9c541725 6856 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6857 }
8b70b953 6858
b98664d3 6859 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6860 " the entry at offset %s, signature %s"),
6861 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6862 hex_string (header.signature));
78d4d2c5
JK
6863 }
6864 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6865
78d4d2c5 6866 if (dwarf_read_debug > 1)
9d8780f0
SM
6867 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6868 sect_offset_str (sect_off),
43988095 6869 hex_string (header.signature));
3019eac3 6870
78d4d2c5
JK
6871 info_ptr += length;
6872 }
6873}
3019eac3 6874
78d4d2c5
JK
6875/* Create the hash table of all entries in the .debug_types
6876 (or .debug_types.dwo) section(s).
6877 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6878 otherwise it is NULL.
b3c8eb43 6879
78d4d2c5 6880 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6881
78d4d2c5 6882 Note: This function processes DWO files only, not DWP files. */
348e048f 6883
78d4d2c5 6884static void
ed2dc618
SM
6885create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6886 struct dwo_file *dwo_file,
fd5866f6 6887 gdb::array_view<dwarf2_section_info> type_sections,
78d4d2c5
JK
6888 htab_t &types_htab)
6889{
fd5866f6
SM
6890 for (dwarf2_section_info &section : type_sections)
6891 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
ed2dc618 6892 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6893}
6894
6895/* Create the hash table of all entries in the .debug_types section,
6896 and initialize all_type_units.
6897 The result is zero if there is an error (e.g. missing .debug_types section),
6898 otherwise non-zero. */
6899
6900static int
ed2dc618 6901create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6902{
78d4d2c5 6903 htab_t types_htab = NULL;
3019eac3 6904
ed2dc618
SM
6905 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6906 &dwarf2_per_objfile->info, types_htab,
43988095 6907 rcuh_kind::COMPILE);
ed2dc618
SM
6908 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6909 dwarf2_per_objfile->types, types_htab);
3019eac3
DE
6910 if (types_htab == NULL)
6911 {
6912 dwarf2_per_objfile->signatured_types = NULL;
6913 return 0;
6914 }
6915
348e048f
DE
6916 dwarf2_per_objfile->signatured_types = types_htab;
6917
b2bdb8cf
SM
6918 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6919 dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
6920
6921 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
6922 &dwarf2_per_objfile->all_type_units);
1fd400ff 6923
348e048f
DE
6924 return 1;
6925}
6926
6aa5f3a6
DE
6927/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6928 If SLOT is non-NULL, it is the entry to use in the hash table.
6929 Otherwise we find one. */
6930
6931static struct signatured_type *
ed2dc618
SM
6932add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6933 void **slot)
6aa5f3a6
DE
6934{
6935 struct objfile *objfile = dwarf2_per_objfile->objfile;
6aa5f3a6 6936
b2bdb8cf
SM
6937 if (dwarf2_per_objfile->all_type_units.size ()
6938 == dwarf2_per_objfile->all_type_units.capacity ())
6939 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6940
b2bdb8cf
SM
6941 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6942 struct signatured_type);
6943
6944 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6945 sig_type->signature = sig;
6946 sig_type->per_cu.is_debug_types = 1;
6947 if (dwarf2_per_objfile->using_index)
6948 {
6949 sig_type->per_cu.v.quick =
6950 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6951 struct dwarf2_per_cu_quick_data);
6952 }
6953
6954 if (slot == NULL)
6955 {
6956 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6957 sig_type, INSERT);
6958 }
6959 gdb_assert (*slot == NULL);
6960 *slot = sig_type;
6961 /* The rest of sig_type must be filled in by the caller. */
6962 return sig_type;
6963}
6964
a2ce51a0
DE
6965/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6966 Fill in SIG_ENTRY with DWO_ENTRY. */
6967
6968static void
ed2dc618 6969fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
6970 struct signatured_type *sig_entry,
6971 struct dwo_unit *dwo_entry)
6972{
7ee85ab1 6973 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
6974 gdb_assert (! sig_entry->per_cu.queued);
6975 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
6976 if (dwarf2_per_objfile->using_index)
6977 {
6978 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 6979 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
6980 }
6981 else
6982 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6983 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6984 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6985 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6986 gdb_assert (sig_entry->dwo_unit == NULL);
6987
6988 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6989 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6990 sig_entry->per_cu.length = dwo_entry->length;
6991 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 6992 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
a2ce51a0
DE
6993 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6994 sig_entry->dwo_unit = dwo_entry;
6995}
6996
6997/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6998 If we haven't read the TU yet, create the signatured_type data structure
6999 for a TU to be read in directly from a DWO file, bypassing the stub.
7000 This is the "Stay in DWO Optimization": When there is no DWP file and we're
7001 using .gdb_index, then when reading a CU we want to stay in the DWO file
7002 containing that CU. Otherwise we could end up reading several other DWO
7003 files (due to comdat folding) to process the transitive closure of all the
7004 mentioned TUs, and that can be slow. The current DWO file will have every
7005 type signature that it needs.
a2ce51a0
DE
7006 We only do this for .gdb_index because in the psymtab case we already have
7007 to read all the DWOs to build the type unit groups. */
7008
7009static struct signatured_type *
7010lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7011{
518817b3
SM
7012 struct dwarf2_per_objfile *dwarf2_per_objfile
7013 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0
DE
7014 struct objfile *objfile = dwarf2_per_objfile->objfile;
7015 struct dwo_file *dwo_file;
7016 struct dwo_unit find_dwo_entry, *dwo_entry;
7017 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 7018 void **slot;
a2ce51a0
DE
7019
7020 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7021
6aa5f3a6
DE
7022 /* If TU skeletons have been removed then we may not have read in any
7023 TUs yet. */
7024 if (dwarf2_per_objfile->signatured_types == NULL)
7025 {
7026 dwarf2_per_objfile->signatured_types
7027 = allocate_signatured_type_table (objfile);
7028 }
a2ce51a0
DE
7029
7030 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
7031 Use the global signatured_types array to do our own comdat-folding
7032 of types. If this is the first time we're reading this TU, and
7033 the TU has an entry in .gdb_index, replace the recorded data from
7034 .gdb_index with this TU. */
a2ce51a0 7035
a2ce51a0 7036 find_sig_entry.signature = sig;
6aa5f3a6
DE
7037 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7038 &find_sig_entry, INSERT);
9a3c8263 7039 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
7040
7041 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
7042 read. Don't reassign the global entry to point to this DWO if that's
7043 the case. Also note that if the TU is already being read, it may not
7044 have come from a DWO, the program may be a mix of Fission-compiled
7045 code and non-Fission-compiled code. */
7046
7047 /* Have we already tried to read this TU?
7048 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7049 needn't exist in the global table yet). */
7050 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
7051 return sig_entry;
7052
6aa5f3a6
DE
7053 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
7054 dwo_unit of the TU itself. */
7055 dwo_file = cu->dwo_unit->dwo_file;
7056
a2ce51a0
DE
7057 /* Ok, this is the first time we're reading this TU. */
7058 if (dwo_file->tus == NULL)
7059 return NULL;
7060 find_dwo_entry.signature = sig;
9a3c8263 7061 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
a2ce51a0
DE
7062 if (dwo_entry == NULL)
7063 return NULL;
7064
6aa5f3a6
DE
7065 /* If the global table doesn't have an entry for this TU, add one. */
7066 if (sig_entry == NULL)
ed2dc618 7067 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 7068
ed2dc618 7069 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 7070 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
7071 return sig_entry;
7072}
7073
a2ce51a0
DE
7074/* Subroutine of lookup_signatured_type.
7075 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
7076 then try the DWP file. If the TU stub (skeleton) has been removed then
7077 it won't be in .gdb_index. */
a2ce51a0
DE
7078
7079static struct signatured_type *
7080lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7081{
518817b3
SM
7082 struct dwarf2_per_objfile *dwarf2_per_objfile
7083 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0 7084 struct objfile *objfile = dwarf2_per_objfile->objfile;
ed2dc618 7085 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
7086 struct dwo_unit *dwo_entry;
7087 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 7088 void **slot;
a2ce51a0
DE
7089
7090 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7091 gdb_assert (dwp_file != NULL);
7092
6aa5f3a6
DE
7093 /* If TU skeletons have been removed then we may not have read in any
7094 TUs yet. */
7095 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 7096 {
6aa5f3a6
DE
7097 dwarf2_per_objfile->signatured_types
7098 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
7099 }
7100
6aa5f3a6
DE
7101 find_sig_entry.signature = sig;
7102 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7103 &find_sig_entry, INSERT);
9a3c8263 7104 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
7105
7106 /* Have we already tried to read this TU?
7107 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7108 needn't exist in the global table yet). */
7109 if (sig_entry != NULL)
7110 return sig_entry;
7111
a2ce51a0
DE
7112 if (dwp_file->tus == NULL)
7113 return NULL;
ed2dc618 7114 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 7115 sig, 1 /* is_debug_types */);
a2ce51a0
DE
7116 if (dwo_entry == NULL)
7117 return NULL;
7118
ed2dc618
SM
7119 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7120 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 7121
a2ce51a0
DE
7122 return sig_entry;
7123}
7124
380bca97 7125/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
7126 Returns NULL if signature SIG is not present in the table.
7127 It is up to the caller to complain about this. */
348e048f
DE
7128
7129static struct signatured_type *
a2ce51a0 7130lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 7131{
518817b3
SM
7132 struct dwarf2_per_objfile *dwarf2_per_objfile
7133 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 7134
a2ce51a0
DE
7135 if (cu->dwo_unit
7136 && dwarf2_per_objfile->using_index)
7137 {
7138 /* We're in a DWO/DWP file, and we're using .gdb_index.
7139 These cases require special processing. */
ed2dc618 7140 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
7141 return lookup_dwo_signatured_type (cu, sig);
7142 else
7143 return lookup_dwp_signatured_type (cu, sig);
7144 }
7145 else
7146 {
7147 struct signatured_type find_entry, *entry;
348e048f 7148
a2ce51a0
DE
7149 if (dwarf2_per_objfile->signatured_types == NULL)
7150 return NULL;
7151 find_entry.signature = sig;
9a3c8263
SM
7152 entry = ((struct signatured_type *)
7153 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
a2ce51a0
DE
7154 return entry;
7155 }
348e048f 7156}
42e7ad6c
DE
7157\f
7158/* Low level DIE reading support. */
348e048f 7159
d85a05f0
DJ
7160/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7161
7162static void
7163init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 7164 struct dwarf2_cu *cu,
3019eac3 7165 struct dwarf2_section_info *section,
685af9cd
TT
7166 struct dwo_file *dwo_file,
7167 struct abbrev_table *abbrev_table)
d85a05f0 7168{
fceca515 7169 gdb_assert (section->readin && section->buffer != NULL);
a32a8923 7170 reader->abfd = get_section_bfd_owner (section);
d85a05f0 7171 reader->cu = cu;
3019eac3 7172 reader->dwo_file = dwo_file;
dee91e82
DE
7173 reader->die_section = section;
7174 reader->buffer = section->buffer;
f664829e 7175 reader->buffer_end = section->buffer + section->size;
a2ce51a0 7176 reader->comp_dir = NULL;
685af9cd 7177 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
7178}
7179
b0c7bfa9
DE
7180/* Subroutine of init_cutu_and_read_dies to simplify it.
7181 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7182 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7183 already.
7184
7185 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7186 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
7187 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7188 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
7189 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7190 STUB_COMP_DIR may be non-NULL.
b0c7bfa9
DE
7191 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7192 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
7193 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7194 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7195 kept around for at least as long as *RESULT_READER.
7196
b0c7bfa9
DE
7197 The result is non-zero if a valid (non-dummy) DIE was found. */
7198
7199static int
7200read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7201 struct dwo_unit *dwo_unit,
b0c7bfa9 7202 struct die_info *stub_comp_unit_die,
a2ce51a0 7203 const char *stub_comp_dir,
b0c7bfa9 7204 struct die_reader_specs *result_reader,
d521ce57 7205 const gdb_byte **result_info_ptr,
b0c7bfa9 7206 struct die_info **result_comp_unit_die,
685af9cd
TT
7207 int *result_has_children,
7208 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 7209{
ed2dc618 7210 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
b0c7bfa9
DE
7211 struct objfile *objfile = dwarf2_per_objfile->objfile;
7212 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 7213 bfd *abfd;
d521ce57 7214 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
7215 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7216 int i,num_extra_attrs;
7217 struct dwarf2_section_info *dwo_abbrev_section;
7218 struct attribute *attr;
7219 struct die_info *comp_unit_die;
7220
b0aeadb3
DE
7221 /* At most one of these may be provided. */
7222 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 7223
b0c7bfa9
DE
7224 /* These attributes aren't processed until later:
7225 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
7226 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7227 referenced later. However, these attributes are found in the stub
7228 which we won't have later. In order to not impose this complication
7229 on the rest of the code, we read them here and copy them to the
7230 DWO CU/TU die. */
b0c7bfa9
DE
7231
7232 stmt_list = NULL;
7233 low_pc = NULL;
7234 high_pc = NULL;
7235 ranges = NULL;
7236 comp_dir = NULL;
7237
7238 if (stub_comp_unit_die != NULL)
7239 {
7240 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7241 DWO file. */
7242 if (! this_cu->is_debug_types)
7243 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7244 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7245 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7246 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7247 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7248
7249 /* There should be a DW_AT_addr_base attribute here (if needed).
336d760d
AT
7250 We need the value before we can process DW_FORM_GNU_addr_index
7251 or DW_FORM_addrx. */
b0c7bfa9
DE
7252 cu->addr_base = 0;
7253 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7254 if (attr)
7255 cu->addr_base = DW_UNSND (attr);
7256
7257 /* There should be a DW_AT_ranges_base attribute here (if needed).
7258 We need the value before we can process DW_AT_ranges. */
7259 cu->ranges_base = 0;
7260 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7261 if (attr)
7262 cu->ranges_base = DW_UNSND (attr);
7263 }
a2ce51a0
DE
7264 else if (stub_comp_dir != NULL)
7265 {
7266 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 7267 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
7268 comp_dir->name = DW_AT_comp_dir;
7269 comp_dir->form = DW_FORM_string;
7270 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7271 DW_STRING (comp_dir) = stub_comp_dir;
7272 }
b0c7bfa9
DE
7273
7274 /* Set up for reading the DWO CU/TU. */
7275 cu->dwo_unit = dwo_unit;
685af9cd 7276 dwarf2_section_info *section = dwo_unit->section;
b0c7bfa9 7277 dwarf2_read_section (objfile, section);
a32a8923 7278 abfd = get_section_bfd_owner (section);
9c541725
PA
7279 begin_info_ptr = info_ptr = (section->buffer
7280 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 7281 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9
DE
7282
7283 if (this_cu->is_debug_types)
7284 {
b0c7bfa9
DE
7285 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7286
ed2dc618
SM
7287 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7288 &cu->header, section,
b0c7bfa9 7289 dwo_abbrev_section,
43988095 7290 info_ptr, rcuh_kind::TYPE);
a2ce51a0 7291 /* This is not an assert because it can be caused by bad debug info. */
43988095 7292 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
7293 {
7294 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 7295 " TU at offset %s [in module %s]"),
a2ce51a0 7296 hex_string (sig_type->signature),
43988095 7297 hex_string (cu->header.signature),
9d8780f0 7298 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
7299 bfd_get_filename (abfd));
7300 }
9c541725 7301 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7302 /* For DWOs coming from DWP files, we don't know the CU length
7303 nor the type's offset in the TU until now. */
7304 dwo_unit->length = get_cu_length (&cu->header);
9c541725 7305 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
7306
7307 /* Establish the type offset that can be used to lookup the type.
7308 For DWO files, we don't know it until now. */
9c541725
PA
7309 sig_type->type_offset_in_section
7310 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
7311 }
7312 else
7313 {
ed2dc618
SM
7314 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7315 &cu->header, section,
b0c7bfa9 7316 dwo_abbrev_section,
43988095 7317 info_ptr, rcuh_kind::COMPILE);
9c541725 7318 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7319 /* For DWOs coming from DWP files, we don't know the CU length
7320 until now. */
7321 dwo_unit->length = get_cu_length (&cu->header);
7322 }
7323
685af9cd
TT
7324 *result_dwo_abbrev_table
7325 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7326 cu->header.abbrev_sect_off);
7327 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7328 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
7329
7330 /* Read in the die, but leave space to copy over the attributes
7331 from the stub. This has the benefit of simplifying the rest of
7332 the code - all the work to maintain the illusion of a single
7333 DW_TAG_{compile,type}_unit DIE is done here. */
7334 num_extra_attrs = ((stmt_list != NULL)
7335 + (low_pc != NULL)
7336 + (high_pc != NULL)
7337 + (ranges != NULL)
7338 + (comp_dir != NULL));
7339 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7340 result_has_children, num_extra_attrs);
7341
7342 /* Copy over the attributes from the stub to the DIE we just read in. */
7343 comp_unit_die = *result_comp_unit_die;
7344 i = comp_unit_die->num_attrs;
7345 if (stmt_list != NULL)
7346 comp_unit_die->attrs[i++] = *stmt_list;
7347 if (low_pc != NULL)
7348 comp_unit_die->attrs[i++] = *low_pc;
7349 if (high_pc != NULL)
7350 comp_unit_die->attrs[i++] = *high_pc;
7351 if (ranges != NULL)
7352 comp_unit_die->attrs[i++] = *ranges;
7353 if (comp_dir != NULL)
7354 comp_unit_die->attrs[i++] = *comp_dir;
7355 comp_unit_die->num_attrs += num_extra_attrs;
7356
b4f54984 7357 if (dwarf_die_debug)
bf6af496
DE
7358 {
7359 fprintf_unfiltered (gdb_stdlog,
7360 "Read die from %s@0x%x of %s:\n",
a32a8923 7361 get_section_name (section),
bf6af496
DE
7362 (unsigned) (begin_info_ptr - section->buffer),
7363 bfd_get_filename (abfd));
b4f54984 7364 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
7365 }
7366
a2ce51a0
DE
7367 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7368 TUs by skipping the stub and going directly to the entry in the DWO file.
7369 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7370 to get it via circuitous means. Blech. */
7371 if (comp_dir != NULL)
7372 result_reader->comp_dir = DW_STRING (comp_dir);
7373
b0c7bfa9
DE
7374 /* Skip dummy compilation units. */
7375 if (info_ptr >= begin_info_ptr + dwo_unit->length
7376 || peek_abbrev_code (abfd, info_ptr) == 0)
7377 return 0;
7378
7379 *result_info_ptr = info_ptr;
7380 return 1;
7381}
7382
a084a2a6
AT
7383/* Return the signature of the compile unit, if found. In DWARF 4 and before,
7384 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7385 signature is part of the header. */
7386static gdb::optional<ULONGEST>
7387lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7388{
7389 if (cu->header.version >= 5)
7390 return cu->header.signature;
7391 struct attribute *attr;
7392 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7393 if (attr == nullptr)
7394 return gdb::optional<ULONGEST> ();
7395 return DW_UNSND (attr);
7396}
7397
b0c7bfa9
DE
7398/* Subroutine of init_cutu_and_read_dies to simplify it.
7399 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 7400 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
7401
7402static struct dwo_unit *
7403lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7404 struct die_info *comp_unit_die)
7405{
7406 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9
DE
7407 struct dwo_unit *dwo_unit;
7408 const char *comp_dir, *dwo_name;
7409
a2ce51a0
DE
7410 gdb_assert (cu != NULL);
7411
b0c7bfa9 7412 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 7413 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 7414 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
7415
7416 if (this_cu->is_debug_types)
7417 {
7418 struct signatured_type *sig_type;
7419
7420 /* Since this_cu is the first member of struct signatured_type,
7421 we can go from a pointer to one to a pointer to the other. */
7422 sig_type = (struct signatured_type *) this_cu;
b0c7bfa9
DE
7423 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7424 }
7425 else
7426 {
a084a2a6
AT
7427 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7428 if (!signature.has_value ())
b0c7bfa9
DE
7429 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7430 " [in module %s]"),
e3b94546 7431 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
b0c7bfa9 7432 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
a084a2a6 7433 *signature);
b0c7bfa9
DE
7434 }
7435
b0c7bfa9
DE
7436 return dwo_unit;
7437}
7438
a2ce51a0 7439/* Subroutine of init_cutu_and_read_dies to simplify it.
6aa5f3a6 7440 See it for a description of the parameters.
fcd3b13d 7441 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0
DE
7442
7443static void
6aa5f3a6
DE
7444init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7445 int use_existing_cu, int keep,
a2ce51a0
DE
7446 die_reader_func_ftype *die_reader_func,
7447 void *data)
7448{
fcd3b13d 7449 std::unique_ptr<dwarf2_cu> new_cu;
a2ce51a0 7450 struct signatured_type *sig_type;
a2ce51a0
DE
7451 struct die_reader_specs reader;
7452 const gdb_byte *info_ptr;
7453 struct die_info *comp_unit_die;
7454 int has_children;
ed2dc618 7455 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
a2ce51a0
DE
7456
7457 /* Verify we can do the following downcast, and that we have the
7458 data we need. */
7459 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7460 sig_type = (struct signatured_type *) this_cu;
7461 gdb_assert (sig_type->dwo_unit != NULL);
7462
6aa5f3a6
DE
7463 if (use_existing_cu && this_cu->cu != NULL)
7464 {
7465 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6
DE
7466 /* There's no need to do the rereading_dwo_cu handling that
7467 init_cutu_and_read_dies does since we don't read the stub. */
7468 }
7469 else
7470 {
7471 /* If !use_existing_cu, this_cu->cu must be NULL. */
7472 gdb_assert (this_cu->cu == NULL);
fcd3b13d 7473 new_cu.reset (new dwarf2_cu (this_cu));
6aa5f3a6
DE
7474 }
7475
7476 /* A future optimization, if needed, would be to use an existing
7477 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7478 could share abbrev tables. */
a2ce51a0 7479
685af9cd
TT
7480 /* The abbreviation table used by READER, this must live at least as long as
7481 READER. */
7482 abbrev_table_up dwo_abbrev_table;
7483
a2ce51a0 7484 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
a2ce51a0
DE
7485 NULL /* stub_comp_unit_die */,
7486 sig_type->dwo_unit->dwo_file->comp_dir,
7487 &reader, &info_ptr,
685af9cd
TT
7488 &comp_unit_die, &has_children,
7489 &dwo_abbrev_table) == 0)
a2ce51a0
DE
7490 {
7491 /* Dummy die. */
a2ce51a0
DE
7492 return;
7493 }
7494
7495 /* All the "real" work is done here. */
7496 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7497
6aa5f3a6 7498 /* This duplicates the code in init_cutu_and_read_dies,
a2ce51a0
DE
7499 but the alternative is making the latter more complex.
7500 This function is only for the special case of using DWO files directly:
7501 no point in overly complicating the general case just to handle this. */
fcd3b13d 7502 if (new_cu != NULL && keep)
a2ce51a0 7503 {
fcd3b13d
SM
7504 /* Link this CU into read_in_chain. */
7505 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7506 dwarf2_per_objfile->read_in_chain = this_cu;
7507 /* The chain owns it now. */
7508 new_cu.release ();
a2ce51a0 7509 }
a2ce51a0
DE
7510}
7511
fd820528 7512/* Initialize a CU (or TU) and read its DIEs.
3019eac3 7513 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 7514
f4dc4d17
DE
7515 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7516 Otherwise the table specified in the comp unit header is read in and used.
7517 This is an optimization for when we already have the abbrev table.
7518
dee91e82
DE
7519 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7520 Otherwise, a new CU is allocated with xmalloc.
7521
7522 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7523 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7524
7525 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 7526 linker) then DIE_READER_FUNC will not get called. */
aaa75496 7527
70221824 7528static void
fd820528 7529init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 7530 struct abbrev_table *abbrev_table,
fd820528 7531 int use_existing_cu, int keep,
58f0c718 7532 bool skip_partial,
fd820528
DE
7533 die_reader_func_ftype *die_reader_func,
7534 void *data)
c906108c 7535{
ed2dc618 7536 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7537 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7538 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7539 bfd *abfd = get_section_bfd_owner (section);
dee91e82 7540 struct dwarf2_cu *cu;
d521ce57 7541 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7542 struct die_reader_specs reader;
d85a05f0 7543 struct die_info *comp_unit_die;
dee91e82 7544 int has_children;
dee91e82 7545 struct signatured_type *sig_type = NULL;
4bdcc0c1 7546 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7547 /* Non-zero if CU currently points to a DWO file and we need to
7548 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7549 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7550 int rereading_dwo_cu = 0;
c906108c 7551
b4f54984 7552 if (dwarf_die_debug)
9d8780f0 7553 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7554 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7555 sect_offset_str (this_cu->sect_off));
09406207 7556
dee91e82
DE
7557 if (use_existing_cu)
7558 gdb_assert (keep);
23745b47 7559
a2ce51a0
DE
7560 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7561 file (instead of going through the stub), short-circuit all of this. */
7562 if (this_cu->reading_dwo_directly)
7563 {
7564 /* Narrow down the scope of possibilities to have to understand. */
7565 gdb_assert (this_cu->is_debug_types);
7566 gdb_assert (abbrev_table == NULL);
6aa5f3a6
DE
7567 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7568 die_reader_func, data);
a2ce51a0
DE
7569 return;
7570 }
7571
dee91e82
DE
7572 /* This is cheap if the section is already read in. */
7573 dwarf2_read_section (objfile, section);
7574
9c541725 7575 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7576
7577 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 7578
fcd3b13d 7579 std::unique_ptr<dwarf2_cu> new_cu;
dee91e82
DE
7580 if (use_existing_cu && this_cu->cu != NULL)
7581 {
7582 cu = this_cu->cu;
42e7ad6c
DE
7583 /* If this CU is from a DWO file we need to start over, we need to
7584 refetch the attributes from the skeleton CU.
7585 This could be optimized by retrieving those attributes from when we
7586 were here the first time: the previous comp_unit_die was stored in
7587 comp_unit_obstack. But there's no data yet that we need this
7588 optimization. */
7589 if (cu->dwo_unit != NULL)
7590 rereading_dwo_cu = 1;
dee91e82
DE
7591 }
7592 else
7593 {
7594 /* If !use_existing_cu, this_cu->cu must be NULL. */
7595 gdb_assert (this_cu->cu == NULL);
fcd3b13d
SM
7596 new_cu.reset (new dwarf2_cu (this_cu));
7597 cu = new_cu.get ();
42e7ad6c 7598 }
dee91e82 7599
b0c7bfa9 7600 /* Get the header. */
9c541725 7601 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7602 {
7603 /* We already have the header, there's no need to read it in again. */
9c541725 7604 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7605 }
7606 else
7607 {
3019eac3 7608 if (this_cu->is_debug_types)
dee91e82 7609 {
ed2dc618
SM
7610 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7611 &cu->header, section,
4bdcc0c1 7612 abbrev_section, info_ptr,
43988095 7613 rcuh_kind::TYPE);
dee91e82 7614
42e7ad6c
DE
7615 /* Since per_cu is the first member of struct signatured_type,
7616 we can go from a pointer to one to a pointer to the other. */
7617 sig_type = (struct signatured_type *) this_cu;
43988095 7618 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7619 gdb_assert (sig_type->type_offset_in_tu
7620 == cu->header.type_cu_offset_in_tu);
7621 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7622
42e7ad6c
DE
7623 /* LENGTH has not been set yet for type units if we're
7624 using .gdb_index. */
1ce1cefd 7625 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
7626
7627 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7628 sig_type->type_offset_in_section =
7629 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7630
7631 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7632 }
7633 else
7634 {
ed2dc618
SM
7635 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7636 &cu->header, section,
4bdcc0c1 7637 abbrev_section,
43988095
JK
7638 info_ptr,
7639 rcuh_kind::COMPILE);
dee91e82 7640
9c541725 7641 gdb_assert (this_cu->sect_off == cu->header.sect_off);
1ce1cefd 7642 gdb_assert (this_cu->length == get_cu_length (&cu->header));
43988095 7643 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7644 }
7645 }
10b3939b 7646
6caca83c 7647 /* Skip dummy compilation units. */
dee91e82 7648 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7649 || peek_abbrev_code (abfd, info_ptr) == 0)
fcd3b13d 7650 return;
6caca83c 7651
433df2d4
DE
7652 /* If we don't have them yet, read the abbrevs for this compilation unit.
7653 And if we need to read them now, make sure they're freed when we're
685af9cd
TT
7654 done (own the table through ABBREV_TABLE_HOLDER). */
7655 abbrev_table_up abbrev_table_holder;
f4dc4d17 7656 if (abbrev_table != NULL)
685af9cd
TT
7657 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7658 else
f4dc4d17 7659 {
685af9cd
TT
7660 abbrev_table_holder
7661 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7662 cu->header.abbrev_sect_off);
7663 abbrev_table = abbrev_table_holder.get ();
42e7ad6c 7664 }
af703f96 7665
dee91e82 7666 /* Read the top level CU/TU die. */
685af9cd 7667 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
dee91e82 7668 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 7669
58f0c718
TT
7670 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7671 return;
7672
b0c7bfa9 7673 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7674 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7675 table from the DWO file and pass the ownership over to us. It will be
7676 referenced from READER, so we must make sure to free it after we're done
7677 with READER.
7678
b0c7bfa9
DE
7679 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7680 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 7681 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
685af9cd 7682 abbrev_table_up dwo_abbrev_table;
a084a2a6 7683 if (dwo_name != nullptr)
3019eac3 7684 {
3019eac3 7685 struct dwo_unit *dwo_unit;
b0c7bfa9 7686 struct die_info *dwo_comp_unit_die;
3019eac3
DE
7687
7688 if (has_children)
6a506a2d 7689 {
b98664d3 7690 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7691 " has children (offset %s) [in module %s]"),
7692 sect_offset_str (this_cu->sect_off),
7693 bfd_get_filename (abfd));
6a506a2d 7694 }
b0c7bfa9 7695 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 7696 if (dwo_unit != NULL)
3019eac3 7697 {
6a506a2d 7698 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
a2ce51a0 7699 comp_unit_die, NULL,
6a506a2d 7700 &reader, &info_ptr,
685af9cd
TT
7701 &dwo_comp_unit_die, &has_children,
7702 &dwo_abbrev_table) == 0)
6a506a2d
DE
7703 {
7704 /* Dummy die. */
6a506a2d
DE
7705 return;
7706 }
7707 comp_unit_die = dwo_comp_unit_die;
7708 }
7709 else
7710 {
7711 /* Yikes, we couldn't find the rest of the DIE, we only have
7712 the stub. A complaint has already been logged. There's
7713 not much more we can do except pass on the stub DIE to
7714 die_reader_func. We don't want to throw an error on bad
7715 debug info. */
3019eac3
DE
7716 }
7717 }
7718
b0c7bfa9 7719 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
7720 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7721
b0c7bfa9 7722 /* Done, clean up. */
fcd3b13d 7723 if (new_cu != NULL && keep)
348e048f 7724 {
fcd3b13d
SM
7725 /* Link this CU into read_in_chain. */
7726 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7727 dwarf2_per_objfile->read_in_chain = this_cu;
7728 /* The chain owns it now. */
7729 new_cu.release ();
348e048f 7730 }
dee91e82
DE
7731}
7732
33e80786
DE
7733/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7734 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7735 to have already done the lookup to find the DWO file).
dee91e82
DE
7736
7737 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7738 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7739
7740 We fill in THIS_CU->length.
7741
7742 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7743 linker) then DIE_READER_FUNC will not get called.
7744
7745 THIS_CU->cu is always freed when done.
3019eac3
DE
7746 This is done in order to not leave THIS_CU->cu in a state where we have
7747 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
7748
7749static void
7750init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3019eac3 7751 struct dwo_file *dwo_file,
dee91e82
DE
7752 die_reader_func_ftype *die_reader_func,
7753 void *data)
7754{
ed2dc618 7755 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7756 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7757 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7758 bfd *abfd = get_section_bfd_owner (section);
33e80786 7759 struct dwarf2_section_info *abbrev_section;
d521ce57 7760 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7761 struct die_reader_specs reader;
dee91e82
DE
7762 struct die_info *comp_unit_die;
7763 int has_children;
7764
b4f54984 7765 if (dwarf_die_debug)
9d8780f0 7766 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7767 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7768 sect_offset_str (this_cu->sect_off));
09406207 7769
dee91e82
DE
7770 gdb_assert (this_cu->cu == NULL);
7771
33e80786
DE
7772 abbrev_section = (dwo_file != NULL
7773 ? &dwo_file->sections.abbrev
7774 : get_abbrev_section_for_cu (this_cu));
7775
dee91e82
DE
7776 /* This is cheap if the section is already read in. */
7777 dwarf2_read_section (objfile, section);
7778
fcd3b13d 7779 struct dwarf2_cu cu (this_cu);
dee91e82 7780
9c541725 7781 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618
SM
7782 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7783 &cu.header, section,
4bdcc0c1 7784 abbrev_section, info_ptr,
43988095
JK
7785 (this_cu->is_debug_types
7786 ? rcuh_kind::TYPE
7787 : rcuh_kind::COMPILE));
dee91e82 7788
1ce1cefd 7789 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
7790
7791 /* Skip dummy compilation units. */
7792 if (info_ptr >= begin_info_ptr + this_cu->length
7793 || peek_abbrev_code (abfd, info_ptr) == 0)
fcd3b13d 7794 return;
72bf9492 7795
685af9cd
TT
7796 abbrev_table_up abbrev_table
7797 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7798 cu.header.abbrev_sect_off);
dee91e82 7799
685af9cd 7800 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
dee91e82
DE
7801 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7802
7803 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
dee91e82
DE
7804}
7805
3019eac3
DE
7806/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
7807 does not lookup the specified DWO file.
7808 This cannot be used to read DWO files.
dee91e82
DE
7809
7810 THIS_CU->cu is always freed when done.
3019eac3
DE
7811 This is done in order to not leave THIS_CU->cu in a state where we have
7812 to care whether it refers to the "main" CU or the DWO CU.
7813 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
7814
7815static void
7816init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
7817 die_reader_func_ftype *die_reader_func,
7818 void *data)
7819{
33e80786 7820 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
dee91e82 7821}
0018ea6f
DE
7822\f
7823/* Type Unit Groups.
dee91e82 7824
0018ea6f
DE
7825 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7826 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7827 so that all types coming from the same compilation (.o file) are grouped
7828 together. A future step could be to put the types in the same symtab as
7829 the CU the types ultimately came from. */
ff013f42 7830
f4dc4d17
DE
7831static hashval_t
7832hash_type_unit_group (const void *item)
7833{
9a3c8263
SM
7834 const struct type_unit_group *tu_group
7835 = (const struct type_unit_group *) item;
f4dc4d17 7836
094b34ac 7837 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7838}
348e048f
DE
7839
7840static int
f4dc4d17 7841eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7842{
9a3c8263
SM
7843 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7844 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7845
094b34ac 7846 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7847}
348e048f 7848
f4dc4d17
DE
7849/* Allocate a hash table for type unit groups. */
7850
7851static htab_t
ed2dc618 7852allocate_type_unit_groups_table (struct objfile *objfile)
f4dc4d17
DE
7853{
7854 return htab_create_alloc_ex (3,
7855 hash_type_unit_group,
7856 eq_type_unit_group,
7857 NULL,
ed2dc618 7858 &objfile->objfile_obstack,
f4dc4d17
DE
7859 hashtab_obstack_allocate,
7860 dummy_obstack_deallocate);
7861}
dee91e82 7862
f4dc4d17
DE
7863/* Type units that don't have DW_AT_stmt_list are grouped into their own
7864 partial symtabs. We combine several TUs per psymtab to not let the size
7865 of any one psymtab grow too big. */
7866#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7867#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7868
094b34ac 7869/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7870 Create the type_unit_group object used to hold one or more TUs. */
7871
7872static struct type_unit_group *
094b34ac 7873create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7874{
518817b3
SM
7875 struct dwarf2_per_objfile *dwarf2_per_objfile
7876 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17 7877 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 7878 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7879 struct type_unit_group *tu_group;
f4dc4d17
DE
7880
7881 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7882 struct type_unit_group);
094b34ac 7883 per_cu = &tu_group->per_cu;
518817b3 7884 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
f4dc4d17 7885
094b34ac
DE
7886 if (dwarf2_per_objfile->using_index)
7887 {
7888 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7889 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7890 }
7891 else
7892 {
9c541725 7893 unsigned int line_offset = to_underlying (line_offset_struct);
094b34ac 7894 struct partial_symtab *pst;
528e1572 7895 std::string name;
094b34ac
DE
7896
7897 /* Give the symtab a useful name for debug purposes. */
7898 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7899 name = string_printf ("<type_units_%d>",
7900 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7901 else
528e1572 7902 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7903
528e1572 7904 pst = create_partial_symtab (per_cu, name.c_str ());
094b34ac 7905 pst->anonymous = 1;
094b34ac 7906 }
f4dc4d17 7907
094b34ac 7908 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7909 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7910
7911 return tu_group;
7912}
7913
094b34ac
DE
7914/* Look up the type_unit_group for type unit CU, and create it if necessary.
7915 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7916
7917static struct type_unit_group *
ff39bb5e 7918get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7919{
518817b3
SM
7920 struct dwarf2_per_objfile *dwarf2_per_objfile
7921 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7922 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7923 struct type_unit_group *tu_group;
7924 void **slot;
7925 unsigned int line_offset;
7926 struct type_unit_group type_unit_group_for_lookup;
7927
7928 if (dwarf2_per_objfile->type_unit_groups == NULL)
7929 {
7930 dwarf2_per_objfile->type_unit_groups =
ed2dc618 7931 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
f4dc4d17
DE
7932 }
7933
7934 /* Do we need to create a new group, or can we use an existing one? */
7935
7936 if (stmt_list)
7937 {
7938 line_offset = DW_UNSND (stmt_list);
7939 ++tu_stats->nr_symtab_sharers;
7940 }
7941 else
7942 {
7943 /* Ugh, no stmt_list. Rare, but we have to handle it.
7944 We can do various things here like create one group per TU or
7945 spread them over multiple groups to split up the expansion work.
7946 To avoid worst case scenarios (too many groups or too large groups)
7947 we, umm, group them in bunches. */
7948 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7949 | (tu_stats->nr_stmt_less_type_units
7950 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7951 ++tu_stats->nr_stmt_less_type_units;
7952 }
7953
094b34ac 7954 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7955 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
f4dc4d17
DE
7956 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7957 &type_unit_group_for_lookup, INSERT);
7958 if (*slot != NULL)
7959 {
9a3c8263 7960 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7961 gdb_assert (tu_group != NULL);
7962 }
7963 else
7964 {
9c541725 7965 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7966 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7967 *slot = tu_group;
7968 ++tu_stats->nr_symtabs;
7969 }
7970
7971 return tu_group;
7972}
0018ea6f
DE
7973\f
7974/* Partial symbol tables. */
7975
7976/* Create a psymtab named NAME and assign it to PER_CU.
7977
7978 The caller must fill in the following details:
7979 dirname, textlow, texthigh. */
7980
7981static struct partial_symtab *
7982create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7983{
e3b94546 7984 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
0018ea6f
DE
7985 struct partial_symtab *pst;
7986
939652a5 7987 pst = start_psymtab_common (objfile, name, 0);
0018ea6f
DE
7988
7989 pst->psymtabs_addrmap_supported = 1;
7990
7991 /* This is the glue that links PST into GDB's symbol API. */
7992 pst->read_symtab_private = per_cu;
7993 pst->read_symtab = dwarf2_read_symtab;
7994 per_cu->v.psymtab = pst;
7995
7996 return pst;
7997}
7998
b93601f3
TT
7999/* The DATA object passed to process_psymtab_comp_unit_reader has this
8000 type. */
8001
8002struct process_psymtab_comp_unit_data
8003{
8004 /* True if we are reading a DW_TAG_partial_unit. */
8005
8006 int want_partial_unit;
8007
8008 /* The "pretend" language that is used if the CU doesn't declare a
8009 language. */
8010
8011 enum language pretend_language;
8012};
8013
0018ea6f
DE
8014/* die_reader_func for process_psymtab_comp_unit. */
8015
8016static void
8017process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8018 const gdb_byte *info_ptr,
0018ea6f
DE
8019 struct die_info *comp_unit_die,
8020 int has_children,
8021 void *data)
8022{
8023 struct dwarf2_cu *cu = reader->cu;
518817b3 8024 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 8025 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 8026 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
8027 CORE_ADDR baseaddr;
8028 CORE_ADDR best_lowpc = 0, best_highpc = 0;
8029 struct partial_symtab *pst;
3a2b436a 8030 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 8031 const char *filename;
9a3c8263
SM
8032 struct process_psymtab_comp_unit_data *info
8033 = (struct process_psymtab_comp_unit_data *) data;
0018ea6f 8034
b93601f3 8035 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
0018ea6f
DE
8036 return;
8037
8038 gdb_assert (! per_cu->is_debug_types);
8039
b93601f3 8040 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
0018ea6f 8041
0018ea6f 8042 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
8043 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
8044 if (filename == NULL)
0018ea6f 8045 filename = "";
0018ea6f
DE
8046
8047 pst = create_partial_symtab (per_cu, filename);
8048
8049 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 8050 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f
DE
8051
8052 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
8053
8054 dwarf2_find_base_address (comp_unit_die, cu);
8055
8056 /* Possibly set the default values of LOWPC and HIGHPC from
8057 `DW_AT_ranges'. */
3a2b436a
JK
8058 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
8059 &best_highpc, cu, pst);
8060 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
8061 {
8062 CORE_ADDR low
8063 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
8064 - baseaddr);
8065 CORE_ADDR high
8066 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
8067 - baseaddr - 1);
8068 /* Store the contiguous range if it is not empty; it can be
8069 empty for CUs with no code. */
d320c2b5
TT
8070 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8071 low, high, pst);
79748972 8072 }
0018ea6f
DE
8073
8074 /* Check if comp unit has_children.
8075 If so, read the rest of the partial symbols from this comp unit.
8076 If not, there's no more debug_info for this comp unit. */
8077 if (has_children)
8078 {
8079 struct partial_die_info *first_die;
8080 CORE_ADDR lowpc, highpc;
8081
8082 lowpc = ((CORE_ADDR) -1);
8083 highpc = ((CORE_ADDR) 0);
8084
8085 first_die = load_partial_dies (reader, info_ptr, 1);
8086
8087 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 8088 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
8089
8090 /* If we didn't find a lowpc, set it to highpc to avoid
8091 complaints from `maint check'. */
8092 if (lowpc == ((CORE_ADDR) -1))
8093 lowpc = highpc;
8094
8095 /* If the compilation unit didn't have an explicit address range,
8096 then use the information extracted from its child dies. */
e385593e 8097 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
8098 {
8099 best_lowpc = lowpc;
8100 best_highpc = highpc;
8101 }
8102 }
4ae976d1 8103 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
8104 best_lowpc + baseaddr)
8105 - baseaddr);
4ae976d1 8106 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
8107 best_highpc + baseaddr)
8108 - baseaddr);
0018ea6f 8109
8763cede 8110 end_psymtab_common (objfile, pst);
0018ea6f 8111
ae640021 8112 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
8113 {
8114 int i;
ae640021 8115 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
8116
8117 /* Fill in 'dependencies' here; we fill in 'users' in a
8118 post-pass. */
8119 pst->number_of_dependencies = len;
a9342b62
TT
8120 pst->dependencies
8121 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
8122 for (i = 0; i < len; ++i)
8123 {
8124 pst->dependencies[i]
8125 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
8126 }
0018ea6f 8127
ae640021 8128 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
8129 }
8130
8131 /* Get the list of files included in the current compilation unit,
8132 and build a psymtab for each of them. */
8133 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8134
b4f54984 8135 if (dwarf_read_debug)
b926417a
TT
8136 fprintf_unfiltered (gdb_stdlog,
8137 "Psymtab for %s unit @%s: %s - %s"
8138 ", %d global, %d static syms\n",
8139 per_cu->is_debug_types ? "type" : "comp",
8140 sect_offset_str (per_cu->sect_off),
8141 paddress (gdbarch, pst->text_low (objfile)),
8142 paddress (gdbarch, pst->text_high (objfile)),
8143 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
8144}
8145
8146/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8147 Process compilation unit THIS_CU for a psymtab. */
8148
8149static void
8150process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
8151 int want_partial_unit,
8152 enum language pretend_language)
0018ea6f
DE
8153{
8154 /* If this compilation unit was already read in, free the
8155 cached copy in order to read it in again. This is
8156 necessary because we skipped some symbols when we first
8157 read in the compilation unit (see load_partial_dies).
8158 This problem could be avoided, but the benefit is unclear. */
8159 if (this_cu->cu != NULL)
8160 free_one_cached_comp_unit (this_cu);
8161
f1902523 8162 if (this_cu->is_debug_types)
58f0c718
TT
8163 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8164 build_type_psymtabs_reader, NULL);
f1902523
JK
8165 else
8166 {
8167 process_psymtab_comp_unit_data info;
8168 info.want_partial_unit = want_partial_unit;
8169 info.pretend_language = pretend_language;
58f0c718 8170 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
f1902523
JK
8171 process_psymtab_comp_unit_reader, &info);
8172 }
0018ea6f
DE
8173
8174 /* Age out any secondary CUs. */
ed2dc618 8175 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 8176}
f4dc4d17
DE
8177
8178/* Reader function for build_type_psymtabs. */
8179
8180static void
8181build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 8182 const gdb_byte *info_ptr,
f4dc4d17
DE
8183 struct die_info *type_unit_die,
8184 int has_children,
8185 void *data)
8186{
ed2dc618 8187 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 8188 = reader->cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
8189 struct objfile *objfile = dwarf2_per_objfile->objfile;
8190 struct dwarf2_cu *cu = reader->cu;
8191 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 8192 struct signatured_type *sig_type;
f4dc4d17
DE
8193 struct type_unit_group *tu_group;
8194 struct attribute *attr;
8195 struct partial_die_info *first_die;
8196 CORE_ADDR lowpc, highpc;
8197 struct partial_symtab *pst;
8198
8199 gdb_assert (data == NULL);
0186c6a7
DE
8200 gdb_assert (per_cu->is_debug_types);
8201 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
8202
8203 if (! has_children)
8204 return;
8205
8206 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 8207 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 8208
df07e2c7 8209 if (tu_group->tus == nullptr)
a8b3b8e9 8210 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 8211 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
8212
8213 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
f4dc4d17
DE
8214 pst = create_partial_symtab (per_cu, "");
8215 pst->anonymous = 1;
8216
8217 first_die = load_partial_dies (reader, info_ptr, 1);
8218
8219 lowpc = (CORE_ADDR) -1;
8220 highpc = (CORE_ADDR) 0;
8221 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8222
8763cede 8223 end_psymtab_common (objfile, pst);
f4dc4d17
DE
8224}
8225
73051182
DE
8226/* Struct used to sort TUs by their abbreviation table offset. */
8227
8228struct tu_abbrev_offset
8229{
b2bdb8cf
SM
8230 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
8231 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
8232 {}
8233
8234 signatured_type *sig_type;
73051182
DE
8235 sect_offset abbrev_offset;
8236};
8237
484cf504 8238/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 8239
484cf504
TT
8240static bool
8241sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8242 const struct tu_abbrev_offset &b)
73051182 8243{
484cf504 8244 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
8245}
8246
8247/* Efficiently read all the type units.
8248 This does the bulk of the work for build_type_psymtabs.
8249
8250 The efficiency is because we sort TUs by the abbrev table they use and
8251 only read each abbrev table once. In one program there are 200K TUs
8252 sharing 8K abbrev tables.
8253
8254 The main purpose of this function is to support building the
8255 dwarf2_per_objfile->type_unit_groups table.
8256 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8257 can collapse the search space by grouping them by stmt_list.
8258 The savings can be significant, in the same program from above the 200K TUs
8259 share 8K stmt_list tables.
8260
8261 FUNC is expected to call get_type_unit_group, which will create the
8262 struct type_unit_group if necessary and add it to
8263 dwarf2_per_objfile->type_unit_groups. */
8264
8265static void
ed2dc618 8266build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 8267{
73051182 8268 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
685af9cd 8269 abbrev_table_up abbrev_table;
73051182 8270 sect_offset abbrev_offset;
73051182
DE
8271
8272 /* It's up to the caller to not call us multiple times. */
8273 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8274
b2bdb8cf 8275 if (dwarf2_per_objfile->all_type_units.empty ())
73051182
DE
8276 return;
8277
8278 /* TUs typically share abbrev tables, and there can be way more TUs than
8279 abbrev tables. Sort by abbrev table to reduce the number of times we
8280 read each abbrev table in.
8281 Alternatives are to punt or to maintain a cache of abbrev tables.
8282 This is simpler and efficient enough for now.
8283
8284 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8285 symtab to use). Typically TUs with the same abbrev offset have the same
8286 stmt_list value too so in practice this should work well.
8287
8288 The basic algorithm here is:
8289
8290 sort TUs by abbrev table
8291 for each TU with same abbrev table:
8292 read abbrev table if first user
8293 read TU top level DIE
8294 [IWBN if DWO skeletons had DW_AT_stmt_list]
8295 call FUNC */
8296
b4f54984 8297 if (dwarf_read_debug)
73051182
DE
8298 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8299
8300 /* Sort in a separate table to maintain the order of all_type_units
8301 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf
SM
8302 std::vector<tu_abbrev_offset> sorted_by_abbrev;
8303 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
8304
8305 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
8306 sorted_by_abbrev.emplace_back
8307 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
8308 sig_type->per_cu.section,
8309 sig_type->per_cu.sect_off));
73051182 8310
484cf504
TT
8311 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8312 sort_tu_by_abbrev_offset);
73051182 8313
9c541725 8314 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 8315
b2bdb8cf 8316 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 8317 {
73051182
DE
8318 /* Switch to the next abbrev table if necessary. */
8319 if (abbrev_table == NULL
b2bdb8cf 8320 || tu.abbrev_offset != abbrev_offset)
73051182 8321 {
b2bdb8cf 8322 abbrev_offset = tu.abbrev_offset;
73051182 8323 abbrev_table =
ed2dc618
SM
8324 abbrev_table_read_table (dwarf2_per_objfile,
8325 &dwarf2_per_objfile->abbrev,
73051182
DE
8326 abbrev_offset);
8327 ++tu_stats->nr_uniq_abbrev_tables;
8328 }
8329
b2bdb8cf 8330 init_cutu_and_read_dies (&tu.sig_type->per_cu, abbrev_table.get (),
58f0c718 8331 0, 0, false, build_type_psymtabs_reader, NULL);
73051182 8332 }
6aa5f3a6 8333}
73051182 8334
6aa5f3a6
DE
8335/* Print collected type unit statistics. */
8336
8337static void
ed2dc618 8338print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8339{
8340 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8341
8342 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf
SM
8343 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8344 dwarf2_per_objfile->all_type_units.size ());
6aa5f3a6
DE
8345 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8346 tu_stats->nr_uniq_abbrev_tables);
8347 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8348 tu_stats->nr_symtabs);
8349 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8350 tu_stats->nr_symtab_sharers);
8351 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8352 tu_stats->nr_stmt_less_type_units);
8353 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8354 tu_stats->nr_all_type_units_reallocs);
73051182
DE
8355}
8356
f4dc4d17
DE
8357/* Traversal function for build_type_psymtabs. */
8358
8359static int
8360build_type_psymtab_dependencies (void **slot, void *info)
8361{
ed2dc618
SM
8362 struct dwarf2_per_objfile *dwarf2_per_objfile
8363 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
8364 struct objfile *objfile = dwarf2_per_objfile->objfile;
8365 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 8366 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 8367 struct partial_symtab *pst = per_cu->v.psymtab;
df07e2c7 8368 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
8369 int i;
8370
8371 gdb_assert (len > 0);
0186c6a7 8372 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
8373
8374 pst->number_of_dependencies = len;
a9342b62 8375 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 8376 for (i = 0; i < len; ++i)
f4dc4d17 8377 {
df07e2c7 8378 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
8379 gdb_assert (iter->per_cu.is_debug_types);
8380 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 8381 iter->type_unit_group = tu_group;
f4dc4d17
DE
8382 }
8383
df07e2c7
AB
8384 delete tu_group->tus;
8385 tu_group->tus = nullptr;
348e048f
DE
8386
8387 return 1;
8388}
8389
8390/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8391 Build partial symbol tables for the .debug_types comp-units. */
8392
8393static void
ed2dc618 8394build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 8395{
ed2dc618 8396 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
8397 return;
8398
ed2dc618 8399 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 8400}
f4dc4d17 8401
6aa5f3a6
DE
8402/* Traversal function for process_skeletonless_type_unit.
8403 Read a TU in a DWO file and build partial symbols for it. */
8404
8405static int
8406process_skeletonless_type_unit (void **slot, void *info)
8407{
8408 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
8409 struct dwarf2_per_objfile *dwarf2_per_objfile
8410 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
8411 struct signatured_type find_entry, *entry;
8412
8413 /* If this TU doesn't exist in the global table, add it and read it in. */
8414
8415 if (dwarf2_per_objfile->signatured_types == NULL)
8416 {
8417 dwarf2_per_objfile->signatured_types
ed2dc618 8418 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
6aa5f3a6
DE
8419 }
8420
8421 find_entry.signature = dwo_unit->signature;
8422 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8423 INSERT);
8424 /* If we've already seen this type there's nothing to do. What's happening
8425 is we're doing our own version of comdat-folding here. */
8426 if (*slot != NULL)
8427 return 1;
8428
8429 /* This does the job that create_all_type_units would have done for
8430 this TU. */
ed2dc618
SM
8431 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8432 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
8433 *slot = entry;
8434
8435 /* This does the job that build_type_psymtabs_1 would have done. */
58f0c718 8436 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0, false,
6aa5f3a6
DE
8437 build_type_psymtabs_reader, NULL);
8438
8439 return 1;
8440}
8441
8442/* Traversal function for process_skeletonless_type_units. */
8443
8444static int
8445process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8446{
8447 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8448
8449 if (dwo_file->tus != NULL)
8450 {
8451 htab_traverse_noresize (dwo_file->tus,
8452 process_skeletonless_type_unit, info);
8453 }
8454
8455 return 1;
8456}
8457
8458/* Scan all TUs of DWO files, verifying we've processed them.
8459 This is needed in case a TU was emitted without its skeleton.
8460 Note: This can't be done until we know what all the DWO files are. */
8461
8462static void
ed2dc618 8463process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8464{
8465 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 8466 if (get_dwp_file (dwarf2_per_objfile) == NULL
6aa5f3a6
DE
8467 && dwarf2_per_objfile->dwo_files != NULL)
8468 {
51ac9db5 8469 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
6aa5f3a6 8470 process_dwo_file_for_skeletonless_type_units,
ed2dc618 8471 dwarf2_per_objfile);
6aa5f3a6 8472 }
348e048f
DE
8473}
8474
ed2dc618 8475/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
8476
8477static void
ed2dc618 8478set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 8479{
b76e467d 8480 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
95554aad 8481 {
95554aad 8482 struct partial_symtab *pst = per_cu->v.psymtab;
95554aad 8483
36586728
TT
8484 if (pst == NULL)
8485 continue;
8486
b76e467d 8487 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
8488 {
8489 /* Set the 'user' field only if it is not already set. */
8490 if (pst->dependencies[j]->user == NULL)
8491 pst->dependencies[j]->user = pst;
8492 }
8493 }
8494}
8495
93311388
DE
8496/* Build the partial symbol table by doing a quick pass through the
8497 .debug_info and .debug_abbrev sections. */
72bf9492 8498
93311388 8499static void
ed2dc618 8500dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 8501{
ed2dc618 8502 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 8503
b4f54984 8504 if (dwarf_read_debug)
45cfd468
DE
8505 {
8506 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 8507 objfile_name (objfile));
45cfd468
DE
8508 }
8509
98bfdba5
PA
8510 dwarf2_per_objfile->reading_partial_symbols = 1;
8511
be391dca 8512 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 8513
93311388
DE
8514 /* Any cached compilation units will be linked by the per-objfile
8515 read_in_chain. Make sure to free them when we're done. */
11ed8cad 8516 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 8517
ed2dc618 8518 build_type_psymtabs (dwarf2_per_objfile);
348e048f 8519
ed2dc618 8520 create_all_comp_units (dwarf2_per_objfile);
c906108c 8521
60606b2c
TT
8522 /* Create a temporary address map on a temporary obstack. We later
8523 copy this to the final obstack. */
8268c778 8524 auto_obstack temp_obstack;
791afaa2
TT
8525
8526 scoped_restore save_psymtabs_addrmap
d320c2b5 8527 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 8528 addrmap_create_mutable (&temp_obstack));
72bf9492 8529
b76e467d
SM
8530 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8531 process_psymtab_comp_unit (per_cu, 0, language_minimal);
ff013f42 8532
6aa5f3a6 8533 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 8534 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
8535
8536 /* Now that all TUs have been processed we can fill in the dependencies. */
8537 if (dwarf2_per_objfile->type_unit_groups != NULL)
8538 {
8539 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
ed2dc618 8540 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
8541 }
8542
b4f54984 8543 if (dwarf_read_debug)
ed2dc618 8544 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 8545
ed2dc618 8546 set_partial_user (dwarf2_per_objfile);
95554aad 8547
d320c2b5
TT
8548 objfile->partial_symtabs->psymtabs_addrmap
8549 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 8550 objfile->partial_symtabs->obstack ());
791afaa2
TT
8551 /* At this point we want to keep the address map. */
8552 save_psymtabs_addrmap.release ();
ff013f42 8553
b4f54984 8554 if (dwarf_read_debug)
45cfd468 8555 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 8556 objfile_name (objfile));
ae038cb0
DJ
8557}
8558
3019eac3 8559/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
8560
8561static void
dee91e82 8562load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8563 const gdb_byte *info_ptr,
dee91e82
DE
8564 struct die_info *comp_unit_die,
8565 int has_children,
8566 void *data)
ae038cb0 8567{
dee91e82 8568 struct dwarf2_cu *cu = reader->cu;
ae038cb0 8569
95554aad 8570 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 8571
ae038cb0
DJ
8572 /* Check if comp unit has_children.
8573 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 8574 If not, there's no more debug_info for this comp unit. */
d85a05f0 8575 if (has_children)
dee91e82
DE
8576 load_partial_dies (reader, info_ptr, 0);
8577}
98bfdba5 8578
dee91e82
DE
8579/* Load the partial DIEs for a secondary CU into memory.
8580 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8581
dee91e82
DE
8582static void
8583load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8584{
58f0c718 8585 init_cutu_and_read_dies (this_cu, NULL, 1, 1, false,
f4dc4d17 8586 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
8587}
8588
ae038cb0 8589static void
ed2dc618 8590read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 8591 struct dwarf2_section_info *section,
f1902523 8592 struct dwarf2_section_info *abbrev_section,
b76e467d 8593 unsigned int is_dwz)
ae038cb0 8594{
d521ce57 8595 const gdb_byte *info_ptr;
ed2dc618 8596 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 8597
b4f54984 8598 if (dwarf_read_debug)
bf6af496 8599 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
a32a8923
DE
8600 get_section_name (section),
8601 get_section_file_name (section));
bf6af496 8602
36586728 8603 dwarf2_read_section (objfile, section);
ae038cb0 8604
36586728 8605 info_ptr = section->buffer;
6e70227d 8606
36586728 8607 while (info_ptr < section->buffer + section->size)
ae038cb0 8608 {
ae038cb0 8609 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8610
9c541725 8611 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8612
f1902523 8613 comp_unit_head cu_header;
ed2dc618
SM
8614 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8615 abbrev_section, info_ptr,
8616 rcuh_kind::COMPILE);
ae038cb0
DJ
8617
8618 /* Save the compilation unit for later lookup. */
f1902523
JK
8619 if (cu_header.unit_type != DW_UT_type)
8620 {
8621 this_cu = XOBNEW (&objfile->objfile_obstack,
8622 struct dwarf2_per_cu_data);
8623 memset (this_cu, 0, sizeof (*this_cu));
8624 }
8625 else
8626 {
8627 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8628 struct signatured_type);
8629 memset (sig_type, 0, sizeof (*sig_type));
8630 sig_type->signature = cu_header.signature;
8631 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8632 this_cu = &sig_type->per_cu;
8633 }
8634 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8635 this_cu->sect_off = sect_off;
f1902523 8636 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8637 this_cu->is_dwz = is_dwz;
e3b94546 8638 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 8639 this_cu->section = section;
ae038cb0 8640
b76e467d 8641 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8642
8643 info_ptr = info_ptr + this_cu->length;
8644 }
36586728
TT
8645}
8646
8647/* Create a list of all compilation units in OBJFILE.
8648 This is only done for -readnow and building partial symtabs. */
8649
8650static void
ed2dc618 8651create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 8652{
b76e467d 8653 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
ed2dc618 8654 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
b76e467d 8655 &dwarf2_per_objfile->abbrev, 0);
36586728 8656
b76e467d 8657 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 8658 if (dwz != NULL)
ed2dc618 8659 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
b76e467d 8660 1);
c906108c
SS
8661}
8662
5734ee8b 8663/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8664 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8665 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8666 DW_AT_ranges). See the comments of add_partial_subprogram on how
8667 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8668
72bf9492
DJ
8669static void
8670scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8671 CORE_ADDR *highpc, int set_addrmap,
8672 struct dwarf2_cu *cu)
c906108c 8673{
72bf9492 8674 struct partial_die_info *pdi;
c906108c 8675
91c24f0a
DC
8676 /* Now, march along the PDI's, descending into ones which have
8677 interesting children but skipping the children of the other ones,
8678 until we reach the end of the compilation unit. */
c906108c 8679
72bf9492 8680 pdi = first_die;
91c24f0a 8681
72bf9492
DJ
8682 while (pdi != NULL)
8683 {
52356b79 8684 pdi->fixup (cu);
c906108c 8685
f55ee35c 8686 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8687 children, so we need to look at them. Ditto for anonymous
8688 enums. */
933c6fe4 8689
72bf9492 8690 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8691 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8692 || pdi->tag == DW_TAG_imported_unit
8693 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8694 {
72bf9492 8695 switch (pdi->tag)
c906108c
SS
8696 {
8697 case DW_TAG_subprogram:
b1dc1806 8698 case DW_TAG_inlined_subroutine:
cdc07690 8699 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 8700 break;
72929c62 8701 case DW_TAG_constant:
c906108c
SS
8702 case DW_TAG_variable:
8703 case DW_TAG_typedef:
91c24f0a 8704 case DW_TAG_union_type:
72bf9492 8705 if (!pdi->is_declaration)
63d06c5c 8706 {
72bf9492 8707 add_partial_symbol (pdi, cu);
63d06c5c
DC
8708 }
8709 break;
c906108c 8710 case DW_TAG_class_type:
680b30c7 8711 case DW_TAG_interface_type:
c906108c 8712 case DW_TAG_structure_type:
72bf9492 8713 if (!pdi->is_declaration)
c906108c 8714 {
72bf9492 8715 add_partial_symbol (pdi, cu);
c906108c 8716 }
b7fee5a3
KS
8717 if ((cu->language == language_rust
8718 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8719 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8720 set_addrmap, cu);
c906108c 8721 break;
91c24f0a 8722 case DW_TAG_enumeration_type:
72bf9492
DJ
8723 if (!pdi->is_declaration)
8724 add_partial_enumeration (pdi, cu);
c906108c
SS
8725 break;
8726 case DW_TAG_base_type:
a02abb62 8727 case DW_TAG_subrange_type:
c906108c 8728 /* File scope base type definitions are added to the partial
c5aa993b 8729 symbol table. */
72bf9492 8730 add_partial_symbol (pdi, cu);
c906108c 8731 break;
d9fa45fe 8732 case DW_TAG_namespace:
cdc07690 8733 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8734 break;
5d7cb8df 8735 case DW_TAG_module:
cdc07690 8736 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8737 break;
95554aad
TT
8738 case DW_TAG_imported_unit:
8739 {
8740 struct dwarf2_per_cu_data *per_cu;
8741
f4dc4d17
DE
8742 /* For now we don't handle imported units in type units. */
8743 if (cu->per_cu->is_debug_types)
8744 {
8745 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8746 " supported in type units [in module %s]"),
518817b3 8747 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
8748 }
8749
e3b94546
SM
8750 per_cu = dwarf2_find_containing_comp_unit
8751 (pdi->d.sect_off, pdi->is_dwz,
518817b3 8752 cu->per_cu->dwarf2_per_objfile);
95554aad
TT
8753
8754 /* Go read the partial unit, if needed. */
8755 if (per_cu->v.psymtab == NULL)
b93601f3 8756 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 8757
ae640021 8758 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8759 }
8760 break;
74921315
KS
8761 case DW_TAG_imported_declaration:
8762 add_partial_symbol (pdi, cu);
8763 break;
c906108c
SS
8764 default:
8765 break;
8766 }
8767 }
8768
72bf9492
DJ
8769 /* If the die has a sibling, skip to the sibling. */
8770
8771 pdi = pdi->die_sibling;
8772 }
8773}
8774
8775/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8776
72bf9492 8777 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8778 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8779 Enumerators are an exception; they use the scope of their parent
8780 enumeration type, i.e. the name of the enumeration type is not
8781 prepended to the enumerator.
91c24f0a 8782
72bf9492
DJ
8783 There are two complexities. One is DW_AT_specification; in this
8784 case "parent" means the parent of the target of the specification,
8785 instead of the direct parent of the DIE. The other is compilers
8786 which do not emit DW_TAG_namespace; in this case we try to guess
8787 the fully qualified name of structure types from their members'
8788 linkage names. This must be done using the DIE's children rather
8789 than the children of any DW_AT_specification target. We only need
8790 to do this for structures at the top level, i.e. if the target of
8791 any DW_AT_specification (if any; otherwise the DIE itself) does not
8792 have a parent. */
8793
8794/* Compute the scope prefix associated with PDI's parent, in
8795 compilation unit CU. The result will be allocated on CU's
8796 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8797 field. NULL is returned if no prefix is necessary. */
15d034d0 8798static const char *
72bf9492
DJ
8799partial_die_parent_scope (struct partial_die_info *pdi,
8800 struct dwarf2_cu *cu)
8801{
15d034d0 8802 const char *grandparent_scope;
72bf9492 8803 struct partial_die_info *parent, *real_pdi;
91c24f0a 8804
72bf9492
DJ
8805 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8806 then this means the parent of the specification DIE. */
8807
8808 real_pdi = pdi;
72bf9492 8809 while (real_pdi->has_specification)
fb816e8b 8810 {
122cf0f2
AB
8811 auto res = find_partial_die (real_pdi->spec_offset,
8812 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8813 real_pdi = res.pdi;
8814 cu = res.cu;
8815 }
72bf9492
DJ
8816
8817 parent = real_pdi->die_parent;
8818 if (parent == NULL)
8819 return NULL;
8820
8821 if (parent->scope_set)
8822 return parent->scope;
8823
52356b79 8824 parent->fixup (cu);
72bf9492 8825
10b3939b 8826 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8827
acebe513
UW
8828 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8829 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8830 Work around this problem here. */
8831 if (cu->language == language_cplus
6e70227d 8832 && parent->tag == DW_TAG_namespace
acebe513
UW
8833 && strcmp (parent->name, "::") == 0
8834 && grandparent_scope == NULL)
8835 {
8836 parent->scope = NULL;
8837 parent->scope_set = 1;
8838 return NULL;
8839 }
8840
0a4b0913 8841 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8842 if (pdi->tag == DW_TAG_enumerator)
8843 /* Enumerators should not get the name of the enumeration as a prefix. */
8844 parent->scope = grandparent_scope;
8845 else if (parent->tag == DW_TAG_namespace
f55ee35c 8846 || parent->tag == DW_TAG_module
72bf9492
DJ
8847 || parent->tag == DW_TAG_structure_type
8848 || parent->tag == DW_TAG_class_type
680b30c7 8849 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8850 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8851 || parent->tag == DW_TAG_enumeration_type
8852 || (cu->language == language_fortran
8853 && parent->tag == DW_TAG_subprogram
8854 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8855 {
8856 if (grandparent_scope == NULL)
8857 parent->scope = parent->name;
8858 else
3e43a32a
MS
8859 parent->scope = typename_concat (&cu->comp_unit_obstack,
8860 grandparent_scope,
f55ee35c 8861 parent->name, 0, cu);
72bf9492 8862 }
72bf9492
DJ
8863 else
8864 {
8865 /* FIXME drow/2004-04-01: What should we be doing with
8866 function-local names? For partial symbols, we should probably be
8867 ignoring them. */
fa9c3fa0
TT
8868 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8869 dwarf_tag_name (parent->tag),
8870 sect_offset_str (pdi->sect_off));
72bf9492 8871 parent->scope = grandparent_scope;
c906108c
SS
8872 }
8873
72bf9492
DJ
8874 parent->scope_set = 1;
8875 return parent->scope;
8876}
8877
8878/* Return the fully scoped name associated with PDI, from compilation unit
8879 CU. The result will be allocated with malloc. */
4568ecf9 8880
72bf9492
DJ
8881static char *
8882partial_die_full_name (struct partial_die_info *pdi,
8883 struct dwarf2_cu *cu)
8884{
15d034d0 8885 const char *parent_scope;
72bf9492 8886
98bfdba5
PA
8887 /* If this is a template instantiation, we can not work out the
8888 template arguments from partial DIEs. So, unfortunately, we have
8889 to go through the full DIEs. At least any work we do building
8890 types here will be reused if full symbols are loaded later. */
8891 if (pdi->has_template_arguments)
8892 {
52356b79 8893 pdi->fixup (cu);
98bfdba5
PA
8894
8895 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8896 {
8897 struct die_info *die;
8898 struct attribute attr;
8899 struct dwarf2_cu *ref_cu = cu;
8900
b64f50a1 8901 /* DW_FORM_ref_addr is using section offset. */
b4069958 8902 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8903 attr.form = DW_FORM_ref_addr;
9c541725 8904 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8905 die = follow_die_ref (NULL, &attr, &ref_cu);
8906
8907 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8908 }
8909 }
8910
72bf9492
DJ
8911 parent_scope = partial_die_parent_scope (pdi, cu);
8912 if (parent_scope == NULL)
8913 return NULL;
8914 else
f55ee35c 8915 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
8916}
8917
8918static void
72bf9492 8919add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8920{
518817b3
SM
8921 struct dwarf2_per_objfile *dwarf2_per_objfile
8922 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 8923 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 8924 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 8925 CORE_ADDR addr = 0;
15d034d0 8926 const char *actual_name = NULL;
e142c38c 8927 CORE_ADDR baseaddr;
15d034d0 8928 char *built_actual_name;
e142c38c
DJ
8929
8930 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 8931
15d034d0
TT
8932 built_actual_name = partial_die_full_name (pdi, cu);
8933 if (built_actual_name != NULL)
8934 actual_name = built_actual_name;
63d06c5c 8935
72bf9492
DJ
8936 if (actual_name == NULL)
8937 actual_name = pdi->name;
8938
c906108c
SS
8939 switch (pdi->tag)
8940 {
b1dc1806 8941 case DW_TAG_inlined_subroutine:
c906108c 8942 case DW_TAG_subprogram:
79748972
TT
8943 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8944 - baseaddr);
0a4b0913
AB
8945 if (pdi->is_external
8946 || cu->language == language_ada
8947 || (cu->language == language_fortran
8948 && pdi->die_parent != NULL
8949 && pdi->die_parent->tag == DW_TAG_subprogram))
8950 {
8951 /* Normally, only "external" DIEs are part of the global scope.
8952 But in Ada and Fortran, we want to be able to access nested
8953 procedures globally. So all Ada and Fortran subprograms are
8954 stored in the global scope. */
f47fb265 8955 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8956 built_actual_name != NULL,
f47fb265 8957 VAR_DOMAIN, LOC_BLOCK,
79748972 8958 SECT_OFF_TEXT (objfile),
75aedd27 8959 psymbol_placement::GLOBAL,
79748972
TT
8960 addr,
8961 cu->language, objfile);
c906108c
SS
8962 }
8963 else
8964 {
f47fb265 8965 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8966 built_actual_name != NULL,
f47fb265 8967 VAR_DOMAIN, LOC_BLOCK,
79748972 8968 SECT_OFF_TEXT (objfile),
75aedd27 8969 psymbol_placement::STATIC,
1762568f 8970 addr, cu->language, objfile);
c906108c 8971 }
0c1b455e
TT
8972
8973 if (pdi->main_subprogram && actual_name != NULL)
8974 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8975 break;
72929c62 8976 case DW_TAG_constant:
75aedd27
TT
8977 add_psymbol_to_list (actual_name, strlen (actual_name),
8978 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8979 -1, (pdi->is_external
8980 ? psymbol_placement::GLOBAL
8981 : psymbol_placement::STATIC),
8982 0, cu->language, objfile);
72929c62 8983 break;
c906108c 8984 case DW_TAG_variable:
95554aad
TT
8985 if (pdi->d.locdesc)
8986 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8987
95554aad 8988 if (pdi->d.locdesc
caac4577
JG
8989 && addr == 0
8990 && !dwarf2_per_objfile->has_section_at_zero)
8991 {
8992 /* A global or static variable may also have been stripped
8993 out by the linker if unused, in which case its address
8994 will be nullified; do not add such variables into partial
8995 symbol table then. */
8996 }
8997 else if (pdi->is_external)
c906108c
SS
8998 {
8999 /* Global Variable.
9000 Don't enter into the minimal symbol tables as there is
9001 a minimal symbol table entry from the ELF symbols already.
9002 Enter into partial symbol table if it has a location
9003 descriptor or a type.
9004 If the location descriptor is missing, new_symbol will create
9005 a LOC_UNRESOLVED symbol, the address of the variable will then
9006 be determined from the minimal symbol table whenever the variable
9007 is referenced.
9008 The address for the partial symbol table entry is not
9009 used by GDB, but it comes in handy for debugging partial symbol
9010 table building. */
9011
95554aad 9012 if (pdi->d.locdesc || pdi->has_type)
f47fb265 9013 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9014 built_actual_name != NULL,
f47fb265 9015 VAR_DOMAIN, LOC_STATIC,
79748972 9016 SECT_OFF_TEXT (objfile),
75aedd27 9017 psymbol_placement::GLOBAL,
79748972 9018 addr, cu->language, objfile);
c906108c
SS
9019 }
9020 else
9021 {
ff908ebf
AW
9022 int has_loc = pdi->d.locdesc != NULL;
9023
9024 /* Static Variable. Skip symbols whose value we cannot know (those
9025 without location descriptors or constant values). */
9026 if (!has_loc && !pdi->has_const_value)
decbce07 9027 {
15d034d0 9028 xfree (built_actual_name);
decbce07
MS
9029 return;
9030 }
ff908ebf 9031
f47fb265 9032 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9033 built_actual_name != NULL,
f47fb265 9034 VAR_DOMAIN, LOC_STATIC,
79748972 9035 SECT_OFF_TEXT (objfile),
75aedd27 9036 psymbol_placement::STATIC,
79748972 9037 has_loc ? addr : 0,
f47fb265 9038 cu->language, objfile);
c906108c
SS
9039 }
9040 break;
9041 case DW_TAG_typedef:
9042 case DW_TAG_base_type:
a02abb62 9043 case DW_TAG_subrange_type:
38d518c9 9044 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9045 built_actual_name != NULL,
79748972 9046 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 9047 psymbol_placement::STATIC,
1762568f 9048 0, cu->language, objfile);
c906108c 9049 break;
74921315 9050 case DW_TAG_imported_declaration:
72bf9492
DJ
9051 case DW_TAG_namespace:
9052 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9053 built_actual_name != NULL,
79748972 9054 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 9055 psymbol_placement::GLOBAL,
1762568f 9056 0, cu->language, objfile);
72bf9492 9057 break;
530e8392 9058 case DW_TAG_module:
a5fd13a9
BH
9059 /* With Fortran 77 there might be a "BLOCK DATA" module
9060 available without any name. If so, we skip the module as it
9061 doesn't bring any value. */
9062 if (actual_name != nullptr)
9063 add_psymbol_to_list (actual_name, strlen (actual_name),
9064 built_actual_name != NULL,
9065 MODULE_DOMAIN, LOC_TYPEDEF, -1,
9066 psymbol_placement::GLOBAL,
9067 0, cu->language, objfile);
530e8392 9068 break;
c906108c 9069 case DW_TAG_class_type:
680b30c7 9070 case DW_TAG_interface_type:
c906108c
SS
9071 case DW_TAG_structure_type:
9072 case DW_TAG_union_type:
9073 case DW_TAG_enumeration_type:
fa4028e9
JB
9074 /* Skip external references. The DWARF standard says in the section
9075 about "Structure, Union, and Class Type Entries": "An incomplete
9076 structure, union or class type is represented by a structure,
9077 union or class entry that does not have a byte size attribute
9078 and that has a DW_AT_declaration attribute." */
9079 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 9080 {
15d034d0 9081 xfree (built_actual_name);
decbce07
MS
9082 return;
9083 }
fa4028e9 9084
63d06c5c
DC
9085 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9086 static vs. global. */
38d518c9 9087 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9088 built_actual_name != NULL,
79748972 9089 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
9c37b5ae 9090 cu->language == language_cplus
75aedd27
TT
9091 ? psymbol_placement::GLOBAL
9092 : psymbol_placement::STATIC,
1762568f 9093 0, cu->language, objfile);
c906108c 9094
c906108c
SS
9095 break;
9096 case DW_TAG_enumerator:
38d518c9 9097 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9098 built_actual_name != NULL,
79748972 9099 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 9100 cu->language == language_cplus
75aedd27
TT
9101 ? psymbol_placement::GLOBAL
9102 : psymbol_placement::STATIC,
1762568f 9103 0, cu->language, objfile);
c906108c
SS
9104 break;
9105 default:
9106 break;
9107 }
5c4e30ca 9108
15d034d0 9109 xfree (built_actual_name);
c906108c
SS
9110}
9111
5c4e30ca
DC
9112/* Read a partial die corresponding to a namespace; also, add a symbol
9113 corresponding to that namespace to the symbol table. NAMESPACE is
9114 the name of the enclosing namespace. */
91c24f0a 9115
72bf9492
DJ
9116static void
9117add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 9118 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9119 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 9120{
72bf9492 9121 /* Add a symbol for the namespace. */
e7c27a73 9122
72bf9492 9123 add_partial_symbol (pdi, cu);
5c4e30ca
DC
9124
9125 /* Now scan partial symbols in that namespace. */
9126
91c24f0a 9127 if (pdi->has_children)
cdc07690 9128 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
9129}
9130
5d7cb8df
JK
9131/* Read a partial die corresponding to a Fortran module. */
9132
9133static void
9134add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 9135 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 9136{
530e8392
KB
9137 /* Add a symbol for the namespace. */
9138
9139 add_partial_symbol (pdi, cu);
9140
f55ee35c 9141 /* Now scan partial symbols in that module. */
5d7cb8df
JK
9142
9143 if (pdi->has_children)
cdc07690 9144 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
9145}
9146
b1dc1806
XR
9147/* Read a partial die corresponding to a subprogram or an inlined
9148 subprogram and create a partial symbol for that subprogram.
9149 When the CU language allows it, this routine also defines a partial
9150 symbol for each nested subprogram that this subprogram contains.
9151 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9152 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 9153
cdc07690
YQ
9154 PDI may also be a lexical block, in which case we simply search
9155 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
9156 Again, this is only performed when the CU language allows this
9157 type of definitions. */
9158
9159static void
9160add_partial_subprogram (struct partial_die_info *pdi,
9161 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9162 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 9163{
b1dc1806 9164 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
9165 {
9166 if (pdi->has_pc_info)
9167 {
9168 if (pdi->lowpc < *lowpc)
9169 *lowpc = pdi->lowpc;
9170 if (pdi->highpc > *highpc)
9171 *highpc = pdi->highpc;
cdc07690 9172 if (set_addrmap)
5734ee8b 9173 {
518817b3 9174 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a
MR
9175 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9176 CORE_ADDR baseaddr;
b926417a
TT
9177 CORE_ADDR this_highpc;
9178 CORE_ADDR this_lowpc;
5734ee8b
DJ
9179
9180 baseaddr = ANOFFSET (objfile->section_offsets,
9181 SECT_OFF_TEXT (objfile));
b926417a
TT
9182 this_lowpc
9183 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9184 pdi->lowpc + baseaddr)
9185 - baseaddr);
9186 this_highpc
9187 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9188 pdi->highpc + baseaddr)
9189 - baseaddr);
d320c2b5 9190 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 9191 this_lowpc, this_highpc - 1,
9291a0cd 9192 cu->per_cu->v.psymtab);
5734ee8b 9193 }
481860b3
GB
9194 }
9195
9196 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9197 {
bc30ff58 9198 if (!pdi->is_declaration)
e8d05480
JB
9199 /* Ignore subprogram DIEs that do not have a name, they are
9200 illegal. Do not emit a complaint at this point, we will
9201 do so when we convert this psymtab into a symtab. */
9202 if (pdi->name)
9203 add_partial_symbol (pdi, cu);
bc30ff58
JB
9204 }
9205 }
6e70227d 9206
bc30ff58
JB
9207 if (! pdi->has_children)
9208 return;
9209
0a4b0913 9210 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
9211 {
9212 pdi = pdi->die_child;
9213 while (pdi != NULL)
9214 {
52356b79 9215 pdi->fixup (cu);
bc30ff58 9216 if (pdi->tag == DW_TAG_subprogram
b1dc1806 9217 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 9218 || pdi->tag == DW_TAG_lexical_block)
cdc07690 9219 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
9220 pdi = pdi->die_sibling;
9221 }
9222 }
9223}
9224
91c24f0a
DC
9225/* Read a partial die corresponding to an enumeration type. */
9226
72bf9492
DJ
9227static void
9228add_partial_enumeration (struct partial_die_info *enum_pdi,
9229 struct dwarf2_cu *cu)
91c24f0a 9230{
72bf9492 9231 struct partial_die_info *pdi;
91c24f0a
DC
9232
9233 if (enum_pdi->name != NULL)
72bf9492
DJ
9234 add_partial_symbol (enum_pdi, cu);
9235
9236 pdi = enum_pdi->die_child;
9237 while (pdi)
91c24f0a 9238 {
72bf9492 9239 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
b98664d3 9240 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 9241 else
72bf9492
DJ
9242 add_partial_symbol (pdi, cu);
9243 pdi = pdi->die_sibling;
91c24f0a 9244 }
91c24f0a
DC
9245}
9246
6caca83c
CC
9247/* Return the initial uleb128 in the die at INFO_PTR. */
9248
9249static unsigned int
d521ce57 9250peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
9251{
9252 unsigned int bytes_read;
9253
9254 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9255}
9256
685af9cd
TT
9257/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9258 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9259
4bb7a0a7
DJ
9260 Return the corresponding abbrev, or NULL if the number is zero (indicating
9261 an empty DIE). In either case *BYTES_READ will be set to the length of
9262 the initial number. */
9263
9264static struct abbrev_info *
685af9cd
TT
9265peek_die_abbrev (const die_reader_specs &reader,
9266 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 9267{
685af9cd 9268 dwarf2_cu *cu = reader.cu;
518817b3 9269 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
685af9cd
TT
9270 unsigned int abbrev_number
9271 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
9272
9273 if (abbrev_number == 0)
9274 return NULL;
9275
685af9cd 9276 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
9277 if (!abbrev)
9278 {
422b9917 9279 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 9280 " at offset %s [in module %s]"),
422b9917 9281 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 9282 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
9283 }
9284
9285 return abbrev;
9286}
9287
93311388
DE
9288/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9289 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
9290 DIE. Any children of the skipped DIEs will also be skipped. */
9291
d521ce57
TT
9292static const gdb_byte *
9293skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 9294{
4bb7a0a7
DJ
9295 while (1)
9296 {
685af9cd
TT
9297 unsigned int bytes_read;
9298 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9299
4bb7a0a7
DJ
9300 if (abbrev == NULL)
9301 return info_ptr + bytes_read;
9302 else
dee91e82 9303 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
9304 }
9305}
9306
93311388
DE
9307/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9308 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
9309 abbrev corresponding to that skipped uleb128 should be passed in
9310 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9311 children. */
9312
d521ce57
TT
9313static const gdb_byte *
9314skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 9315 struct abbrev_info *abbrev)
4bb7a0a7
DJ
9316{
9317 unsigned int bytes_read;
9318 struct attribute attr;
dee91e82
DE
9319 bfd *abfd = reader->abfd;
9320 struct dwarf2_cu *cu = reader->cu;
d521ce57 9321 const gdb_byte *buffer = reader->buffer;
f664829e 9322 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
9323 unsigned int form, i;
9324
9325 for (i = 0; i < abbrev->num_attrs; i++)
9326 {
9327 /* The only abbrev we care about is DW_AT_sibling. */
9328 if (abbrev->attrs[i].name == DW_AT_sibling)
9329 {
dee91e82 9330 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 9331 if (attr.form == DW_FORM_ref_addr)
b98664d3 9332 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 9333 else
b9502d3f 9334 {
9c541725
PA
9335 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9336 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
9337
9338 if (sibling_ptr < info_ptr)
b98664d3 9339 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
9340 else if (sibling_ptr > reader->buffer_end)
9341 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
9342 else
9343 return sibling_ptr;
9344 }
4bb7a0a7
DJ
9345 }
9346
9347 /* If it isn't DW_AT_sibling, skip this attribute. */
9348 form = abbrev->attrs[i].form;
9349 skip_attribute:
9350 switch (form)
9351 {
4bb7a0a7 9352 case DW_FORM_ref_addr:
ae411497
TT
9353 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9354 and later it is offset sized. */
9355 if (cu->header.version == 2)
9356 info_ptr += cu->header.addr_size;
9357 else
9358 info_ptr += cu->header.offset_size;
9359 break;
36586728
TT
9360 case DW_FORM_GNU_ref_alt:
9361 info_ptr += cu->header.offset_size;
9362 break;
ae411497 9363 case DW_FORM_addr:
4bb7a0a7
DJ
9364 info_ptr += cu->header.addr_size;
9365 break;
9366 case DW_FORM_data1:
9367 case DW_FORM_ref1:
9368 case DW_FORM_flag:
8fe0f950 9369 case DW_FORM_strx1:
4bb7a0a7
DJ
9370 info_ptr += 1;
9371 break;
2dc7f7b3 9372 case DW_FORM_flag_present:
43988095 9373 case DW_FORM_implicit_const:
2dc7f7b3 9374 break;
4bb7a0a7
DJ
9375 case DW_FORM_data2:
9376 case DW_FORM_ref2:
8fe0f950 9377 case DW_FORM_strx2:
4bb7a0a7
DJ
9378 info_ptr += 2;
9379 break;
8fe0f950
AT
9380 case DW_FORM_strx3:
9381 info_ptr += 3;
9382 break;
4bb7a0a7
DJ
9383 case DW_FORM_data4:
9384 case DW_FORM_ref4:
8fe0f950 9385 case DW_FORM_strx4:
4bb7a0a7
DJ
9386 info_ptr += 4;
9387 break;
9388 case DW_FORM_data8:
9389 case DW_FORM_ref8:
55f1336d 9390 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
9391 info_ptr += 8;
9392 break;
0224619f
JK
9393 case DW_FORM_data16:
9394 info_ptr += 16;
9395 break;
4bb7a0a7 9396 case DW_FORM_string:
9b1c24c8 9397 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
9398 info_ptr += bytes_read;
9399 break;
2dc7f7b3 9400 case DW_FORM_sec_offset:
4bb7a0a7 9401 case DW_FORM_strp:
36586728 9402 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
9403 info_ptr += cu->header.offset_size;
9404 break;
2dc7f7b3 9405 case DW_FORM_exprloc:
4bb7a0a7
DJ
9406 case DW_FORM_block:
9407 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9408 info_ptr += bytes_read;
9409 break;
9410 case DW_FORM_block1:
9411 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9412 break;
9413 case DW_FORM_block2:
9414 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9415 break;
9416 case DW_FORM_block4:
9417 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9418 break;
336d760d 9419 case DW_FORM_addrx:
cf532bd1 9420 case DW_FORM_strx:
4bb7a0a7
DJ
9421 case DW_FORM_sdata:
9422 case DW_FORM_udata:
9423 case DW_FORM_ref_udata:
3019eac3
DE
9424 case DW_FORM_GNU_addr_index:
9425 case DW_FORM_GNU_str_index:
d521ce57 9426 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
9427 break;
9428 case DW_FORM_indirect:
9429 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9430 info_ptr += bytes_read;
9431 /* We need to continue parsing from here, so just go back to
9432 the top. */
9433 goto skip_attribute;
9434
9435 default:
3e43a32a
MS
9436 error (_("Dwarf Error: Cannot handle %s "
9437 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
9438 dwarf_form_name (form),
9439 bfd_get_filename (abfd));
9440 }
9441 }
9442
9443 if (abbrev->has_children)
dee91e82 9444 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
9445 else
9446 return info_ptr;
9447}
9448
93311388 9449/* Locate ORIG_PDI's sibling.
dee91e82 9450 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 9451
d521ce57 9452static const gdb_byte *
dee91e82
DE
9453locate_pdi_sibling (const struct die_reader_specs *reader,
9454 struct partial_die_info *orig_pdi,
d521ce57 9455 const gdb_byte *info_ptr)
91c24f0a
DC
9456{
9457 /* Do we know the sibling already? */
72bf9492 9458
91c24f0a
DC
9459 if (orig_pdi->sibling)
9460 return orig_pdi->sibling;
9461
9462 /* Are there any children to deal with? */
9463
9464 if (!orig_pdi->has_children)
9465 return info_ptr;
9466
4bb7a0a7 9467 /* Skip the children the long way. */
91c24f0a 9468
dee91e82 9469 return skip_children (reader, info_ptr);
91c24f0a
DC
9470}
9471
257e7a09 9472/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 9473 not NULL. */
c906108c
SS
9474
9475static void
257e7a09
YQ
9476dwarf2_read_symtab (struct partial_symtab *self,
9477 struct objfile *objfile)
c906108c 9478{
ed2dc618
SM
9479 struct dwarf2_per_objfile *dwarf2_per_objfile
9480 = get_dwarf2_per_objfile (objfile);
9481
257e7a09 9482 if (self->readin)
c906108c 9483 {
442e4d9c 9484 warning (_("bug: psymtab for %s is already read in."),
257e7a09 9485 self->filename);
442e4d9c
YQ
9486 }
9487 else
9488 {
9489 if (info_verbose)
c906108c 9490 {
442e4d9c 9491 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 9492 self->filename);
442e4d9c 9493 gdb_flush (gdb_stdout);
c906108c 9494 }
c906108c 9495
442e4d9c
YQ
9496 /* If this psymtab is constructed from a debug-only objfile, the
9497 has_section_at_zero flag will not necessarily be correct. We
9498 can get the correct value for this flag by looking at the data
9499 associated with the (presumably stripped) associated objfile. */
9500 if (objfile->separate_debug_objfile_backlink)
9501 {
9502 struct dwarf2_per_objfile *dpo_backlink
ed2dc618 9503 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9a619af0 9504
442e4d9c
YQ
9505 dwarf2_per_objfile->has_section_at_zero
9506 = dpo_backlink->has_section_at_zero;
9507 }
b2ab525c 9508
442e4d9c 9509 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 9510
257e7a09 9511 psymtab_to_symtab_1 (self);
c906108c 9512
442e4d9c
YQ
9513 /* Finish up the debug error message. */
9514 if (info_verbose)
9515 printf_filtered (_("done.\n"));
c906108c 9516 }
95554aad 9517
ed2dc618 9518 process_cu_includes (dwarf2_per_objfile);
c906108c 9519}
9cdd5dbd
DE
9520\f
9521/* Reading in full CUs. */
c906108c 9522
10b3939b
DJ
9523/* Add PER_CU to the queue. */
9524
9525static void
95554aad
TT
9526queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9527 enum language pretend_language)
10b3939b
DJ
9528{
9529 struct dwarf2_queue_item *item;
9530
9531 per_cu->queued = 1;
8d749320 9532 item = XNEW (struct dwarf2_queue_item);
10b3939b 9533 item->per_cu = per_cu;
95554aad 9534 item->pretend_language = pretend_language;
10b3939b
DJ
9535 item->next = NULL;
9536
9537 if (dwarf2_queue == NULL)
9538 dwarf2_queue = item;
9539 else
9540 dwarf2_queue_tail->next = item;
9541
9542 dwarf2_queue_tail = item;
9543}
9544
89e63ee4
DE
9545/* If PER_CU is not yet queued, add it to the queue.
9546 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9547 dependency.
0907af0c 9548 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
9549 meaning either PER_CU is already queued or it is already loaded.
9550
9551 N.B. There is an invariant here that if a CU is queued then it is loaded.
9552 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
9553
9554static int
89e63ee4 9555maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
9556 struct dwarf2_per_cu_data *per_cu,
9557 enum language pretend_language)
9558{
9559 /* We may arrive here during partial symbol reading, if we need full
9560 DIEs to process an unusual case (e.g. template arguments). Do
9561 not queue PER_CU, just tell our caller to load its DIEs. */
ed2dc618 9562 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
0907af0c
DE
9563 {
9564 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9565 return 1;
9566 return 0;
9567 }
9568
9569 /* Mark the dependence relation so that we don't flush PER_CU
9570 too early. */
89e63ee4
DE
9571 if (dependent_cu != NULL)
9572 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
9573
9574 /* If it's already on the queue, we have nothing to do. */
9575 if (per_cu->queued)
9576 return 0;
9577
9578 /* If the compilation unit is already loaded, just mark it as
9579 used. */
9580 if (per_cu->cu != NULL)
9581 {
9582 per_cu->cu->last_used = 0;
9583 return 0;
9584 }
9585
9586 /* Add it to the queue. */
9587 queue_comp_unit (per_cu, pretend_language);
9588
9589 return 1;
9590}
9591
10b3939b
DJ
9592/* Process the queue. */
9593
9594static void
ed2dc618 9595process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b
DJ
9596{
9597 struct dwarf2_queue_item *item, *next_item;
9598
b4f54984 9599 if (dwarf_read_debug)
45cfd468
DE
9600 {
9601 fprintf_unfiltered (gdb_stdlog,
9602 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 9603 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
9604 }
9605
03dd20cc
DJ
9606 /* The queue starts out with one item, but following a DIE reference
9607 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
9608 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9609 {
cc12ce38
DE
9610 if ((dwarf2_per_objfile->using_index
9611 ? !item->per_cu->v.quick->compunit_symtab
9612 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9613 /* Skip dummy CUs. */
9614 && item->per_cu->cu != NULL)
f4dc4d17
DE
9615 {
9616 struct dwarf2_per_cu_data *per_cu = item->per_cu;
73be47f5 9617 unsigned int debug_print_threshold;
247f5c4f 9618 char buf[100];
f4dc4d17 9619
247f5c4f 9620 if (per_cu->is_debug_types)
f4dc4d17 9621 {
247f5c4f
DE
9622 struct signatured_type *sig_type =
9623 (struct signatured_type *) per_cu;
9624
9d8780f0 9625 sprintf (buf, "TU %s at offset %s",
73be47f5 9626 hex_string (sig_type->signature),
9d8780f0 9627 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9628 /* There can be 100s of TUs.
9629 Only print them in verbose mode. */
9630 debug_print_threshold = 2;
f4dc4d17 9631 }
247f5c4f 9632 else
73be47f5 9633 {
9d8780f0
SM
9634 sprintf (buf, "CU at offset %s",
9635 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9636 debug_print_threshold = 1;
9637 }
247f5c4f 9638
b4f54984 9639 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9640 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
9641
9642 if (per_cu->is_debug_types)
9643 process_full_type_unit (per_cu, item->pretend_language);
9644 else
9645 process_full_comp_unit (per_cu, item->pretend_language);
9646
b4f54984 9647 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9648 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 9649 }
10b3939b
DJ
9650
9651 item->per_cu->queued = 0;
9652 next_item = item->next;
9653 xfree (item);
9654 }
9655
9656 dwarf2_queue_tail = NULL;
45cfd468 9657
b4f54984 9658 if (dwarf_read_debug)
45cfd468
DE
9659 {
9660 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 9661 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 9662 }
10b3939b
DJ
9663}
9664
10b3939b
DJ
9665/* Read in full symbols for PST, and anything it depends on. */
9666
c906108c 9667static void
fba45db2 9668psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 9669{
10b3939b 9670 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
9671 int i;
9672
95554aad
TT
9673 if (pst->readin)
9674 return;
9675
aaa75496 9676 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
9677 if (!pst->dependencies[i]->readin
9678 && pst->dependencies[i]->user == NULL)
aaa75496
JB
9679 {
9680 /* Inform about additional files that need to be read in. */
9681 if (info_verbose)
9682 {
a3f17187 9683 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
9684 fputs_filtered (" ", gdb_stdout);
9685 wrap_here ("");
9686 fputs_filtered ("and ", gdb_stdout);
9687 wrap_here ("");
9688 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 9689 wrap_here (""); /* Flush output. */
aaa75496
JB
9690 gdb_flush (gdb_stdout);
9691 }
9692 psymtab_to_symtab_1 (pst->dependencies[i]);
9693 }
9694
9a3c8263 9695 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10b3939b
DJ
9696
9697 if (per_cu == NULL)
aaa75496
JB
9698 {
9699 /* It's an include file, no symbols to read for it.
9700 Everything is in the parent symtab. */
9701 pst->readin = 1;
9702 return;
9703 }
c906108c 9704
58f0c718 9705 dw2_do_instantiate_symtab (per_cu, false);
10b3939b
DJ
9706}
9707
dee91e82
DE
9708/* Trivial hash function for die_info: the hash value of a DIE
9709 is its offset in .debug_info for this objfile. */
10b3939b 9710
dee91e82
DE
9711static hashval_t
9712die_hash (const void *item)
10b3939b 9713{
9a3c8263 9714 const struct die_info *die = (const struct die_info *) item;
6502dd73 9715
9c541725 9716 return to_underlying (die->sect_off);
dee91e82 9717}
63d06c5c 9718
dee91e82
DE
9719/* Trivial comparison function for die_info structures: two DIEs
9720 are equal if they have the same offset. */
98bfdba5 9721
dee91e82
DE
9722static int
9723die_eq (const void *item_lhs, const void *item_rhs)
9724{
9a3c8263
SM
9725 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9726 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9727
9c541725 9728 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9729}
c906108c 9730
dee91e82
DE
9731/* die_reader_func for load_full_comp_unit.
9732 This is identical to read_signatured_type_reader,
9733 but is kept separate for now. */
c906108c 9734
dee91e82
DE
9735static void
9736load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 9737 const gdb_byte *info_ptr,
dee91e82
DE
9738 struct die_info *comp_unit_die,
9739 int has_children,
9740 void *data)
9741{
9742 struct dwarf2_cu *cu = reader->cu;
9a3c8263 9743 enum language *language_ptr = (enum language *) data;
6caca83c 9744
dee91e82
DE
9745 gdb_assert (cu->die_hash == NULL);
9746 cu->die_hash =
9747 htab_create_alloc_ex (cu->header.length / 12,
9748 die_hash,
9749 die_eq,
9750 NULL,
9751 &cu->comp_unit_obstack,
9752 hashtab_obstack_allocate,
9753 dummy_obstack_deallocate);
e142c38c 9754
dee91e82
DE
9755 if (has_children)
9756 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
9757 &info_ptr, comp_unit_die);
9758 cu->dies = comp_unit_die;
9759 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9760
9761 /* We try not to read any attributes in this function, because not
9cdd5dbd 9762 all CUs needed for references have been loaded yet, and symbol
10b3939b 9763 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9764 or we won't be able to build types correctly.
9765 Similarly, if we do not read the producer, we can not apply
9766 producer-specific interpretation. */
95554aad 9767 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 9768}
10b3939b 9769
dee91e82 9770/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 9771
dee91e82 9772static void
95554aad 9773load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
58f0c718 9774 bool skip_partial,
95554aad 9775 enum language pretend_language)
dee91e82 9776{
3019eac3 9777 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 9778
58f0c718 9779 init_cutu_and_read_dies (this_cu, NULL, 1, 1, skip_partial,
f4dc4d17 9780 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
9781}
9782
3da10d80
KS
9783/* Add a DIE to the delayed physname list. */
9784
9785static void
9786add_to_method_list (struct type *type, int fnfield_index, int index,
9787 const char *name, struct die_info *die,
9788 struct dwarf2_cu *cu)
9789{
9790 struct delayed_method_info mi;
9791 mi.type = type;
9792 mi.fnfield_index = fnfield_index;
9793 mi.index = index;
9794 mi.name = name;
9795 mi.die = die;
c89b44cd 9796 cu->method_list.push_back (mi);
3da10d80
KS
9797}
9798
3693fdb3
PA
9799/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9800 "const" / "volatile". If so, decrements LEN by the length of the
9801 modifier and return true. Otherwise return false. */
9802
9803template<size_t N>
9804static bool
9805check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9806{
9807 size_t mod_len = sizeof (mod) - 1;
9808 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9809 {
9810 len -= mod_len;
9811 return true;
9812 }
9813 return false;
9814}
9815
3da10d80
KS
9816/* Compute the physnames of any methods on the CU's method list.
9817
9818 The computation of method physnames is delayed in order to avoid the
9819 (bad) condition that one of the method's formal parameters is of an as yet
9820 incomplete type. */
9821
9822static void
9823compute_delayed_physnames (struct dwarf2_cu *cu)
9824{
3693fdb3 9825 /* Only C++ delays computing physnames. */
c89b44cd 9826 if (cu->method_list.empty ())
3693fdb3
PA
9827 return;
9828 gdb_assert (cu->language == language_cplus);
9829
52941706 9830 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9831 {
1d06ead6 9832 const char *physname;
3da10d80 9833 struct fn_fieldlist *fn_flp
c89b44cd
TT
9834 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9835 physname = dwarf2_physname (mi.name, mi.die, cu);
9836 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9837 = physname ? physname : "";
3693fdb3
PA
9838
9839 /* Since there's no tag to indicate whether a method is a
9840 const/volatile overload, extract that information out of the
9841 demangled name. */
9842 if (physname != NULL)
9843 {
9844 size_t len = strlen (physname);
9845
9846 while (1)
9847 {
9848 if (physname[len] == ')') /* shortcut */
9849 break;
9850 else if (check_modifier (physname, len, " const"))
c89b44cd 9851 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9852 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9853 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9854 else
9855 break;
9856 }
9857 }
3da10d80 9858 }
c89b44cd
TT
9859
9860 /* The list is no longer needed. */
9861 cu->method_list.clear ();
3da10d80
KS
9862}
9863
a766d390
DE
9864/* Go objects should be embedded in a DW_TAG_module DIE,
9865 and it's not clear if/how imported objects will appear.
9866 To keep Go support simple until that's worked out,
9867 go back through what we've read and create something usable.
9868 We could do this while processing each DIE, and feels kinda cleaner,
9869 but that way is more invasive.
9870 This is to, for example, allow the user to type "p var" or "b main"
9871 without having to specify the package name, and allow lookups
9872 of module.object to work in contexts that use the expression
9873 parser. */
9874
9875static void
9876fixup_go_packaging (struct dwarf2_cu *cu)
9877{
9878 char *package_name = NULL;
9879 struct pending *list;
9880 int i;
9881
c24bdb02 9882 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9883 list != NULL;
9884 list = list->next)
a766d390
DE
9885 {
9886 for (i = 0; i < list->nsyms; ++i)
9887 {
9888 struct symbol *sym = list->symbol[i];
9889
9890 if (SYMBOL_LANGUAGE (sym) == language_go
9891 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9892 {
9893 char *this_package_name = go_symbol_package_name (sym);
9894
9895 if (this_package_name == NULL)
9896 continue;
9897 if (package_name == NULL)
9898 package_name = this_package_name;
9899 else
9900 {
518817b3
SM
9901 struct objfile *objfile
9902 = cu->per_cu->dwarf2_per_objfile->objfile;
a766d390 9903 if (strcmp (package_name, this_package_name) != 0)
b98664d3 9904 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9905 (symbol_symtab (sym) != NULL
9906 ? symtab_to_filename_for_display
9907 (symbol_symtab (sym))
e3b94546 9908 : objfile_name (objfile)),
a766d390
DE
9909 this_package_name, package_name);
9910 xfree (this_package_name);
9911 }
9912 }
9913 }
9914 }
9915
9916 if (package_name != NULL)
9917 {
518817b3 9918 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
34a68019 9919 const char *saved_package_name
021887d8 9920 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name);
19f392bc
UW
9921 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9922 saved_package_name);
a766d390
DE
9923 struct symbol *sym;
9924
e623cf5d 9925 sym = allocate_symbol (objfile);
f85f34ed 9926 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
9927 SYMBOL_SET_NAMES (sym, saved_package_name,
9928 strlen (saved_package_name), 0, objfile);
a766d390
DE
9929 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9930 e.g., "main" finds the "main" module and not C's main(). */
9931 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9932 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9933 SYMBOL_TYPE (sym) = type;
9934
c24bdb02 9935 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9936
9937 xfree (package_name);
9938 }
9939}
9940
c9317f21
TT
9941/* Allocate a fully-qualified name consisting of the two parts on the
9942 obstack. */
9943
9944static const char *
9945rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9946{
9947 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9948}
9949
9950/* A helper that allocates a struct discriminant_info to attach to a
9951 union type. */
9952
9953static struct discriminant_info *
9954alloc_discriminant_info (struct type *type, int discriminant_index,
9955 int default_index)
9956{
9957 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
c7b15a66
TT
9958 gdb_assert (discriminant_index == -1
9959 || (discriminant_index >= 0
9960 && discriminant_index < TYPE_NFIELDS (type)));
c9317f21 9961 gdb_assert (default_index == -1
c7b15a66 9962 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
c9317f21
TT
9963
9964 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9965
9966 struct discriminant_info *disc
9967 = ((struct discriminant_info *)
9968 TYPE_ZALLOC (type,
9969 offsetof (struct discriminant_info, discriminants)
9970 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9971 disc->default_index = default_index;
9972 disc->discriminant_index = discriminant_index;
9973
9974 struct dynamic_prop prop;
9975 prop.kind = PROP_UNDEFINED;
9976 prop.data.baton = disc;
9977
9978 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9979
9980 return disc;
9981}
9982
9983/* Some versions of rustc emitted enums in an unusual way.
9984
9985 Ordinary enums were emitted as unions. The first element of each
9986 structure in the union was named "RUST$ENUM$DISR". This element
9987 held the discriminant.
9988
9989 These versions of Rust also implemented the "non-zero"
9990 optimization. When the enum had two values, and one is empty and
9991 the other holds a pointer that cannot be zero, the pointer is used
9992 as the discriminant, with a zero value meaning the empty variant.
9993 Here, the union's first member is of the form
9994 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9995 where the fieldnos are the indices of the fields that should be
9996 traversed in order to find the field (which may be several fields deep)
9997 and the variantname is the name of the variant of the case when the
9998 field is zero.
9999
10000 This function recognizes whether TYPE is of one of these forms,
10001 and, if so, smashes it to be a variant type. */
10002
10003static void
10004quirk_rust_enum (struct type *type, struct objfile *objfile)
10005{
10006 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
10007
10008 /* We don't need to deal with empty enums. */
10009 if (TYPE_NFIELDS (type) == 0)
10010 return;
10011
10012#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
10013 if (TYPE_NFIELDS (type) == 1
10014 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
10015 {
10016 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
10017
10018 /* Decode the field name to find the offset of the
10019 discriminant. */
10020 ULONGEST bit_offset = 0;
10021 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
10022 while (name[0] >= '0' && name[0] <= '9')
10023 {
10024 char *tail;
10025 unsigned long index = strtoul (name, &tail, 10);
10026 name = tail;
10027 if (*name != '$'
10028 || index >= TYPE_NFIELDS (field_type)
10029 || (TYPE_FIELD_LOC_KIND (field_type, index)
10030 != FIELD_LOC_KIND_BITPOS))
10031 {
b98664d3 10032 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
10033 "[in module %s]"),
10034 TYPE_FIELD_NAME (type, 0),
10035 objfile_name (objfile));
10036 return;
10037 }
10038 ++name;
10039
10040 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
10041 field_type = TYPE_FIELD_TYPE (field_type, index);
10042 }
10043
10044 /* Make a union to hold the variants. */
10045 struct type *union_type = alloc_type (objfile);
10046 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10047 TYPE_NFIELDS (union_type) = 3;
10048 TYPE_FIELDS (union_type)
10049 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
10050 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10051 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10052
10053 /* Put the discriminant must at index 0. */
10054 TYPE_FIELD_TYPE (union_type, 0) = field_type;
10055 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10056 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10057 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
10058
10059 /* The order of fields doesn't really matter, so put the real
10060 field at index 1 and the data-less field at index 2. */
10061 struct discriminant_info *disc
10062 = alloc_discriminant_info (union_type, 0, 1);
10063 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
10064 TYPE_FIELD_NAME (union_type, 1)
10065 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
10066 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
10067 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10068 TYPE_FIELD_NAME (union_type, 1));
10069
10070 const char *dataless_name
10071 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
10072 name);
10073 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
10074 dataless_name);
10075 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
10076 /* NAME points into the original discriminant name, which
10077 already has the correct lifetime. */
10078 TYPE_FIELD_NAME (union_type, 2) = name;
10079 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
10080 disc->discriminants[2] = 0;
10081
10082 /* Smash this type to be a structure type. We have to do this
10083 because the type has already been recorded. */
10084 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10085 TYPE_NFIELDS (type) = 1;
10086 TYPE_FIELDS (type)
10087 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
10088
10089 /* Install the variant part. */
10090 TYPE_FIELD_TYPE (type, 0) = union_type;
10091 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10092 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10093 }
77c2dba3
TT
10094 /* A union with a single anonymous field is probably an old-style
10095 univariant enum. */
10096 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 10097 {
c9317f21
TT
10098 /* Smash this type to be a structure type. We have to do this
10099 because the type has already been recorded. */
10100 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10101
10102 /* Make a union to hold the variants. */
10103 struct type *union_type = alloc_type (objfile);
10104 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10105 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
10106 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10107 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10108 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
10109
10110 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
10111 const char *variant_name
10112 = rust_last_path_segment (TYPE_NAME (field_type));
10113 TYPE_FIELD_NAME (union_type, 0) = variant_name;
10114 TYPE_NAME (field_type)
10115 = rust_fully_qualify (&objfile->objfile_obstack,
c7b15a66 10116 TYPE_NAME (type), variant_name);
c9317f21
TT
10117
10118 /* Install the union in the outer struct type. */
10119 TYPE_NFIELDS (type) = 1;
10120 TYPE_FIELDS (type)
10121 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
10122 TYPE_FIELD_TYPE (type, 0) = union_type;
10123 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10124 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10125
10126 alloc_discriminant_info (union_type, -1, 0);
10127 }
10128 else
10129 {
10130 struct type *disr_type = nullptr;
10131 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10132 {
10133 disr_type = TYPE_FIELD_TYPE (type, i);
10134
a037790e
TT
10135 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
10136 {
10137 /* All fields of a true enum will be structs. */
10138 return;
10139 }
10140 else if (TYPE_NFIELDS (disr_type) == 0)
c9317f21
TT
10141 {
10142 /* Could be data-less variant, so keep going. */
a037790e 10143 disr_type = nullptr;
c9317f21
TT
10144 }
10145 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10146 "RUST$ENUM$DISR") != 0)
10147 {
10148 /* Not a Rust enum. */
10149 return;
10150 }
10151 else
10152 {
10153 /* Found one. */
10154 break;
10155 }
10156 }
10157
10158 /* If we got here without a discriminant, then it's probably
10159 just a union. */
10160 if (disr_type == nullptr)
10161 return;
10162
10163 /* Smash this type to be a structure type. We have to do this
10164 because the type has already been recorded. */
10165 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10166
10167 /* Make a union to hold the variants. */
10168 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10169 struct type *union_type = alloc_type (objfile);
10170 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10171 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10172 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10173 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10174 TYPE_FIELDS (union_type)
10175 = (struct field *) TYPE_ZALLOC (union_type,
10176 (TYPE_NFIELDS (union_type)
10177 * sizeof (struct field)));
10178
10179 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10180 TYPE_NFIELDS (type) * sizeof (struct field));
10181
10182 /* Install the discriminant at index 0 in the union. */
10183 TYPE_FIELD (union_type, 0) = *disr_field;
10184 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10185 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10186
10187 /* Install the union in the outer struct type. */
10188 TYPE_FIELD_TYPE (type, 0) = union_type;
10189 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10190 TYPE_NFIELDS (type) = 1;
10191
10192 /* Set the size and offset of the union type. */
10193 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10194
10195 /* We need a way to find the correct discriminant given a
10196 variant name. For convenience we build a map here. */
10197 struct type *enum_type = FIELD_TYPE (*disr_field);
10198 std::unordered_map<std::string, ULONGEST> discriminant_map;
10199 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10200 {
10201 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10202 {
10203 const char *name
10204 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10205 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10206 }
10207 }
10208
10209 int n_fields = TYPE_NFIELDS (union_type);
10210 struct discriminant_info *disc
10211 = alloc_discriminant_info (union_type, 0, -1);
10212 /* Skip the discriminant here. */
10213 for (int i = 1; i < n_fields; ++i)
10214 {
10215 /* Find the final word in the name of this variant's type.
10216 That name can be used to look up the correct
10217 discriminant. */
10218 const char *variant_name
10219 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10220 i)));
10221
10222 auto iter = discriminant_map.find (variant_name);
10223 if (iter != discriminant_map.end ())
10224 disc->discriminants[i] = iter->second;
10225
bedda9ac 10226 /* Remove the discriminant field, if it exists. */
c9317f21 10227 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
bedda9ac
TT
10228 if (TYPE_NFIELDS (sub_type) > 0)
10229 {
10230 --TYPE_NFIELDS (sub_type);
10231 ++TYPE_FIELDS (sub_type);
10232 }
c9317f21
TT
10233 TYPE_FIELD_NAME (union_type, i) = variant_name;
10234 TYPE_NAME (sub_type)
10235 = rust_fully_qualify (&objfile->objfile_obstack,
10236 TYPE_NAME (type), variant_name);
10237 }
10238 }
10239}
10240
10241/* Rewrite some Rust unions to be structures with variants parts. */
10242
10243static void
10244rust_union_quirks (struct dwarf2_cu *cu)
10245{
10246 gdb_assert (cu->language == language_rust);
52941706
SM
10247 for (type *type_ : cu->rust_unions)
10248 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
2d79090e
TT
10249 /* We don't need this any more. */
10250 cu->rust_unions.clear ();
c9317f21
TT
10251}
10252
95554aad
TT
10253/* Return the symtab for PER_CU. This works properly regardless of
10254 whether we're using the index or psymtabs. */
10255
43f3e411
DE
10256static struct compunit_symtab *
10257get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad 10258{
ed2dc618 10259 return (per_cu->dwarf2_per_objfile->using_index
43f3e411
DE
10260 ? per_cu->v.quick->compunit_symtab
10261 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
10262}
10263
10264/* A helper function for computing the list of all symbol tables
10265 included by PER_CU. */
10266
10267static void
4c39bc03 10268recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 10269 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 10270 struct dwarf2_per_cu_data *per_cu,
43f3e411 10271 struct compunit_symtab *immediate_parent)
95554aad
TT
10272{
10273 void **slot;
43f3e411 10274 struct compunit_symtab *cust;
95554aad
TT
10275
10276 slot = htab_find_slot (all_children, per_cu, INSERT);
10277 if (*slot != NULL)
10278 {
10279 /* This inclusion and its children have been processed. */
10280 return;
10281 }
10282
10283 *slot = per_cu;
10284 /* Only add a CU if it has a symbol table. */
43f3e411
DE
10285 cust = get_compunit_symtab (per_cu);
10286 if (cust != NULL)
ec94af83
DE
10287 {
10288 /* If this is a type unit only add its symbol table if we haven't
10289 seen it yet (type unit per_cu's can share symtabs). */
10290 if (per_cu->is_debug_types)
10291 {
43f3e411 10292 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
10293 if (*slot == NULL)
10294 {
43f3e411 10295 *slot = cust;
4c39bc03 10296 result->push_back (cust);
43f3e411
DE
10297 if (cust->user == NULL)
10298 cust->user = immediate_parent;
ec94af83
DE
10299 }
10300 }
10301 else
f9125b6c 10302 {
4c39bc03 10303 result->push_back (cust);
43f3e411
DE
10304 if (cust->user == NULL)
10305 cust->user = immediate_parent;
f9125b6c 10306 }
ec94af83 10307 }
95554aad 10308
ae640021
AB
10309 if (!per_cu->imported_symtabs_empty ())
10310 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
10311 {
10312 recursively_compute_inclusions (result, all_children,
10313 all_type_symtabs, ptr, cust);
10314 }
95554aad
TT
10315}
10316
43f3e411 10317/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
10318 PER_CU. */
10319
10320static void
43f3e411 10321compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 10322{
f4dc4d17
DE
10323 gdb_assert (! per_cu->is_debug_types);
10324
ae640021 10325 if (!per_cu->imported_symtabs_empty ())
95554aad 10326 {
ae640021 10327 int len;
4c39bc03 10328 std::vector<compunit_symtab *> result_symtabs;
ec94af83 10329 htab_t all_children, all_type_symtabs;
43f3e411 10330 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
10331
10332 /* If we don't have a symtab, we can just skip this case. */
43f3e411 10333 if (cust == NULL)
95554aad
TT
10334 return;
10335
10336 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10337 NULL, xcalloc, xfree);
ec94af83
DE
10338 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10339 NULL, xcalloc, xfree);
95554aad 10340
ae640021 10341 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83
DE
10342 {
10343 recursively_compute_inclusions (&result_symtabs, all_children,
ae640021 10344 all_type_symtabs, ptr, cust);
ec94af83 10345 }
95554aad 10346
ec94af83 10347 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 10348 len = result_symtabs.size ();
43f3e411 10349 cust->includes
ed2dc618 10350 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 10351 struct compunit_symtab *, len + 1);
4c39bc03
TT
10352 memcpy (cust->includes, result_symtabs.data (),
10353 len * sizeof (compunit_symtab *));
43f3e411 10354 cust->includes[len] = NULL;
95554aad 10355
95554aad 10356 htab_delete (all_children);
ec94af83 10357 htab_delete (all_type_symtabs);
95554aad
TT
10358 }
10359}
10360
10361/* Compute the 'includes' field for the symtabs of all the CUs we just
10362 read. */
10363
10364static void
ed2dc618 10365process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 10366{
71b73764 10367 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
f4dc4d17
DE
10368 {
10369 if (! iter->is_debug_types)
43f3e411 10370 compute_compunit_symtab_includes (iter);
f4dc4d17 10371 }
95554aad 10372
c5d0225d 10373 dwarf2_per_objfile->just_read_cus.clear ();
95554aad
TT
10374}
10375
9cdd5dbd 10376/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
10377 already been loaded into memory. */
10378
10379static void
95554aad
TT
10380process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10381 enum language pretend_language)
10b3939b 10382{
10b3939b 10383 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10384 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10385 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 10386 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 10387 CORE_ADDR lowpc, highpc;
43f3e411 10388 struct compunit_symtab *cust;
10b3939b 10389 CORE_ADDR baseaddr;
4359dff1 10390 struct block *static_block;
3e29f34a 10391 CORE_ADDR addr;
10b3939b
DJ
10392
10393 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10394
c89b44cd
TT
10395 /* Clear the list here in case something was left over. */
10396 cu->method_list.clear ();
10b3939b 10397
95554aad
TT
10398 cu->language = pretend_language;
10399 cu->language_defn = language_def (cu->language);
10400
c906108c 10401 /* Do line number decoding in read_file_scope () */
10b3939b 10402 process_die (cu->dies, cu);
c906108c 10403
a766d390
DE
10404 /* For now fudge the Go package. */
10405 if (cu->language == language_go)
10406 fixup_go_packaging (cu);
10407
5f48f8f3 10408 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
10409 should be complete, and it should now be safe to compute all of the
10410 physnames. */
10411 compute_delayed_physnames (cu);
3da10d80 10412
c9317f21
TT
10413 if (cu->language == language_rust)
10414 rust_union_quirks (cu);
10415
fae299cd
DC
10416 /* Some compilers don't define a DW_AT_high_pc attribute for the
10417 compilation unit. If the DW_AT_high_pc is missing, synthesize
10418 it, by scanning the DIE's below the compilation unit. */
10b3939b 10419 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 10420
3e29f34a 10421 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 10422 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
10423
10424 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10425 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10426 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10427 addrmap to help ensure it has an accurate map of pc values belonging to
10428 this comp unit. */
10429 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10430
c24bdb02 10431 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
10432 SECT_OFF_TEXT (objfile),
10433 0);
c906108c 10434
43f3e411 10435 if (cust != NULL)
c906108c 10436 {
df15bd07 10437 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 10438
8be455d7
JK
10439 /* Set symtab language to language from DW_AT_language. If the
10440 compilation is from a C file generated by language preprocessors, do
10441 not set the language if it was already deduced by start_subfile. */
43f3e411 10442 if (!(cu->language == language_c
40e3ad0e 10443 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 10444 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
10445
10446 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10447 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
10448 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10449 there were bugs in prologue debug info, fixed later in GCC-4.5
10450 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
10451
10452 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10453 needed, it would be wrong due to missing DW_AT_producer there.
10454
10455 Still one can confuse GDB by using non-standard GCC compilation
10456 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 10457 */
ab260dad 10458 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 10459 cust->locations_valid = 1;
e0d00bc7
JK
10460
10461 if (gcc_4_minor >= 5)
43f3e411 10462 cust->epilogue_unwind_valid = 1;
96408a79 10463
43f3e411 10464 cust->call_site_htab = cu->call_site_htab;
c906108c 10465 }
9291a0cd
TT
10466
10467 if (dwarf2_per_objfile->using_index)
43f3e411 10468 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
10469 else
10470 {
10471 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10472 pst->compunit_symtab = cust;
9291a0cd
TT
10473 pst->readin = 1;
10474 }
c906108c 10475
95554aad 10476 /* Push it for inclusion processing later. */
c5d0225d 10477 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
804d2729
TT
10478
10479 /* Not needed any more. */
c24bdb02 10480 cu->reset_builder ();
f4dc4d17 10481}
45cfd468 10482
f4dc4d17
DE
10483/* Generate full symbol information for type unit PER_CU, whose DIEs have
10484 already been loaded into memory. */
10485
10486static void
10487process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10488 enum language pretend_language)
10489{
10490 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10491 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10492 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 10493 struct compunit_symtab *cust;
0186c6a7
DE
10494 struct signatured_type *sig_type;
10495
10496 gdb_assert (per_cu->is_debug_types);
10497 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 10498
c89b44cd
TT
10499 /* Clear the list here in case something was left over. */
10500 cu->method_list.clear ();
f4dc4d17 10501
f4dc4d17
DE
10502 cu->language = pretend_language;
10503 cu->language_defn = language_def (cu->language);
10504
10505 /* The symbol tables are set up in read_type_unit_scope. */
10506 process_die (cu->dies, cu);
10507
10508 /* For now fudge the Go package. */
10509 if (cu->language == language_go)
10510 fixup_go_packaging (cu);
10511
5f48f8f3 10512 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
10513 should be complete, and it should now be safe to compute all of the
10514 physnames. */
10515 compute_delayed_physnames (cu);
f4dc4d17 10516
c9317f21
TT
10517 if (cu->language == language_rust)
10518 rust_union_quirks (cu);
10519
f4dc4d17
DE
10520 /* TUs share symbol tables.
10521 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
10522 of it with end_expandable_symtab. Otherwise, complete the addition of
10523 this TU's symbols to the existing symtab. */
43f3e411 10524 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 10525 {
c24bdb02
KS
10526 buildsym_compunit *builder = cu->get_builder ();
10527 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
43f3e411 10528 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 10529
43f3e411 10530 if (cust != NULL)
f4dc4d17
DE
10531 {
10532 /* Set symtab language to language from DW_AT_language. If the
10533 compilation is from a C file generated by language preprocessors,
10534 do not set the language if it was already deduced by
10535 start_subfile. */
43f3e411
DE
10536 if (!(cu->language == language_c
10537 && COMPUNIT_FILETABS (cust)->language != language_c))
10538 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
10539 }
10540 }
10541 else
10542 {
c24bdb02 10543 cu->get_builder ()->augment_type_symtab ();
43f3e411 10544 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
10545 }
10546
10547 if (dwarf2_per_objfile->using_index)
43f3e411 10548 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
10549 else
10550 {
10551 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10552 pst->compunit_symtab = cust;
f4dc4d17 10553 pst->readin = 1;
45cfd468 10554 }
804d2729
TT
10555
10556 /* Not needed any more. */
c24bdb02 10557 cu->reset_builder ();
c906108c
SS
10558}
10559
95554aad
TT
10560/* Process an imported unit DIE. */
10561
10562static void
10563process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10564{
10565 struct attribute *attr;
10566
f4dc4d17
DE
10567 /* For now we don't handle imported units in type units. */
10568 if (cu->per_cu->is_debug_types)
10569 {
10570 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10571 " supported in type units [in module %s]"),
518817b3 10572 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
10573 }
10574
95554aad
TT
10575 attr = dwarf2_attr (die, DW_AT_import, cu);
10576 if (attr != NULL)
10577 {
9c541725
PA
10578 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10579 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10580 dwarf2_per_cu_data *per_cu
e3b94546 10581 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
518817b3 10582 cu->per_cu->dwarf2_per_objfile);
95554aad 10583
69d751e3 10584 /* If necessary, add it to the queue and load its DIEs. */
95554aad 10585 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 10586 load_full_comp_unit (per_cu, false, cu->language);
95554aad 10587
ae640021 10588 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
10589 }
10590}
10591
4c8aa72d
PA
10592/* RAII object that represents a process_die scope: i.e.,
10593 starts/finishes processing a DIE. */
10594class process_die_scope
adde2bff 10595{
4c8aa72d
PA
10596public:
10597 process_die_scope (die_info *die, dwarf2_cu *cu)
10598 : m_die (die), m_cu (cu)
10599 {
10600 /* We should only be processing DIEs not already in process. */
10601 gdb_assert (!m_die->in_process);
10602 m_die->in_process = true;
10603 }
8c3cb9fa 10604
4c8aa72d
PA
10605 ~process_die_scope ()
10606 {
10607 m_die->in_process = false;
10608
10609 /* If we're done processing the DIE for the CU that owns the line
10610 header, we don't need the line header anymore. */
10611 if (m_cu->line_header_die_owner == m_die)
10612 {
10613 delete m_cu->line_header;
10614 m_cu->line_header = NULL;
10615 m_cu->line_header_die_owner = NULL;
10616 }
10617 }
10618
10619private:
10620 die_info *m_die;
10621 dwarf2_cu *m_cu;
10622};
adde2bff 10623
c906108c
SS
10624/* Process a die and its children. */
10625
10626static void
e7c27a73 10627process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10628{
4c8aa72d 10629 process_die_scope scope (die, cu);
adde2bff 10630
c906108c
SS
10631 switch (die->tag)
10632 {
10633 case DW_TAG_padding:
10634 break;
10635 case DW_TAG_compile_unit:
95554aad 10636 case DW_TAG_partial_unit:
e7c27a73 10637 read_file_scope (die, cu);
c906108c 10638 break;
348e048f
DE
10639 case DW_TAG_type_unit:
10640 read_type_unit_scope (die, cu);
10641 break;
c906108c 10642 case DW_TAG_subprogram:
0a4b0913
AB
10643 /* Nested subprograms in Fortran get a prefix. */
10644 if (cu->language == language_fortran
10645 && die->parent != NULL
10646 && die->parent->tag == DW_TAG_subprogram)
10647 cu->processing_has_namespace_info = true;
10648 /* Fall through. */
c906108c 10649 case DW_TAG_inlined_subroutine:
edb3359d 10650 read_func_scope (die, cu);
c906108c
SS
10651 break;
10652 case DW_TAG_lexical_block:
14898363
L
10653 case DW_TAG_try_block:
10654 case DW_TAG_catch_block:
e7c27a73 10655 read_lexical_block_scope (die, cu);
c906108c 10656 break;
216f72a1 10657 case DW_TAG_call_site:
96408a79
SA
10658 case DW_TAG_GNU_call_site:
10659 read_call_site_scope (die, cu);
10660 break;
c906108c 10661 case DW_TAG_class_type:
680b30c7 10662 case DW_TAG_interface_type:
c906108c
SS
10663 case DW_TAG_structure_type:
10664 case DW_TAG_union_type:
134d01f1 10665 process_structure_scope (die, cu);
c906108c
SS
10666 break;
10667 case DW_TAG_enumeration_type:
134d01f1 10668 process_enumeration_scope (die, cu);
c906108c 10669 break;
134d01f1 10670
f792889a
DJ
10671 /* These dies have a type, but processing them does not create
10672 a symbol or recurse to process the children. Therefore we can
10673 read them on-demand through read_type_die. */
c906108c 10674 case DW_TAG_subroutine_type:
72019c9c 10675 case DW_TAG_set_type:
c906108c 10676 case DW_TAG_array_type:
c906108c 10677 case DW_TAG_pointer_type:
c906108c 10678 case DW_TAG_ptr_to_member_type:
c906108c 10679 case DW_TAG_reference_type:
4297a3f0 10680 case DW_TAG_rvalue_reference_type:
c906108c 10681 case DW_TAG_string_type:
c906108c 10682 break;
134d01f1 10683
c906108c 10684 case DW_TAG_base_type:
a02abb62 10685 case DW_TAG_subrange_type:
cb249c71 10686 case DW_TAG_typedef:
134d01f1
DJ
10687 /* Add a typedef symbol for the type definition, if it has a
10688 DW_AT_name. */
f792889a 10689 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10690 break;
c906108c 10691 case DW_TAG_common_block:
e7c27a73 10692 read_common_block (die, cu);
c906108c
SS
10693 break;
10694 case DW_TAG_common_inclusion:
10695 break;
d9fa45fe 10696 case DW_TAG_namespace:
9068261f 10697 cu->processing_has_namespace_info = true;
e7c27a73 10698 read_namespace (die, cu);
d9fa45fe 10699 break;
5d7cb8df 10700 case DW_TAG_module:
9068261f 10701 cu->processing_has_namespace_info = true;
5d7cb8df
JK
10702 read_module (die, cu);
10703 break;
d9fa45fe 10704 case DW_TAG_imported_declaration:
9068261f 10705 cu->processing_has_namespace_info = true;
74921315
KS
10706 if (read_namespace_alias (die, cu))
10707 break;
86a73007
TT
10708 /* The declaration is not a global namespace alias. */
10709 /* Fall through. */
d9fa45fe 10710 case DW_TAG_imported_module:
9068261f 10711 cu->processing_has_namespace_info = true;
27aa8d6a
SW
10712 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10713 || cu->language != language_fortran))
b98664d3 10714 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10715 dwarf_tag_name (die->tag));
10716 read_import_statement (die, cu);
d9fa45fe 10717 break;
95554aad
TT
10718
10719 case DW_TAG_imported_unit:
10720 process_imported_unit_die (die, cu);
10721 break;
10722
71a3c369
TT
10723 case DW_TAG_variable:
10724 read_variable (die, cu);
10725 break;
10726
c906108c 10727 default:
e7c27a73 10728 new_symbol (die, NULL, cu);
c906108c
SS
10729 break;
10730 }
10731}
ca69b9e6
DE
10732\f
10733/* DWARF name computation. */
c906108c 10734
94af9270
KS
10735/* A helper function for dwarf2_compute_name which determines whether DIE
10736 needs to have the name of the scope prepended to the name listed in the
10737 die. */
10738
10739static int
10740die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10741{
1c809c68
TT
10742 struct attribute *attr;
10743
94af9270
KS
10744 switch (die->tag)
10745 {
10746 case DW_TAG_namespace:
10747 case DW_TAG_typedef:
10748 case DW_TAG_class_type:
10749 case DW_TAG_interface_type:
10750 case DW_TAG_structure_type:
10751 case DW_TAG_union_type:
10752 case DW_TAG_enumeration_type:
10753 case DW_TAG_enumerator:
10754 case DW_TAG_subprogram:
08a76f8a 10755 case DW_TAG_inlined_subroutine:
94af9270 10756 case DW_TAG_member:
74921315 10757 case DW_TAG_imported_declaration:
94af9270
KS
10758 return 1;
10759
10760 case DW_TAG_variable:
c2b0a229 10761 case DW_TAG_constant:
94af9270
KS
10762 /* We only need to prefix "globally" visible variables. These include
10763 any variable marked with DW_AT_external or any variable that
10764 lives in a namespace. [Variables in anonymous namespaces
10765 require prefixing, but they are not DW_AT_external.] */
10766
10767 if (dwarf2_attr (die, DW_AT_specification, cu))
10768 {
10769 struct dwarf2_cu *spec_cu = cu;
9a619af0 10770
94af9270
KS
10771 return die_needs_namespace (die_specification (die, &spec_cu),
10772 spec_cu);
10773 }
10774
1c809c68 10775 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10776 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10777 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10778 return 0;
10779 /* A variable in a lexical block of some kind does not need a
10780 namespace, even though in C++ such variables may be external
10781 and have a mangled name. */
10782 if (die->parent->tag == DW_TAG_lexical_block
10783 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10784 || die->parent->tag == DW_TAG_catch_block
10785 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10786 return 0;
10787 return 1;
94af9270
KS
10788
10789 default:
10790 return 0;
10791 }
10792}
10793
73b9be8b
KS
10794/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10795 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10796 defined for the given DIE. */
10797
10798static struct attribute *
10799dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10800{
10801 struct attribute *attr;
10802
10803 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10804 if (attr == NULL)
10805 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10806
10807 return attr;
10808}
10809
10810/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10811 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10812 defined for the given DIE. */
10813
10814static const char *
10815dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10816{
10817 const char *linkage_name;
10818
10819 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10820 if (linkage_name == NULL)
10821 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10822
10823 return linkage_name;
10824}
10825
94af9270 10826/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10827 compute the physname for the object, which include a method's:
9c37b5ae 10828 - formal parameters (C++),
a766d390 10829 - receiver type (Go),
a766d390
DE
10830
10831 The term "physname" is a bit confusing.
10832 For C++, for example, it is the demangled name.
10833 For Go, for example, it's the mangled name.
94af9270 10834
af6b7be1
JB
10835 For Ada, return the DIE's linkage name rather than the fully qualified
10836 name. PHYSNAME is ignored..
10837
94af9270
KS
10838 The result is allocated on the objfile_obstack and canonicalized. */
10839
10840static const char *
15d034d0
TT
10841dwarf2_compute_name (const char *name,
10842 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10843 int physname)
10844{
518817b3 10845 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
bb5ed363 10846
94af9270
KS
10847 if (name == NULL)
10848 name = dwarf2_name (die, cu);
10849
2ee7123e
DE
10850 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10851 but otherwise compute it by typename_concat inside GDB.
10852 FIXME: Actually this is not really true, or at least not always true.
10853 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
5e2db402 10854 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10855 will set the demangled name to the result of dwarf2_full_name, and it is
10856 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10857 if (cu->language == language_ada
10858 || (cu->language == language_fortran && physname))
10859 {
10860 /* For Ada unit, we prefer the linkage name over the name, as
10861 the former contains the exported name, which the user expects
10862 to be able to reference. Ideally, we want the user to be able
10863 to reference this entity using either natural or linkage name,
10864 but we haven't started looking at this enhancement yet. */
73b9be8b 10865 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10866
2ee7123e
DE
10867 if (linkage_name != NULL)
10868 return linkage_name;
f55ee35c
JK
10869 }
10870
94af9270
KS
10871 /* These are the only languages we know how to qualify names in. */
10872 if (name != NULL
9c37b5ae 10873 && (cu->language == language_cplus
c44af4eb
TT
10874 || cu->language == language_fortran || cu->language == language_d
10875 || cu->language == language_rust))
94af9270
KS
10876 {
10877 if (die_needs_namespace (die, cu))
10878 {
0d5cff50 10879 const char *prefix;
34a68019 10880 const char *canonical_name = NULL;
94af9270 10881
d7e74731
PA
10882 string_file buf;
10883
94af9270 10884 prefix = determine_prefix (die, cu);
94af9270
KS
10885 if (*prefix != '\0')
10886 {
f55ee35c
JK
10887 char *prefixed_name = typename_concat (NULL, prefix, name,
10888 physname, cu);
9a619af0 10889
d7e74731 10890 buf.puts (prefixed_name);
94af9270
KS
10891 xfree (prefixed_name);
10892 }
10893 else
d7e74731 10894 buf.puts (name);
94af9270 10895
98bfdba5
PA
10896 /* Template parameters may be specified in the DIE's DW_AT_name, or
10897 as children with DW_TAG_template_type_param or
10898 DW_TAG_value_type_param. If the latter, add them to the name
10899 here. If the name already has template parameters, then
10900 skip this step; some versions of GCC emit both, and
10901 it is more efficient to use the pre-computed name.
10902
10903 Something to keep in mind about this process: it is very
10904 unlikely, or in some cases downright impossible, to produce
10905 something that will match the mangled name of a function.
10906 If the definition of the function has the same debug info,
10907 we should be able to match up with it anyway. But fallbacks
10908 using the minimal symbol, for instance to find a method
10909 implemented in a stripped copy of libstdc++, will not work.
10910 If we do not have debug info for the definition, we will have to
10911 match them up some other way.
10912
10913 When we do name matching there is a related problem with function
10914 templates; two instantiated function templates are allowed to
10915 differ only by their return types, which we do not add here. */
10916
10917 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10918 {
10919 struct attribute *attr;
10920 struct die_info *child;
10921 int first = 1;
10922
10923 die->building_fullname = 1;
10924
10925 for (child = die->child; child != NULL; child = child->sibling)
10926 {
10927 struct type *type;
12df843f 10928 LONGEST value;
d521ce57 10929 const gdb_byte *bytes;
98bfdba5
PA
10930 struct dwarf2_locexpr_baton *baton;
10931 struct value *v;
10932
10933 if (child->tag != DW_TAG_template_type_param
10934 && child->tag != DW_TAG_template_value_param)
10935 continue;
10936
10937 if (first)
10938 {
d7e74731 10939 buf.puts ("<");
98bfdba5
PA
10940 first = 0;
10941 }
10942 else
d7e74731 10943 buf.puts (", ");
98bfdba5
PA
10944
10945 attr = dwarf2_attr (child, DW_AT_type, cu);
10946 if (attr == NULL)
10947 {
b98664d3 10948 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10949 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10950 continue;
10951 }
10952 type = die_type (child, cu);
10953
10954 if (child->tag == DW_TAG_template_type_param)
10955 {
c1ec8cea
TT
10956 c_print_type (type, "", &buf, -1, 0, cu->language,
10957 &type_print_raw_options);
98bfdba5
PA
10958 continue;
10959 }
10960
10961 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10962 if (attr == NULL)
10963 {
b98664d3 10964 complaint (_("template parameter missing "
3e43a32a 10965 "DW_AT_const_value"));
d7e74731 10966 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10967 continue;
10968 }
10969
10970 dwarf2_const_value_attr (attr, type, name,
10971 &cu->comp_unit_obstack, cu,
10972 &value, &bytes, &baton);
10973
10974 if (TYPE_NOSIGN (type))
10975 /* GDB prints characters as NUMBER 'CHAR'. If that's
10976 changed, this can use value_print instead. */
d7e74731 10977 c_printchar (value, type, &buf);
98bfdba5
PA
10978 else
10979 {
10980 struct value_print_options opts;
10981
10982 if (baton != NULL)
10983 v = dwarf2_evaluate_loc_desc (type, NULL,
10984 baton->data,
10985 baton->size,
10986 baton->per_cu);
10987 else if (bytes != NULL)
10988 {
10989 v = allocate_value (type);
10990 memcpy (value_contents_writeable (v), bytes,
10991 TYPE_LENGTH (type));
10992 }
10993 else
10994 v = value_from_longest (type, value);
10995
3e43a32a
MS
10996 /* Specify decimal so that we do not depend on
10997 the radix. */
98bfdba5
PA
10998 get_formatted_print_options (&opts, 'd');
10999 opts.raw = 1;
d7e74731 11000 value_print (v, &buf, &opts);
98bfdba5 11001 release_value (v);
98bfdba5
PA
11002 }
11003 }
11004
11005 die->building_fullname = 0;
11006
11007 if (!first)
11008 {
11009 /* Close the argument list, with a space if necessary
11010 (nested templates). */
d7e74731
PA
11011 if (!buf.empty () && buf.string ().back () == '>')
11012 buf.puts (" >");
98bfdba5 11013 else
d7e74731 11014 buf.puts (">");
98bfdba5
PA
11015 }
11016 }
11017
9c37b5ae 11018 /* For C++ methods, append formal parameter type
94af9270 11019 information, if PHYSNAME. */
6e70227d 11020
94af9270 11021 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 11022 && cu->language == language_cplus)
94af9270
KS
11023 {
11024 struct type *type = read_type_die (die, cu);
11025
d7e74731 11026 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 11027 &type_print_raw_options);
94af9270 11028
9c37b5ae 11029 if (cu->language == language_cplus)
94af9270 11030 {
60430eff
DJ
11031 /* Assume that an artificial first parameter is
11032 "this", but do not crash if it is not. RealView
11033 marks unnamed (and thus unused) parameters as
11034 artificial; there is no way to differentiate
11035 the two cases. */
94af9270
KS
11036 if (TYPE_NFIELDS (type) > 0
11037 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 11038 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
11039 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
11040 0))))
d7e74731 11041 buf.puts (" const");
94af9270
KS
11042 }
11043 }
11044
d7e74731 11045 const std::string &intermediate_name = buf.string ();
94af9270
KS
11046
11047 if (cu->language == language_cplus)
34a68019 11048 canonical_name
322a8516 11049 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
11050 &objfile->per_bfd->storage_obstack);
11051
11052 /* If we only computed INTERMEDIATE_NAME, or if
11053 INTERMEDIATE_NAME is already canonical, then we need to
11054 copy it to the appropriate obstack. */
322a8516 11055 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
efba19b0
TT
11056 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
11057 intermediate_name);
34a68019
TT
11058 else
11059 name = canonical_name;
94af9270
KS
11060 }
11061 }
11062
11063 return name;
11064}
11065
0114d602
DJ
11066/* Return the fully qualified name of DIE, based on its DW_AT_name.
11067 If scope qualifiers are appropriate they will be added. The result
34a68019 11068 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
11069 not have a name. NAME may either be from a previous call to
11070 dwarf2_name or NULL.
11071
9c37b5ae 11072 The output string will be canonicalized (if C++). */
0114d602
DJ
11073
11074static const char *
15d034d0 11075dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 11076{
94af9270
KS
11077 return dwarf2_compute_name (name, die, cu, 0);
11078}
0114d602 11079
94af9270
KS
11080/* Construct a physname for the given DIE in CU. NAME may either be
11081 from a previous call to dwarf2_name or NULL. The result will be
11082 allocated on the objfile_objstack or NULL if the DIE does not have a
11083 name.
0114d602 11084
9c37b5ae 11085 The output string will be canonicalized (if C++). */
0114d602 11086
94af9270 11087static const char *
15d034d0 11088dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 11089{
518817b3 11090 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
900e11f9 11091 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
11092 int need_copy = 1;
11093
11094 /* In this case dwarf2_compute_name is just a shortcut not building anything
11095 on its own. */
11096 if (!die_needs_namespace (die, cu))
11097 return dwarf2_compute_name (name, die, cu, 1);
11098
73b9be8b 11099 mangled = dw2_linkage_name (die, cu);
900e11f9 11100
e98c9e7c
TT
11101 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11102 See https://github.com/rust-lang/rust/issues/32925. */
11103 if (cu->language == language_rust && mangled != NULL
11104 && strchr (mangled, '{') != NULL)
11105 mangled = NULL;
11106
900e11f9
JK
11107 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11108 has computed. */
791afaa2 11109 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 11110 if (mangled != NULL)
900e11f9 11111 {
900e11f9 11112
59cc4834
JB
11113 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
11114 {
11115 /* Do nothing (do not demangle the symbol name). */
11116 }
11117 else if (cu->language == language_go)
a766d390 11118 {
5e2db402
TT
11119 /* This is a lie, but we already lie to the caller new_symbol.
11120 new_symbol assumes we return the mangled name.
a766d390 11121 This just undoes that lie until things are cleaned up. */
a766d390
DE
11122 }
11123 else
11124 {
0eb876f5
JB
11125 /* Use DMGL_RET_DROP for C++ template functions to suppress
11126 their return type. It is easier for GDB users to search
11127 for such functions as `name(params)' than `long name(params)'.
11128 In such case the minimal symbol names do not match the full
11129 symbol names but for template functions there is never a need
11130 to look up their definition from their declaration so
11131 the only disadvantage remains the minimal symbol variant
11132 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
11133 demangled.reset (gdb_demangle (mangled,
11134 (DMGL_PARAMS | DMGL_ANSI
11135 | DMGL_RET_DROP)));
a766d390 11136 }
900e11f9 11137 if (demangled)
791afaa2 11138 canon = demangled.get ();
900e11f9
JK
11139 else
11140 {
11141 canon = mangled;
11142 need_copy = 0;
11143 }
11144 }
11145
11146 if (canon == NULL || check_physname)
11147 {
11148 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11149
11150 if (canon != NULL && strcmp (physname, canon) != 0)
11151 {
11152 /* It may not mean a bug in GDB. The compiler could also
11153 compute DW_AT_linkage_name incorrectly. But in such case
11154 GDB would need to be bug-to-bug compatible. */
11155
b98664d3 11156 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
11157 "(from linkage <%s>) - DIE at %s [in module %s]"),
11158 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 11159 objfile_name (objfile));
900e11f9
JK
11160
11161 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11162 is available here - over computed PHYSNAME. It is safer
11163 against both buggy GDB and buggy compilers. */
11164
11165 retval = canon;
11166 }
11167 else
11168 {
11169 retval = physname;
11170 need_copy = 0;
11171 }
11172 }
11173 else
11174 retval = canon;
11175
11176 if (need_copy)
021887d8 11177 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
900e11f9 11178
900e11f9 11179 return retval;
0114d602
DJ
11180}
11181
74921315
KS
11182/* Inspect DIE in CU for a namespace alias. If one exists, record
11183 a new symbol for it.
11184
11185 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11186
11187static int
11188read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11189{
11190 struct attribute *attr;
11191
11192 /* If the die does not have a name, this is not a namespace
11193 alias. */
11194 attr = dwarf2_attr (die, DW_AT_name, cu);
11195 if (attr != NULL)
11196 {
11197 int num;
11198 struct die_info *d = die;
11199 struct dwarf2_cu *imported_cu = cu;
11200
11201 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11202 keep inspecting DIEs until we hit the underlying import. */
11203#define MAX_NESTED_IMPORTED_DECLARATIONS 100
11204 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11205 {
11206 attr = dwarf2_attr (d, DW_AT_import, cu);
11207 if (attr == NULL)
11208 break;
11209
11210 d = follow_die_ref (d, attr, &imported_cu);
11211 if (d->tag != DW_TAG_imported_declaration)
11212 break;
11213 }
11214
11215 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11216 {
b98664d3 11217 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 11218 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
11219 return 0;
11220 }
11221
11222 if (attr != NULL)
11223 {
11224 struct type *type;
9c541725 11225 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 11226
9c541725 11227 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
11228 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11229 {
11230 /* This declaration is a global namespace alias. Add
11231 a symbol for it whose type is the aliased namespace. */
11232 new_symbol (die, type, cu);
11233 return 1;
11234 }
11235 }
11236 }
11237
11238 return 0;
11239}
11240
22cee43f 11241/* Return the using directives repository (global or local?) to use in the
804d2729 11242 current context for CU.
22cee43f
PMR
11243
11244 For Ada, imported declarations can materialize renamings, which *may* be
11245 global. However it is impossible (for now?) in DWARF to distinguish
11246 "external" imported declarations and "static" ones. As all imported
11247 declarations seem to be static in all other languages, make them all CU-wide
11248 global only in Ada. */
11249
11250static struct using_direct **
804d2729 11251using_directives (struct dwarf2_cu *cu)
22cee43f 11252{
c24bdb02
KS
11253 if (cu->language == language_ada
11254 && cu->get_builder ()->outermost_context_p ())
11255 return cu->get_builder ()->get_global_using_directives ();
22cee43f 11256 else
c24bdb02 11257 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
11258}
11259
27aa8d6a
SW
11260/* Read the import statement specified by the given die and record it. */
11261
11262static void
11263read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11264{
518817b3 11265 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
27aa8d6a 11266 struct attribute *import_attr;
32019081 11267 struct die_info *imported_die, *child_die;
de4affc9 11268 struct dwarf2_cu *imported_cu;
27aa8d6a 11269 const char *imported_name;
794684b6 11270 const char *imported_name_prefix;
13387711
SW
11271 const char *canonical_name;
11272 const char *import_alias;
11273 const char *imported_declaration = NULL;
794684b6 11274 const char *import_prefix;
eb1e02fd 11275 std::vector<const char *> excludes;
13387711 11276
27aa8d6a
SW
11277 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11278 if (import_attr == NULL)
11279 {
b98664d3 11280 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
11281 dwarf_tag_name (die->tag));
11282 return;
11283 }
11284
de4affc9
CC
11285 imported_cu = cu;
11286 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11287 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
11288 if (imported_name == NULL)
11289 {
11290 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11291
11292 The import in the following code:
11293 namespace A
11294 {
11295 typedef int B;
11296 }
11297
11298 int main ()
11299 {
11300 using A::B;
11301 B b;
11302 return b;
11303 }
11304
11305 ...
11306 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11307 <52> DW_AT_decl_file : 1
11308 <53> DW_AT_decl_line : 6
11309 <54> DW_AT_import : <0x75>
11310 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11311 <59> DW_AT_name : B
11312 <5b> DW_AT_decl_file : 1
11313 <5c> DW_AT_decl_line : 2
11314 <5d> DW_AT_type : <0x6e>
11315 ...
11316 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11317 <76> DW_AT_byte_size : 4
11318 <77> DW_AT_encoding : 5 (signed)
11319
11320 imports the wrong die ( 0x75 instead of 0x58 ).
11321 This case will be ignored until the gcc bug is fixed. */
11322 return;
11323 }
11324
82856980
SW
11325 /* Figure out the local name after import. */
11326 import_alias = dwarf2_name (die, cu);
27aa8d6a 11327
794684b6
SW
11328 /* Figure out where the statement is being imported to. */
11329 import_prefix = determine_prefix (die, cu);
11330
11331 /* Figure out what the scope of the imported die is and prepend it
11332 to the name of the imported die. */
de4affc9 11333 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 11334
f55ee35c
JK
11335 if (imported_die->tag != DW_TAG_namespace
11336 && imported_die->tag != DW_TAG_module)
794684b6 11337 {
13387711
SW
11338 imported_declaration = imported_name;
11339 canonical_name = imported_name_prefix;
794684b6 11340 }
13387711 11341 else if (strlen (imported_name_prefix) > 0)
12aaed36 11342 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
11343 imported_name_prefix,
11344 (cu->language == language_d ? "." : "::"),
11345 imported_name, (char *) NULL);
13387711
SW
11346 else
11347 canonical_name = imported_name;
794684b6 11348
32019081
JK
11349 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11350 for (child_die = die->child; child_die && child_die->tag;
11351 child_die = sibling_die (child_die))
11352 {
11353 /* DWARF-4: A Fortran use statement with a “rename list” may be
11354 represented by an imported module entry with an import attribute
11355 referring to the module and owned entries corresponding to those
11356 entities that are renamed as part of being imported. */
11357
11358 if (child_die->tag != DW_TAG_imported_declaration)
11359 {
b98664d3 11360 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
11361 "- DIE at %s [in module %s]"),
11362 sect_offset_str (child_die->sect_off),
11363 objfile_name (objfile));
32019081
JK
11364 continue;
11365 }
11366
11367 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11368 if (import_attr == NULL)
11369 {
b98664d3 11370 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
11371 dwarf_tag_name (child_die->tag));
11372 continue;
11373 }
11374
11375 imported_cu = cu;
11376 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11377 &imported_cu);
11378 imported_name = dwarf2_name (imported_die, imported_cu);
11379 if (imported_name == NULL)
11380 {
b98664d3 11381 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
11382 "imported name - DIE at %s [in module %s]"),
11383 sect_offset_str (child_die->sect_off),
11384 objfile_name (objfile));
32019081
JK
11385 continue;
11386 }
11387
eb1e02fd 11388 excludes.push_back (imported_name);
32019081
JK
11389
11390 process_die (child_die, cu);
11391 }
11392
804d2729 11393 add_using_directive (using_directives (cu),
22cee43f
PMR
11394 import_prefix,
11395 canonical_name,
11396 import_alias,
11397 imported_declaration,
11398 excludes,
11399 0,
11400 &objfile->objfile_obstack);
27aa8d6a
SW
11401}
11402
5230b05a
WT
11403/* ICC<14 does not output the required DW_AT_declaration on incomplete
11404 types, but gives them a size of zero. Starting with version 14,
11405 ICC is compatible with GCC. */
11406
9068261f 11407static bool
5230b05a
WT
11408producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11409{
11410 if (!cu->checked_producer)
11411 check_producer (cu);
11412
11413 return cu->producer_is_icc_lt_14;
11414}
11415
eb77c9df
AB
11416/* ICC generates a DW_AT_type for C void functions. This was observed on
11417 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11418 which says that void functions should not have a DW_AT_type. */
11419
11420static bool
11421producer_is_icc (struct dwarf2_cu *cu)
11422{
11423 if (!cu->checked_producer)
11424 check_producer (cu);
11425
11426 return cu->producer_is_icc;
11427}
11428
1b80a9fa
JK
11429/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11430 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11431 this, it was first present in GCC release 4.3.0. */
11432
9068261f 11433static bool
1b80a9fa
JK
11434producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11435{
11436 if (!cu->checked_producer)
11437 check_producer (cu);
11438
11439 return cu->producer_is_gcc_lt_4_3;
11440}
11441
d721ba37
PA
11442static file_and_directory
11443find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 11444{
d721ba37
PA
11445 file_and_directory res;
11446
9291a0cd
TT
11447 /* Find the filename. Do not use dwarf2_name here, since the filename
11448 is not a source language identifier. */
d721ba37
PA
11449 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11450 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 11451
d721ba37
PA
11452 if (res.comp_dir == NULL
11453 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11454 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 11455 {
d721ba37
PA
11456 res.comp_dir_storage = ldirname (res.name);
11457 if (!res.comp_dir_storage.empty ())
11458 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 11459 }
d721ba37 11460 if (res.comp_dir != NULL)
9291a0cd
TT
11461 {
11462 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11463 directory, get rid of it. */
d721ba37 11464 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 11465
d721ba37
PA
11466 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11467 res.comp_dir = cp + 1;
9291a0cd
TT
11468 }
11469
d721ba37
PA
11470 if (res.name == NULL)
11471 res.name = "<unknown>";
11472
11473 return res;
9291a0cd
TT
11474}
11475
f4dc4d17
DE
11476/* Handle DW_AT_stmt_list for a compilation unit.
11477 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
11478 COMP_DIR is the compilation directory. LOWPC is passed to
11479 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
11480
11481static void
11482handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 11483 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 11484{
518817b3
SM
11485 struct dwarf2_per_objfile *dwarf2_per_objfile
11486 = cu->per_cu->dwarf2_per_objfile;
527f3840 11487 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 11488 struct attribute *attr;
527f3840
JK
11489 struct line_header line_header_local;
11490 hashval_t line_header_local_hash;
527f3840
JK
11491 void **slot;
11492 int decode_mapping;
2ab95328 11493
f4dc4d17
DE
11494 gdb_assert (! cu->per_cu->is_debug_types);
11495
2ab95328 11496 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
11497 if (attr == NULL)
11498 return;
11499
9c541725 11500 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
11501
11502 /* The line header hash table is only created if needed (it exists to
11503 prevent redundant reading of the line table for partial_units).
11504 If we're given a partial_unit, we'll need it. If we're given a
11505 compile_unit, then use the line header hash table if it's already
11506 created, but don't create one just yet. */
11507
11508 if (dwarf2_per_objfile->line_header_hash == NULL
11509 && die->tag == DW_TAG_partial_unit)
2ab95328 11510 {
527f3840
JK
11511 dwarf2_per_objfile->line_header_hash
11512 = htab_create_alloc_ex (127, line_header_hash_voidp,
11513 line_header_eq_voidp,
11514 free_line_header_voidp,
11515 &objfile->objfile_obstack,
11516 hashtab_obstack_allocate,
11517 dummy_obstack_deallocate);
11518 }
2ab95328 11519
9c541725 11520 line_header_local.sect_off = line_offset;
527f3840
JK
11521 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11522 line_header_local_hash = line_header_hash (&line_header_local);
11523 if (dwarf2_per_objfile->line_header_hash != NULL)
11524 {
11525 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11526 &line_header_local,
11527 line_header_local_hash, NO_INSERT);
11528
11529 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11530 is not present in *SLOT (since if there is something in *SLOT then
11531 it will be for a partial_unit). */
11532 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 11533 {
527f3840 11534 gdb_assert (*slot != NULL);
9a3c8263 11535 cu->line_header = (struct line_header *) *slot;
527f3840 11536 return;
dee91e82 11537 }
2ab95328 11538 }
527f3840
JK
11539
11540 /* dwarf_decode_line_header does not yet provide sufficient information.
11541 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
11542 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11543 if (lh == NULL)
527f3840 11544 return;
4c8aa72d
PA
11545
11546 cu->line_header = lh.release ();
11547 cu->line_header_die_owner = die;
527f3840
JK
11548
11549 if (dwarf2_per_objfile->line_header_hash == NULL)
11550 slot = NULL;
11551 else
11552 {
11553 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11554 &line_header_local,
11555 line_header_local_hash, INSERT);
11556 gdb_assert (slot != NULL);
11557 }
11558 if (slot != NULL && *slot == NULL)
11559 {
11560 /* This newly decoded line number information unit will be owned
11561 by line_header_hash hash table. */
11562 *slot = cu->line_header;
4c8aa72d 11563 cu->line_header_die_owner = NULL;
527f3840
JK
11564 }
11565 else
11566 {
11567 /* We cannot free any current entry in (*slot) as that struct line_header
11568 may be already used by multiple CUs. Create only temporary decoded
11569 line_header for this CU - it may happen at most once for each line
11570 number information unit. And if we're not using line_header_hash
11571 then this is what we want as well. */
11572 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
11573 }
11574 decode_mapping = (die->tag != DW_TAG_partial_unit);
11575 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11576 decode_mapping);
fff8551c 11577
2ab95328
TT
11578}
11579
95554aad 11580/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 11581
c906108c 11582static void
e7c27a73 11583read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11584{
518817b3
SM
11585 struct dwarf2_per_objfile *dwarf2_per_objfile
11586 = cu->per_cu->dwarf2_per_objfile;
dee91e82 11587 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 11588 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 11589 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11590 CORE_ADDR highpc = ((CORE_ADDR) 0);
11591 struct attribute *attr;
c906108c 11592 struct die_info *child_die;
e142c38c 11593 CORE_ADDR baseaddr;
6e70227d 11594
380618d6 11595 prepare_one_comp_unit (cu, die, cu->language);
e142c38c 11596 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11597
fae299cd 11598 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11599
11600 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11601 from finish_block. */
2acceee2 11602 if (lowpc == ((CORE_ADDR) -1))
c906108c 11603 lowpc = highpc;
3e29f34a 11604 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 11605
d721ba37 11606 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 11607
f4b8a18d
KW
11608 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11609 standardised yet. As a workaround for the language detection we fall
11610 back to the DW_AT_producer string. */
11611 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11612 cu->language = language_opencl;
11613
3019eac3
DE
11614 /* Similar hack for Go. */
11615 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11616 set_cu_language (DW_LANG_Go, cu);
11617
c24bdb02 11618 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11619
11620 /* Decode line number information if present. We do this before
11621 processing child DIEs, so that the line header table is available
11622 for DW_AT_decl_file. */
d721ba37 11623 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11624
11625 /* Process all dies in compilation unit. */
11626 if (die->child != NULL)
11627 {
11628 child_die = die->child;
11629 while (child_die && child_die->tag)
11630 {
11631 process_die (child_die, cu);
11632 child_die = sibling_die (child_die);
11633 }
11634 }
11635
11636 /* Decode macro information, if present. Dwarf 2 macro information
11637 refers to information in the line number info statement program
11638 header, so we can only read it if we've read the header
11639 successfully. */
0af92d60
JK
11640 attr = dwarf2_attr (die, DW_AT_macros, cu);
11641 if (attr == NULL)
11642 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
11643 if (attr && cu->line_header)
11644 {
11645 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 11646 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11647
43f3e411 11648 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
11649 }
11650 else
11651 {
11652 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11653 if (attr && cu->line_header)
11654 {
11655 unsigned int macro_offset = DW_UNSND (attr);
11656
43f3e411 11657 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11658 }
11659 }
3019eac3
DE
11660}
11661
c24bdb02
KS
11662void
11663dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 11664{
f4dc4d17
DE
11665 struct type_unit_group *tu_group;
11666 int first_time;
3019eac3 11667 struct attribute *attr;
9c541725 11668 unsigned int i;
0186c6a7 11669 struct signatured_type *sig_type;
3019eac3 11670
f4dc4d17 11671 gdb_assert (per_cu->is_debug_types);
0186c6a7 11672 sig_type = (struct signatured_type *) per_cu;
3019eac3 11673
c24bdb02 11674 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 11675
f4dc4d17 11676 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11677 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 11678 if (sig_type->type_unit_group == NULL)
c24bdb02 11679 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 11680 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11681
11682 /* If we've already processed this stmt_list there's no real need to
11683 do it again, we could fake it and just recreate the part we need
11684 (file name,index -> symtab mapping). If data shows this optimization
11685 is useful we can do it then. */
43f3e411 11686 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
11687
11688 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11689 debug info. */
fff8551c 11690 line_header_up lh;
f4dc4d17 11691 if (attr != NULL)
3019eac3 11692 {
9c541725 11693 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
c24bdb02 11694 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
11695 }
11696 if (lh == NULL)
11697 {
11698 if (first_time)
c24bdb02 11699 start_symtab ("", NULL, 0);
f4dc4d17
DE
11700 else
11701 {
11702 gdb_assert (tu_group->symtabs == NULL);
c24bdb02 11703 gdb_assert (m_builder == nullptr);
804d2729 11704 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11705 m_builder.reset (new struct buildsym_compunit
11706 (COMPUNIT_OBJFILE (cust), "",
11707 COMPUNIT_DIRNAME (cust),
11708 compunit_language (cust),
11709 0, cust));
f4dc4d17 11710 }
f4dc4d17 11711 return;
3019eac3
DE
11712 }
11713
c24bdb02
KS
11714 line_header = lh.release ();
11715 line_header_die_owner = die;
3019eac3 11716
f4dc4d17
DE
11717 if (first_time)
11718 {
c24bdb02 11719 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 11720
1fd60fc0
DE
11721 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11722 still initializing it, and our caller (a few levels up)
11723 process_full_type_unit still needs to know if this is the first
11724 time. */
11725
7ba99d21 11726 tu_group->num_symtabs = line_header->file_names_size ();
4c8aa72d 11727 tu_group->symtabs = XNEWVEC (struct symtab *,
7ba99d21 11728 line_header->file_names_size ());
3019eac3 11729
7ba99d21
AT
11730 auto &file_names = line_header->file_names ();
11731 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11732 {
7ba99d21 11733 file_entry &fe = file_names[i];
c24bdb02
KS
11734 dwarf2_start_subfile (this, fe.name,
11735 fe.include_dir (line_header));
11736 buildsym_compunit *b = get_builder ();
11737 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 11738 {
4c8aa72d
PA
11739 /* NOTE: start_subfile will recognize when it's been
11740 passed a file it has already seen. So we can't
11741 assume there's a simple mapping from
11742 cu->line_header->file_names to subfiles, plus
11743 cu->line_header->file_names may contain dups. */
c24bdb02
KS
11744 b->get_current_subfile ()->symtab
11745 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11746 }
11747
c24bdb02 11748 fe.symtab = b->get_current_subfile ()->symtab;
8c43009f 11749 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
11750 }
11751 }
11752 else
3019eac3 11753 {
c24bdb02 11754 gdb_assert (m_builder == nullptr);
804d2729 11755 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11756 m_builder.reset (new struct buildsym_compunit
11757 (COMPUNIT_OBJFILE (cust), "",
11758 COMPUNIT_DIRNAME (cust),
11759 compunit_language (cust),
11760 0, cust));
f4dc4d17 11761
7ba99d21
AT
11762 auto &file_names = line_header->file_names ();
11763 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11764 {
7ba99d21 11765 file_entry &fe = file_names[i];
4c8aa72d 11766 fe.symtab = tu_group->symtabs[i];
f4dc4d17 11767 }
3019eac3
DE
11768 }
11769
f4dc4d17
DE
11770 /* The main symtab is allocated last. Type units don't have DW_AT_name
11771 so they don't have a "real" (so to speak) symtab anyway.
11772 There is later code that will assign the main symtab to all symbols
11773 that don't have one. We need to handle the case of a symbol with a
11774 missing symtab (DW_AT_decl_file) anyway. */
11775}
3019eac3 11776
f4dc4d17
DE
11777/* Process DW_TAG_type_unit.
11778 For TUs we want to skip the first top level sibling if it's not the
11779 actual type being defined by this TU. In this case the first top
11780 level sibling is there to provide context only. */
3019eac3 11781
f4dc4d17
DE
11782static void
11783read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11784{
11785 struct die_info *child_die;
3019eac3 11786
f4dc4d17
DE
11787 prepare_one_comp_unit (cu, die, language_minimal);
11788
11789 /* Initialize (or reinitialize) the machinery for building symtabs.
11790 We do this before processing child DIEs, so that the line header table
11791 is available for DW_AT_decl_file. */
c24bdb02 11792 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11793
11794 if (die->child != NULL)
11795 {
11796 child_die = die->child;
11797 while (child_die && child_die->tag)
11798 {
11799 process_die (child_die, cu);
11800 child_die = sibling_die (child_die);
11801 }
11802 }
3019eac3
DE
11803}
11804\f
80626a55
DE
11805/* DWO/DWP files.
11806
11807 http://gcc.gnu.org/wiki/DebugFission
11808 http://gcc.gnu.org/wiki/DebugFissionDWP
11809
11810 To simplify handling of both DWO files ("object" files with the DWARF info)
11811 and DWP files (a file with the DWOs packaged up into one file), we treat
11812 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11813
11814static hashval_t
11815hash_dwo_file (const void *item)
11816{
9a3c8263 11817 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11818 hashval_t hash;
3019eac3 11819
a2ce51a0
DE
11820 hash = htab_hash_string (dwo_file->dwo_name);
11821 if (dwo_file->comp_dir != NULL)
11822 hash += htab_hash_string (dwo_file->comp_dir);
11823 return hash;
3019eac3
DE
11824}
11825
11826static int
11827eq_dwo_file (const void *item_lhs, const void *item_rhs)
11828{
9a3c8263
SM
11829 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11830 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11831
a2ce51a0
DE
11832 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11833 return 0;
11834 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11835 return lhs->comp_dir == rhs->comp_dir;
11836 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11837}
11838
11839/* Allocate a hash table for DWO files. */
11840
51ac9db5 11841static htab_up
ed2dc618 11842allocate_dwo_file_hash_table (struct objfile *objfile)
3019eac3 11843{
51ac9db5
SM
11844 auto delete_dwo_file = [] (void *item)
11845 {
11846 struct dwo_file *dwo_file = (struct dwo_file *) item;
11847
11848 delete dwo_file;
11849 };
11850
11851 return htab_up (htab_create_alloc_ex (41,
11852 hash_dwo_file,
11853 eq_dwo_file,
11854 delete_dwo_file,
11855 &objfile->objfile_obstack,
11856 hashtab_obstack_allocate,
11857 dummy_obstack_deallocate));
3019eac3
DE
11858}
11859
80626a55
DE
11860/* Lookup DWO file DWO_NAME. */
11861
11862static void **
ed2dc618
SM
11863lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11864 const char *dwo_name,
11865 const char *comp_dir)
80626a55
DE
11866{
11867 struct dwo_file find_entry;
11868 void **slot;
11869
11870 if (dwarf2_per_objfile->dwo_files == NULL)
ed2dc618
SM
11871 dwarf2_per_objfile->dwo_files
11872 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
80626a55 11873
0ac5b59e
DE
11874 find_entry.dwo_name = dwo_name;
11875 find_entry.comp_dir = comp_dir;
51ac9db5
SM
11876 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11877 INSERT);
80626a55
DE
11878
11879 return slot;
11880}
11881
3019eac3
DE
11882static hashval_t
11883hash_dwo_unit (const void *item)
11884{
9a3c8263 11885 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11886
11887 /* This drops the top 32 bits of the id, but is ok for a hash. */
11888 return dwo_unit->signature;
11889}
11890
11891static int
11892eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11893{
9a3c8263
SM
11894 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11895 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11896
11897 /* The signature is assumed to be unique within the DWO file.
11898 So while object file CU dwo_id's always have the value zero,
11899 that's OK, assuming each object file DWO file has only one CU,
11900 and that's the rule for now. */
11901 return lhs->signature == rhs->signature;
11902}
11903
11904/* Allocate a hash table for DWO CUs,TUs.
11905 There is one of these tables for each of CUs,TUs for each DWO file. */
11906
11907static htab_t
11908allocate_dwo_unit_table (struct objfile *objfile)
11909{
11910 /* Start out with a pretty small number.
11911 Generally DWO files contain only one CU and maybe some TUs. */
11912 return htab_create_alloc_ex (3,
11913 hash_dwo_unit,
11914 eq_dwo_unit,
11915 NULL,
11916 &objfile->objfile_obstack,
11917 hashtab_obstack_allocate,
11918 dummy_obstack_deallocate);
11919}
11920
80626a55 11921/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 11922
19c3d4c9 11923struct create_dwo_cu_data
3019eac3
DE
11924{
11925 struct dwo_file *dwo_file;
19c3d4c9 11926 struct dwo_unit dwo_unit;
3019eac3
DE
11927};
11928
19c3d4c9 11929/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11930
11931static void
19c3d4c9
DE
11932create_dwo_cu_reader (const struct die_reader_specs *reader,
11933 const gdb_byte *info_ptr,
11934 struct die_info *comp_unit_die,
11935 int has_children,
11936 void *datap)
3019eac3
DE
11937{
11938 struct dwarf2_cu *cu = reader->cu;
9c541725 11939 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11940 struct dwarf2_section_info *section = cu->per_cu->section;
9a3c8263 11941 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
3019eac3 11942 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 11943 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 11944
a084a2a6
AT
11945 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11946 if (!signature.has_value ())
3019eac3 11947 {
b98664d3 11948 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11949 " its dwo_id [in module %s]"),
9d8780f0 11950 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11951 return;
11952 }
11953
3019eac3 11954 dwo_unit->dwo_file = dwo_file;
a084a2a6 11955 dwo_unit->signature = *signature;
8a0459fd 11956 dwo_unit->section = section;
9c541725 11957 dwo_unit->sect_off = sect_off;
3019eac3
DE
11958 dwo_unit->length = cu->per_cu->length;
11959
b4f54984 11960 if (dwarf_read_debug)
9d8780f0
SM
11961 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11962 sect_offset_str (sect_off),
9c541725 11963 hex_string (dwo_unit->signature));
3019eac3
DE
11964}
11965
33c5cd75 11966/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11967 Note: This function processes DWO files only, not DWP files. */
3019eac3 11968
33c5cd75 11969static void
ed2dc618
SM
11970create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11971 struct dwo_file &dwo_file, dwarf2_section_info &section,
33c5cd75 11972 htab_t &cus_htab)
3019eac3
DE
11973{
11974 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 11975 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11976
33c5cd75
DB
11977 dwarf2_read_section (objfile, &section);
11978 info_ptr = section.buffer;
3019eac3
DE
11979
11980 if (info_ptr == NULL)
33c5cd75 11981 return;
3019eac3 11982
b4f54984 11983 if (dwarf_read_debug)
19c3d4c9
DE
11984 {
11985 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
33c5cd75
DB
11986 get_section_name (&section),
11987 get_section_file_name (&section));
19c3d4c9 11988 }
3019eac3 11989
33c5cd75 11990 end_ptr = info_ptr + section.size;
3019eac3
DE
11991 while (info_ptr < end_ptr)
11992 {
11993 struct dwarf2_per_cu_data per_cu;
33c5cd75
DB
11994 struct create_dwo_cu_data create_dwo_cu_data;
11995 struct dwo_unit *dwo_unit;
11996 void **slot;
11997 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3 11998
19c3d4c9
DE
11999 memset (&create_dwo_cu_data.dwo_unit, 0,
12000 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3 12001 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 12002 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3019eac3 12003 per_cu.is_debug_types = 0;
33c5cd75
DB
12004 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
12005 per_cu.section = &section;
c5ed0576 12006 create_dwo_cu_data.dwo_file = &dwo_file;
33c5cd75
DB
12007
12008 init_cutu_and_read_dies_no_follow (
12009 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
12010 info_ptr += per_cu.length;
12011
12012 // If the unit could not be parsed, skip it.
12013 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
12014 continue;
3019eac3 12015
33c5cd75
DB
12016 if (cus_htab == NULL)
12017 cus_htab = allocate_dwo_unit_table (objfile);
19c3d4c9 12018
33c5cd75
DB
12019 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12020 *dwo_unit = create_dwo_cu_data.dwo_unit;
12021 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
12022 gdb_assert (slot != NULL);
12023 if (*slot != NULL)
19c3d4c9 12024 {
33c5cd75
DB
12025 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
12026 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 12027
b98664d3 12028 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
12029 " the entry at offset %s, signature %s"),
12030 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 12031 hex_string (dwo_unit->signature));
19c3d4c9 12032 }
33c5cd75 12033 *slot = (void *)dwo_unit;
3019eac3 12034 }
3019eac3
DE
12035}
12036
80626a55
DE
12037/* DWP file .debug_{cu,tu}_index section format:
12038 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
12039
d2415c6c
DE
12040 DWP Version 1:
12041
80626a55
DE
12042 Both index sections have the same format, and serve to map a 64-bit
12043 signature to a set of section numbers. Each section begins with a header,
12044 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
12045 indexes, and a pool of 32-bit section numbers. The index sections will be
12046 aligned at 8-byte boundaries in the file.
12047
d2415c6c
DE
12048 The index section header consists of:
12049
12050 V, 32 bit version number
12051 -, 32 bits unused
12052 N, 32 bit number of compilation units or type units in the index
12053 M, 32 bit number of slots in the hash table
80626a55 12054
d2415c6c 12055 Numbers are recorded using the byte order of the application binary.
80626a55 12056
d2415c6c
DE
12057 The hash table begins at offset 16 in the section, and consists of an array
12058 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
12059 order of the application binary). Unused slots in the hash table are 0.
12060 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 12061
d2415c6c
DE
12062 The parallel table begins immediately after the hash table
12063 (at offset 16 + 8 * M from the beginning of the section), and consists of an
12064 array of 32-bit indexes (using the byte order of the application binary),
12065 corresponding 1-1 with slots in the hash table. Each entry in the parallel
12066 table contains a 32-bit index into the pool of section numbers. For unused
12067 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 12068
73869dc2
DE
12069 The pool of section numbers begins immediately following the hash table
12070 (at offset 16 + 12 * M from the beginning of the section). The pool of
12071 section numbers consists of an array of 32-bit words (using the byte order
12072 of the application binary). Each item in the array is indexed starting
12073 from 0. The hash table entry provides the index of the first section
12074 number in the set. Additional section numbers in the set follow, and the
12075 set is terminated by a 0 entry (section number 0 is not used in ELF).
12076
12077 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12078 section must be the first entry in the set, and the .debug_abbrev.dwo must
12079 be the second entry. Other members of the set may follow in any order.
12080
12081 ---
12082
12083 DWP Version 2:
12084
12085 DWP Version 2 combines all the .debug_info, etc. sections into one,
12086 and the entries in the index tables are now offsets into these sections.
12087 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12088 section.
12089
12090 Index Section Contents:
12091 Header
12092 Hash Table of Signatures dwp_hash_table.hash_table
12093 Parallel Table of Indices dwp_hash_table.unit_table
12094 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12095 Table of Section Sizes dwp_hash_table.v2.sizes
12096
12097 The index section header consists of:
12098
12099 V, 32 bit version number
12100 L, 32 bit number of columns in the table of section offsets
12101 N, 32 bit number of compilation units or type units in the index
12102 M, 32 bit number of slots in the hash table
12103
12104 Numbers are recorded using the byte order of the application binary.
12105
12106 The hash table has the same format as version 1.
12107 The parallel table of indices has the same format as version 1,
12108 except that the entries are origin-1 indices into the table of sections
12109 offsets and the table of section sizes.
12110
12111 The table of offsets begins immediately following the parallel table
12112 (at offset 16 + 12 * M from the beginning of the section). The table is
12113 a two-dimensional array of 32-bit words (using the byte order of the
12114 application binary), with L columns and N+1 rows, in row-major order.
12115 Each row in the array is indexed starting from 0. The first row provides
12116 a key to the remaining rows: each column in this row provides an identifier
12117 for a debug section, and the offsets in the same column of subsequent rows
12118 refer to that section. The section identifiers are:
12119
12120 DW_SECT_INFO 1 .debug_info.dwo
12121 DW_SECT_TYPES 2 .debug_types.dwo
12122 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12123 DW_SECT_LINE 4 .debug_line.dwo
12124 DW_SECT_LOC 5 .debug_loc.dwo
12125 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12126 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12127 DW_SECT_MACRO 8 .debug_macro.dwo
12128
12129 The offsets provided by the CU and TU index sections are the base offsets
12130 for the contributions made by each CU or TU to the corresponding section
12131 in the package file. Each CU and TU header contains an abbrev_offset
12132 field, used to find the abbreviations table for that CU or TU within the
12133 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12134 be interpreted as relative to the base offset given in the index section.
12135 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12136 should be interpreted as relative to the base offset for .debug_line.dwo,
12137 and offsets into other debug sections obtained from DWARF attributes should
12138 also be interpreted as relative to the corresponding base offset.
12139
12140 The table of sizes begins immediately following the table of offsets.
12141 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12142 with L columns and N rows, in row-major order. Each row in the array is
12143 indexed starting from 1 (row 0 is shared by the two tables).
12144
12145 ---
12146
12147 Hash table lookup is handled the same in version 1 and 2:
12148
12149 We assume that N and M will not exceed 2^32 - 1.
12150 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12151
d2415c6c
DE
12152 Given a 64-bit compilation unit signature or a type signature S, an entry
12153 in the hash table is located as follows:
80626a55 12154
d2415c6c
DE
12155 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12156 the low-order k bits all set to 1.
80626a55 12157
d2415c6c 12158 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 12159
d2415c6c
DE
12160 3) If the hash table entry at index H matches the signature, use that
12161 entry. If the hash table entry at index H is unused (all zeroes),
12162 terminate the search: the signature is not present in the table.
80626a55 12163
d2415c6c 12164 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 12165
d2415c6c 12166 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 12167 to stop at an unused slot or find the match. */
80626a55
DE
12168
12169/* Create a hash table to map DWO IDs to their CU/TU entry in
12170 .debug_{info,types}.dwo in DWP_FILE.
12171 Returns NULL if there isn't one.
12172 Note: This function processes DWP files only, not DWO files. */
12173
12174static struct dwp_hash_table *
ed2dc618
SM
12175create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12176 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
12177{
12178 struct objfile *objfile = dwarf2_per_objfile->objfile;
400174b1 12179 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 12180 const gdb_byte *index_ptr, *index_end;
80626a55 12181 struct dwarf2_section_info *index;
73869dc2 12182 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
12183 struct dwp_hash_table *htab;
12184
12185 if (is_debug_types)
12186 index = &dwp_file->sections.tu_index;
12187 else
12188 index = &dwp_file->sections.cu_index;
12189
12190 if (dwarf2_section_empty_p (index))
12191 return NULL;
12192 dwarf2_read_section (objfile, index);
12193
12194 index_ptr = index->buffer;
12195 index_end = index_ptr + index->size;
12196
12197 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
12198 index_ptr += 4;
12199 if (version == 2)
12200 nr_columns = read_4_bytes (dbfd, index_ptr);
12201 else
12202 nr_columns = 0;
12203 index_ptr += 4;
80626a55
DE
12204 nr_units = read_4_bytes (dbfd, index_ptr);
12205 index_ptr += 4;
12206 nr_slots = read_4_bytes (dbfd, index_ptr);
12207 index_ptr += 4;
12208
73869dc2 12209 if (version != 1 && version != 2)
80626a55 12210 {
21aa081e 12211 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 12212 " [in module %s]"),
21aa081e 12213 pulongest (version), dwp_file->name);
80626a55
DE
12214 }
12215 if (nr_slots != (nr_slots & -nr_slots))
12216 {
21aa081e 12217 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 12218 " is not power of 2 [in module %s]"),
21aa081e 12219 pulongest (nr_slots), dwp_file->name);
80626a55
DE
12220 }
12221
12222 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
12223 htab->version = version;
12224 htab->nr_columns = nr_columns;
80626a55
DE
12225 htab->nr_units = nr_units;
12226 htab->nr_slots = nr_slots;
12227 htab->hash_table = index_ptr;
12228 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
12229
12230 /* Exit early if the table is empty. */
12231 if (nr_slots == 0 || nr_units == 0
12232 || (version == 2 && nr_columns == 0))
12233 {
12234 /* All must be zero. */
12235 if (nr_slots != 0 || nr_units != 0
12236 || (version == 2 && nr_columns != 0))
12237 {
b98664d3 12238 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
12239 " all zero [in modules %s]"),
12240 dwp_file->name);
12241 }
12242 return htab;
12243 }
12244
12245 if (version == 1)
12246 {
12247 htab->section_pool.v1.indices =
12248 htab->unit_table + sizeof (uint32_t) * nr_slots;
12249 /* It's harder to decide whether the section is too small in v1.
12250 V1 is deprecated anyway so we punt. */
12251 }
12252 else
12253 {
12254 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12255 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 12256 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
12257 /* Reverse map for error checking. */
12258 int ids_seen[DW_SECT_MAX + 1];
12259 int i;
12260
12261 if (nr_columns < 2)
12262 {
12263 error (_("Dwarf Error: bad DWP hash table, too few columns"
12264 " in section table [in module %s]"),
12265 dwp_file->name);
12266 }
12267 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12268 {
12269 error (_("Dwarf Error: bad DWP hash table, too many columns"
12270 " in section table [in module %s]"),
12271 dwp_file->name);
12272 }
04fd5eed
GB
12273 memset (ids, 255, sizeof_ids);
12274 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
12275 for (i = 0; i < nr_columns; ++i)
12276 {
12277 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12278
12279 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12280 {
12281 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12282 " in section table [in module %s]"),
12283 id, dwp_file->name);
12284 }
12285 if (ids_seen[id] != -1)
12286 {
12287 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12288 " id %d in section table [in module %s]"),
12289 id, dwp_file->name);
12290 }
12291 ids_seen[id] = i;
12292 ids[i] = id;
12293 }
12294 /* Must have exactly one info or types section. */
12295 if (((ids_seen[DW_SECT_INFO] != -1)
12296 + (ids_seen[DW_SECT_TYPES] != -1))
12297 != 1)
12298 {
12299 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12300 " DWO info/types section [in module %s]"),
12301 dwp_file->name);
12302 }
12303 /* Must have an abbrev section. */
12304 if (ids_seen[DW_SECT_ABBREV] == -1)
12305 {
12306 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12307 " section [in module %s]"),
12308 dwp_file->name);
12309 }
12310 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12311 htab->section_pool.v2.sizes =
12312 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12313 * nr_units * nr_columns);
12314 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12315 * nr_units * nr_columns))
12316 > index_end)
12317 {
12318 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12319 " [in module %s]"),
12320 dwp_file->name);
12321 }
12322 }
80626a55
DE
12323
12324 return htab;
12325}
12326
12327/* Update SECTIONS with the data from SECTP.
12328
12329 This function is like the other "locate" section routines that are
12330 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 12331 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
12332
12333 The result is non-zero for success, or zero if an error was found. */
12334
12335static int
73869dc2
DE
12336locate_v1_virtual_dwo_sections (asection *sectp,
12337 struct virtual_v1_dwo_sections *sections)
80626a55
DE
12338{
12339 const struct dwop_section_names *names = &dwop_section_names;
12340
12341 if (section_is_p (sectp->name, &names->abbrev_dwo))
12342 {
12343 /* There can be only one. */
049412e3 12344 if (sections->abbrev.s.section != NULL)
80626a55 12345 return 0;
049412e3 12346 sections->abbrev.s.section = sectp;
fd361982 12347 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12348 }
12349 else if (section_is_p (sectp->name, &names->info_dwo)
12350 || section_is_p (sectp->name, &names->types_dwo))
12351 {
12352 /* There can be only one. */
049412e3 12353 if (sections->info_or_types.s.section != NULL)
80626a55 12354 return 0;
049412e3 12355 sections->info_or_types.s.section = sectp;
fd361982 12356 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
12357 }
12358 else if (section_is_p (sectp->name, &names->line_dwo))
12359 {
12360 /* There can be only one. */
049412e3 12361 if (sections->line.s.section != NULL)
80626a55 12362 return 0;
049412e3 12363 sections->line.s.section = sectp;
fd361982 12364 sections->line.size = bfd_section_size (sectp);
80626a55
DE
12365 }
12366 else if (section_is_p (sectp->name, &names->loc_dwo))
12367 {
12368 /* There can be only one. */
049412e3 12369 if (sections->loc.s.section != NULL)
80626a55 12370 return 0;
049412e3 12371 sections->loc.s.section = sectp;
fd361982 12372 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
12373 }
12374 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12375 {
12376 /* There can be only one. */
049412e3 12377 if (sections->macinfo.s.section != NULL)
80626a55 12378 return 0;
049412e3 12379 sections->macinfo.s.section = sectp;
fd361982 12380 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12381 }
12382 else if (section_is_p (sectp->name, &names->macro_dwo))
12383 {
12384 /* There can be only one. */
049412e3 12385 if (sections->macro.s.section != NULL)
80626a55 12386 return 0;
049412e3 12387 sections->macro.s.section = sectp;
fd361982 12388 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12389 }
12390 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12391 {
12392 /* There can be only one. */
049412e3 12393 if (sections->str_offsets.s.section != NULL)
80626a55 12394 return 0;
049412e3 12395 sections->str_offsets.s.section = sectp;
fd361982 12396 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12397 }
12398 else
12399 {
12400 /* No other kind of section is valid. */
12401 return 0;
12402 }
12403
12404 return 1;
12405}
12406
73869dc2
DE
12407/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12408 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12409 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12410 This is for DWP version 1 files. */
80626a55
DE
12411
12412static struct dwo_unit *
ed2dc618
SM
12413create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12414 struct dwp_file *dwp_file,
73869dc2
DE
12415 uint32_t unit_index,
12416 const char *comp_dir,
12417 ULONGEST signature, int is_debug_types)
80626a55
DE
12418{
12419 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
12420 const struct dwp_hash_table *dwp_htab =
12421 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12422 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
12423 const char *kind = is_debug_types ? "TU" : "CU";
12424 struct dwo_file *dwo_file;
12425 struct dwo_unit *dwo_unit;
73869dc2 12426 struct virtual_v1_dwo_sections sections;
80626a55 12427 void **dwo_file_slot;
80626a55
DE
12428 int i;
12429
73869dc2
DE
12430 gdb_assert (dwp_file->version == 1);
12431
b4f54984 12432 if (dwarf_read_debug)
80626a55 12433 {
73869dc2 12434 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 12435 kind,
73869dc2 12436 pulongest (unit_index), hex_string (signature),
80626a55
DE
12437 dwp_file->name);
12438 }
12439
19ac8c2e 12440 /* Fetch the sections of this DWO unit.
80626a55
DE
12441 Put a limit on the number of sections we look for so that bad data
12442 doesn't cause us to loop forever. */
12443
73869dc2 12444#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
12445 (1 /* .debug_info or .debug_types */ \
12446 + 1 /* .debug_abbrev */ \
12447 + 1 /* .debug_line */ \
12448 + 1 /* .debug_loc */ \
12449 + 1 /* .debug_str_offsets */ \
19ac8c2e 12450 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
12451 + 1 /* trailing zero */)
12452
12453 memset (&sections, 0, sizeof (sections));
80626a55 12454
73869dc2 12455 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
12456 {
12457 asection *sectp;
12458 uint32_t section_nr =
12459 read_4_bytes (dbfd,
73869dc2
DE
12460 dwp_htab->section_pool.v1.indices
12461 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
12462
12463 if (section_nr == 0)
12464 break;
12465 if (section_nr >= dwp_file->num_sections)
12466 {
12467 error (_("Dwarf Error: bad DWP hash table, section number too large"
12468 " [in module %s]"),
12469 dwp_file->name);
12470 }
12471
12472 sectp = dwp_file->elf_sections[section_nr];
73869dc2 12473 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
12474 {
12475 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12476 " [in module %s]"),
12477 dwp_file->name);
12478 }
12479 }
12480
12481 if (i < 2
a32a8923
DE
12482 || dwarf2_section_empty_p (&sections.info_or_types)
12483 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
12484 {
12485 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12486 " [in module %s]"),
12487 dwp_file->name);
12488 }
73869dc2 12489 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
12490 {
12491 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12492 " [in module %s]"),
12493 dwp_file->name);
12494 }
12495
12496 /* It's easier for the rest of the code if we fake a struct dwo_file and
12497 have dwo_unit "live" in that. At least for now.
12498
12499 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 12500 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
12501 file, we can combine them back into a virtual DWO file to save space
12502 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
12503 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12504
791afaa2
TT
12505 std::string virtual_dwo_name =
12506 string_printf ("virtual-dwo/%d-%d-%d-%d",
12507 get_section_id (&sections.abbrev),
12508 get_section_id (&sections.line),
12509 get_section_id (&sections.loc),
12510 get_section_id (&sections.str_offsets));
80626a55 12511 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12512 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12513 virtual_dwo_name.c_str (),
12514 comp_dir);
80626a55
DE
12515 /* Create one if necessary. */
12516 if (*dwo_file_slot == NULL)
12517 {
b4f54984 12518 if (dwarf_read_debug)
80626a55
DE
12519 {
12520 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12521 virtual_dwo_name.c_str ());
80626a55 12522 }
51ac9db5 12523 dwo_file = new struct dwo_file;
efba19b0
TT
12524 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12525 virtual_dwo_name);
0ac5b59e 12526 dwo_file->comp_dir = comp_dir;
80626a55
DE
12527 dwo_file->sections.abbrev = sections.abbrev;
12528 dwo_file->sections.line = sections.line;
12529 dwo_file->sections.loc = sections.loc;
12530 dwo_file->sections.macinfo = sections.macinfo;
12531 dwo_file->sections.macro = sections.macro;
12532 dwo_file->sections.str_offsets = sections.str_offsets;
12533 /* The "str" section is global to the entire DWP file. */
12534 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 12535 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
12536 there's no need to record it in dwo_file.
12537 Also, we can't simply record type sections in dwo_file because
12538 we record a pointer into the vector in dwo_unit. As we collect more
12539 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
12540 for it, invalidating all copies of pointers into the previous
12541 contents. */
80626a55
DE
12542 *dwo_file_slot = dwo_file;
12543 }
12544 else
12545 {
b4f54984 12546 if (dwarf_read_debug)
80626a55
DE
12547 {
12548 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12549 virtual_dwo_name.c_str ());
80626a55 12550 }
9a3c8263 12551 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 12552 }
80626a55
DE
12553
12554 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12555 dwo_unit->dwo_file = dwo_file;
12556 dwo_unit->signature = signature;
8d749320
SM
12557 dwo_unit->section =
12558 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 12559 *dwo_unit->section = sections.info_or_types;
57d63ce2 12560 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
12561
12562 return dwo_unit;
12563}
12564
73869dc2
DE
12565/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12566 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12567 piece within that section used by a TU/CU, return a virtual section
12568 of just that piece. */
12569
12570static struct dwarf2_section_info
ed2dc618
SM
12571create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12572 struct dwarf2_section_info *section,
73869dc2
DE
12573 bfd_size_type offset, bfd_size_type size)
12574{
12575 struct dwarf2_section_info result;
12576 asection *sectp;
12577
12578 gdb_assert (section != NULL);
12579 gdb_assert (!section->is_virtual);
12580
12581 memset (&result, 0, sizeof (result));
12582 result.s.containing_section = section;
dc4ccb6f 12583 result.is_virtual = true;
73869dc2
DE
12584
12585 if (size == 0)
12586 return result;
12587
12588 sectp = get_section_bfd_section (section);
12589
12590 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12591 bounds of the real section. This is a pretty-rare event, so just
12592 flag an error (easier) instead of a warning and trying to cope. */
12593 if (sectp == NULL
fd361982 12594 || offset + size > bfd_section_size (sectp))
73869dc2 12595 {
73869dc2
DE
12596 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12597 " in section %s [in module %s]"),
fd361982 12598 sectp ? bfd_section_name (sectp) : "<unknown>",
73869dc2
DE
12599 objfile_name (dwarf2_per_objfile->objfile));
12600 }
12601
12602 result.virtual_offset = offset;
12603 result.size = size;
12604 return result;
12605}
12606
12607/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12608 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12609 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12610 This is for DWP version 2 files. */
12611
12612static struct dwo_unit *
ed2dc618
SM
12613create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12614 struct dwp_file *dwp_file,
73869dc2
DE
12615 uint32_t unit_index,
12616 const char *comp_dir,
12617 ULONGEST signature, int is_debug_types)
12618{
12619 struct objfile *objfile = dwarf2_per_objfile->objfile;
12620 const struct dwp_hash_table *dwp_htab =
12621 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12622 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
12623 const char *kind = is_debug_types ? "TU" : "CU";
12624 struct dwo_file *dwo_file;
12625 struct dwo_unit *dwo_unit;
12626 struct virtual_v2_dwo_sections sections;
12627 void **dwo_file_slot;
73869dc2
DE
12628 int i;
12629
12630 gdb_assert (dwp_file->version == 2);
12631
b4f54984 12632 if (dwarf_read_debug)
73869dc2
DE
12633 {
12634 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12635 kind,
12636 pulongest (unit_index), hex_string (signature),
12637 dwp_file->name);
12638 }
12639
12640 /* Fetch the section offsets of this DWO unit. */
12641
12642 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12643
12644 for (i = 0; i < dwp_htab->nr_columns; ++i)
12645 {
12646 uint32_t offset = read_4_bytes (dbfd,
12647 dwp_htab->section_pool.v2.offsets
12648 + (((unit_index - 1) * dwp_htab->nr_columns
12649 + i)
12650 * sizeof (uint32_t)));
12651 uint32_t size = read_4_bytes (dbfd,
12652 dwp_htab->section_pool.v2.sizes
12653 + (((unit_index - 1) * dwp_htab->nr_columns
12654 + i)
12655 * sizeof (uint32_t)));
12656
12657 switch (dwp_htab->section_pool.v2.section_ids[i])
12658 {
12659 case DW_SECT_INFO:
12660 case DW_SECT_TYPES:
12661 sections.info_or_types_offset = offset;
12662 sections.info_or_types_size = size;
12663 break;
12664 case DW_SECT_ABBREV:
12665 sections.abbrev_offset = offset;
12666 sections.abbrev_size = size;
12667 break;
12668 case DW_SECT_LINE:
12669 sections.line_offset = offset;
12670 sections.line_size = size;
12671 break;
12672 case DW_SECT_LOC:
12673 sections.loc_offset = offset;
12674 sections.loc_size = size;
12675 break;
12676 case DW_SECT_STR_OFFSETS:
12677 sections.str_offsets_offset = offset;
12678 sections.str_offsets_size = size;
12679 break;
12680 case DW_SECT_MACINFO:
12681 sections.macinfo_offset = offset;
12682 sections.macinfo_size = size;
12683 break;
12684 case DW_SECT_MACRO:
12685 sections.macro_offset = offset;
12686 sections.macro_size = size;
12687 break;
12688 }
12689 }
12690
12691 /* It's easier for the rest of the code if we fake a struct dwo_file and
12692 have dwo_unit "live" in that. At least for now.
12693
12694 The DWP file can be made up of a random collection of CUs and TUs.
12695 However, for each CU + set of TUs that came from the same original DWO
12696 file, we can combine them back into a virtual DWO file to save space
12697 (fewer struct dwo_file objects to allocate). Remember that for really
12698 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12699
791afaa2
TT
12700 std::string virtual_dwo_name =
12701 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12702 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12703 (long) (sections.line_size ? sections.line_offset : 0),
12704 (long) (sections.loc_size ? sections.loc_offset : 0),
12705 (long) (sections.str_offsets_size
12706 ? sections.str_offsets_offset : 0));
73869dc2 12707 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12708 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12709 virtual_dwo_name.c_str (),
12710 comp_dir);
73869dc2
DE
12711 /* Create one if necessary. */
12712 if (*dwo_file_slot == NULL)
12713 {
b4f54984 12714 if (dwarf_read_debug)
73869dc2
DE
12715 {
12716 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12717 virtual_dwo_name.c_str ());
73869dc2 12718 }
51ac9db5 12719 dwo_file = new struct dwo_file;
efba19b0
TT
12720 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12721 virtual_dwo_name);
73869dc2
DE
12722 dwo_file->comp_dir = comp_dir;
12723 dwo_file->sections.abbrev =
ed2dc618 12724 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
12725 sections.abbrev_offset, sections.abbrev_size);
12726 dwo_file->sections.line =
ed2dc618 12727 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
12728 sections.line_offset, sections.line_size);
12729 dwo_file->sections.loc =
ed2dc618 12730 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
12731 sections.loc_offset, sections.loc_size);
12732 dwo_file->sections.macinfo =
ed2dc618 12733 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
12734 sections.macinfo_offset, sections.macinfo_size);
12735 dwo_file->sections.macro =
ed2dc618 12736 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
12737 sections.macro_offset, sections.macro_size);
12738 dwo_file->sections.str_offsets =
ed2dc618
SM
12739 create_dwp_v2_section (dwarf2_per_objfile,
12740 &dwp_file->sections.str_offsets,
73869dc2
DE
12741 sections.str_offsets_offset,
12742 sections.str_offsets_size);
12743 /* The "str" section is global to the entire DWP file. */
12744 dwo_file->sections.str = dwp_file->sections.str;
12745 /* The info or types section is assigned below to dwo_unit,
12746 there's no need to record it in dwo_file.
12747 Also, we can't simply record type sections in dwo_file because
12748 we record a pointer into the vector in dwo_unit. As we collect more
12749 types we'll grow the vector and eventually have to reallocate space
12750 for it, invalidating all copies of pointers into the previous
12751 contents. */
12752 *dwo_file_slot = dwo_file;
12753 }
12754 else
12755 {
b4f54984 12756 if (dwarf_read_debug)
73869dc2
DE
12757 {
12758 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12759 virtual_dwo_name.c_str ());
73869dc2 12760 }
9a3c8263 12761 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12762 }
73869dc2
DE
12763
12764 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12765 dwo_unit->dwo_file = dwo_file;
12766 dwo_unit->signature = signature;
8d749320
SM
12767 dwo_unit->section =
12768 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
ed2dc618
SM
12769 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12770 is_debug_types
73869dc2
DE
12771 ? &dwp_file->sections.types
12772 : &dwp_file->sections.info,
12773 sections.info_or_types_offset,
12774 sections.info_or_types_size);
12775 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12776
12777 return dwo_unit;
12778}
12779
57d63ce2
DE
12780/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12781 Returns NULL if the signature isn't found. */
80626a55
DE
12782
12783static struct dwo_unit *
ed2dc618
SM
12784lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12785 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12786 ULONGEST signature, int is_debug_types)
80626a55 12787{
57d63ce2
DE
12788 const struct dwp_hash_table *dwp_htab =
12789 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12790 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12791 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12792 uint32_t hash = signature & mask;
12793 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12794 unsigned int i;
12795 void **slot;
870f88f7 12796 struct dwo_unit find_dwo_cu;
80626a55
DE
12797
12798 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12799 find_dwo_cu.signature = signature;
19ac8c2e
DE
12800 slot = htab_find_slot (is_debug_types
12801 ? dwp_file->loaded_tus
12802 : dwp_file->loaded_cus,
12803 &find_dwo_cu, INSERT);
80626a55
DE
12804
12805 if (*slot != NULL)
9a3c8263 12806 return (struct dwo_unit *) *slot;
80626a55
DE
12807
12808 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12809 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12810 {
12811 ULONGEST signature_in_table;
12812
12813 signature_in_table =
57d63ce2 12814 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12815 if (signature_in_table == signature)
12816 {
57d63ce2
DE
12817 uint32_t unit_index =
12818 read_4_bytes (dbfd,
12819 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12820
73869dc2
DE
12821 if (dwp_file->version == 1)
12822 {
ed2dc618
SM
12823 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12824 dwp_file, unit_index,
73869dc2
DE
12825 comp_dir, signature,
12826 is_debug_types);
12827 }
12828 else
12829 {
ed2dc618
SM
12830 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12831 dwp_file, unit_index,
73869dc2
DE
12832 comp_dir, signature,
12833 is_debug_types);
12834 }
9a3c8263 12835 return (struct dwo_unit *) *slot;
80626a55
DE
12836 }
12837 if (signature_in_table == 0)
12838 return NULL;
12839 hash = (hash + hash2) & mask;
12840 }
12841
12842 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12843 " [in module %s]"),
12844 dwp_file->name);
12845}
12846
ab5088bf 12847/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12848 Open the file specified by FILE_NAME and hand it off to BFD for
12849 preliminary analysis. Return a newly initialized bfd *, which
12850 includes a canonicalized copy of FILE_NAME.
80626a55 12851 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12852 SEARCH_CWD is true if the current directory is to be searched.
12853 It will be searched before debug-file-directory.
13aaf454
DE
12854 If successful, the file is added to the bfd include table of the
12855 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12856 If unable to find/open the file, return NULL.
3019eac3
DE
12857 NOTE: This function is derived from symfile_bfd_open. */
12858
192b62ce 12859static gdb_bfd_ref_ptr
ed2dc618
SM
12860try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12861 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12862{
24b9144d 12863 int desc;
9c02c129
DE
12864 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12865 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12866 to debug_file_directory. */
e0cc99a6 12867 const char *search_path;
9c02c129
DE
12868 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12869
e0cc99a6 12870 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12871 if (search_cwd)
12872 {
12873 if (*debug_file_directory != '\0')
e0cc99a6
TT
12874 {
12875 search_path_holder.reset (concat (".", dirname_separator_string,
12876 debug_file_directory,
12877 (char *) NULL));
12878 search_path = search_path_holder.get ();
12879 }
6ac97d4c 12880 else
e0cc99a6 12881 search_path = ".";
6ac97d4c 12882 }
9c02c129 12883 else
e0cc99a6 12884 search_path = debug_file_directory;
3019eac3 12885
24b9144d 12886 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12887 if (is_dwp)
12888 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12889
12890 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12891 desc = openp (search_path, flags, file_name,
3019eac3
DE
12892 O_RDONLY | O_BINARY, &absolute_name);
12893 if (desc < 0)
12894 return NULL;
12895
e0cc99a6
TT
12896 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12897 gnutarget, desc));
9c02c129
DE
12898 if (sym_bfd == NULL)
12899 return NULL;
192b62ce 12900 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12901
192b62ce
TT
12902 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12903 return NULL;
3019eac3 12904
13aaf454
DE
12905 /* Success. Record the bfd as having been included by the objfile's bfd.
12906 This is important because things like demangled_names_hash lives in the
12907 objfile's per_bfd space and may have references to things like symbol
12908 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12909 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12910
3019eac3
DE
12911 return sym_bfd;
12912}
12913
ab5088bf 12914/* Try to open DWO file FILE_NAME.
3019eac3
DE
12915 COMP_DIR is the DW_AT_comp_dir attribute.
12916 The result is the bfd handle of the file.
12917 If there is a problem finding or opening the file, return NULL.
12918 Upon success, the canonicalized path of the file is stored in the bfd,
12919 same as symfile_bfd_open. */
12920
192b62ce 12921static gdb_bfd_ref_ptr
ed2dc618
SM
12922open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12923 const char *file_name, const char *comp_dir)
3019eac3 12924{
80626a55 12925 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12926 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12927 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12928
12929 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12930
12931 if (comp_dir != NULL)
12932 {
b36cec19
PA
12933 char *path_to_try = concat (comp_dir, SLASH_STRING,
12934 file_name, (char *) NULL);
3019eac3
DE
12935
12936 /* NOTE: If comp_dir is a relative path, this will also try the
12937 search path, which seems useful. */
ed2dc618
SM
12938 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12939 path_to_try,
12940 0 /*is_dwp*/,
192b62ce 12941 1 /*search_cwd*/));
3019eac3
DE
12942 xfree (path_to_try);
12943 if (abfd != NULL)
12944 return abfd;
12945 }
12946
12947 /* That didn't work, try debug-file-directory, which, despite its name,
12948 is a list of paths. */
12949
12950 if (*debug_file_directory == '\0')
12951 return NULL;
12952
ed2dc618
SM
12953 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12954 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12955}
12956
80626a55
DE
12957/* This function is mapped across the sections and remembers the offset and
12958 size of each of the DWO debugging sections we are interested in. */
12959
12960static void
12961dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12962{
9a3c8263 12963 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12964 const struct dwop_section_names *names = &dwop_section_names;
12965
12966 if (section_is_p (sectp->name, &names->abbrev_dwo))
12967 {
049412e3 12968 dwo_sections->abbrev.s.section = sectp;
fd361982 12969 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12970 }
12971 else if (section_is_p (sectp->name, &names->info_dwo))
12972 {
049412e3 12973 dwo_sections->info.s.section = sectp;
fd361982 12974 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12975 }
12976 else if (section_is_p (sectp->name, &names->line_dwo))
12977 {
049412e3 12978 dwo_sections->line.s.section = sectp;
fd361982 12979 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12980 }
12981 else if (section_is_p (sectp->name, &names->loc_dwo))
12982 {
049412e3 12983 dwo_sections->loc.s.section = sectp;
fd361982 12984 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55
DE
12985 }
12986 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12987 {
049412e3 12988 dwo_sections->macinfo.s.section = sectp;
fd361982 12989 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12990 }
12991 else if (section_is_p (sectp->name, &names->macro_dwo))
12992 {
049412e3 12993 dwo_sections->macro.s.section = sectp;
fd361982 12994 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12995 }
12996 else if (section_is_p (sectp->name, &names->str_dwo))
12997 {
049412e3 12998 dwo_sections->str.s.section = sectp;
fd361982 12999 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
13000 }
13001 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13002 {
049412e3 13003 dwo_sections->str_offsets.s.section = sectp;
fd361982 13004 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
13005 }
13006 else if (section_is_p (sectp->name, &names->types_dwo))
13007 {
13008 struct dwarf2_section_info type_section;
13009
13010 memset (&type_section, 0, sizeof (type_section));
049412e3 13011 type_section.s.section = sectp;
fd361982 13012 type_section.size = bfd_section_size (sectp);
fd5866f6 13013 dwo_sections->types.push_back (type_section);
80626a55
DE
13014 }
13015}
13016
ab5088bf 13017/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 13018 by PER_CU. This is for the non-DWP case.
80626a55 13019 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
13020
13021static struct dwo_file *
0ac5b59e
DE
13022open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
13023 const char *dwo_name, const char *comp_dir)
3019eac3 13024{
ed2dc618 13025 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 13026
fb1eb2f9 13027 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
80626a55
DE
13028 if (dbfd == NULL)
13029 {
b4f54984 13030 if (dwarf_read_debug)
80626a55
DE
13031 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
13032 return NULL;
13033 }
263db9a1 13034
51ac9db5 13035 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
13036 dwo_file->dwo_name = dwo_name;
13037 dwo_file->comp_dir = comp_dir;
fb1eb2f9 13038 dwo_file->dbfd = std::move (dbfd);
3019eac3 13039
fb1eb2f9 13040 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
192b62ce 13041 &dwo_file->sections);
3019eac3 13042
ed2dc618
SM
13043 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
13044 dwo_file->cus);
3019eac3 13045
263db9a1 13046 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 13047 dwo_file->sections.types, dwo_file->tus);
3019eac3 13048
b4f54984 13049 if (dwarf_read_debug)
80626a55
DE
13050 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
13051
263db9a1 13052 return dwo_file.release ();
3019eac3
DE
13053}
13054
80626a55 13055/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
13056 size of each of the DWP debugging sections common to version 1 and 2 that
13057 we are interested in. */
3019eac3 13058
80626a55 13059static void
73869dc2
DE
13060dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
13061 void *dwp_file_ptr)
3019eac3 13062{
9a3c8263 13063 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
13064 const struct dwop_section_names *names = &dwop_section_names;
13065 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 13066
80626a55 13067 /* Record the ELF section number for later lookup: this is what the
73869dc2 13068 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
13069 gdb_assert (elf_section_nr < dwp_file->num_sections);
13070 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 13071
80626a55
DE
13072 /* Look for specific sections that we need. */
13073 if (section_is_p (sectp->name, &names->str_dwo))
13074 {
049412e3 13075 dwp_file->sections.str.s.section = sectp;
fd361982 13076 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
13077 }
13078 else if (section_is_p (sectp->name, &names->cu_index))
13079 {
049412e3 13080 dwp_file->sections.cu_index.s.section = sectp;
fd361982 13081 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
13082 }
13083 else if (section_is_p (sectp->name, &names->tu_index))
13084 {
049412e3 13085 dwp_file->sections.tu_index.s.section = sectp;
fd361982 13086 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
13087 }
13088}
3019eac3 13089
73869dc2
DE
13090/* This function is mapped across the sections and remembers the offset and
13091 size of each of the DWP version 2 debugging sections that we are interested
13092 in. This is split into a separate function because we don't know if we
13093 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13094
13095static void
13096dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13097{
9a3c8263 13098 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
13099 const struct dwop_section_names *names = &dwop_section_names;
13100 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13101
13102 /* Record the ELF section number for later lookup: this is what the
13103 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13104 gdb_assert (elf_section_nr < dwp_file->num_sections);
13105 dwp_file->elf_sections[elf_section_nr] = sectp;
13106
13107 /* Look for specific sections that we need. */
13108 if (section_is_p (sectp->name, &names->abbrev_dwo))
13109 {
049412e3 13110 dwp_file->sections.abbrev.s.section = sectp;
fd361982 13111 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
13112 }
13113 else if (section_is_p (sectp->name, &names->info_dwo))
13114 {
049412e3 13115 dwp_file->sections.info.s.section = sectp;
fd361982 13116 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
13117 }
13118 else if (section_is_p (sectp->name, &names->line_dwo))
13119 {
049412e3 13120 dwp_file->sections.line.s.section = sectp;
fd361982 13121 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
13122 }
13123 else if (section_is_p (sectp->name, &names->loc_dwo))
13124 {
049412e3 13125 dwp_file->sections.loc.s.section = sectp;
fd361982 13126 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
13127 }
13128 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13129 {
049412e3 13130 dwp_file->sections.macinfo.s.section = sectp;
fd361982 13131 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
13132 }
13133 else if (section_is_p (sectp->name, &names->macro_dwo))
13134 {
049412e3 13135 dwp_file->sections.macro.s.section = sectp;
fd361982 13136 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
13137 }
13138 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13139 {
049412e3 13140 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 13141 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
13142 }
13143 else if (section_is_p (sectp->name, &names->types_dwo))
13144 {
049412e3 13145 dwp_file->sections.types.s.section = sectp;
fd361982 13146 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
13147 }
13148}
13149
80626a55 13150/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 13151
80626a55
DE
13152static hashval_t
13153hash_dwp_loaded_cutus (const void *item)
13154{
9a3c8263 13155 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 13156
80626a55
DE
13157 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13158 return dwo_unit->signature;
3019eac3
DE
13159}
13160
80626a55 13161/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 13162
80626a55
DE
13163static int
13164eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 13165{
9a3c8263
SM
13166 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13167 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 13168
80626a55
DE
13169 return dua->signature == dub->signature;
13170}
3019eac3 13171
80626a55 13172/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 13173
80626a55
DE
13174static htab_t
13175allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13176{
13177 return htab_create_alloc_ex (3,
13178 hash_dwp_loaded_cutus,
13179 eq_dwp_loaded_cutus,
13180 NULL,
13181 &objfile->objfile_obstack,
13182 hashtab_obstack_allocate,
13183 dummy_obstack_deallocate);
13184}
3019eac3 13185
ab5088bf
DE
13186/* Try to open DWP file FILE_NAME.
13187 The result is the bfd handle of the file.
13188 If there is a problem finding or opening the file, return NULL.
13189 Upon success, the canonicalized path of the file is stored in the bfd,
13190 same as symfile_bfd_open. */
13191
192b62ce 13192static gdb_bfd_ref_ptr
ed2dc618
SM
13193open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13194 const char *file_name)
ab5088bf 13195{
ed2dc618
SM
13196 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13197 1 /*is_dwp*/,
192b62ce 13198 1 /*search_cwd*/));
6ac97d4c
DE
13199 if (abfd != NULL)
13200 return abfd;
13201
13202 /* Work around upstream bug 15652.
13203 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13204 [Whether that's a "bug" is debatable, but it is getting in our way.]
13205 We have no real idea where the dwp file is, because gdb's realpath-ing
13206 of the executable's path may have discarded the needed info.
13207 [IWBN if the dwp file name was recorded in the executable, akin to
13208 .gnu_debuglink, but that doesn't exist yet.]
13209 Strip the directory from FILE_NAME and search again. */
13210 if (*debug_file_directory != '\0')
13211 {
13212 /* Don't implicitly search the current directory here.
13213 If the user wants to search "." to handle this case,
13214 it must be added to debug-file-directory. */
ed2dc618
SM
13215 return try_open_dwop_file (dwarf2_per_objfile,
13216 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
13217 0 /*search_cwd*/);
13218 }
13219
13220 return NULL;
ab5088bf
DE
13221}
13222
80626a55
DE
13223/* Initialize the use of the DWP file for the current objfile.
13224 By convention the name of the DWP file is ${objfile}.dwp.
13225 The result is NULL if it can't be found. */
a766d390 13226
400174b1 13227static std::unique_ptr<struct dwp_file>
ed2dc618 13228open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
13229{
13230 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 13231
82bf32bc
JK
13232 /* Try to find first .dwp for the binary file before any symbolic links
13233 resolving. */
6c447423
DE
13234
13235 /* If the objfile is a debug file, find the name of the real binary
13236 file and get the name of dwp file from there. */
d721ba37 13237 std::string dwp_name;
6c447423
DE
13238 if (objfile->separate_debug_objfile_backlink != NULL)
13239 {
13240 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13241 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 13242
d721ba37 13243 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
13244 }
13245 else
d721ba37
PA
13246 dwp_name = objfile->original_name;
13247
13248 dwp_name += ".dwp";
80626a55 13249
ed2dc618 13250 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
13251 if (dbfd == NULL
13252 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13253 {
13254 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
13255 dwp_name = objfile_name (objfile);
13256 dwp_name += ".dwp";
ed2dc618 13257 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
13258 }
13259
80626a55
DE
13260 if (dbfd == NULL)
13261 {
b4f54984 13262 if (dwarf_read_debug)
d721ba37 13263 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 13264 return std::unique_ptr<dwp_file> ();
3019eac3 13265 }
400174b1
TT
13266
13267 const char *name = bfd_get_filename (dbfd.get ());
13268 std::unique_ptr<struct dwp_file> dwp_file
13269 (new struct dwp_file (name, std::move (dbfd)));
c906108c 13270
0a0f4c01 13271 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55
DE
13272 dwp_file->elf_sections =
13273 OBSTACK_CALLOC (&objfile->objfile_obstack,
13274 dwp_file->num_sections, asection *);
13275
400174b1
TT
13276 bfd_map_over_sections (dwp_file->dbfd.get (),
13277 dwarf2_locate_common_dwp_sections,
13278 dwp_file.get ());
80626a55 13279
400174b1
TT
13280 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13281 0);
80626a55 13282
400174b1
TT
13283 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13284 1);
80626a55 13285
73869dc2 13286 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
13287 if (dwp_file->cus && dwp_file->tus
13288 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
13289 {
13290 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 13291 pretty bizarre. We use pulongest here because that's the established
4d65956b 13292 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
13293 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13294 " TU version %s [in DWP file %s]"),
13295 pulongest (dwp_file->cus->version),
d721ba37 13296 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 13297 }
08302ed2
DE
13298
13299 if (dwp_file->cus)
13300 dwp_file->version = dwp_file->cus->version;
13301 else if (dwp_file->tus)
13302 dwp_file->version = dwp_file->tus->version;
13303 else
13304 dwp_file->version = 2;
73869dc2
DE
13305
13306 if (dwp_file->version == 2)
400174b1
TT
13307 bfd_map_over_sections (dwp_file->dbfd.get (),
13308 dwarf2_locate_v2_dwp_sections,
13309 dwp_file.get ());
73869dc2 13310
19ac8c2e
DE
13311 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13312 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 13313
b4f54984 13314 if (dwarf_read_debug)
80626a55
DE
13315 {
13316 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13317 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
13318 " %s CUs, %s TUs\n",
13319 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13320 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
13321 }
13322
13323 return dwp_file;
3019eac3 13324}
c906108c 13325
ab5088bf
DE
13326/* Wrapper around open_and_init_dwp_file, only open it once. */
13327
13328static struct dwp_file *
ed2dc618 13329get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf
DE
13330{
13331 if (! dwarf2_per_objfile->dwp_checked)
13332 {
ed2dc618
SM
13333 dwarf2_per_objfile->dwp_file
13334 = open_and_init_dwp_file (dwarf2_per_objfile);
ab5088bf
DE
13335 dwarf2_per_objfile->dwp_checked = 1;
13336 }
400174b1 13337 return dwarf2_per_objfile->dwp_file.get ();
ab5088bf
DE
13338}
13339
80626a55
DE
13340/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13341 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13342 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 13343 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
13344 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13345
13346 This is called, for example, when wanting to read a variable with a
13347 complex location. Therefore we don't want to do file i/o for every call.
13348 Therefore we don't want to look for a DWO file on every call.
13349 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13350 then we check if we've already seen DWO_NAME, and only THEN do we check
13351 for a DWO file.
13352
1c658ad5 13353 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 13354 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 13355
3019eac3 13356static struct dwo_unit *
80626a55
DE
13357lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13358 const char *dwo_name, const char *comp_dir,
13359 ULONGEST signature, int is_debug_types)
3019eac3 13360{
ed2dc618 13361 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
3019eac3 13362 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
13363 const char *kind = is_debug_types ? "TU" : "CU";
13364 void **dwo_file_slot;
3019eac3 13365 struct dwo_file *dwo_file;
80626a55 13366 struct dwp_file *dwp_file;
cb1df416 13367
6a506a2d
DE
13368 /* First see if there's a DWP file.
13369 If we have a DWP file but didn't find the DWO inside it, don't
13370 look for the original DWO file. It makes gdb behave differently
13371 depending on whether one is debugging in the build tree. */
cf2c3c16 13372
ed2dc618 13373 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 13374 if (dwp_file != NULL)
cf2c3c16 13375 {
80626a55
DE
13376 const struct dwp_hash_table *dwp_htab =
13377 is_debug_types ? dwp_file->tus : dwp_file->cus;
13378
13379 if (dwp_htab != NULL)
13380 {
13381 struct dwo_unit *dwo_cutu =
ed2dc618 13382 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 13383 signature, is_debug_types);
80626a55
DE
13384
13385 if (dwo_cutu != NULL)
13386 {
b4f54984 13387 if (dwarf_read_debug)
80626a55
DE
13388 {
13389 fprintf_unfiltered (gdb_stdlog,
13390 "Virtual DWO %s %s found: @%s\n",
13391 kind, hex_string (signature),
13392 host_address_to_string (dwo_cutu));
13393 }
13394 return dwo_cutu;
13395 }
13396 }
13397 }
6a506a2d 13398 else
80626a55 13399 {
6a506a2d 13400 /* No DWP file, look for the DWO file. */
80626a55 13401
ed2dc618
SM
13402 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13403 dwo_name, comp_dir);
6a506a2d 13404 if (*dwo_file_slot == NULL)
80626a55 13405 {
6a506a2d
DE
13406 /* Read in the file and build a table of the CUs/TUs it contains. */
13407 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 13408 }
6a506a2d 13409 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 13410 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 13411
6a506a2d 13412 if (dwo_file != NULL)
19c3d4c9 13413 {
6a506a2d
DE
13414 struct dwo_unit *dwo_cutu = NULL;
13415
13416 if (is_debug_types && dwo_file->tus)
13417 {
13418 struct dwo_unit find_dwo_cutu;
13419
13420 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13421 find_dwo_cutu.signature = signature;
9a3c8263
SM
13422 dwo_cutu
13423 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
6a506a2d 13424 }
33c5cd75 13425 else if (!is_debug_types && dwo_file->cus)
80626a55 13426 {
33c5cd75
DB
13427 struct dwo_unit find_dwo_cutu;
13428
13429 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13430 find_dwo_cutu.signature = signature;
13431 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13432 &find_dwo_cutu);
6a506a2d
DE
13433 }
13434
13435 if (dwo_cutu != NULL)
13436 {
b4f54984 13437 if (dwarf_read_debug)
6a506a2d
DE
13438 {
13439 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13440 kind, dwo_name, hex_string (signature),
13441 host_address_to_string (dwo_cutu));
13442 }
13443 return dwo_cutu;
80626a55
DE
13444 }
13445 }
2e276125 13446 }
9cdd5dbd 13447
80626a55
DE
13448 /* We didn't find it. This could mean a dwo_id mismatch, or
13449 someone deleted the DWO/DWP file, or the search path isn't set up
13450 correctly to find the file. */
13451
b4f54984 13452 if (dwarf_read_debug)
80626a55
DE
13453 {
13454 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13455 kind, dwo_name, hex_string (signature));
13456 }
3019eac3 13457
6656a72d
DE
13458 /* This is a warning and not a complaint because it can be caused by
13459 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
13460 {
13461 /* Print the name of the DWP file if we looked there, helps the user
13462 better diagnose the problem. */
791afaa2 13463 std::string dwp_text;
43942612
DE
13464
13465 if (dwp_file != NULL)
791afaa2
TT
13466 dwp_text = string_printf (" [in DWP file %s]",
13467 lbasename (dwp_file->name));
43942612 13468
9d8780f0 13469 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612
DE
13470 " [in module %s]"),
13471 kind, dwo_name, hex_string (signature),
791afaa2 13472 dwp_text.c_str (),
43942612 13473 this_unit->is_debug_types ? "TU" : "CU",
9d8780f0 13474 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
43942612 13475 }
3019eac3 13476 return NULL;
5fb290d7
DJ
13477}
13478
80626a55
DE
13479/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13480 See lookup_dwo_cutu_unit for details. */
13481
13482static struct dwo_unit *
13483lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13484 const char *dwo_name, const char *comp_dir,
13485 ULONGEST signature)
13486{
13487 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13488}
13489
13490/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13491 See lookup_dwo_cutu_unit for details. */
13492
13493static struct dwo_unit *
13494lookup_dwo_type_unit (struct signatured_type *this_tu,
13495 const char *dwo_name, const char *comp_dir)
13496{
13497 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13498}
13499
89e63ee4
DE
13500/* Traversal function for queue_and_load_all_dwo_tus. */
13501
13502static int
13503queue_and_load_dwo_tu (void **slot, void *info)
13504{
13505 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13506 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13507 ULONGEST signature = dwo_unit->signature;
13508 struct signatured_type *sig_type =
13509 lookup_dwo_signatured_type (per_cu->cu, signature);
13510
13511 if (sig_type != NULL)
13512 {
13513 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13514
13515 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13516 a real dependency of PER_CU on SIG_TYPE. That is detected later
13517 while processing PER_CU. */
13518 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13519 load_full_type_unit (sig_cu);
ae640021 13520 per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
13521 }
13522
13523 return 1;
13524}
13525
13526/* Queue all TUs contained in the DWO of PER_CU to be read in.
13527 The DWO may have the only definition of the type, though it may not be
13528 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13529 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13530
13531static void
13532queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13533{
13534 struct dwo_unit *dwo_unit;
13535 struct dwo_file *dwo_file;
13536
13537 gdb_assert (!per_cu->is_debug_types);
ed2dc618 13538 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
89e63ee4
DE
13539 gdb_assert (per_cu->cu != NULL);
13540
13541 dwo_unit = per_cu->cu->dwo_unit;
13542 gdb_assert (dwo_unit != NULL);
13543
13544 dwo_file = dwo_unit->dwo_file;
13545 if (dwo_file->tus != NULL)
13546 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13547}
13548
3019eac3 13549/* Read in various DIEs. */
348e048f 13550
d389af10 13551/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
13552 Inherit only the children of the DW_AT_abstract_origin DIE not being
13553 already referenced by DW_AT_abstract_origin from the children of the
13554 current DIE. */
d389af10
JK
13555
13556static void
13557inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13558{
13559 struct die_info *child_die;
791afaa2 13560 sect_offset *offsetp;
d389af10
JK
13561 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13562 struct die_info *origin_die;
13563 /* Iterator of the ORIGIN_DIE children. */
13564 struct die_info *origin_child_die;
d389af10 13565 struct attribute *attr;
cd02d79d
PA
13566 struct dwarf2_cu *origin_cu;
13567 struct pending **origin_previous_list_in_scope;
d389af10
JK
13568
13569 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13570 if (!attr)
13571 return;
13572
cd02d79d
PA
13573 /* Note that following die references may follow to a die in a
13574 different cu. */
13575
13576 origin_cu = cu;
13577 origin_die = follow_die_ref (die, attr, &origin_cu);
13578
13579 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13580 symbols in. */
13581 origin_previous_list_in_scope = origin_cu->list_in_scope;
13582 origin_cu->list_in_scope = cu->list_in_scope;
13583
edb3359d
DJ
13584 if (die->tag != origin_die->tag
13585 && !(die->tag == DW_TAG_inlined_subroutine
13586 && origin_die->tag == DW_TAG_subprogram))
b98664d3 13587 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
13588 sect_offset_str (die->sect_off),
13589 sect_offset_str (origin_die->sect_off));
d389af10 13590
791afaa2 13591 std::vector<sect_offset> offsets;
d389af10 13592
3ea89b92
PMR
13593 for (child_die = die->child;
13594 child_die && child_die->tag;
13595 child_die = sibling_die (child_die))
13596 {
13597 struct die_info *child_origin_die;
13598 struct dwarf2_cu *child_origin_cu;
13599
13600 /* We are trying to process concrete instance entries:
216f72a1 13601 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13602 it's not relevant to our analysis here. i.e. detecting DIEs that are
13603 present in the abstract instance but not referenced in the concrete
13604 one. */
216f72a1
JK
13605 if (child_die->tag == DW_TAG_call_site
13606 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
13607 continue;
13608
c38f313d
DJ
13609 /* For each CHILD_DIE, find the corresponding child of
13610 ORIGIN_DIE. If there is more than one layer of
13611 DW_AT_abstract_origin, follow them all; there shouldn't be,
13612 but GCC versions at least through 4.4 generate this (GCC PR
13613 40573). */
3ea89b92
PMR
13614 child_origin_die = child_die;
13615 child_origin_cu = cu;
c38f313d
DJ
13616 while (1)
13617 {
cd02d79d
PA
13618 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13619 child_origin_cu);
c38f313d
DJ
13620 if (attr == NULL)
13621 break;
cd02d79d
PA
13622 child_origin_die = follow_die_ref (child_origin_die, attr,
13623 &child_origin_cu);
c38f313d
DJ
13624 }
13625
d389af10
JK
13626 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13627 counterpart may exist. */
c38f313d 13628 if (child_origin_die != child_die)
d389af10 13629 {
edb3359d
DJ
13630 if (child_die->tag != child_origin_die->tag
13631 && !(child_die->tag == DW_TAG_inlined_subroutine
13632 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 13633 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13634 "different tags"),
9d8780f0
SM
13635 sect_offset_str (child_die->sect_off),
13636 sect_offset_str (child_origin_die->sect_off));
c38f313d 13637 if (child_origin_die->parent != origin_die)
b98664d3 13638 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13639 "different parents"),
9d8780f0
SM
13640 sect_offset_str (child_die->sect_off),
13641 sect_offset_str (child_origin_die->sect_off));
c38f313d 13642 else
791afaa2 13643 offsets.push_back (child_origin_die->sect_off);
d389af10 13644 }
d389af10 13645 }
791afaa2
TT
13646 std::sort (offsets.begin (), offsets.end ());
13647 sect_offset *offsets_end = offsets.data () + offsets.size ();
13648 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13649 if (offsetp[-1] == *offsetp)
b98664d3 13650 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
13651 "to DIE %s as their abstract origin"),
13652 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13653
791afaa2 13654 offsetp = offsets.data ();
d389af10
JK
13655 origin_child_die = origin_die->child;
13656 while (origin_child_die && origin_child_die->tag)
13657 {
13658 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13659 while (offsetp < offsets_end
9c541725 13660 && *offsetp < origin_child_die->sect_off)
d389af10 13661 offsetp++;
b64f50a1 13662 if (offsetp >= offsets_end
9c541725 13663 || *offsetp > origin_child_die->sect_off)
d389af10 13664 {
adde2bff
DE
13665 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13666 Check whether we're already processing ORIGIN_CHILD_DIE.
13667 This can happen with mutually referenced abstract_origins.
13668 PR 16581. */
13669 if (!origin_child_die->in_process)
13670 process_die (origin_child_die, origin_cu);
d389af10
JK
13671 }
13672 origin_child_die = sibling_die (origin_child_die);
13673 }
cd02d79d 13674 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
13675}
13676
c906108c 13677static void
e7c27a73 13678read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13679{
518817b3 13680 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13681 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 13682 struct context_stack *newobj;
c906108c
SS
13683 CORE_ADDR lowpc;
13684 CORE_ADDR highpc;
13685 struct die_info *child_die;
edb3359d 13686 struct attribute *attr, *call_line, *call_file;
15d034d0 13687 const char *name;
e142c38c 13688 CORE_ADDR baseaddr;
801e3a5b 13689 struct block *block;
edb3359d 13690 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13691 std::vector<struct symbol *> template_args;
34eaf542 13692 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13693
13694 if (inlined_func)
13695 {
13696 /* If we do not have call site information, we can't show the
13697 caller of this inlined function. That's too confusing, so
13698 only use the scope for local variables. */
13699 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13700 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13701 if (call_line == NULL || call_file == NULL)
13702 {
13703 read_lexical_block_scope (die, cu);
13704 return;
13705 }
13706 }
c906108c 13707
e142c38c
DJ
13708 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13709
94af9270 13710 name = dwarf2_name (die, cu);
c906108c 13711
e8d05480
JB
13712 /* Ignore functions with missing or empty names. These are actually
13713 illegal according to the DWARF standard. */
13714 if (name == NULL)
13715 {
b98664d3 13716 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13717 sect_offset_str (die->sect_off));
e8d05480
JB
13718 return;
13719 }
13720
13721 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13722 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13723 <= PC_BOUNDS_INVALID)
e8d05480 13724 {
ae4d0c03
PM
13725 attr = dwarf2_attr (die, DW_AT_external, cu);
13726 if (!attr || !DW_UNSND (attr))
b98664d3 13727 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13728 "for subprogram DIE at %s"),
13729 sect_offset_str (die->sect_off));
e8d05480
JB
13730 return;
13731 }
c906108c 13732
3e29f34a
MR
13733 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13734 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13735
34eaf542
TT
13736 /* If we have any template arguments, then we must allocate a
13737 different sort of symbol. */
13738 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13739 {
13740 if (child_die->tag == DW_TAG_template_type_param
13741 || child_die->tag == DW_TAG_template_value_param)
13742 {
e623cf5d 13743 templ_func = allocate_template_symbol (objfile);
cf724bc9 13744 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13745 break;
13746 }
13747 }
13748
c24bdb02 13749 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13750 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13751 (struct symbol *) templ_func);
4c2df51b 13752
81873cc8
TV
13753 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13754 set_objfile_main_name (objfile, SYMBOL_LINKAGE_NAME (newobj->name),
13755 cu->language);
13756
4cecd739
DJ
13757 /* If there is a location expression for DW_AT_frame_base, record
13758 it. */
e142c38c 13759 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 13760 if (attr)
fe978cb0 13761 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13762
63e43d3a
PMR
13763 /* If there is a location for the static link, record it. */
13764 newobj->static_link = NULL;
13765 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13766 if (attr)
13767 {
224c3ddb
SM
13768 newobj->static_link
13769 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d
AB
13770 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13771 dwarf2_per_cu_addr_type (cu->per_cu));
63e43d3a
PMR
13772 }
13773
c24bdb02 13774 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13775
639d11d3 13776 if (die->child != NULL)
c906108c 13777 {
639d11d3 13778 child_die = die->child;
c906108c
SS
13779 while (child_die && child_die->tag)
13780 {
34eaf542
TT
13781 if (child_die->tag == DW_TAG_template_type_param
13782 || child_die->tag == DW_TAG_template_value_param)
13783 {
13784 struct symbol *arg = new_symbol (child_die, NULL, cu);
13785
f1078f66 13786 if (arg != NULL)
2f4732b0 13787 template_args.push_back (arg);
34eaf542
TT
13788 }
13789 else
13790 process_die (child_die, cu);
c906108c
SS
13791 child_die = sibling_die (child_die);
13792 }
13793 }
13794
d389af10
JK
13795 inherit_abstract_dies (die, cu);
13796
4a811a97
UW
13797 /* If we have a DW_AT_specification, we might need to import using
13798 directives from the context of the specification DIE. See the
13799 comment in determine_prefix. */
13800 if (cu->language == language_cplus
13801 && dwarf2_attr (die, DW_AT_specification, cu))
13802 {
13803 struct dwarf2_cu *spec_cu = cu;
13804 struct die_info *spec_die = die_specification (die, &spec_cu);
13805
13806 while (spec_die)
13807 {
13808 child_die = spec_die->child;
13809 while (child_die && child_die->tag)
13810 {
13811 if (child_die->tag == DW_TAG_imported_module)
13812 process_die (child_die, spec_cu);
13813 child_die = sibling_die (child_die);
13814 }
13815
13816 /* In some cases, GCC generates specification DIEs that
13817 themselves contain DW_AT_specification attributes. */
13818 spec_die = die_specification (spec_die, &spec_cu);
13819 }
13820 }
13821
c24bdb02 13822 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13823 /* Make a block for the local symbols within. */
c24bdb02 13824 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13825 cstk.static_link, lowpc, highpc);
801e3a5b 13826
df8a16a1 13827 /* For C++, set the block's scope. */
45280282
IB
13828 if ((cu->language == language_cplus
13829 || cu->language == language_fortran
c44af4eb
TT
13830 || cu->language == language_d
13831 || cu->language == language_rust)
4d4ec4e5 13832 && cu->processing_has_namespace_info)
195a3f6c
TT
13833 block_set_scope (block, determine_prefix (die, cu),
13834 &objfile->objfile_obstack);
df8a16a1 13835
801e3a5b
JB
13836 /* If we have address ranges, record them. */
13837 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13838
a60f3166 13839 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13840
34eaf542 13841 /* Attach template arguments to function. */
2f4732b0 13842 if (!template_args.empty ())
34eaf542
TT
13843 {
13844 gdb_assert (templ_func != NULL);
13845
2f4732b0 13846 templ_func->n_template_arguments = template_args.size ();
34eaf542 13847 templ_func->template_arguments
8d749320
SM
13848 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13849 templ_func->n_template_arguments);
34eaf542 13850 memcpy (templ_func->template_arguments,
2f4732b0 13851 template_args.data (),
34eaf542 13852 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13853
13854 /* Make sure that the symtab is set on the new symbols. Even
13855 though they don't appear in this symtab directly, other parts
13856 of gdb assume that symbols do, and this is reasonably
13857 true. */
8634679f 13858 for (symbol *sym : template_args)
3e1d3d8c 13859 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13860 }
13861
208d8187
JB
13862 /* In C++, we can have functions nested inside functions (e.g., when
13863 a function declares a class that has methods). This means that
13864 when we finish processing a function scope, we may need to go
13865 back to building a containing block's symbol lists. */
c24bdb02
KS
13866 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13867 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13868
921e78cf
JB
13869 /* If we've finished processing a top-level function, subsequent
13870 symbols go in the file symbol list. */
c24bdb02
KS
13871 if (cu->get_builder ()->outermost_context_p ())
13872 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13873}
13874
13875/* Process all the DIES contained within a lexical block scope. Start
13876 a new scope, process the dies, and then close the scope. */
13877
13878static void
e7c27a73 13879read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13880{
518817b3 13881 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13882 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
13883 CORE_ADDR lowpc, highpc;
13884 struct die_info *child_die;
e142c38c
DJ
13885 CORE_ADDR baseaddr;
13886
13887 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
13888
13889 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13890 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13891 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13892 be nasty. Might be easier to properly extend generic blocks to
af34e669 13893 describe ranges. */
e385593e
JK
13894 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13895 {
13896 case PC_BOUNDS_NOT_PRESENT:
13897 /* DW_TAG_lexical_block has no attributes, process its children as if
13898 there was no wrapping by that DW_TAG_lexical_block.
13899 GCC does no longer produces such DWARF since GCC r224161. */
13900 for (child_die = die->child;
13901 child_die != NULL && child_die->tag;
13902 child_die = sibling_die (child_die))
13903 process_die (child_die, cu);
13904 return;
13905 case PC_BOUNDS_INVALID:
13906 return;
13907 }
3e29f34a
MR
13908 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13909 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13910
c24bdb02 13911 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13912 if (die->child != NULL)
c906108c 13913 {
639d11d3 13914 child_die = die->child;
c906108c
SS
13915 while (child_die && child_die->tag)
13916 {
e7c27a73 13917 process_die (child_die, cu);
c906108c
SS
13918 child_die = sibling_die (child_die);
13919 }
13920 }
3ea89b92 13921 inherit_abstract_dies (die, cu);
c24bdb02 13922 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13923
c24bdb02
KS
13924 if (*cu->get_builder ()->get_local_symbols () != NULL
13925 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13926 {
801e3a5b 13927 struct block *block
c24bdb02 13928 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13929 cstk.start_addr, highpc);
801e3a5b
JB
13930
13931 /* Note that recording ranges after traversing children, as we
13932 do here, means that recording a parent's ranges entails
13933 walking across all its children's ranges as they appear in
13934 the address map, which is quadratic behavior.
13935
13936 It would be nicer to record the parent's ranges before
13937 traversing its children, simply overriding whatever you find
13938 there. But since we don't even decide whether to create a
13939 block until after we've traversed its children, that's hard
13940 to do. */
13941 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13942 }
c24bdb02
KS
13943 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13944 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13945}
13946
216f72a1 13947/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13948
13949static void
13950read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13951{
518817b3 13952 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
96408a79
SA
13953 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13954 CORE_ADDR pc, baseaddr;
13955 struct attribute *attr;
13956 struct call_site *call_site, call_site_local;
13957 void **slot;
13958 int nparams;
13959 struct die_info *child_die;
13960
13961 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13962
216f72a1
JK
13963 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13964 if (attr == NULL)
13965 {
13966 /* This was a pre-DWARF-5 GNU extension alias
13967 for DW_AT_call_return_pc. */
13968 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13969 }
96408a79
SA
13970 if (!attr)
13971 {
b98664d3 13972 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13973 "DIE %s [in module %s]"),
13974 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13975 return;
13976 }
31aa7e4e 13977 pc = attr_value_as_address (attr) + baseaddr;
3e29f34a 13978 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13979
13980 if (cu->call_site_htab == NULL)
13981 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13982 NULL, &objfile->objfile_obstack,
13983 hashtab_obstack_allocate, NULL);
13984 call_site_local.pc = pc;
13985 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13986 if (*slot != NULL)
13987 {
b98664d3 13988 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13989 "DIE %s [in module %s]"),
13990 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13991 objfile_name (objfile));
96408a79
SA
13992 return;
13993 }
13994
13995 /* Count parameters at the caller. */
13996
13997 nparams = 0;
13998 for (child_die = die->child; child_die && child_die->tag;
13999 child_die = sibling_die (child_die))
14000 {
216f72a1
JK
14001 if (child_die->tag != DW_TAG_call_site_parameter
14002 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 14003 {
b98664d3 14004 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
14005 "DW_TAG_call_site child DIE %s [in module %s]"),
14006 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 14007 objfile_name (objfile));
96408a79
SA
14008 continue;
14009 }
14010
14011 nparams++;
14012 }
14013
224c3ddb
SM
14014 call_site
14015 = ((struct call_site *)
14016 obstack_alloc (&objfile->objfile_obstack,
14017 sizeof (*call_site)
14018 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
14019 *slot = call_site;
14020 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
14021 call_site->pc = pc;
14022
216f72a1
JK
14023 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
14024 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
14025 {
14026 struct die_info *func_die;
14027
14028 /* Skip also over DW_TAG_inlined_subroutine. */
14029 for (func_die = die->parent;
14030 func_die && func_die->tag != DW_TAG_subprogram
14031 && func_die->tag != DW_TAG_subroutine_type;
14032 func_die = func_die->parent);
14033
216f72a1
JK
14034 /* DW_AT_call_all_calls is a superset
14035 of DW_AT_call_all_tail_calls. */
96408a79 14036 if (func_die
216f72a1 14037 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 14038 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 14039 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
14040 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
14041 {
14042 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
14043 not complete. But keep CALL_SITE for look ups via call_site_htab,
14044 both the initial caller containing the real return address PC and
14045 the final callee containing the current PC of a chain of tail
14046 calls do not need to have the tail call list complete. But any
14047 function candidate for a virtual tail call frame searched via
14048 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
14049 determined unambiguously. */
14050 }
14051 else
14052 {
14053 struct type *func_type = NULL;
14054
14055 if (func_die)
14056 func_type = get_die_type (func_die, cu);
14057 if (func_type != NULL)
14058 {
14059 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
14060
14061 /* Enlist this call site to the function. */
14062 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
14063 TYPE_TAIL_CALL_LIST (func_type) = call_site;
14064 }
14065 else
b98664d3 14066 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
14067 "DIE %s [in module %s]"),
14068 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14069 }
14070 }
14071
216f72a1
JK
14072 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14073 if (attr == NULL)
14074 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14075 if (attr == NULL)
14076 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 14077 if (attr == NULL)
216f72a1
JK
14078 {
14079 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14080 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14081 }
96408a79
SA
14082 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14083 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14084 /* Keep NULL DWARF_BLOCK. */;
14085 else if (attr_form_is_block (attr))
14086 {
14087 struct dwarf2_locexpr_baton *dlbaton;
14088
8d749320 14089 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
14090 dlbaton->data = DW_BLOCK (attr)->data;
14091 dlbaton->size = DW_BLOCK (attr)->size;
14092 dlbaton->per_cu = cu->per_cu;
14093
14094 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14095 }
7771576e 14096 else if (attr_form_is_ref (attr))
96408a79 14097 {
96408a79
SA
14098 struct dwarf2_cu *target_cu = cu;
14099 struct die_info *target_die;
14100
ac9ec31b 14101 target_die = follow_die_ref (die, attr, &target_cu);
518817b3 14102 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
96408a79
SA
14103 if (die_is_declaration (target_die, target_cu))
14104 {
7d45c7c3 14105 const char *target_physname;
9112db09
JK
14106
14107 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 14108 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 14109 if (target_physname == NULL)
9112db09 14110 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 14111 if (target_physname == NULL)
b98664d3 14112 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
14113 "physname, for referencing DIE %s [in module %s]"),
14114 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14115 else
7d455152 14116 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
14117 }
14118 else
14119 {
14120 CORE_ADDR lowpc;
14121
14122 /* DW_AT_entry_pc should be preferred. */
3a2b436a 14123 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 14124 <= PC_BOUNDS_INVALID)
b98664d3 14125 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
14126 "low pc, for referencing DIE %s [in module %s]"),
14127 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14128 else
3e29f34a
MR
14129 {
14130 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14131 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14132 }
96408a79
SA
14133 }
14134 }
14135 else
b98664d3 14136 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
14137 "block nor reference, for DIE %s [in module %s]"),
14138 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14139
14140 call_site->per_cu = cu->per_cu;
14141
14142 for (child_die = die->child;
14143 child_die && child_die->tag;
14144 child_die = sibling_die (child_die))
14145 {
96408a79 14146 struct call_site_parameter *parameter;
1788b2d3 14147 struct attribute *loc, *origin;
96408a79 14148
216f72a1
JK
14149 if (child_die->tag != DW_TAG_call_site_parameter
14150 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
14151 {
14152 /* Already printed the complaint above. */
14153 continue;
14154 }
14155
14156 gdb_assert (call_site->parameter_count < nparams);
14157 parameter = &call_site->parameter[call_site->parameter_count];
14158
1788b2d3
JK
14159 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14160 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 14161 register is contained in DW_AT_call_value. */
96408a79 14162
24c5c679 14163 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
14164 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14165 if (origin == NULL)
14166 {
14167 /* This was a pre-DWARF-5 GNU extension alias
14168 for DW_AT_call_parameter. */
14169 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14170 }
7771576e 14171 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3 14172 {
1788b2d3 14173 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
14174
14175 sect_offset sect_off
14176 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14177 if (!offset_in_cu_p (&cu->header, sect_off))
d76b7dbc
JK
14178 {
14179 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14180 binding can be done only inside one CU. Such referenced DIE
14181 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 14182 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
14183 "DW_TAG_call_site child DIE %s [in module %s]"),
14184 sect_offset_str (child_die->sect_off),
9c541725 14185 objfile_name (objfile));
d76b7dbc
JK
14186 continue;
14187 }
9c541725
PA
14188 parameter->u.param_cu_off
14189 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3
JK
14190 }
14191 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79 14192 {
b98664d3 14193 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
14194 "DW_TAG_call_site child DIE %s [in module %s]"),
14195 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
14196 continue;
14197 }
24c5c679 14198 else
96408a79 14199 {
24c5c679
JK
14200 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14201 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14202 if (parameter->u.dwarf_reg != -1)
14203 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14204 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14205 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14206 &parameter->u.fb_offset))
14207 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14208 else
14209 {
b98664d3 14210 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 14211 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 14212 "DW_TAG_call_site child DIE %s "
24c5c679 14213 "[in module %s]"),
9d8780f0 14214 sect_offset_str (child_die->sect_off),
9c541725 14215 objfile_name (objfile));
24c5c679
JK
14216 continue;
14217 }
96408a79
SA
14218 }
14219
216f72a1
JK
14220 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14221 if (attr == NULL)
14222 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
96408a79
SA
14223 if (!attr_form_is_block (attr))
14224 {
b98664d3 14225 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
14226 "DW_TAG_call_site child DIE %s [in module %s]"),
14227 sect_offset_str (child_die->sect_off),
9c541725 14228 objfile_name (objfile));
96408a79
SA
14229 continue;
14230 }
14231 parameter->value = DW_BLOCK (attr)->data;
14232 parameter->value_size = DW_BLOCK (attr)->size;
14233
14234 /* Parameters are not pre-cleared by memset above. */
14235 parameter->data_value = NULL;
14236 parameter->data_value_size = 0;
14237 call_site->parameter_count++;
14238
216f72a1
JK
14239 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14240 if (attr == NULL)
14241 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
96408a79
SA
14242 if (attr)
14243 {
14244 if (!attr_form_is_block (attr))
b98664d3 14245 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
14246 "DW_TAG_call_site child DIE %s [in module %s]"),
14247 sect_offset_str (child_die->sect_off),
9c541725 14248 objfile_name (objfile));
96408a79
SA
14249 else
14250 {
14251 parameter->data_value = DW_BLOCK (attr)->data;
14252 parameter->data_value_size = DW_BLOCK (attr)->size;
14253 }
14254 }
14255 }
14256}
14257
71a3c369
TT
14258/* Helper function for read_variable. If DIE represents a virtual
14259 table, then return the type of the concrete object that is
14260 associated with the virtual table. Otherwise, return NULL. */
14261
14262static struct type *
14263rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14264{
14265 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14266 if (attr == NULL)
14267 return NULL;
14268
14269 /* Find the type DIE. */
14270 struct die_info *type_die = NULL;
14271 struct dwarf2_cu *type_cu = cu;
14272
14273 if (attr_form_is_ref (attr))
14274 type_die = follow_die_ref (die, attr, &type_cu);
14275 if (type_die == NULL)
14276 return NULL;
14277
14278 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14279 return NULL;
14280 return die_containing_type (type_die, type_cu);
14281}
14282
14283/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14284
14285static void
14286read_variable (struct die_info *die, struct dwarf2_cu *cu)
14287{
14288 struct rust_vtable_symbol *storage = NULL;
14289
14290 if (cu->language == language_rust)
14291 {
14292 struct type *containing_type = rust_containing_type (die, cu);
14293
14294 if (containing_type != NULL)
14295 {
518817b3 14296 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
71a3c369
TT
14297
14298 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14299 struct rust_vtable_symbol);
14300 initialize_objfile_symbol (storage);
14301 storage->concrete_type = containing_type;
cf724bc9 14302 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
14303 }
14304 }
14305
e4a62c65
TV
14306 struct symbol *res = new_symbol (die, NULL, cu, storage);
14307 struct attribute *abstract_origin
14308 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14309 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14310 if (res == NULL && loc && abstract_origin)
14311 {
14312 /* We have a variable without a name, but with a location and an abstract
14313 origin. This may be a concrete instance of an abstract variable
14314 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14315 later. */
14316 struct dwarf2_cu *origin_cu = cu;
14317 struct die_info *origin_die
14318 = follow_die_ref (die, abstract_origin, &origin_cu);
14319 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
3360b6e7 14320 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
e4a62c65 14321 }
71a3c369
TT
14322}
14323
43988095
JK
14324/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14325 reading .debug_rnglists.
14326 Callback's type should be:
14327 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14328 Return true if the attributes are present and valid, otherwise,
14329 return false. */
14330
14331template <typename Callback>
14332static bool
14333dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14334 Callback &&callback)
14335{
ed2dc618 14336 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14337 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14338 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 14339 bfd *obfd = objfile->obfd;
43988095
JK
14340 /* Base address selection entry. */
14341 CORE_ADDR base;
14342 int found_base;
43988095 14343 const gdb_byte *buffer;
43988095
JK
14344 CORE_ADDR baseaddr;
14345 bool overflow = false;
14346
14347 found_base = cu->base_known;
14348 base = cu->base_address;
14349
14350 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14351 if (offset >= dwarf2_per_objfile->rnglists.size)
14352 {
b98664d3 14353 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
14354 offset);
14355 return false;
14356 }
14357 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14358
14359 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14360
14361 while (1)
14362 {
7814882a
JK
14363 /* Initialize it due to a false compiler warning. */
14364 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
14365 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14366 + dwarf2_per_objfile->rnglists.size);
14367 unsigned int bytes_read;
14368
14369 if (buffer == buf_end)
14370 {
14371 overflow = true;
14372 break;
14373 }
14374 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14375 switch (rlet)
14376 {
14377 case DW_RLE_end_of_list:
14378 break;
14379 case DW_RLE_base_address:
14380 if (buffer + cu->header.addr_size > buf_end)
14381 {
14382 overflow = true;
14383 break;
14384 }
14385 base = read_address (obfd, buffer, cu, &bytes_read);
14386 found_base = 1;
14387 buffer += bytes_read;
14388 break;
14389 case DW_RLE_start_length:
14390 if (buffer + cu->header.addr_size > buf_end)
14391 {
14392 overflow = true;
14393 break;
14394 }
14395 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14396 buffer += bytes_read;
14397 range_end = (range_beginning
14398 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14399 buffer += bytes_read;
14400 if (buffer > buf_end)
14401 {
14402 overflow = true;
14403 break;
14404 }
14405 break;
14406 case DW_RLE_offset_pair:
14407 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14408 buffer += bytes_read;
14409 if (buffer > buf_end)
14410 {
14411 overflow = true;
14412 break;
14413 }
14414 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14415 buffer += bytes_read;
14416 if (buffer > buf_end)
14417 {
14418 overflow = true;
14419 break;
14420 }
14421 break;
14422 case DW_RLE_start_end:
14423 if (buffer + 2 * cu->header.addr_size > buf_end)
14424 {
14425 overflow = true;
14426 break;
14427 }
14428 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14429 buffer += bytes_read;
14430 range_end = read_address (obfd, buffer, cu, &bytes_read);
14431 buffer += bytes_read;
14432 break;
14433 default:
b98664d3 14434 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14435 return false;
14436 }
14437 if (rlet == DW_RLE_end_of_list || overflow)
14438 break;
14439 if (rlet == DW_RLE_base_address)
14440 continue;
14441
14442 if (!found_base)
14443 {
14444 /* We have no valid base address for the ranges
14445 data. */
b98664d3 14446 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14447 return false;
14448 }
14449
14450 if (range_beginning > range_end)
14451 {
14452 /* Inverted range entries are invalid. */
b98664d3 14453 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
14454 return false;
14455 }
14456
14457 /* Empty range entries have no effect. */
14458 if (range_beginning == range_end)
14459 continue;
14460
14461 range_beginning += base;
14462 range_end += base;
14463
14464 /* A not-uncommon case of bad debug info.
14465 Don't pollute the addrmap with bad data. */
14466 if (range_beginning + baseaddr == 0
14467 && !dwarf2_per_objfile->has_section_at_zero)
14468 {
b98664d3 14469 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
14470 " [in module %s]"), objfile_name (objfile));
14471 continue;
14472 }
14473
14474 callback (range_beginning, range_end);
14475 }
14476
14477 if (overflow)
14478 {
b98664d3 14479 complaint (_("Offset %d is not terminated "
43988095
JK
14480 "for DW_AT_ranges attribute"),
14481 offset);
14482 return false;
14483 }
14484
14485 return true;
14486}
14487
14488/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14489 Callback's type should be:
14490 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 14491 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 14492
43988095 14493template <typename Callback>
43039443 14494static int
5f46c5a5 14495dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 14496 Callback &&callback)
43039443 14497{
ed2dc618 14498 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14499 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14500 struct objfile *objfile = dwarf2_per_objfile->objfile;
43039443
JK
14501 struct comp_unit_head *cu_header = &cu->header;
14502 bfd *obfd = objfile->obfd;
14503 unsigned int addr_size = cu_header->addr_size;
14504 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14505 /* Base address selection entry. */
14506 CORE_ADDR base;
14507 int found_base;
14508 unsigned int dummy;
d521ce57 14509 const gdb_byte *buffer;
ff013f42 14510 CORE_ADDR baseaddr;
43039443 14511
43988095
JK
14512 if (cu_header->version >= 5)
14513 return dwarf2_rnglists_process (offset, cu, callback);
14514
d00adf39
DE
14515 found_base = cu->base_known;
14516 base = cu->base_address;
43039443 14517
be391dca 14518 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 14519 if (offset >= dwarf2_per_objfile->ranges.size)
43039443 14520 {
b98664d3 14521 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
14522 offset);
14523 return 0;
14524 }
dce234bc 14525 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 14526
e7030f15 14527 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 14528
43039443
JK
14529 while (1)
14530 {
14531 CORE_ADDR range_beginning, range_end;
14532
14533 range_beginning = read_address (obfd, buffer, cu, &dummy);
14534 buffer += addr_size;
14535 range_end = read_address (obfd, buffer, cu, &dummy);
14536 buffer += addr_size;
14537 offset += 2 * addr_size;
14538
14539 /* An end of list marker is a pair of zero addresses. */
14540 if (range_beginning == 0 && range_end == 0)
14541 /* Found the end of list entry. */
14542 break;
14543
14544 /* Each base address selection entry is a pair of 2 values.
14545 The first is the largest possible address, the second is
14546 the base address. Check for a base address here. */
14547 if ((range_beginning & mask) == mask)
14548 {
28d2bfb9
AB
14549 /* If we found the largest possible address, then we already
14550 have the base address in range_end. */
14551 base = range_end;
43039443
JK
14552 found_base = 1;
14553 continue;
14554 }
14555
14556 if (!found_base)
14557 {
14558 /* We have no valid base address for the ranges
14559 data. */
b98664d3 14560 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
14561 return 0;
14562 }
14563
9277c30c
UW
14564 if (range_beginning > range_end)
14565 {
14566 /* Inverted range entries are invalid. */
b98664d3 14567 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
14568 return 0;
14569 }
14570
14571 /* Empty range entries have no effect. */
14572 if (range_beginning == range_end)
14573 continue;
14574
43039443
JK
14575 range_beginning += base;
14576 range_end += base;
14577
01093045
DE
14578 /* A not-uncommon case of bad debug info.
14579 Don't pollute the addrmap with bad data. */
14580 if (range_beginning + baseaddr == 0
14581 && !dwarf2_per_objfile->has_section_at_zero)
14582 {
b98664d3 14583 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 14584 " [in module %s]"), objfile_name (objfile));
01093045
DE
14585 continue;
14586 }
14587
5f46c5a5
JK
14588 callback (range_beginning, range_end);
14589 }
14590
14591 return 1;
14592}
14593
14594/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14595 Return 1 if the attributes are present and valid, otherwise, return 0.
14596 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14597
14598static int
14599dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14600 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14601 struct partial_symtab *ranges_pst)
14602{
518817b3 14603 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5f46c5a5
JK
14604 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14605 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14606 SECT_OFF_TEXT (objfile));
14607 int low_set = 0;
14608 CORE_ADDR low = 0;
14609 CORE_ADDR high = 0;
14610 int retval;
14611
14612 retval = dwarf2_ranges_process (offset, cu,
14613 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14614 {
9277c30c 14615 if (ranges_pst != NULL)
3e29f34a
MR
14616 {
14617 CORE_ADDR lowpc;
14618 CORE_ADDR highpc;
14619
79748972
TT
14620 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14621 range_beginning + baseaddr)
14622 - baseaddr);
14623 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14624 range_end + baseaddr)
14625 - baseaddr);
d320c2b5
TT
14626 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14627 lowpc, highpc - 1, ranges_pst);
3e29f34a 14628 }
ff013f42 14629
43039443
JK
14630 /* FIXME: This is recording everything as a low-high
14631 segment of consecutive addresses. We should have a
14632 data structure for discontiguous block ranges
14633 instead. */
14634 if (! low_set)
14635 {
14636 low = range_beginning;
14637 high = range_end;
14638 low_set = 1;
14639 }
14640 else
14641 {
14642 if (range_beginning < low)
14643 low = range_beginning;
14644 if (range_end > high)
14645 high = range_end;
14646 }
5f46c5a5
JK
14647 });
14648 if (!retval)
14649 return 0;
43039443
JK
14650
14651 if (! low_set)
14652 /* If the first entry is an end-of-list marker, the range
14653 describes an empty scope, i.e. no instructions. */
14654 return 0;
14655
14656 if (low_return)
14657 *low_return = low;
14658 if (high_return)
14659 *high_return = high;
14660 return 1;
14661}
14662
3a2b436a
JK
14663/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14664 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14665 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14666
3a2b436a 14667static enum pc_bounds_kind
af34e669 14668dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
14669 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14670 struct partial_symtab *pst)
c906108c 14671{
518817b3
SM
14672 struct dwarf2_per_objfile *dwarf2_per_objfile
14673 = cu->per_cu->dwarf2_per_objfile;
c906108c 14674 struct attribute *attr;
91da1414 14675 struct attribute *attr_high;
af34e669
DJ
14676 CORE_ADDR low = 0;
14677 CORE_ADDR high = 0;
e385593e 14678 enum pc_bounds_kind ret;
c906108c 14679
91da1414
MW
14680 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14681 if (attr_high)
af34e669 14682 {
e142c38c 14683 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 14684 if (attr)
91da1414 14685 {
31aa7e4e
JB
14686 low = attr_value_as_address (attr);
14687 high = attr_value_as_address (attr_high);
14688 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14689 high += low;
91da1414 14690 }
af34e669
DJ
14691 else
14692 /* Found high w/o low attribute. */
e385593e 14693 return PC_BOUNDS_INVALID;
af34e669
DJ
14694
14695 /* Found consecutive range of addresses. */
3a2b436a 14696 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14697 }
c906108c 14698 else
af34e669 14699 {
e142c38c 14700 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
14701 if (attr != NULL)
14702 {
ab435259
DE
14703 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14704 We take advantage of the fact that DW_AT_ranges does not appear
14705 in DW_TAG_compile_unit of DWO files. */
14706 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14707 unsigned int ranges_offset = (DW_UNSND (attr)
14708 + (need_ranges_base
14709 ? cu->ranges_base
14710 : 0));
2e3cf129 14711
af34e669 14712 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14713 .debug_ranges section. */
2e3cf129 14714 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 14715 return PC_BOUNDS_INVALID;
43039443 14716 /* Found discontinuous range of addresses. */
3a2b436a 14717 ret = PC_BOUNDS_RANGES;
af34e669 14718 }
e385593e
JK
14719 else
14720 return PC_BOUNDS_NOT_PRESENT;
af34e669 14721 }
c906108c 14722
48fbe735 14723 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14724 if (high <= low)
e385593e 14725 return PC_BOUNDS_INVALID;
c906108c
SS
14726
14727 /* When using the GNU linker, .gnu.linkonce. sections are used to
14728 eliminate duplicate copies of functions and vtables and such.
14729 The linker will arbitrarily choose one and discard the others.
14730 The AT_*_pc values for such functions refer to local labels in
14731 these sections. If the section from that file was discarded, the
14732 labels are not in the output, so the relocs get a value of 0.
14733 If this is a discarded function, mark the pc bounds as invalid,
14734 so that GDB will ignore it. */
72dca2f5 14735 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 14736 return PC_BOUNDS_INVALID;
c906108c
SS
14737
14738 *lowpc = low;
96408a79
SA
14739 if (highpc)
14740 *highpc = high;
af34e669 14741 return ret;
c906108c
SS
14742}
14743
b084d499
JB
14744/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14745 its low and high PC addresses. Do nothing if these addresses could not
14746 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14747 and HIGHPC to the high address if greater than HIGHPC. */
14748
14749static void
14750dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14751 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14752 struct dwarf2_cu *cu)
14753{
14754 CORE_ADDR low, high;
14755 struct die_info *child = die->child;
14756
e385593e 14757 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14758 {
325fac50
PA
14759 *lowpc = std::min (*lowpc, low);
14760 *highpc = std::max (*highpc, high);
b084d499
JB
14761 }
14762
14763 /* If the language does not allow nested subprograms (either inside
14764 subprograms or lexical blocks), we're done. */
14765 if (cu->language != language_ada)
14766 return;
6e70227d 14767
b084d499
JB
14768 /* Check all the children of the given DIE. If it contains nested
14769 subprograms, then check their pc bounds. Likewise, we need to
14770 check lexical blocks as well, as they may also contain subprogram
14771 definitions. */
14772 while (child && child->tag)
14773 {
14774 if (child->tag == DW_TAG_subprogram
14775 || child->tag == DW_TAG_lexical_block)
14776 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14777 child = sibling_die (child);
14778 }
14779}
14780
fae299cd
DC
14781/* Get the low and high pc's represented by the scope DIE, and store
14782 them in *LOWPC and *HIGHPC. If the correct values can't be
14783 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14784
14785static void
14786get_scope_pc_bounds (struct die_info *die,
14787 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14788 struct dwarf2_cu *cu)
14789{
14790 CORE_ADDR best_low = (CORE_ADDR) -1;
14791 CORE_ADDR best_high = (CORE_ADDR) 0;
14792 CORE_ADDR current_low, current_high;
14793
3a2b436a 14794 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14795 >= PC_BOUNDS_RANGES)
fae299cd
DC
14796 {
14797 best_low = current_low;
14798 best_high = current_high;
14799 }
14800 else
14801 {
14802 struct die_info *child = die->child;
14803
14804 while (child && child->tag)
14805 {
14806 switch (child->tag) {
14807 case DW_TAG_subprogram:
b084d499 14808 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14809 break;
14810 case DW_TAG_namespace:
f55ee35c 14811 case DW_TAG_module:
fae299cd
DC
14812 /* FIXME: carlton/2004-01-16: Should we do this for
14813 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14814 that current GCC's always emit the DIEs corresponding
14815 to definitions of methods of classes as children of a
14816 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14817 the DIEs giving the declarations, which could be
14818 anywhere). But I don't see any reason why the
14819 standards says that they have to be there. */
14820 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14821
14822 if (current_low != ((CORE_ADDR) -1))
14823 {
325fac50
PA
14824 best_low = std::min (best_low, current_low);
14825 best_high = std::max (best_high, current_high);
fae299cd
DC
14826 }
14827 break;
14828 default:
0963b4bd 14829 /* Ignore. */
fae299cd
DC
14830 break;
14831 }
14832
14833 child = sibling_die (child);
14834 }
14835 }
14836
14837 *lowpc = best_low;
14838 *highpc = best_high;
14839}
14840
801e3a5b
JB
14841/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14842 in DIE. */
380bca97 14843
801e3a5b
JB
14844static void
14845dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14846 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14847{
518817b3 14848 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 14849 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 14850 struct attribute *attr;
91da1414 14851 struct attribute *attr_high;
801e3a5b 14852
91da1414
MW
14853 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14854 if (attr_high)
801e3a5b 14855 {
801e3a5b
JB
14856 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14857 if (attr)
14858 {
31aa7e4e
JB
14859 CORE_ADDR low = attr_value_as_address (attr);
14860 CORE_ADDR high = attr_value_as_address (attr_high);
14861
14862 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14863 high += low;
9a619af0 14864
3e29f34a
MR
14865 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14866 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14867 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
14868 }
14869 }
14870
14871 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14872 if (attr)
14873 {
ab435259
DE
14874 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14875 We take advantage of the fact that DW_AT_ranges does not appear
14876 in DW_TAG_compile_unit of DWO files. */
14877 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14878
14879 /* The value of the DW_AT_ranges attribute is the offset of the
14880 address range list in the .debug_ranges section. */
ab435259
DE
14881 unsigned long offset = (DW_UNSND (attr)
14882 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14883
2d5f09ec 14884 std::vector<blockrange> blockvec;
5f46c5a5
JK
14885 dwarf2_ranges_process (offset, cu,
14886 [&] (CORE_ADDR start, CORE_ADDR end)
14887 {
58fdfd2c
JK
14888 start += baseaddr;
14889 end += baseaddr;
5f46c5a5
JK
14890 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14891 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14892 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14893 blockvec.emplace_back (start, end);
5f46c5a5 14894 });
2d5f09ec
KB
14895
14896 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14897 }
14898}
14899
685b1105
JK
14900/* Check whether the producer field indicates either of GCC < 4.6, or the
14901 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14902
685b1105
JK
14903static void
14904check_producer (struct dwarf2_cu *cu)
60d5a603 14905{
38360086 14906 int major, minor;
60d5a603
JK
14907
14908 if (cu->producer == NULL)
14909 {
14910 /* For unknown compilers expect their behavior is DWARF version
14911 compliant.
14912
14913 GCC started to support .debug_types sections by -gdwarf-4 since
14914 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14915 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14916 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14917 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14918 }
b1ffba5a 14919 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14920 {
38360086
MW
14921 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14922 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14923 }
5230b05a 14924 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14925 {
14926 cu->producer_is_icc = true;
14927 cu->producer_is_icc_lt_14 = major < 14;
14928 }
c258c396
JD
14929 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14930 cu->producer_is_codewarrior = true;
685b1105
JK
14931 else
14932 {
14933 /* For other non-GCC compilers, expect their behavior is DWARF version
14934 compliant. */
60d5a603
JK
14935 }
14936
9068261f 14937 cu->checked_producer = true;
685b1105 14938}
ba919b58 14939
685b1105
JK
14940/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14941 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14942 during 4.6.0 experimental. */
14943
9068261f 14944static bool
685b1105
JK
14945producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14946{
14947 if (!cu->checked_producer)
14948 check_producer (cu);
14949
14950 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14951}
14952
c258c396
JD
14953
14954/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14955 with incorrect is_stmt attributes. */
14956
14957static bool
14958producer_is_codewarrior (struct dwarf2_cu *cu)
14959{
14960 if (!cu->checked_producer)
14961 check_producer (cu);
14962
14963 return cu->producer_is_codewarrior;
14964}
14965
405feb71 14966/* Return the default accessibility type if it is not overridden by
60d5a603
JK
14967 DW_AT_accessibility. */
14968
14969static enum dwarf_access_attribute
14970dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14971{
14972 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14973 {
14974 /* The default DWARF 2 accessibility for members is public, the default
14975 accessibility for inheritance is private. */
14976
14977 if (die->tag != DW_TAG_inheritance)
14978 return DW_ACCESS_public;
14979 else
14980 return DW_ACCESS_private;
14981 }
14982 else
14983 {
14984 /* DWARF 3+ defines the default accessibility a different way. The same
14985 rules apply now for DW_TAG_inheritance as for the members and it only
14986 depends on the container kind. */
14987
14988 if (die->parent->tag == DW_TAG_class_type)
14989 return DW_ACCESS_private;
14990 else
14991 return DW_ACCESS_public;
14992 }
14993}
14994
74ac6d43
TT
14995/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14996 offset. If the attribute was not found return 0, otherwise return
14997 1. If it was found but could not properly be handled, set *OFFSET
14998 to 0. */
14999
15000static int
15001handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15002 LONGEST *offset)
15003{
15004 struct attribute *attr;
15005
15006 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15007 if (attr != NULL)
15008 {
15009 *offset = 0;
15010
15011 /* Note that we do not check for a section offset first here.
15012 This is because DW_AT_data_member_location is new in DWARF 4,
15013 so if we see it, we can assume that a constant form is really
15014 a constant and not a section offset. */
15015 if (attr_form_is_constant (attr))
15016 *offset = dwarf2_get_attr_constant_value (attr, 0);
15017 else if (attr_form_is_section_offset (attr))
15018 dwarf2_complex_location_expr_complaint ();
15019 else if (attr_form_is_block (attr))
15020 *offset = decode_locdesc (DW_BLOCK (attr), cu);
15021 else
15022 dwarf2_complex_location_expr_complaint ();
15023
15024 return 1;
15025 }
15026
15027 return 0;
15028}
15029
c906108c
SS
15030/* Add an aggregate field to the field list. */
15031
15032static void
107d2387 15033dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 15034 struct dwarf2_cu *cu)
6e70227d 15035{
518817b3 15036 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5e2b427d 15037 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
15038 struct nextfield *new_field;
15039 struct attribute *attr;
15040 struct field *fp;
15d034d0 15041 const char *fieldname = "";
c906108c 15042
7d0ccb61
DJ
15043 if (die->tag == DW_TAG_inheritance)
15044 {
be2daae6
TT
15045 fip->baseclasses.emplace_back ();
15046 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
15047 }
15048 else
15049 {
be2daae6
TT
15050 fip->fields.emplace_back ();
15051 new_field = &fip->fields.back ();
7d0ccb61 15052 }
be2daae6 15053
c906108c
SS
15054 fip->nfields++;
15055
e142c38c 15056 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
15057 if (attr)
15058 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
15059 else
15060 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
15061 if (new_field->accessibility != DW_ACCESS_public)
15062 fip->non_public_fields = 1;
60d5a603 15063
e142c38c 15064 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
15065 if (attr)
15066 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
15067 else
15068 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
15069
15070 fp = &new_field->field;
a9a9bd0f 15071
e142c38c 15072 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 15073 {
74ac6d43
TT
15074 LONGEST offset;
15075
a9a9bd0f 15076 /* Data member other than a C++ static data member. */
6e70227d 15077
c906108c 15078 /* Get type of field. */
e7c27a73 15079 fp->type = die_type (die, cu);
c906108c 15080
d6a843b5 15081 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 15082
c906108c 15083 /* Get bit size of field (zero if none). */
e142c38c 15084 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
15085 if (attr)
15086 {
15087 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15088 }
15089 else
15090 {
15091 FIELD_BITSIZE (*fp) = 0;
15092 }
15093
15094 /* Get bit offset of field. */
74ac6d43
TT
15095 if (handle_data_member_location (die, cu, &offset))
15096 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 15097 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
15098 if (attr)
15099 {
5e2b427d 15100 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
15101 {
15102 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
15103 additional bit offset from the MSB of the containing
15104 anonymous object to the MSB of the field. We don't
15105 have to do anything special since we don't need to
15106 know the size of the anonymous object. */
f41f5e61 15107 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
15108 }
15109 else
15110 {
15111 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
15112 MSB of the anonymous object, subtract off the number of
15113 bits from the MSB of the field to the MSB of the
15114 object, and then subtract off the number of bits of
15115 the field itself. The result is the bit offset of
15116 the LSB of the field. */
c906108c
SS
15117 int anonymous_size;
15118 int bit_offset = DW_UNSND (attr);
15119
e142c38c 15120 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
15121 if (attr)
15122 {
15123 /* The size of the anonymous object containing
15124 the bit field is explicit, so use the
15125 indicated size (in bytes). */
15126 anonymous_size = DW_UNSND (attr);
15127 }
15128 else
15129 {
15130 /* The size of the anonymous object containing
15131 the bit field must be inferred from the type
15132 attribute of the data member containing the
15133 bit field. */
15134 anonymous_size = TYPE_LENGTH (fp->type);
15135 }
f41f5e61
PA
15136 SET_FIELD_BITPOS (*fp,
15137 (FIELD_BITPOS (*fp)
15138 + anonymous_size * bits_per_byte
15139 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
15140 }
15141 }
da5b30da
AA
15142 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15143 if (attr != NULL)
15144 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15145 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
15146
15147 /* Get name of field. */
39cbfefa
DJ
15148 fieldname = dwarf2_name (die, cu);
15149 if (fieldname == NULL)
15150 fieldname = "";
d8151005
DJ
15151
15152 /* The name is already allocated along with this objfile, so we don't
15153 need to duplicate it for the type. */
15154 fp->name = fieldname;
c906108c
SS
15155
15156 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 15157 pointer or virtual base class pointer) to private. */
e142c38c 15158 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 15159 {
d48cc9dd 15160 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
15161 new_field->accessibility = DW_ACCESS_private;
15162 fip->non_public_fields = 1;
15163 }
15164 }
a9a9bd0f 15165 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 15166 {
a9a9bd0f
DC
15167 /* C++ static member. */
15168
15169 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15170 is a declaration, but all versions of G++ as of this writing
15171 (so through at least 3.2.1) incorrectly generate
15172 DW_TAG_variable tags. */
6e70227d 15173
ff355380 15174 const char *physname;
c906108c 15175
a9a9bd0f 15176 /* Get name of field. */
39cbfefa
DJ
15177 fieldname = dwarf2_name (die, cu);
15178 if (fieldname == NULL)
c906108c
SS
15179 return;
15180
254e6b9e 15181 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
15182 if (attr
15183 /* Only create a symbol if this is an external value.
15184 new_symbol checks this and puts the value in the global symbol
15185 table, which we want. If it is not external, new_symbol
15186 will try to put the value in cu->list_in_scope which is wrong. */
15187 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
15188 {
15189 /* A static const member, not much different than an enum as far as
15190 we're concerned, except that we can support more types. */
15191 new_symbol (die, NULL, cu);
15192 }
15193
2df3850c 15194 /* Get physical name. */
ff355380 15195 physname = dwarf2_physname (fieldname, die, cu);
c906108c 15196
d8151005
DJ
15197 /* The name is already allocated along with this objfile, so we don't
15198 need to duplicate it for the type. */
15199 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 15200 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 15201 FIELD_NAME (*fp) = fieldname;
c906108c
SS
15202 }
15203 else if (die->tag == DW_TAG_inheritance)
15204 {
74ac6d43 15205 LONGEST offset;
d4b96c9a 15206
74ac6d43
TT
15207 /* C++ base class field. */
15208 if (handle_data_member_location (die, cu, &offset))
15209 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 15210 FIELD_BITSIZE (*fp) = 0;
e7c27a73 15211 FIELD_TYPE (*fp) = die_type (die, cu);
a737d952 15212 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
c906108c 15213 }
2ddeaf8a
TT
15214 else if (die->tag == DW_TAG_variant_part)
15215 {
15216 /* process_structure_scope will treat this DIE as a union. */
15217 process_structure_scope (die, cu);
15218
15219 /* The variant part is relative to the start of the enclosing
15220 structure. */
15221 SET_FIELD_BITPOS (*fp, 0);
15222 fp->type = get_die_type (die, cu);
15223 fp->artificial = 1;
15224 fp->name = "<<variant>>";
c8c81635
TT
15225
15226 /* Normally a DW_TAG_variant_part won't have a size, but our
15227 representation requires one, so set it to the maximum of the
15228 child sizes. */
15229 if (TYPE_LENGTH (fp->type) == 0)
15230 {
15231 unsigned max = 0;
15232 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
15233 if (TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)) > max)
15234 max = TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i));
15235 TYPE_LENGTH (fp->type) = max;
15236 }
2ddeaf8a
TT
15237 }
15238 else
15239 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
15240}
15241
883fd55a
KS
15242/* Can the type given by DIE define another type? */
15243
15244static bool
15245type_can_define_types (const struct die_info *die)
15246{
15247 switch (die->tag)
15248 {
15249 case DW_TAG_typedef:
15250 case DW_TAG_class_type:
15251 case DW_TAG_structure_type:
15252 case DW_TAG_union_type:
15253 case DW_TAG_enumeration_type:
15254 return true;
15255
15256 default:
15257 return false;
15258 }
15259}
15260
15261/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
15262
15263static void
883fd55a
KS
15264dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15265 struct dwarf2_cu *cu)
6e70227d 15266{
be2daae6
TT
15267 struct decl_field fp;
15268 memset (&fp, 0, sizeof (fp));
98751a41 15269
883fd55a 15270 gdb_assert (type_can_define_types (die));
98751a41 15271
883fd55a 15272 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
15273 fp.name = dwarf2_name (die, cu);
15274 fp.type = read_type_die (die, cu);
98751a41 15275
c191a687
KS
15276 /* Save accessibility. */
15277 enum dwarf_access_attribute accessibility;
15278 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15279 if (attr != NULL)
15280 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15281 else
15282 accessibility = dwarf2_default_access_attribute (die, cu);
15283 switch (accessibility)
15284 {
15285 case DW_ACCESS_public:
15286 /* The assumed value if neither private nor protected. */
15287 break;
15288 case DW_ACCESS_private:
be2daae6 15289 fp.is_private = 1;
c191a687
KS
15290 break;
15291 case DW_ACCESS_protected:
be2daae6 15292 fp.is_protected = 1;
c191a687
KS
15293 break;
15294 default:
b98664d3 15295 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
15296 }
15297
883fd55a 15298 if (die->tag == DW_TAG_typedef)
be2daae6 15299 fip->typedef_field_list.push_back (fp);
883fd55a 15300 else
be2daae6 15301 fip->nested_types_list.push_back (fp);
98751a41
JK
15302}
15303
c906108c
SS
15304/* Create the vector of fields, and attach it to the type. */
15305
15306static void
fba45db2 15307dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15308 struct dwarf2_cu *cu)
c906108c
SS
15309{
15310 int nfields = fip->nfields;
15311
15312 /* Record the field count, allocate space for the array of fields,
15313 and create blank accessibility bitfields if necessary. */
15314 TYPE_NFIELDS (type) = nfields;
15315 TYPE_FIELDS (type) = (struct field *)
be2daae6 15316 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
c906108c 15317
b4ba55a1 15318 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
15319 {
15320 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15321
15322 TYPE_FIELD_PRIVATE_BITS (type) =
15323 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15324 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15325
15326 TYPE_FIELD_PROTECTED_BITS (type) =
15327 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15328 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15329
774b6a14
TT
15330 TYPE_FIELD_IGNORE_BITS (type) =
15331 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15332 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
15333 }
15334
15335 /* If the type has baseclasses, allocate and clear a bit vector for
15336 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 15337 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 15338 {
be2daae6 15339 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 15340 unsigned char *pointer;
c906108c
SS
15341
15342 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 15343 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 15344 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
15345 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15346 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
15347 }
15348
2ddeaf8a
TT
15349 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15350 {
15351 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15352
be2daae6 15353 for (int index = 0; index < nfields; ++index)
2ddeaf8a 15354 {
be2daae6
TT
15355 struct nextfield &field = fip->fields[index];
15356
15357 if (field.variant.is_discriminant)
2ddeaf8a 15358 di->discriminant_index = index;
be2daae6 15359 else if (field.variant.default_branch)
2ddeaf8a
TT
15360 di->default_index = index;
15361 else
be2daae6 15362 di->discriminants[index] = field.variant.discriminant_value;
2ddeaf8a
TT
15363 }
15364 }
15365
be2daae6
TT
15366 /* Copy the saved-up fields into the field vector. */
15367 for (int i = 0; i < nfields; ++i)
c906108c 15368 {
be2daae6
TT
15369 struct nextfield &field
15370 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15371 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 15372
be2daae6
TT
15373 TYPE_FIELD (type, i) = field.field;
15374 switch (field.accessibility)
c906108c 15375 {
c5aa993b 15376 case DW_ACCESS_private:
b4ba55a1 15377 if (cu->language != language_ada)
be2daae6 15378 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 15379 break;
c906108c 15380
c5aa993b 15381 case DW_ACCESS_protected:
b4ba55a1 15382 if (cu->language != language_ada)
be2daae6 15383 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 15384 break;
c906108c 15385
c5aa993b
JM
15386 case DW_ACCESS_public:
15387 break;
c906108c 15388
c5aa993b
JM
15389 default:
15390 /* Unknown accessibility. Complain and treat it as public. */
15391 {
b98664d3 15392 complaint (_("unsupported accessibility %d"),
be2daae6 15393 field.accessibility);
c5aa993b
JM
15394 }
15395 break;
c906108c 15396 }
be2daae6 15397 if (i < fip->baseclasses.size ())
c906108c 15398 {
be2daae6 15399 switch (field.virtuality)
c906108c 15400 {
c5aa993b
JM
15401 case DW_VIRTUALITY_virtual:
15402 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15403 if (cu->language == language_ada)
a73c6dcd 15404 error (_("unexpected virtuality in component of Ada type"));
be2daae6 15405 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 15406 break;
c906108c
SS
15407 }
15408 }
c906108c
SS
15409 }
15410}
15411
7d27a96d
TT
15412/* Return true if this member function is a constructor, false
15413 otherwise. */
15414
15415static int
15416dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15417{
15418 const char *fieldname;
fe978cb0 15419 const char *type_name;
7d27a96d
TT
15420 int len;
15421
15422 if (die->parent == NULL)
15423 return 0;
15424
15425 if (die->parent->tag != DW_TAG_structure_type
15426 && die->parent->tag != DW_TAG_union_type
15427 && die->parent->tag != DW_TAG_class_type)
15428 return 0;
15429
15430 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15431 type_name = dwarf2_name (die->parent, cu);
15432 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15433 return 0;
15434
15435 len = strlen (fieldname);
fe978cb0
PA
15436 return (strncmp (fieldname, type_name, len) == 0
15437 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15438}
15439
c906108c
SS
15440/* Add a member function to the proper fieldlist. */
15441
15442static void
107d2387 15443dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15444 struct type *type, struct dwarf2_cu *cu)
c906108c 15445{
518817b3 15446 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15447 struct attribute *attr;
c906108c 15448 int i;
be2daae6 15449 struct fnfieldlist *flp = nullptr;
c906108c 15450 struct fn_field *fnp;
15d034d0 15451 const char *fieldname;
f792889a 15452 struct type *this_type;
60d5a603 15453 enum dwarf_access_attribute accessibility;
c906108c 15454
b4ba55a1 15455 if (cu->language == language_ada)
a73c6dcd 15456 error (_("unexpected member function in Ada type"));
b4ba55a1 15457
2df3850c 15458 /* Get name of member function. */
39cbfefa
DJ
15459 fieldname = dwarf2_name (die, cu);
15460 if (fieldname == NULL)
2df3850c 15461 return;
c906108c 15462
c906108c 15463 /* Look up member function name in fieldlist. */
be2daae6 15464 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15465 {
27bfe10e 15466 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15467 {
15468 flp = &fip->fnfieldlists[i];
15469 break;
15470 }
c906108c
SS
15471 }
15472
be2daae6
TT
15473 /* Create a new fnfieldlist if necessary. */
15474 if (flp == nullptr)
c906108c 15475 {
be2daae6
TT
15476 fip->fnfieldlists.emplace_back ();
15477 flp = &fip->fnfieldlists.back ();
c906108c 15478 flp->name = fieldname;
be2daae6 15479 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15480 }
15481
be2daae6
TT
15482 /* Create a new member function field and add it to the vector of
15483 fnfieldlists. */
15484 flp->fnfields.emplace_back ();
15485 fnp = &flp->fnfields.back ();
3da10d80
KS
15486
15487 /* Delay processing of the physname until later. */
9c37b5ae 15488 if (cu->language == language_cplus)
be2daae6
TT
15489 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15490 die, cu);
3da10d80
KS
15491 else
15492 {
1d06ead6 15493 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15494 fnp->physname = physname ? physname : "";
15495 }
15496
c906108c 15497 fnp->type = alloc_type (objfile);
f792889a
DJ
15498 this_type = read_type_die (die, cu);
15499 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 15500 {
f792889a 15501 int nparams = TYPE_NFIELDS (this_type);
c906108c 15502
f792889a 15503 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15504 of the method itself (TYPE_CODE_METHOD). */
15505 smash_to_method_type (fnp->type, type,
f792889a
DJ
15506 TYPE_TARGET_TYPE (this_type),
15507 TYPE_FIELDS (this_type),
15508 TYPE_NFIELDS (this_type),
15509 TYPE_VARARGS (this_type));
c906108c
SS
15510
15511 /* Handle static member functions.
c5aa993b 15512 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15513 member functions. G++ helps GDB by marking the first
15514 parameter for non-static member functions (which is the this
15515 pointer) as artificial. We obtain this information from
15516 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15517 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15518 fnp->voffset = VOFFSET_STATIC;
15519 }
15520 else
b98664d3 15521 complaint (_("member function type missing for '%s'"),
3da10d80 15522 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15523
15524 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15525 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15526 fnp->fcontext = die_containing_type (die, cu);
c906108c 15527
3e43a32a
MS
15528 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15529 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15530
15531 /* Get accessibility. */
e142c38c 15532 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 15533 if (attr)
aead7601 15534 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
15535 else
15536 accessibility = dwarf2_default_access_attribute (die, cu);
15537 switch (accessibility)
c906108c 15538 {
60d5a603
JK
15539 case DW_ACCESS_private:
15540 fnp->is_private = 1;
15541 break;
15542 case DW_ACCESS_protected:
15543 fnp->is_protected = 1;
15544 break;
c906108c
SS
15545 }
15546
b02dede2 15547 /* Check for artificial methods. */
e142c38c 15548 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15549 if (attr && DW_UNSND (attr) != 0)
15550 fnp->is_artificial = 1;
15551
7d27a96d
TT
15552 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15553
0d564a31 15554 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15555 function. For older versions of GCC, this is an offset in the
15556 appropriate virtual table, as specified by DW_AT_containing_type.
15557 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15558 to the object address. */
15559
e142c38c 15560 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 15561 if (attr)
8e19ed76 15562 {
aec5aa8b 15563 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 15564 {
aec5aa8b
TT
15565 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15566 {
15567 /* Old-style GCC. */
15568 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15569 }
15570 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15571 || (DW_BLOCK (attr)->size > 1
15572 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15573 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15574 {
aec5aa8b
TT
15575 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15576 if ((fnp->voffset % cu->header.addr_size) != 0)
15577 dwarf2_complex_location_expr_complaint ();
15578 else
15579 fnp->voffset /= cu->header.addr_size;
15580 fnp->voffset += 2;
15581 }
15582 else
15583 dwarf2_complex_location_expr_complaint ();
15584
15585 if (!fnp->fcontext)
7e993ebf
KS
15586 {
15587 /* If there is no `this' field and no DW_AT_containing_type,
15588 we cannot actually find a base class context for the
15589 vtable! */
15590 if (TYPE_NFIELDS (this_type) == 0
15591 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15592 {
b98664d3 15593 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15594 "function \"%s\" (offset %s)"),
15595 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15596 }
15597 else
15598 {
15599 fnp->fcontext
15600 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15601 }
15602 }
aec5aa8b 15603 }
3690dd37 15604 else if (attr_form_is_section_offset (attr))
8e19ed76 15605 {
4d3c2250 15606 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15607 }
15608 else
15609 {
4d3c2250
KB
15610 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15611 fieldname);
8e19ed76 15612 }
0d564a31 15613 }
d48cc9dd
DJ
15614 else
15615 {
15616 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15617 if (attr && DW_UNSND (attr))
15618 {
15619 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15620 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15621 "but the vtable offset is not specified"),
9d8780f0 15622 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15623 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15624 TYPE_CPLUS_DYNAMIC (type) = 1;
15625 }
15626 }
c906108c
SS
15627}
15628
15629/* Create the vector of member function fields, and attach it to the type. */
15630
15631static void
fba45db2 15632dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15633 struct dwarf2_cu *cu)
c906108c 15634{
b4ba55a1 15635 if (cu->language == language_ada)
a73c6dcd 15636 error (_("unexpected member functions in Ada type"));
b4ba55a1 15637
c906108c
SS
15638 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15639 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15640 TYPE_ALLOC (type,
15641 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15642
be2daae6 15643 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15644 {
be2daae6 15645 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15646 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15647
be2daae6
TT
15648 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15649 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15650 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15651 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15652
15653 for (int k = 0; k < nf.fnfields.size (); ++k)
15654 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15655 }
15656
be2daae6 15657 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15658}
15659
1168df01
JB
15660/* Returns non-zero if NAME is the name of a vtable member in CU's
15661 language, zero otherwise. */
15662static int
15663is_vtable_name (const char *name, struct dwarf2_cu *cu)
15664{
15665 static const char vptr[] = "_vptr";
15666
9c37b5ae
TT
15667 /* Look for the C++ form of the vtable. */
15668 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15669 return 1;
15670
15671 return 0;
15672}
15673
c0dd20ea 15674/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15675 functions, with the ABI-specified layout. If TYPE describes
15676 such a structure, smash it into a member function type.
61049d3b
DJ
15677
15678 GCC shouldn't do this; it should just output pointer to member DIEs.
15679 This is GCC PR debug/28767. */
c0dd20ea 15680
0b92b5bb
TT
15681static void
15682quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15683{
09e2d7c7 15684 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15685
15686 /* Check for a structure with no name and two children. */
0b92b5bb
TT
15687 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15688 return;
c0dd20ea
DJ
15689
15690 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15691 if (TYPE_FIELD_NAME (type, 0) == NULL
15692 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15693 || TYPE_FIELD_NAME (type, 1) == NULL
15694 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15695 return;
c0dd20ea
DJ
15696
15697 /* Find the type of the method. */
0b92b5bb 15698 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
15699 if (pfn_type == NULL
15700 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15701 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 15702 return;
c0dd20ea
DJ
15703
15704 /* Look for the "this" argument. */
15705 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15706 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 15707 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 15708 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 15709 return;
c0dd20ea 15710
09e2d7c7 15711 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 15712 new_type = alloc_type (objfile);
09e2d7c7 15713 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
15714 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15715 TYPE_VARARGS (pfn_type));
0b92b5bb 15716 smash_to_methodptr_type (type, new_type);
c0dd20ea 15717}
1168df01 15718
2b4424c3
TT
15719/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15720 appropriate error checking and issuing complaints if there is a
15721 problem. */
15722
15723static ULONGEST
15724get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15725{
15726 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15727
15728 if (attr == nullptr)
15729 return 0;
15730
15731 if (!attr_form_is_constant (attr))
15732 {
b98664d3 15733 complaint (_("DW_AT_alignment must have constant form"
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
15740 ULONGEST align;
15741 if (attr->form == DW_FORM_sdata)
15742 {
15743 LONGEST val = DW_SND (attr);
15744 if (val < 0)
15745 {
b98664d3 15746 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
15747 " - DIE at %s [in module %s]"),
15748 sect_offset_str (die->sect_off),
15749 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15750 return 0;
15751 }
15752 align = val;
15753 }
15754 else
15755 align = DW_UNSND (attr);
15756
15757 if (align == 0)
15758 {
b98664d3 15759 complaint (_("DW_AT_alignment value must not be zero"
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 return 0;
15764 }
15765 if ((align & (align - 1)) != 0)
15766 {
b98664d3 15767 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15768 " - DIE at %s [in module %s]"),
15769 sect_offset_str (die->sect_off),
15770 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15771 return 0;
15772 }
15773
15774 return align;
15775}
15776
15777/* If the DIE has a DW_AT_alignment attribute, use its value to set
15778 the alignment for TYPE. */
15779
15780static void
15781maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15782 struct type *type)
15783{
15784 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15785 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15786 " - DIE at %s [in module %s]"),
15787 sect_offset_str (die->sect_off),
15788 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15789}
685b1105 15790
c906108c 15791/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15792 (definition) to create a type for the structure or union. Fill in
15793 the type's name and general properties; the members will not be
83655187
DE
15794 processed until process_structure_scope. A symbol table entry for
15795 the type will also not be done until process_structure_scope (assuming
15796 the type has a name).
c906108c 15797
c767944b
DJ
15798 NOTE: we need to call these functions regardless of whether or not the
15799 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15800 structure or union. This gets the type entered into our set of
83655187 15801 user defined types. */
c906108c 15802
f792889a 15803static struct type *
134d01f1 15804read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15805{
518817b3 15806 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
15807 struct type *type;
15808 struct attribute *attr;
15d034d0 15809 const char *name;
c906108c 15810
348e048f
DE
15811 /* If the definition of this type lives in .debug_types, read that type.
15812 Don't follow DW_AT_specification though, that will take us back up
15813 the chain and we want to go down. */
45e58e77 15814 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
15815 if (attr)
15816 {
ac9ec31b 15817 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15818
ac9ec31b 15819 /* The type's CU may not be the same as CU.
02142a6c 15820 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15821 return set_die_type (die, type, cu);
15822 }
15823
c0dd20ea 15824 type = alloc_type (objfile);
c906108c 15825 INIT_CPLUS_SPECIFIC (type);
93311388 15826
39cbfefa
DJ
15827 name = dwarf2_name (die, cu);
15828 if (name != NULL)
c906108c 15829 {
987504bb 15830 if (cu->language == language_cplus
c44af4eb
TT
15831 || cu->language == language_d
15832 || cu->language == language_rust)
63d06c5c 15833 {
15d034d0 15834 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15835
15836 /* dwarf2_full_name might have already finished building the DIE's
15837 type. If so, there is no need to continue. */
15838 if (get_die_type (die, cu) != NULL)
15839 return get_die_type (die, cu);
15840
e86ca25f 15841 TYPE_NAME (type) = full_name;
63d06c5c
DC
15842 }
15843 else
15844 {
d8151005
DJ
15845 /* The name is already allocated along with this objfile, so
15846 we don't need to duplicate it for the type. */
e86ca25f 15847 TYPE_NAME (type) = name;
63d06c5c 15848 }
c906108c
SS
15849 }
15850
15851 if (die->tag == DW_TAG_structure_type)
15852 {
15853 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15854 }
15855 else if (die->tag == DW_TAG_union_type)
15856 {
15857 TYPE_CODE (type) = TYPE_CODE_UNION;
15858 }
2ddeaf8a
TT
15859 else if (die->tag == DW_TAG_variant_part)
15860 {
15861 TYPE_CODE (type) = TYPE_CODE_UNION;
15862 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15863 }
c906108c
SS
15864 else
15865 {
4753d33b 15866 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
15867 }
15868
0cc2414c
TT
15869 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15870 TYPE_DECLARED_CLASS (type) = 1;
15871
e142c38c 15872 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
15873 if (attr)
15874 {
155bfbd3
JB
15875 if (attr_form_is_constant (attr))
15876 TYPE_LENGTH (type) = DW_UNSND (attr);
15877 else
15878 {
15879 /* For the moment, dynamic type sizes are not supported
15880 by GDB's struct type. The actual size is determined
15881 on-demand when resolving the type of a given object,
15882 so set the type's length to zero for now. Otherwise,
15883 we record an expression as the length, and that expression
15884 could lead to a very large value, which could eventually
15885 lead to us trying to allocate that much memory when creating
15886 a value of that type. */
15887 TYPE_LENGTH (type) = 0;
15888 }
c906108c
SS
15889 }
15890 else
15891 {
15892 TYPE_LENGTH (type) = 0;
15893 }
15894
2b4424c3
TT
15895 maybe_set_alignment (cu, die, type);
15896
5230b05a 15897 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15898 {
5230b05a
WT
15899 /* ICC<14 does not output the required DW_AT_declaration on
15900 incomplete types, but gives them a size of zero. */
422b1cb0 15901 TYPE_STUB (type) = 1;
685b1105
JK
15902 }
15903 else
15904 TYPE_STUB_SUPPORTED (type) = 1;
15905
dc718098 15906 if (die_is_declaration (die, cu))
876cecd0 15907 TYPE_STUB (type) = 1;
a6c727b2
DJ
15908 else if (attr == NULL && die->child == NULL
15909 && producer_is_realview (cu->producer))
15910 /* RealView does not output the required DW_AT_declaration
15911 on incomplete types. */
15912 TYPE_STUB (type) = 1;
dc718098 15913
c906108c
SS
15914 /* We need to add the type field to the die immediately so we don't
15915 infinitely recurse when dealing with pointers to the structure
0963b4bd 15916 type within the structure itself. */
1c379e20 15917 set_die_type (die, type, cu);
c906108c 15918
7e314c57
JK
15919 /* set_die_type should be already done. */
15920 set_descriptive_type (type, die, cu);
15921
c767944b
DJ
15922 return type;
15923}
15924
2ddeaf8a
TT
15925/* A helper for process_structure_scope that handles a single member
15926 DIE. */
15927
15928static void
15929handle_struct_member_die (struct die_info *child_die, struct type *type,
15930 struct field_info *fi,
15931 std::vector<struct symbol *> *template_args,
15932 struct dwarf2_cu *cu)
15933{
15934 if (child_die->tag == DW_TAG_member
15935 || child_die->tag == DW_TAG_variable
15936 || child_die->tag == DW_TAG_variant_part)
15937 {
15938 /* NOTE: carlton/2002-11-05: A C++ static data member
15939 should be a DW_TAG_member that is a declaration, but
15940 all versions of G++ as of this writing (so through at
15941 least 3.2.1) incorrectly generate DW_TAG_variable
15942 tags for them instead. */
15943 dwarf2_add_field (fi, child_die, cu);
15944 }
15945 else if (child_die->tag == DW_TAG_subprogram)
15946 {
15947 /* Rust doesn't have member functions in the C++ sense.
15948 However, it does emit ordinary functions as children
15949 of a struct DIE. */
15950 if (cu->language == language_rust)
15951 read_func_scope (child_die, cu);
15952 else
15953 {
15954 /* C++ member function. */
15955 dwarf2_add_member_fn (fi, child_die, type, cu);
15956 }
15957 }
15958 else if (child_die->tag == DW_TAG_inheritance)
15959 {
15960 /* C++ base class field. */
15961 dwarf2_add_field (fi, child_die, cu);
15962 }
15963 else if (type_can_define_types (child_die))
15964 dwarf2_add_type_defn (fi, child_die, cu);
15965 else if (child_die->tag == DW_TAG_template_type_param
15966 || child_die->tag == DW_TAG_template_value_param)
15967 {
15968 struct symbol *arg = new_symbol (child_die, NULL, cu);
15969
15970 if (arg != NULL)
15971 template_args->push_back (arg);
15972 }
15973 else if (child_die->tag == DW_TAG_variant)
15974 {
15975 /* In a variant we want to get the discriminant and also add a
15976 field for our sole member child. */
15977 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15978
bde09ab7 15979 for (die_info *variant_child = child_die->child;
2ddeaf8a
TT
15980 variant_child != NULL;
15981 variant_child = sibling_die (variant_child))
15982 {
15983 if (variant_child->tag == DW_TAG_member)
15984 {
15985 handle_struct_member_die (variant_child, type, fi,
15986 template_args, cu);
15987 /* Only handle the one. */
15988 break;
15989 }
15990 }
15991
15992 /* We don't handle this but we might as well report it if we see
15993 it. */
15994 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
b98664d3 15995 complaint (_("DW_AT_discr_list is not supported yet"
2ddeaf8a
TT
15996 " - DIE at %s [in module %s]"),
15997 sect_offset_str (child_die->sect_off),
15998 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15999
16000 /* The first field was just added, so we can stash the
16001 discriminant there. */
be2daae6 16002 gdb_assert (!fi->fields.empty ());
2ddeaf8a 16003 if (discr == NULL)
be2daae6 16004 fi->fields.back ().variant.default_branch = true;
2ddeaf8a 16005 else
be2daae6 16006 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
2ddeaf8a
TT
16007 }
16008}
16009
c767944b
DJ
16010/* Finish creating a structure or union type, including filling in
16011 its members and creating a symbol for it. */
16012
16013static void
16014process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16015{
518817b3 16016 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
ca040673 16017 struct die_info *child_die;
c767944b
DJ
16018 struct type *type;
16019
16020 type = get_die_type (die, cu);
16021 if (type == NULL)
16022 type = read_structure_type (die, cu);
16023
2ddeaf8a
TT
16024 /* When reading a DW_TAG_variant_part, we need to notice when we
16025 read the discriminant member, so we can record it later in the
16026 discriminant_info. */
16027 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
16028 sect_offset discr_offset;
3e1d3d8c 16029 bool has_template_parameters = false;
2ddeaf8a
TT
16030
16031 if (is_variant_part)
16032 {
16033 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16034 if (discr == NULL)
16035 {
16036 /* Maybe it's a univariant form, an extension we support.
16037 In this case arrange not to check the offset. */
16038 is_variant_part = false;
16039 }
16040 else if (attr_form_is_ref (discr))
16041 {
16042 struct dwarf2_cu *target_cu = cu;
16043 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16044
16045 discr_offset = target_die->sect_off;
16046 }
16047 else
16048 {
b98664d3 16049 complaint (_("DW_AT_discr does not have DIE reference form"
2ddeaf8a
TT
16050 " - DIE at %s [in module %s]"),
16051 sect_offset_str (die->sect_off),
16052 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16053 is_variant_part = false;
16054 }
16055 }
16056
e142c38c 16057 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
16058 {
16059 struct field_info fi;
2f4732b0 16060 std::vector<struct symbol *> template_args;
c906108c 16061
639d11d3 16062 child_die = die->child;
c906108c
SS
16063
16064 while (child_die && child_die->tag)
16065 {
2ddeaf8a 16066 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
34eaf542 16067
2ddeaf8a 16068 if (is_variant_part && discr_offset == child_die->sect_off)
be2daae6 16069 fi.fields.back ().variant.is_discriminant = true;
34eaf542 16070
c906108c
SS
16071 child_die = sibling_die (child_die);
16072 }
16073
34eaf542 16074 /* Attach template arguments to type. */
2f4732b0 16075 if (!template_args.empty ())
34eaf542 16076 {
3e1d3d8c 16077 has_template_parameters = true;
34eaf542 16078 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 16079 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 16080 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
16081 = XOBNEWVEC (&objfile->objfile_obstack,
16082 struct symbol *,
16083 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 16084 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 16085 template_args.data (),
34eaf542
TT
16086 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16087 * sizeof (struct symbol *)));
34eaf542
TT
16088 }
16089
c906108c
SS
16090 /* Attach fields and member functions to the type. */
16091 if (fi.nfields)
e7c27a73 16092 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 16093 if (!fi.fnfieldlists.empty ())
c906108c 16094 {
e7c27a73 16095 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 16096
c5aa993b 16097 /* Get the type which refers to the base class (possibly this
c906108c 16098 class itself) which contains the vtable pointer for the current
0d564a31
DJ
16099 class from the DW_AT_containing_type attribute. This use of
16100 DW_AT_containing_type is a GNU extension. */
c906108c 16101
e142c38c 16102 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 16103 {
e7c27a73 16104 struct type *t = die_containing_type (die, cu);
c906108c 16105
ae6ae975 16106 set_type_vptr_basetype (type, t);
c906108c
SS
16107 if (type == t)
16108 {
c906108c
SS
16109 int i;
16110
16111 /* Our own class provides vtbl ptr. */
16112 for (i = TYPE_NFIELDS (t) - 1;
16113 i >= TYPE_N_BASECLASSES (t);
16114 --i)
16115 {
0d5cff50 16116 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 16117
1168df01 16118 if (is_vtable_name (fieldname, cu))
c906108c 16119 {
ae6ae975 16120 set_type_vptr_fieldno (type, i);
c906108c
SS
16121 break;
16122 }
16123 }
16124
16125 /* Complain if virtual function table field not found. */
16126 if (i < TYPE_N_BASECLASSES (t))
b98664d3 16127 complaint (_("virtual function table pointer "
3e43a32a 16128 "not found when defining class '%s'"),
e86ca25f 16129 TYPE_NAME (type) ? TYPE_NAME (type) : "");
c906108c
SS
16130 }
16131 else
16132 {
ae6ae975 16133 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
16134 }
16135 }
f6235d4c 16136 else if (cu->producer
61012eef 16137 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
16138 {
16139 /* The IBM XLC compiler does not provide direct indication
16140 of the containing type, but the vtable pointer is
16141 always named __vfp. */
16142
16143 int i;
16144
16145 for (i = TYPE_NFIELDS (type) - 1;
16146 i >= TYPE_N_BASECLASSES (type);
16147 --i)
16148 {
16149 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16150 {
ae6ae975
DE
16151 set_type_vptr_fieldno (type, i);
16152 set_type_vptr_basetype (type, type);
f6235d4c
EZ
16153 break;
16154 }
16155 }
16156 }
c906108c 16157 }
98751a41
JK
16158
16159 /* Copy fi.typedef_field_list linked list elements content into the
16160 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 16161 if (!fi.typedef_field_list.empty ())
98751a41 16162 {
be2daae6 16163 int count = fi.typedef_field_list.size ();
98751a41 16164
a0d7a4ff 16165 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 16166 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 16167 = ((struct decl_field *)
be2daae6
TT
16168 TYPE_ALLOC (type,
16169 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16170 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 16171
be2daae6
TT
16172 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16173 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 16174 }
c767944b 16175
883fd55a
KS
16176 /* Copy fi.nested_types_list linked list elements content into the
16177 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 16178 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 16179 {
be2daae6 16180 int count = fi.nested_types_list.size ();
883fd55a
KS
16181
16182 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16183 TYPE_NESTED_TYPES_ARRAY (type)
16184 = ((struct decl_field *)
be2daae6
TT
16185 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16186 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 16187
be2daae6
TT
16188 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16189 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 16190 }
c906108c 16191 }
63d06c5c 16192
bb5ed363 16193 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
16194 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16195 cu->rust_unions.push_back (type);
0b92b5bb 16196
90aeadfc
DC
16197 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16198 snapshots) has been known to create a die giving a declaration
16199 for a class that has, as a child, a die giving a definition for a
16200 nested class. So we have to process our children even if the
16201 current die is a declaration. Normally, of course, a declaration
16202 won't have any children at all. */
134d01f1 16203
ca040673
DE
16204 child_die = die->child;
16205
90aeadfc
DC
16206 while (child_die != NULL && child_die->tag)
16207 {
16208 if (child_die->tag == DW_TAG_member
16209 || child_die->tag == DW_TAG_variable
34eaf542
TT
16210 || child_die->tag == DW_TAG_inheritance
16211 || child_die->tag == DW_TAG_template_value_param
16212 || child_die->tag == DW_TAG_template_type_param)
134d01f1 16213 {
90aeadfc 16214 /* Do nothing. */
134d01f1 16215 }
90aeadfc
DC
16216 else
16217 process_die (child_die, cu);
134d01f1 16218
90aeadfc 16219 child_die = sibling_die (child_die);
134d01f1
DJ
16220 }
16221
fa4028e9
JB
16222 /* Do not consider external references. According to the DWARF standard,
16223 these DIEs are identified by the fact that they have no byte_size
16224 attribute, and a declaration attribute. */
16225 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16226 || !die_is_declaration (die, cu))
3e1d3d8c
TT
16227 {
16228 struct symbol *sym = new_symbol (die, type, cu);
16229
16230 if (has_template_parameters)
16231 {
a776957c
TT
16232 struct symtab *symtab;
16233 if (sym != nullptr)
16234 symtab = symbol_symtab (sym);
16235 else if (cu->line_header != nullptr)
16236 {
16237 /* Any related symtab will do. */
16238 symtab
7ba99d21 16239 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
16240 }
16241 else
16242 {
16243 symtab = nullptr;
16244 complaint (_("could not find suitable "
16245 "symtab for template parameter"
16246 " - DIE at %s [in module %s]"),
16247 sect_offset_str (die->sect_off),
16248 objfile_name (objfile));
16249 }
16250
16251 if (symtab != nullptr)
16252 {
16253 /* Make sure that the symtab is set on the new symbols.
16254 Even though they don't appear in this symtab directly,
16255 other parts of gdb assume that symbols do, and this is
16256 reasonably true. */
16257 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16258 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16259 }
3e1d3d8c
TT
16260 }
16261 }
134d01f1
DJ
16262}
16263
55426c9d
JB
16264/* Assuming DIE is an enumeration type, and TYPE is its associated type,
16265 update TYPE using some information only available in DIE's children. */
16266
16267static void
16268update_enumeration_type_from_children (struct die_info *die,
16269 struct type *type,
16270 struct dwarf2_cu *cu)
16271{
60f7655a 16272 struct die_info *child_die;
55426c9d
JB
16273 int unsigned_enum = 1;
16274 int flag_enum = 1;
16275 ULONGEST mask = 0;
55426c9d 16276
8268c778 16277 auto_obstack obstack;
55426c9d 16278
60f7655a
DE
16279 for (child_die = die->child;
16280 child_die != NULL && child_die->tag;
16281 child_die = sibling_die (child_die))
55426c9d
JB
16282 {
16283 struct attribute *attr;
16284 LONGEST value;
16285 const gdb_byte *bytes;
16286 struct dwarf2_locexpr_baton *baton;
16287 const char *name;
60f7655a 16288
55426c9d
JB
16289 if (child_die->tag != DW_TAG_enumerator)
16290 continue;
16291
16292 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16293 if (attr == NULL)
16294 continue;
16295
16296 name = dwarf2_name (child_die, cu);
16297 if (name == NULL)
16298 name = "<anonymous enumerator>";
16299
16300 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16301 &value, &bytes, &baton);
16302 if (value < 0)
16303 {
16304 unsigned_enum = 0;
16305 flag_enum = 0;
16306 }
16307 else if ((mask & value) != 0)
16308 flag_enum = 0;
16309 else
16310 mask |= value;
16311
16312 /* If we already know that the enum type is neither unsigned, nor
16313 a flag type, no need to look at the rest of the enumerates. */
16314 if (!unsigned_enum && !flag_enum)
16315 break;
55426c9d
JB
16316 }
16317
16318 if (unsigned_enum)
16319 TYPE_UNSIGNED (type) = 1;
16320 if (flag_enum)
16321 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16322}
16323
134d01f1
DJ
16324/* Given a DW_AT_enumeration_type die, set its type. We do not
16325 complete the type's fields yet, or create any symbols. */
c906108c 16326
f792889a 16327static struct type *
134d01f1 16328read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16329{
518817b3 16330 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16331 struct type *type;
c906108c 16332 struct attribute *attr;
0114d602 16333 const char *name;
134d01f1 16334
348e048f
DE
16335 /* If the definition of this type lives in .debug_types, read that type.
16336 Don't follow DW_AT_specification though, that will take us back up
16337 the chain and we want to go down. */
45e58e77 16338 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
16339 if (attr)
16340 {
ac9ec31b 16341 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16342
ac9ec31b 16343 /* The type's CU may not be the same as CU.
02142a6c 16344 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16345 return set_die_type (die, type, cu);
16346 }
16347
c906108c
SS
16348 type = alloc_type (objfile);
16349
16350 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 16351 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16352 if (name != NULL)
e86ca25f 16353 TYPE_NAME (type) = name;
c906108c 16354
0626fc76
TT
16355 attr = dwarf2_attr (die, DW_AT_type, cu);
16356 if (attr != NULL)
16357 {
16358 struct type *underlying_type = die_type (die, cu);
16359
16360 TYPE_TARGET_TYPE (type) = underlying_type;
16361 }
16362
e142c38c 16363 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
16364 if (attr)
16365 {
16366 TYPE_LENGTH (type) = DW_UNSND (attr);
16367 }
16368 else
16369 {
16370 TYPE_LENGTH (type) = 0;
16371 }
16372
2b4424c3
TT
16373 maybe_set_alignment (cu, die, type);
16374
137033e9
JB
16375 /* The enumeration DIE can be incomplete. In Ada, any type can be
16376 declared as private in the package spec, and then defined only
16377 inside the package body. Such types are known as Taft Amendment
16378 Types. When another package uses such a type, an incomplete DIE
16379 may be generated by the compiler. */
02eb380e 16380 if (die_is_declaration (die, cu))
876cecd0 16381 TYPE_STUB (type) = 1;
02eb380e 16382
0626fc76
TT
16383 /* Finish the creation of this type by using the enum's children.
16384 We must call this even when the underlying type has been provided
16385 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
16386 update_enumeration_type_from_children (die, type, cu);
16387
0626fc76
TT
16388 /* If this type has an underlying type that is not a stub, then we
16389 may use its attributes. We always use the "unsigned" attribute
16390 in this situation, because ordinarily we guess whether the type
16391 is unsigned -- but the guess can be wrong and the underlying type
16392 can tell us the reality. However, we defer to a local size
16393 attribute if one exists, because this lets the compiler override
16394 the underlying type if needed. */
16395 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16396 {
16397 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16398 if (TYPE_LENGTH (type) == 0)
16399 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
2b4424c3
TT
16400 if (TYPE_RAW_ALIGN (type) == 0
16401 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16402 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
0626fc76
TT
16403 }
16404
3d567982
TT
16405 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16406
f792889a 16407 return set_die_type (die, type, cu);
134d01f1
DJ
16408}
16409
16410/* Given a pointer to a die which begins an enumeration, process all
16411 the dies that define the members of the enumeration, and create the
16412 symbol for the enumeration type.
16413
16414 NOTE: We reverse the order of the element list. */
16415
16416static void
16417process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16418{
f792889a 16419 struct type *this_type;
134d01f1 16420
f792889a
DJ
16421 this_type = get_die_type (die, cu);
16422 if (this_type == NULL)
16423 this_type = read_enumeration_type (die, cu);
9dc481d3 16424
639d11d3 16425 if (die->child != NULL)
c906108c 16426 {
9dc481d3
DE
16427 struct die_info *child_die;
16428 struct symbol *sym;
16429 struct field *fields = NULL;
16430 int num_fields = 0;
15d034d0 16431 const char *name;
9dc481d3 16432
639d11d3 16433 child_die = die->child;
c906108c
SS
16434 while (child_die && child_die->tag)
16435 {
16436 if (child_die->tag != DW_TAG_enumerator)
16437 {
e7c27a73 16438 process_die (child_die, cu);
c906108c
SS
16439 }
16440 else
16441 {
39cbfefa
DJ
16442 name = dwarf2_name (child_die, cu);
16443 if (name)
c906108c 16444 {
f792889a 16445 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
16446
16447 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16448 {
16449 fields = (struct field *)
16450 xrealloc (fields,
16451 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 16452 * sizeof (struct field));
c906108c
SS
16453 }
16454
3567439c 16455 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 16456 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 16457 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
16458 FIELD_BITSIZE (fields[num_fields]) = 0;
16459
16460 num_fields++;
16461 }
16462 }
16463
16464 child_die = sibling_die (child_die);
16465 }
16466
16467 if (num_fields)
16468 {
f792889a
DJ
16469 TYPE_NFIELDS (this_type) = num_fields;
16470 TYPE_FIELDS (this_type) = (struct field *)
16471 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16472 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 16473 sizeof (struct field) * num_fields);
b8c9b27d 16474 xfree (fields);
c906108c 16475 }
c906108c 16476 }
134d01f1 16477
6c83ed52
TT
16478 /* If we are reading an enum from a .debug_types unit, and the enum
16479 is a declaration, and the enum is not the signatured type in the
16480 unit, then we do not want to add a symbol for it. Adding a
16481 symbol would in some cases obscure the true definition of the
16482 enum, giving users an incomplete type when the definition is
16483 actually available. Note that we do not want to do this for all
16484 enums which are just declarations, because C++0x allows forward
16485 enum declarations. */
3019eac3 16486 if (cu->per_cu->is_debug_types
6c83ed52
TT
16487 && die_is_declaration (die, cu))
16488 {
52dc124a 16489 struct signatured_type *sig_type;
6c83ed52 16490
c0f78cd4 16491 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16492 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16493 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16494 return;
16495 }
16496
f792889a 16497 new_symbol (die, this_type, cu);
c906108c
SS
16498}
16499
16500/* Extract all information from a DW_TAG_array_type DIE and put it in
16501 the DIE's type field. For now, this only handles one dimensional
16502 arrays. */
16503
f792889a 16504static struct type *
e7c27a73 16505read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16506{
518817b3 16507 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16508 struct die_info *child_die;
7e314c57 16509 struct type *type;
c906108c 16510 struct type *element_type, *range_type, *index_type;
c906108c 16511 struct attribute *attr;
15d034d0 16512 const char *name;
a405673c 16513 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16514 unsigned int bit_stride = 0;
c906108c 16515
e7c27a73 16516 element_type = die_type (die, cu);
c906108c 16517
7e314c57
JK
16518 /* The die_type call above may have already set the type for this DIE. */
16519 type = get_die_type (die, cu);
16520 if (type)
16521 return type;
16522
dc53a7ad
JB
16523 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16524 if (attr != NULL)
a405673c
JB
16525 {
16526 int stride_ok;
9a49df9d
AB
16527 struct type *prop_type
16528 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
a405673c
JB
16529
16530 byte_stride_prop
16531 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
16532 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16533 prop_type);
a405673c
JB
16534 if (!stride_ok)
16535 {
b98664d3 16536 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16537 " - DIE at %s [in module %s]"),
16538 sect_offset_str (die->sect_off),
518817b3 16539 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a405673c
JB
16540 /* Ignore this attribute. We will likely not be able to print
16541 arrays of this type correctly, but there is little we can do
16542 to help if we cannot read the attribute's value. */
16543 byte_stride_prop = NULL;
16544 }
16545 }
dc53a7ad
JB
16546
16547 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16548 if (attr != NULL)
16549 bit_stride = DW_UNSND (attr);
16550
c906108c
SS
16551 /* Irix 6.2 native cc creates array types without children for
16552 arrays with unspecified length. */
639d11d3 16553 if (die->child == NULL)
c906108c 16554 {
46bf5051 16555 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16556 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16557 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16558 byte_stride_prop, bit_stride);
f792889a 16559 return set_die_type (die, type, cu);
c906108c
SS
16560 }
16561
791afaa2 16562 std::vector<struct type *> range_types;
639d11d3 16563 child_die = die->child;
c906108c
SS
16564 while (child_die && child_die->tag)
16565 {
16566 if (child_die->tag == DW_TAG_subrange_type)
16567 {
f792889a 16568 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16569
f792889a 16570 if (child_type != NULL)
a02abb62 16571 {
0963b4bd
MS
16572 /* The range type was succesfully read. Save it for the
16573 array type creation. */
791afaa2 16574 range_types.push_back (child_type);
a02abb62 16575 }
c906108c
SS
16576 }
16577 child_die = sibling_die (child_die);
16578 }
16579
16580 /* Dwarf2 dimensions are output from left to right, create the
16581 necessary array types in backwards order. */
7ca2d3a3 16582
c906108c 16583 type = element_type;
7ca2d3a3
DL
16584
16585 if (read_array_order (die, cu) == DW_ORD_col_major)
16586 {
16587 int i = 0;
9a619af0 16588
791afaa2 16589 while (i < range_types.size ())
dc53a7ad 16590 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16591 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16592 }
16593 else
16594 {
791afaa2 16595 size_t ndim = range_types.size ();
7ca2d3a3 16596 while (ndim-- > 0)
dc53a7ad 16597 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16598 byte_stride_prop, bit_stride);
7ca2d3a3 16599 }
c906108c 16600
f5f8a009
EZ
16601 /* Understand Dwarf2 support for vector types (like they occur on
16602 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16603 array type. This is not part of the Dwarf2/3 standard yet, but a
16604 custom vendor extension. The main difference between a regular
16605 array and the vector variant is that vectors are passed by value
16606 to functions. */
e142c38c 16607 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 16608 if (attr)
ea37ba09 16609 make_vector_type (type);
f5f8a009 16610
dbc98a8b
KW
16611 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16612 implementation may choose to implement triple vectors using this
16613 attribute. */
16614 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16615 if (attr)
16616 {
16617 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16618 TYPE_LENGTH (type) = DW_UNSND (attr);
16619 else
b98664d3 16620 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16621 "than the total size of elements"));
dbc98a8b
KW
16622 }
16623
39cbfefa
DJ
16624 name = dwarf2_name (die, cu);
16625 if (name)
16626 TYPE_NAME (type) = name;
6e70227d 16627
2b4424c3
TT
16628 maybe_set_alignment (cu, die, type);
16629
0963b4bd 16630 /* Install the type in the die. */
7e314c57
JK
16631 set_die_type (die, type, cu);
16632
16633 /* set_die_type should be already done. */
b4ba55a1
JB
16634 set_descriptive_type (type, die, cu);
16635
7e314c57 16636 return type;
c906108c
SS
16637}
16638
7ca2d3a3 16639static enum dwarf_array_dim_ordering
6e70227d 16640read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16641{
16642 struct attribute *attr;
16643
16644 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16645
aead7601
SM
16646 if (attr)
16647 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16648
0963b4bd
MS
16649 /* GNU F77 is a special case, as at 08/2004 array type info is the
16650 opposite order to the dwarf2 specification, but data is still
16651 laid out as per normal fortran.
7ca2d3a3 16652
0963b4bd
MS
16653 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16654 version checking. */
7ca2d3a3 16655
905e0470
PM
16656 if (cu->language == language_fortran
16657 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16658 {
16659 return DW_ORD_row_major;
16660 }
16661
6e70227d 16662 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16663 {
16664 case array_column_major:
16665 return DW_ORD_col_major;
16666 case array_row_major:
16667 default:
16668 return DW_ORD_row_major;
16669 };
16670}
16671
72019c9c 16672/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16673 the DIE's type field. */
72019c9c 16674
f792889a 16675static struct type *
72019c9c
GM
16676read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16677{
7e314c57
JK
16678 struct type *domain_type, *set_type;
16679 struct attribute *attr;
f792889a 16680
7e314c57
JK
16681 domain_type = die_type (die, cu);
16682
16683 /* The die_type call above may have already set the type for this DIE. */
16684 set_type = get_die_type (die, cu);
16685 if (set_type)
16686 return set_type;
16687
16688 set_type = create_set_type (NULL, domain_type);
16689
16690 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
16691 if (attr)
16692 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16693
2b4424c3
TT
16694 maybe_set_alignment (cu, die, set_type);
16695
f792889a 16696 return set_die_type (die, set_type, cu);
72019c9c 16697}
7ca2d3a3 16698
0971de02
TT
16699/* A helper for read_common_block that creates a locexpr baton.
16700 SYM is the symbol which we are marking as computed.
16701 COMMON_DIE is the DIE for the common block.
16702 COMMON_LOC is the location expression attribute for the common
16703 block itself.
16704 MEMBER_LOC is the location expression attribute for the particular
16705 member of the common block that we are processing.
16706 CU is the CU from which the above come. */
16707
16708static void
16709mark_common_block_symbol_computed (struct symbol *sym,
16710 struct die_info *common_die,
16711 struct attribute *common_loc,
16712 struct attribute *member_loc,
16713 struct dwarf2_cu *cu)
16714{
518817b3
SM
16715 struct dwarf2_per_objfile *dwarf2_per_objfile
16716 = cu->per_cu->dwarf2_per_objfile;
0971de02
TT
16717 struct objfile *objfile = dwarf2_per_objfile->objfile;
16718 struct dwarf2_locexpr_baton *baton;
16719 gdb_byte *ptr;
16720 unsigned int cu_off;
16721 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16722 LONGEST offset = 0;
16723
16724 gdb_assert (common_loc && member_loc);
16725 gdb_assert (attr_form_is_block (common_loc));
16726 gdb_assert (attr_form_is_block (member_loc)
16727 || attr_form_is_constant (member_loc));
16728
8d749320 16729 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
16730 baton->per_cu = cu->per_cu;
16731 gdb_assert (baton->per_cu);
16732
16733 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16734
16735 if (attr_form_is_constant (member_loc))
16736 {
16737 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16738 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16739 }
16740 else
16741 baton->size += DW_BLOCK (member_loc)->size;
16742
224c3ddb 16743 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16744 baton->data = ptr;
16745
16746 *ptr++ = DW_OP_call4;
9c541725 16747 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16748 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16749 ptr += 4;
16750
16751 if (attr_form_is_constant (member_loc))
16752 {
16753 *ptr++ = DW_OP_addr;
16754 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16755 ptr += cu->header.addr_size;
16756 }
16757 else
16758 {
16759 /* We have to copy the data here, because DW_OP_call4 will only
16760 use a DW_AT_location attribute. */
16761 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16762 ptr += DW_BLOCK (member_loc)->size;
16763 }
16764
16765 *ptr++ = DW_OP_plus;
16766 gdb_assert (ptr - baton->data == baton->size);
16767
0971de02 16768 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16769 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16770}
16771
4357ac6c
TT
16772/* Create appropriate locally-scoped variables for all the
16773 DW_TAG_common_block entries. Also create a struct common_block
16774 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 16775 is used to separate the common blocks name namespace from regular
4357ac6c 16776 variable names. */
c906108c
SS
16777
16778static void
e7c27a73 16779read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16780{
0971de02
TT
16781 struct attribute *attr;
16782
16783 attr = dwarf2_attr (die, DW_AT_location, cu);
16784 if (attr)
16785 {
16786 /* Support the .debug_loc offsets. */
16787 if (attr_form_is_block (attr))
16788 {
16789 /* Ok. */
16790 }
16791 else if (attr_form_is_section_offset (attr))
16792 {
16793 dwarf2_complex_location_expr_complaint ();
16794 attr = NULL;
16795 }
16796 else
16797 {
16798 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16799 "common block member");
16800 attr = NULL;
16801 }
16802 }
16803
639d11d3 16804 if (die->child != NULL)
c906108c 16805 {
518817b3 16806 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
4357ac6c
TT
16807 struct die_info *child_die;
16808 size_t n_entries = 0, size;
16809 struct common_block *common_block;
16810 struct symbol *sym;
74ac6d43 16811
4357ac6c
TT
16812 for (child_die = die->child;
16813 child_die && child_die->tag;
16814 child_die = sibling_die (child_die))
16815 ++n_entries;
16816
16817 size = (sizeof (struct common_block)
16818 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16819 common_block
16820 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16821 size);
4357ac6c
TT
16822 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16823 common_block->n_entries = 0;
16824
16825 for (child_die = die->child;
16826 child_die && child_die->tag;
16827 child_die = sibling_die (child_die))
16828 {
16829 /* Create the symbol in the DW_TAG_common_block block in the current
16830 symbol scope. */
e7c27a73 16831 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16832 if (sym != NULL)
16833 {
16834 struct attribute *member_loc;
16835
16836 common_block->contents[common_block->n_entries++] = sym;
16837
16838 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16839 cu);
16840 if (member_loc)
16841 {
16842 /* GDB has handled this for a long time, but it is
16843 not specified by DWARF. It seems to have been
16844 emitted by gfortran at least as recently as:
16845 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16846 complaint (_("Variable in common block has "
0971de02 16847 "DW_AT_data_member_location "
9d8780f0
SM
16848 "- DIE at %s [in module %s]"),
16849 sect_offset_str (child_die->sect_off),
518817b3 16850 objfile_name (objfile));
0971de02
TT
16851
16852 if (attr_form_is_section_offset (member_loc))
16853 dwarf2_complex_location_expr_complaint ();
16854 else if (attr_form_is_constant (member_loc)
16855 || attr_form_is_block (member_loc))
16856 {
16857 if (attr)
16858 mark_common_block_symbol_computed (sym, die, attr,
16859 member_loc, cu);
16860 }
16861 else
16862 dwarf2_complex_location_expr_complaint ();
16863 }
16864 }
c906108c 16865 }
4357ac6c
TT
16866
16867 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16868 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16869 }
16870}
16871
0114d602 16872/* Create a type for a C++ namespace. */
d9fa45fe 16873
0114d602
DJ
16874static struct type *
16875read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16876{
518817b3 16877 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16878 const char *previous_prefix, *name;
9219021c 16879 int is_anonymous;
0114d602
DJ
16880 struct type *type;
16881
16882 /* For extensions, reuse the type of the original namespace. */
16883 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16884 {
16885 struct die_info *ext_die;
16886 struct dwarf2_cu *ext_cu = cu;
9a619af0 16887
0114d602
DJ
16888 ext_die = dwarf2_extension (die, &ext_cu);
16889 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16890
16891 /* EXT_CU may not be the same as CU.
02142a6c 16892 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16893 return set_die_type (die, type, cu);
16894 }
9219021c 16895
e142c38c 16896 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16897
16898 /* Now build the name of the current namespace. */
16899
0114d602
DJ
16900 previous_prefix = determine_prefix (die, cu);
16901 if (previous_prefix[0] != '\0')
16902 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16903 previous_prefix, name, 0, cu);
0114d602
DJ
16904
16905 /* Create the type. */
19f392bc 16906 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16907
60531b24 16908 return set_die_type (die, type, cu);
0114d602
DJ
16909}
16910
22cee43f 16911/* Read a namespace scope. */
0114d602
DJ
16912
16913static void
16914read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16915{
518817b3 16916 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16917 int is_anonymous;
9219021c 16918
5c4e30ca
DC
16919 /* Add a symbol associated to this if we haven't seen the namespace
16920 before. Also, add a using directive if it's an anonymous
16921 namespace. */
9219021c 16922
f2f0e013 16923 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16924 {
16925 struct type *type;
16926
0114d602 16927 type = read_type_die (die, cu);
e7c27a73 16928 new_symbol (die, type, cu);
5c4e30ca 16929
e8e80198 16930 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16931 if (is_anonymous)
0114d602
DJ
16932 {
16933 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16934
eb1e02fd 16935 std::vector<const char *> excludes;
804d2729 16936 add_using_directive (using_directives (cu),
22cee43f 16937 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 16938 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16939 }
5c4e30ca 16940 }
9219021c 16941
639d11d3 16942 if (die->child != NULL)
d9fa45fe 16943 {
639d11d3 16944 struct die_info *child_die = die->child;
6e70227d 16945
d9fa45fe
DC
16946 while (child_die && child_die->tag)
16947 {
e7c27a73 16948 process_die (child_die, cu);
d9fa45fe
DC
16949 child_die = sibling_die (child_die);
16950 }
16951 }
38d518c9
EZ
16952}
16953
f55ee35c
JK
16954/* Read a Fortran module as type. This DIE can be only a declaration used for
16955 imported module. Still we need that type as local Fortran "use ... only"
16956 declaration imports depend on the created type in determine_prefix. */
16957
16958static struct type *
16959read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16960{
518817b3 16961 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15d034d0 16962 const char *module_name;
f55ee35c
JK
16963 struct type *type;
16964
16965 module_name = dwarf2_name (die, cu);
19f392bc 16966 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 16967
f55ee35c
JK
16968 return set_die_type (die, type, cu);
16969}
16970
5d7cb8df
JK
16971/* Read a Fortran module. */
16972
16973static void
16974read_module (struct die_info *die, struct dwarf2_cu *cu)
16975{
16976 struct die_info *child_die = die->child;
530e8392
KB
16977 struct type *type;
16978
16979 type = read_type_die (die, cu);
16980 new_symbol (die, type, cu);
5d7cb8df 16981
5d7cb8df
JK
16982 while (child_die && child_die->tag)
16983 {
16984 process_die (child_die, cu);
16985 child_die = sibling_die (child_die);
16986 }
16987}
16988
38d518c9
EZ
16989/* Return the name of the namespace represented by DIE. Set
16990 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16991 namespace. */
16992
16993static const char *
e142c38c 16994namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16995{
16996 struct die_info *current_die;
16997 const char *name = NULL;
16998
16999 /* Loop through the extensions until we find a name. */
17000
17001 for (current_die = die;
17002 current_die != NULL;
f2f0e013 17003 current_die = dwarf2_extension (die, &cu))
38d518c9 17004 {
96553a0c
DE
17005 /* We don't use dwarf2_name here so that we can detect the absence
17006 of a name -> anonymous namespace. */
7d45c7c3 17007 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 17008
38d518c9
EZ
17009 if (name != NULL)
17010 break;
17011 }
17012
17013 /* Is it an anonymous namespace? */
17014
17015 *is_anonymous = (name == NULL);
17016 if (*is_anonymous)
2b1dbab0 17017 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
17018
17019 return name;
d9fa45fe
DC
17020}
17021
c906108c
SS
17022/* Extract all information from a DW_TAG_pointer_type DIE and add to
17023 the user defined type vector. */
17024
f792889a 17025static struct type *
e7c27a73 17026read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17027{
518817b3
SM
17028 struct gdbarch *gdbarch
17029 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
e7c27a73 17030 struct comp_unit_head *cu_header = &cu->header;
c906108c 17031 struct type *type;
8b2dbe47
KB
17032 struct attribute *attr_byte_size;
17033 struct attribute *attr_address_class;
17034 int byte_size, addr_class;
7e314c57
JK
17035 struct type *target_type;
17036
17037 target_type = die_type (die, cu);
c906108c 17038
7e314c57
JK
17039 /* The die_type call above may have already set the type for this DIE. */
17040 type = get_die_type (die, cu);
17041 if (type)
17042 return type;
17043
17044 type = lookup_pointer_type (target_type);
8b2dbe47 17045
e142c38c 17046 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
17047 if (attr_byte_size)
17048 byte_size = DW_UNSND (attr_byte_size);
c906108c 17049 else
8b2dbe47
KB
17050 byte_size = cu_header->addr_size;
17051
e142c38c 17052 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
17053 if (attr_address_class)
17054 addr_class = DW_UNSND (attr_address_class);
17055 else
17056 addr_class = DW_ADDR_none;
17057
2b4424c3
TT
17058 ULONGEST alignment = get_alignment (cu, die);
17059
17060 /* If the pointer size, alignment, or address class is different
17061 than the default, create a type variant marked as such and set
17062 the length accordingly. */
17063 if (TYPE_LENGTH (type) != byte_size
17064 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17065 && alignment != TYPE_RAW_ALIGN (type))
17066 || addr_class != DW_ADDR_none)
c906108c 17067 {
5e2b427d 17068 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
17069 {
17070 int type_flags;
17071
849957d9 17072 type_flags = gdbarch_address_class_type_flags
5e2b427d 17073 (gdbarch, byte_size, addr_class);
876cecd0
TT
17074 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17075 == 0);
8b2dbe47
KB
17076 type = make_type_with_address_space (type, type_flags);
17077 }
17078 else if (TYPE_LENGTH (type) != byte_size)
17079 {
b98664d3 17080 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 17081 }
2b4424c3
TT
17082 else if (TYPE_RAW_ALIGN (type) != alignment)
17083 {
b98664d3 17084 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
17085 " - DIE at %s [in module %s]"),
17086 sect_offset_str (die->sect_off),
17087 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17088 }
6e70227d 17089 else
9a619af0
MS
17090 {
17091 /* Should we also complain about unhandled address classes? */
17092 }
c906108c 17093 }
8b2dbe47
KB
17094
17095 TYPE_LENGTH (type) = byte_size;
2b4424c3 17096 set_type_align (type, alignment);
f792889a 17097 return set_die_type (die, type, cu);
c906108c
SS
17098}
17099
17100/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17101 the user defined type vector. */
17102
f792889a 17103static struct type *
e7c27a73 17104read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
17105{
17106 struct type *type;
17107 struct type *to_type;
17108 struct type *domain;
17109
e7c27a73
DJ
17110 to_type = die_type (die, cu);
17111 domain = die_containing_type (die, cu);
0d5de010 17112
7e314c57
JK
17113 /* The calls above may have already set the type for this DIE. */
17114 type = get_die_type (die, cu);
17115 if (type)
17116 return type;
17117
0d5de010
DJ
17118 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17119 type = lookup_methodptr_type (to_type);
7078baeb
TT
17120 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17121 {
518817b3
SM
17122 struct type *new_type
17123 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
7078baeb
TT
17124
17125 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17126 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17127 TYPE_VARARGS (to_type));
17128 type = lookup_methodptr_type (new_type);
17129 }
0d5de010
DJ
17130 else
17131 type = lookup_memberptr_type (to_type, domain);
c906108c 17132
f792889a 17133 return set_die_type (die, type, cu);
c906108c
SS
17134}
17135
4297a3f0 17136/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
17137 the user defined type vector. */
17138
f792889a 17139static struct type *
4297a3f0
AV
17140read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17141 enum type_code refcode)
c906108c 17142{
e7c27a73 17143 struct comp_unit_head *cu_header = &cu->header;
7e314c57 17144 struct type *type, *target_type;
c906108c
SS
17145 struct attribute *attr;
17146
4297a3f0
AV
17147 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17148
7e314c57
JK
17149 target_type = die_type (die, cu);
17150
17151 /* The die_type call above may have already set the type for this DIE. */
17152 type = get_die_type (die, cu);
17153 if (type)
17154 return type;
17155
4297a3f0 17156 type = lookup_reference_type (target_type, refcode);
e142c38c 17157 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
17158 if (attr)
17159 {
17160 TYPE_LENGTH (type) = DW_UNSND (attr);
17161 }
17162 else
17163 {
107d2387 17164 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 17165 }
2b4424c3 17166 maybe_set_alignment (cu, die, type);
f792889a 17167 return set_die_type (die, type, cu);
c906108c
SS
17168}
17169
cf363f18
MW
17170/* Add the given cv-qualifiers to the element type of the array. GCC
17171 outputs DWARF type qualifiers that apply to an array, not the
17172 element type. But GDB relies on the array element type to carry
17173 the cv-qualifiers. This mimics section 6.7.3 of the C99
17174 specification. */
17175
17176static struct type *
17177add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17178 struct type *base_type, int cnst, int voltl)
17179{
17180 struct type *el_type, *inner_array;
17181
17182 base_type = copy_type (base_type);
17183 inner_array = base_type;
17184
17185 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17186 {
17187 TYPE_TARGET_TYPE (inner_array) =
17188 copy_type (TYPE_TARGET_TYPE (inner_array));
17189 inner_array = TYPE_TARGET_TYPE (inner_array);
17190 }
17191
17192 el_type = TYPE_TARGET_TYPE (inner_array);
17193 cnst |= TYPE_CONST (el_type);
17194 voltl |= TYPE_VOLATILE (el_type);
17195 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17196
17197 return set_die_type (die, base_type, cu);
17198}
17199
f792889a 17200static struct type *
e7c27a73 17201read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17202{
f792889a 17203 struct type *base_type, *cv_type;
c906108c 17204
e7c27a73 17205 base_type = die_type (die, cu);
7e314c57
JK
17206
17207 /* The die_type call above may have already set the type for this DIE. */
17208 cv_type = get_die_type (die, cu);
17209 if (cv_type)
17210 return cv_type;
17211
2f608a3a
KW
17212 /* In case the const qualifier is applied to an array type, the element type
17213 is so qualified, not the array type (section 6.7.3 of C99). */
17214 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 17215 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 17216
f792889a
DJ
17217 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17218 return set_die_type (die, cv_type, cu);
c906108c
SS
17219}
17220
f792889a 17221static struct type *
e7c27a73 17222read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17223{
f792889a 17224 struct type *base_type, *cv_type;
c906108c 17225
e7c27a73 17226 base_type = die_type (die, cu);
7e314c57
JK
17227
17228 /* The die_type call above may have already set the type for this DIE. */
17229 cv_type = get_die_type (die, cu);
17230 if (cv_type)
17231 return cv_type;
17232
cf363f18
MW
17233 /* In case the volatile qualifier is applied to an array type, the
17234 element type is so qualified, not the array type (section 6.7.3
17235 of C99). */
17236 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17237 return add_array_cv_type (die, cu, base_type, 0, 1);
17238
f792889a
DJ
17239 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17240 return set_die_type (die, cv_type, cu);
c906108c
SS
17241}
17242
06d66ee9
TT
17243/* Handle DW_TAG_restrict_type. */
17244
17245static struct type *
17246read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17247{
17248 struct type *base_type, *cv_type;
17249
17250 base_type = die_type (die, cu);
17251
17252 /* The die_type call above may have already set the type for this DIE. */
17253 cv_type = get_die_type (die, cu);
17254 if (cv_type)
17255 return cv_type;
17256
17257 cv_type = make_restrict_type (base_type);
17258 return set_die_type (die, cv_type, cu);
17259}
17260
a2c2acaf
MW
17261/* Handle DW_TAG_atomic_type. */
17262
17263static struct type *
17264read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17265{
17266 struct type *base_type, *cv_type;
17267
17268 base_type = die_type (die, cu);
17269
17270 /* The die_type call above may have already set the type for this DIE. */
17271 cv_type = get_die_type (die, cu);
17272 if (cv_type)
17273 return cv_type;
17274
17275 cv_type = make_atomic_type (base_type);
17276 return set_die_type (die, cv_type, cu);
17277}
17278
c906108c
SS
17279/* Extract all information from a DW_TAG_string_type DIE and add to
17280 the user defined type vector. It isn't really a user defined type,
17281 but it behaves like one, with other DIE's using an AT_user_def_type
17282 attribute to reference it. */
17283
f792889a 17284static struct type *
e7c27a73 17285read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17286{
518817b3 17287 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3b7538c0 17288 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
17289 struct type *type, *range_type, *index_type, *char_type;
17290 struct attribute *attr;
17291 unsigned int length;
17292
e142c38c 17293 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
17294 if (attr)
17295 {
17296 length = DW_UNSND (attr);
17297 }
17298 else
17299 {
0963b4bd 17300 /* Check for the DW_AT_byte_size attribute. */
e142c38c 17301 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
17302 if (attr)
17303 {
17304 length = DW_UNSND (attr);
17305 }
17306 else
17307 {
17308 length = 1;
17309 }
c906108c 17310 }
6ccb9162 17311
46bf5051 17312 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 17313 range_type = create_static_range_type (NULL, index_type, 1, length);
3b7538c0
UW
17314 char_type = language_string_char_type (cu->language_defn, gdbarch);
17315 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17316
f792889a 17317 return set_die_type (die, type, cu);
c906108c
SS
17318}
17319
4d804846
JB
17320/* Assuming that DIE corresponds to a function, returns nonzero
17321 if the function is prototyped. */
17322
17323static int
17324prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17325{
17326 struct attribute *attr;
17327
17328 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17329 if (attr && (DW_UNSND (attr) != 0))
17330 return 1;
17331
17332 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 17333 is only meaningful for C, but the concept also extends to other
4d804846
JB
17334 languages that allow unprototyped functions (Eg: Objective C).
17335 For all other languages, assume that functions are always
17336 prototyped. */
17337 if (cu->language != language_c
17338 && cu->language != language_objc
17339 && cu->language != language_opencl)
17340 return 1;
17341
17342 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17343 prototyped and unprototyped functions; default to prototyped,
17344 since that is more common in modern code (and RealView warns
17345 about unprototyped functions). */
17346 if (producer_is_realview (cu->producer))
17347 return 1;
17348
17349 return 0;
17350}
17351
c906108c
SS
17352/* Handle DIES due to C code like:
17353
17354 struct foo
c5aa993b
JM
17355 {
17356 int (*funcp)(int a, long l);
17357 int b;
17358 };
c906108c 17359
0963b4bd 17360 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17361
f792889a 17362static struct type *
e7c27a73 17363read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17364{
518817b3 17365 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0963b4bd
MS
17366 struct type *type; /* Type that this function returns. */
17367 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17368 struct attribute *attr;
17369
e7c27a73 17370 type = die_type (die, cu);
7e314c57
JK
17371
17372 /* The die_type call above may have already set the type for this DIE. */
17373 ftype = get_die_type (die, cu);
17374 if (ftype)
17375 return ftype;
17376
0c8b41f1 17377 ftype = lookup_function_type (type);
c906108c 17378
4d804846 17379 if (prototyped_function_p (die, cu))
a6c727b2 17380 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17381
c055b101
CV
17382 /* Store the calling convention in the type if it's available in
17383 the subroutine die. Otherwise set the calling convention to
17384 the default value DW_CC_normal. */
17385 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
17386 if (attr)
17387 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17388 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17389 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17390 else
17391 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17392
743649fd
MW
17393 /* Record whether the function returns normally to its caller or not
17394 if the DWARF producer set that information. */
17395 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17396 if (attr && (DW_UNSND (attr) != 0))
17397 TYPE_NO_RETURN (ftype) = 1;
17398
76c10ea2
GM
17399 /* We need to add the subroutine type to the die immediately so
17400 we don't infinitely recurse when dealing with parameters
0963b4bd 17401 declared as the same subroutine type. */
76c10ea2 17402 set_die_type (die, ftype, cu);
6e70227d 17403
639d11d3 17404 if (die->child != NULL)
c906108c 17405 {
bb5ed363 17406 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17407 struct die_info *child_die;
8072405b 17408 int nparams, iparams;
c906108c
SS
17409
17410 /* Count the number of parameters.
17411 FIXME: GDB currently ignores vararg functions, but knows about
17412 vararg member functions. */
8072405b 17413 nparams = 0;
639d11d3 17414 child_die = die->child;
c906108c
SS
17415 while (child_die && child_die->tag)
17416 {
17417 if (child_die->tag == DW_TAG_formal_parameter)
17418 nparams++;
17419 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17420 TYPE_VARARGS (ftype) = 1;
c906108c
SS
17421 child_die = sibling_die (child_die);
17422 }
17423
17424 /* Allocate storage for parameters and fill them in. */
17425 TYPE_NFIELDS (ftype) = nparams;
17426 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 17427 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 17428
8072405b
JK
17429 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17430 even if we error out during the parameters reading below. */
17431 for (iparams = 0; iparams < nparams; iparams++)
17432 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17433
17434 iparams = 0;
639d11d3 17435 child_die = die->child;
c906108c
SS
17436 while (child_die && child_die->tag)
17437 {
17438 if (child_die->tag == DW_TAG_formal_parameter)
17439 {
3ce3b1ba
PA
17440 struct type *arg_type;
17441
17442 /* DWARF version 2 has no clean way to discern C++
17443 static and non-static member functions. G++ helps
17444 GDB by marking the first parameter for non-static
17445 member functions (which is the this pointer) as
17446 artificial. We pass this information to
17447 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17448
17449 DWARF version 3 added DW_AT_object_pointer, which GCC
17450 4.5 does not yet generate. */
e142c38c 17451 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
17452 if (attr)
17453 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17454 else
9c37b5ae 17455 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17456 arg_type = die_type (child_die, cu);
17457
17458 /* RealView does not mark THIS as const, which the testsuite
17459 expects. GCC marks THIS as const in method definitions,
17460 but not in the class specifications (GCC PR 43053). */
17461 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17462 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17463 {
17464 int is_this = 0;
17465 struct dwarf2_cu *arg_cu = cu;
17466 const char *name = dwarf2_name (child_die, cu);
17467
17468 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17469 if (attr)
17470 {
17471 /* If the compiler emits this, use it. */
17472 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17473 is_this = 1;
17474 }
17475 else if (name && strcmp (name, "this") == 0)
17476 /* Function definitions will have the argument names. */
17477 is_this = 1;
17478 else if (name == NULL && iparams == 0)
17479 /* Declarations may not have the names, so like
17480 elsewhere in GDB, assume an artificial first
17481 argument is "this". */
17482 is_this = 1;
17483
17484 if (is_this)
17485 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17486 arg_type, 0);
17487 }
17488
17489 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
17490 iparams++;
17491 }
17492 child_die = sibling_die (child_die);
17493 }
17494 }
17495
76c10ea2 17496 return ftype;
c906108c
SS
17497}
17498
f792889a 17499static struct type *
e7c27a73 17500read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17501{
518817b3 17502 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 17503 const char *name = NULL;
3c8e0968 17504 struct type *this_type, *target_type;
c906108c 17505
94af9270 17506 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17507 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17508 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17509 set_die_type (die, this_type, cu);
3c8e0968
DE
17510 target_type = die_type (die, cu);
17511 if (target_type != this_type)
17512 TYPE_TARGET_TYPE (this_type) = target_type;
17513 else
17514 {
17515 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17516 spec and cause infinite loops in GDB. */
b98664d3 17517 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17518 "- DIE at %s [in module %s]"),
17519 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17520 TYPE_TARGET_TYPE (this_type) = NULL;
17521 }
f792889a 17522 return this_type;
c906108c
SS
17523}
17524
9b790ce7
UW
17525/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17526 (which may be different from NAME) to the architecture back-end to allow
17527 it to guess the correct format if necessary. */
17528
17529static struct type *
17530dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17531 const char *name_hint)
17532{
17533 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17534 const struct floatformat **format;
17535 struct type *type;
17536
17537 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17538 if (format)
17539 type = init_float_type (objfile, bits, name, format);
17540 else
77b7c781 17541 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17542
17543 return type;
17544}
17545
eb77c9df
AB
17546/* Allocate an integer type of size BITS and name NAME. */
17547
17548static struct type *
17549dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17550 int bits, int unsigned_p, const char *name)
17551{
17552 struct type *type;
17553
17554 /* Versions of Intel's C Compiler generate an integer type called "void"
17555 instead of using DW_TAG_unspecified_type. This has been seen on
17556 at least versions 14, 17, and 18. */
35ee2dc2
AB
17557 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17558 && strcmp (name, "void") == 0)
eb77c9df
AB
17559 type = objfile_type (objfile)->builtin_void;
17560 else
17561 type = init_integer_type (objfile, bits, unsigned_p, name);
17562
17563 return type;
17564}
17565
8bdc1658
AB
17566/* Initialise and return a floating point type of size BITS suitable for
17567 use as a component of a complex number. The NAME_HINT is passed through
17568 when initialising the floating point type and is the name of the complex
17569 type.
17570
17571 As DWARF doesn't currently provide an explicit name for the components
17572 of a complex number, but it can be helpful to have these components
17573 named, we try to select a suitable name based on the size of the
17574 component. */
17575static struct type *
17576dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17577 struct objfile *objfile,
17578 int bits, const char *name_hint)
17579{
17580 gdbarch *gdbarch = get_objfile_arch (objfile);
17581 struct type *tt = nullptr;
17582
35add35e
AB
17583 /* Try to find a suitable floating point builtin type of size BITS.
17584 We're going to use the name of this type as the name for the complex
17585 target type that we are about to create. */
1db455a7 17586 switch (cu->language)
8bdc1658 17587 {
1db455a7
AB
17588 case language_fortran:
17589 switch (bits)
17590 {
17591 case 32:
17592 tt = builtin_f_type (gdbarch)->builtin_real;
17593 break;
17594 case 64:
17595 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17596 break;
17597 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17598 case 128:
17599 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17600 break;
17601 }
8bdc1658 17602 break;
1db455a7
AB
17603 default:
17604 switch (bits)
17605 {
17606 case 32:
17607 tt = builtin_type (gdbarch)->builtin_float;
17608 break;
17609 case 64:
17610 tt = builtin_type (gdbarch)->builtin_double;
17611 break;
17612 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17613 case 128:
17614 tt = builtin_type (gdbarch)->builtin_long_double;
17615 break;
17616 }
8bdc1658
AB
17617 break;
17618 }
17619
35add35e
AB
17620 /* If the type we found doesn't match the size we were looking for, then
17621 pretend we didn't find a type at all, the complex target type we
17622 create will then be nameless. */
a12e5744 17623 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
17624 tt = nullptr;
17625
8bdc1658
AB
17626 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
17627 return dwarf2_init_float_type (objfile, bits, name, name_hint);
17628}
17629
c906108c
SS
17630/* Find a representation of a given base type and install
17631 it in the TYPE field of the die. */
17632
f792889a 17633static struct type *
e7c27a73 17634read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17635{
518817b3 17636 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
17637 struct type *type;
17638 struct attribute *attr;
19f392bc 17639 int encoding = 0, bits = 0;
15d034d0 17640 const char *name;
c906108c 17641
e142c38c 17642 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
17643 if (attr)
17644 {
17645 encoding = DW_UNSND (attr);
17646 }
e142c38c 17647 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
17648 if (attr)
17649 {
19f392bc 17650 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
c906108c 17651 }
39cbfefa 17652 name = dwarf2_name (die, cu);
6ccb9162 17653 if (!name)
c906108c 17654 {
b98664d3 17655 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
c906108c 17656 }
6ccb9162
UW
17657
17658 switch (encoding)
c906108c 17659 {
6ccb9162
UW
17660 case DW_ATE_address:
17661 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17662 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17663 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17664 break;
17665 case DW_ATE_boolean:
19f392bc 17666 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17667 break;
17668 case DW_ATE_complex_float:
8bdc1658 17669 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name);
19f392bc 17670 type = init_complex_type (objfile, name, type);
6ccb9162
UW
17671 break;
17672 case DW_ATE_decimal_float:
19f392bc 17673 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17674 break;
17675 case DW_ATE_float:
9b790ce7 17676 type = dwarf2_init_float_type (objfile, bits, name, name);
6ccb9162
UW
17677 break;
17678 case DW_ATE_signed:
eb77c9df 17679 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17680 break;
17681 case DW_ATE_unsigned:
3b2b8fea
TT
17682 if (cu->language == language_fortran
17683 && name
61012eef 17684 && startswith (name, "character("))
19f392bc
UW
17685 type = init_character_type (objfile, bits, 1, name);
17686 else
eb77c9df 17687 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
17688 break;
17689 case DW_ATE_signed_char:
6e70227d 17690 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17691 || cu->language == language_pascal
17692 || cu->language == language_fortran)
19f392bc
UW
17693 type = init_character_type (objfile, bits, 0, name);
17694 else
eb77c9df 17695 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17696 break;
17697 case DW_ATE_unsigned_char:
868a0084 17698 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17699 || cu->language == language_pascal
c44af4eb
TT
17700 || cu->language == language_fortran
17701 || cu->language == language_rust)
19f392bc
UW
17702 type = init_character_type (objfile, bits, 1, name);
17703 else
eb77c9df 17704 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 17705 break;
75079b2b 17706 case DW_ATE_UTF:
53e710ac
PA
17707 {
17708 gdbarch *arch = get_objfile_arch (objfile);
17709
17710 if (bits == 16)
17711 type = builtin_type (arch)->builtin_char16;
17712 else if (bits == 32)
17713 type = builtin_type (arch)->builtin_char32;
17714 else
17715 {
b98664d3 17716 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 17717 bits);
eb77c9df 17718 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
17719 }
17720 return set_die_type (die, type, cu);
17721 }
75079b2b
TT
17722 break;
17723
6ccb9162 17724 default:
b98664d3 17725 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 17726 dwarf_type_encoding_name (encoding));
77b7c781 17727 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17728 break;
c906108c 17729 }
6ccb9162 17730
0114d602 17731 if (name && strcmp (name, "char") == 0)
876cecd0 17732 TYPE_NOSIGN (type) = 1;
0114d602 17733
2b4424c3
TT
17734 maybe_set_alignment (cu, die, type);
17735
f792889a 17736 return set_die_type (die, type, cu);
c906108c
SS
17737}
17738
80180f79
SA
17739/* Parse dwarf attribute if it's a block, reference or constant and put the
17740 resulting value of the attribute into struct bound_prop.
17741 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17742
17743static int
17744attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
17745 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17746 struct type *default_type)
80180f79
SA
17747{
17748 struct dwarf2_property_baton *baton;
518817b3
SM
17749 struct obstack *obstack
17750 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
80180f79 17751
9a49df9d
AB
17752 gdb_assert (default_type != NULL);
17753
80180f79
SA
17754 if (attr == NULL || prop == NULL)
17755 return 0;
17756
17757 if (attr_form_is_block (attr))
17758 {
8d749320 17759 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17760 baton->property_type = default_type;
80180f79
SA
17761 baton->locexpr.per_cu = cu->per_cu;
17762 baton->locexpr.size = DW_BLOCK (attr)->size;
17763 baton->locexpr.data = DW_BLOCK (attr)->data;
9a49df9d 17764 baton->locexpr.is_reference = false;
80180f79
SA
17765 prop->data.baton = baton;
17766 prop->kind = PROP_LOCEXPR;
17767 gdb_assert (prop->data.baton != NULL);
17768 }
17769 else if (attr_form_is_ref (attr))
17770 {
17771 struct dwarf2_cu *target_cu = cu;
17772 struct die_info *target_die;
17773 struct attribute *target_attr;
17774
17775 target_die = follow_die_ref (die, attr, &target_cu);
17776 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17777 if (target_attr == NULL)
17778 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17779 target_cu);
80180f79
SA
17780 if (target_attr == NULL)
17781 return 0;
17782
df25ebbd 17783 switch (target_attr->name)
80180f79 17784 {
df25ebbd
JB
17785 case DW_AT_location:
17786 if (attr_form_is_section_offset (target_attr))
17787 {
8d749320 17788 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17789 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17790 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17791 prop->data.baton = baton;
17792 prop->kind = PROP_LOCLIST;
17793 gdb_assert (prop->data.baton != NULL);
17794 }
17795 else if (attr_form_is_block (target_attr))
17796 {
8d749320 17797 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17798 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17799 baton->locexpr.per_cu = cu->per_cu;
17800 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17801 baton->locexpr.data = DW_BLOCK (target_attr)->data;
9a49df9d 17802 baton->locexpr.is_reference = true;
df25ebbd
JB
17803 prop->data.baton = baton;
17804 prop->kind = PROP_LOCEXPR;
17805 gdb_assert (prop->data.baton != NULL);
17806 }
17807 else
17808 {
17809 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17810 "dynamic property");
17811 return 0;
17812 }
17813 break;
17814 case DW_AT_data_member_location:
17815 {
17816 LONGEST offset;
17817
17818 if (!handle_data_member_location (target_die, target_cu,
17819 &offset))
17820 return 0;
17821
8d749320 17822 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17823 baton->property_type = read_type_die (target_die->parent,
6ad395a7 17824 target_cu);
df25ebbd
JB
17825 baton->offset_info.offset = offset;
17826 baton->offset_info.type = die_type (target_die, target_cu);
17827 prop->data.baton = baton;
17828 prop->kind = PROP_ADDR_OFFSET;
17829 break;
17830 }
80180f79
SA
17831 }
17832 }
17833 else if (attr_form_is_constant (attr))
17834 {
17835 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17836 prop->kind = PROP_CONST;
17837 }
17838 else
17839 {
17840 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17841 dwarf2_name (die, cu));
17842 return 0;
17843 }
17844
17845 return 1;
17846}
17847
9a49df9d
AB
17848/* Find an integer type the same size as the address size given in the
17849 compilation unit header for PER_CU. UNSIGNED_P controls if the integer
17850 is unsigned or not. */
17851
17852static struct type *
17853dwarf2_per_cu_addr_sized_int_type (struct dwarf2_per_cu_data *per_cu,
17854 bool unsigned_p)
17855{
17856 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
17857 int addr_size = dwarf2_per_cu_addr_size (per_cu);
17858 struct type *int_type;
17859
17860 /* Helper macro to examine the various builtin types. */
17861#define TRY_TYPE(F) \
17862 int_type = (unsigned_p \
17863 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17864 : objfile_type (objfile)->builtin_ ## F); \
17865 if (int_type != NULL && TYPE_LENGTH (int_type) == addr_size) \
17866 return int_type
17867
17868 TRY_TYPE (char);
17869 TRY_TYPE (short);
17870 TRY_TYPE (int);
17871 TRY_TYPE (long);
17872 TRY_TYPE (long_long);
17873
17874#undef TRY_TYPE
17875
17876 gdb_assert_not_reached ("unable to find suitable integer type");
17877}
17878
b86352cf
AB
17879/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17880 present (which is valid) then compute the default type based on the
17881 compilation units address size. */
17882
17883static struct type *
17884read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17885{
17886 struct type *index_type = die_type (die, cu);
17887
17888 /* Dwarf-2 specifications explicitly allows to create subrange types
17889 without specifying a base type.
17890 In that case, the base type must be set to the type of
17891 the lower bound, upper bound or count, in that order, if any of these
17892 three attributes references an object that has a type.
17893 If no base type is found, the Dwarf-2 specifications say that
17894 a signed integer type of size equal to the size of an address should
17895 be used.
17896 For the following C code: `extern char gdb_int [];'
17897 GCC produces an empty range DIE.
17898 FIXME: muller/2010-05-28: Possible references to object for low bound,
17899 high bound or count are not yet handled by this code. */
17900 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
9a49df9d 17901 index_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
b86352cf
AB
17902
17903 return index_type;
17904}
17905
a02abb62
JB
17906/* Read the given DW_AT_subrange DIE. */
17907
f792889a 17908static struct type *
a02abb62
JB
17909read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17910{
4c9ad8c2 17911 struct type *base_type, *orig_base_type;
a02abb62
JB
17912 struct type *range_type;
17913 struct attribute *attr;
729efb13 17914 struct dynamic_prop low, high;
4fae6e18 17915 int low_default_is_valid;
c451ebe5 17916 int high_bound_is_count = 0;
15d034d0 17917 const char *name;
d359392f 17918 ULONGEST negative_mask;
e77813c8 17919
b86352cf
AB
17920 orig_base_type = read_subrange_index_type (die, cu);
17921
4c9ad8c2
TT
17922 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17923 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17924 creating the range type, but we use the result of check_typedef
17925 when examining properties of the type. */
17926 base_type = check_typedef (orig_base_type);
a02abb62 17927
7e314c57
JK
17928 /* The die_type call above may have already set the type for this DIE. */
17929 range_type = get_die_type (die, cu);
17930 if (range_type)
17931 return range_type;
17932
729efb13
SA
17933 low.kind = PROP_CONST;
17934 high.kind = PROP_CONST;
17935 high.data.const_val = 0;
17936
4fae6e18
JK
17937 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17938 omitting DW_AT_lower_bound. */
17939 switch (cu->language)
6e70227d 17940 {
4fae6e18
JK
17941 case language_c:
17942 case language_cplus:
729efb13 17943 low.data.const_val = 0;
4fae6e18
JK
17944 low_default_is_valid = 1;
17945 break;
17946 case language_fortran:
729efb13 17947 low.data.const_val = 1;
4fae6e18
JK
17948 low_default_is_valid = 1;
17949 break;
17950 case language_d:
4fae6e18 17951 case language_objc:
c44af4eb 17952 case language_rust:
729efb13 17953 low.data.const_val = 0;
4fae6e18
JK
17954 low_default_is_valid = (cu->header.version >= 4);
17955 break;
17956 case language_ada:
17957 case language_m2:
17958 case language_pascal:
729efb13 17959 low.data.const_val = 1;
4fae6e18
JK
17960 low_default_is_valid = (cu->header.version >= 4);
17961 break;
17962 default:
729efb13 17963 low.data.const_val = 0;
4fae6e18
JK
17964 low_default_is_valid = 0;
17965 break;
a02abb62
JB
17966 }
17967
e142c38c 17968 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 17969 if (attr)
9a49df9d 17970 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 17971 else if (!low_default_is_valid)
b98664d3 17972 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
17973 "- DIE at %s [in module %s]"),
17974 sect_offset_str (die->sect_off),
518817b3 17975 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a02abb62 17976
506f5c41
TV
17977 struct attribute *attr_ub, *attr_count;
17978 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 17979 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 17980 {
506f5c41 17981 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 17982 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 17983 {
c451ebe5
SA
17984 /* If bounds are constant do the final calculation here. */
17985 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17986 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17987 else
17988 high_bound_is_count = 1;
c2ff108b 17989 }
506f5c41
TV
17990 else
17991 {
17992 if (attr_ub != NULL)
17993 complaint (_("Unresolved DW_AT_upper_bound "
17994 "- DIE at %s [in module %s]"),
17995 sect_offset_str (die->sect_off),
17996 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17997 if (attr_count != NULL)
17998 complaint (_("Unresolved DW_AT_count "
17999 "- DIE at %s [in module %s]"),
18000 sect_offset_str (die->sect_off),
18001 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
18002 }
e77813c8 18003 }
a02abb62 18004
4e962e74
TT
18005 LONGEST bias = 0;
18006 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
18007 if (bias_attr != nullptr && attr_form_is_constant (bias_attr))
18008 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
18009
dbb9c2b1
JB
18010 /* Normally, the DWARF producers are expected to use a signed
18011 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18012 But this is unfortunately not always the case, as witnessed
18013 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18014 is used instead. To work around that ambiguity, we treat
18015 the bounds as signed, and thus sign-extend their values, when
18016 the base type is signed. */
6e70227d 18017 negative_mask =
d359392f 18018 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
18019 if (low.kind == PROP_CONST
18020 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
18021 low.data.const_val |= negative_mask;
18022 if (high.kind == PROP_CONST
18023 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
18024 high.data.const_val |= negative_mask;
43bbcdc2 18025
4e962e74 18026 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 18027
c451ebe5
SA
18028 if (high_bound_is_count)
18029 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
18030
c2ff108b
JK
18031 /* Ada expects an empty array on no boundary attributes. */
18032 if (attr == NULL && cu->language != language_ada)
729efb13 18033 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 18034
39cbfefa
DJ
18035 name = dwarf2_name (die, cu);
18036 if (name)
18037 TYPE_NAME (range_type) = name;
6e70227d 18038
e142c38c 18039 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
18040 if (attr)
18041 TYPE_LENGTH (range_type) = DW_UNSND (attr);
18042
2b4424c3
TT
18043 maybe_set_alignment (cu, die, range_type);
18044
7e314c57
JK
18045 set_die_type (die, range_type, cu);
18046
18047 /* set_die_type should be already done. */
b4ba55a1
JB
18048 set_descriptive_type (range_type, die, cu);
18049
7e314c57 18050 return range_type;
a02abb62 18051}
6e70227d 18052
f792889a 18053static struct type *
81a17f79
JB
18054read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
18055{
18056 struct type *type;
81a17f79 18057
518817b3
SM
18058 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
18059 NULL);
0114d602 18060 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 18061
74a2f8ff 18062 /* In Ada, an unspecified type is typically used when the description
85102364 18063 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
18064 such a type, we treat it as a stub, and try to resolve it later on,
18065 when needed. */
18066 if (cu->language == language_ada)
18067 TYPE_STUB (type) = 1;
18068
f792889a 18069 return set_die_type (die, type, cu);
81a17f79 18070}
a02abb62 18071
639d11d3
DC
18072/* Read a single die and all its descendents. Set the die's sibling
18073 field to NULL; set other fields in the die correctly, and set all
18074 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18075 location of the info_ptr after reading all of those dies. PARENT
18076 is the parent of the die in question. */
18077
18078static struct die_info *
dee91e82 18079read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
18080 const gdb_byte *info_ptr,
18081 const gdb_byte **new_info_ptr,
dee91e82 18082 struct die_info *parent)
639d11d3
DC
18083{
18084 struct die_info *die;
d521ce57 18085 const gdb_byte *cur_ptr;
639d11d3
DC
18086 int has_children;
18087
bf6af496 18088 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
18089 if (die == NULL)
18090 {
18091 *new_info_ptr = cur_ptr;
18092 return NULL;
18093 }
93311388 18094 store_in_ref_table (die, reader->cu);
639d11d3
DC
18095
18096 if (has_children)
bf6af496 18097 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
18098 else
18099 {
18100 die->child = NULL;
18101 *new_info_ptr = cur_ptr;
18102 }
18103
18104 die->sibling = NULL;
18105 die->parent = parent;
18106 return die;
18107}
18108
18109/* Read a die, all of its descendents, and all of its siblings; set
18110 all of the fields of all of the dies correctly. Arguments are as
18111 in read_die_and_children. */
18112
18113static struct die_info *
bf6af496 18114read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
18115 const gdb_byte *info_ptr,
18116 const gdb_byte **new_info_ptr,
bf6af496 18117 struct die_info *parent)
639d11d3
DC
18118{
18119 struct die_info *first_die, *last_sibling;
d521ce57 18120 const gdb_byte *cur_ptr;
639d11d3 18121
c906108c 18122 cur_ptr = info_ptr;
639d11d3
DC
18123 first_die = last_sibling = NULL;
18124
18125 while (1)
c906108c 18126 {
639d11d3 18127 struct die_info *die
dee91e82 18128 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 18129
1d325ec1 18130 if (die == NULL)
c906108c 18131 {
639d11d3
DC
18132 *new_info_ptr = cur_ptr;
18133 return first_die;
c906108c 18134 }
1d325ec1
DJ
18135
18136 if (!first_die)
18137 first_die = die;
c906108c 18138 else
1d325ec1
DJ
18139 last_sibling->sibling = die;
18140
18141 last_sibling = die;
c906108c 18142 }
c906108c
SS
18143}
18144
bf6af496
DE
18145/* Read a die, all of its descendents, and all of its siblings; set
18146 all of the fields of all of the dies correctly. Arguments are as
18147 in read_die_and_children.
18148 This the main entry point for reading a DIE and all its children. */
18149
18150static struct die_info *
18151read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
18152 const gdb_byte *info_ptr,
18153 const gdb_byte **new_info_ptr,
bf6af496
DE
18154 struct die_info *parent)
18155{
18156 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18157 new_info_ptr, parent);
18158
b4f54984 18159 if (dwarf_die_debug)
bf6af496
DE
18160 {
18161 fprintf_unfiltered (gdb_stdlog,
18162 "Read die from %s@0x%x of %s:\n",
a32a8923 18163 get_section_name (reader->die_section),
bf6af496
DE
18164 (unsigned) (info_ptr - reader->die_section->buffer),
18165 bfd_get_filename (reader->abfd));
b4f54984 18166 dump_die (die, dwarf_die_debug);
bf6af496
DE
18167 }
18168
18169 return die;
18170}
18171
3019eac3
DE
18172/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18173 attributes.
18174 The caller is responsible for filling in the extra attributes
18175 and updating (*DIEP)->num_attrs.
18176 Set DIEP to point to a newly allocated die with its information,
18177 except for its child, sibling, and parent fields.
18178 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 18179
d521ce57 18180static const gdb_byte *
3019eac3 18181read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 18182 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 18183 int *has_children, int num_extra_attrs)
93311388 18184{
b64f50a1 18185 unsigned int abbrev_number, bytes_read, i;
93311388
DE
18186 struct abbrev_info *abbrev;
18187 struct die_info *die;
18188 struct dwarf2_cu *cu = reader->cu;
18189 bfd *abfd = reader->abfd;
18190
9c541725 18191 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
18192 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18193 info_ptr += bytes_read;
18194 if (!abbrev_number)
18195 {
18196 *diep = NULL;
18197 *has_children = 0;
18198 return info_ptr;
18199 }
18200
685af9cd 18201 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 18202 if (!abbrev)
348e048f
DE
18203 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18204 abbrev_number,
18205 bfd_get_filename (abfd));
18206
3019eac3 18207 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 18208 die->sect_off = sect_off;
93311388
DE
18209 die->tag = abbrev->tag;
18210 die->abbrev = abbrev_number;
18211
3019eac3
DE
18212 /* Make the result usable.
18213 The caller needs to update num_attrs after adding the extra
18214 attributes. */
93311388
DE
18215 die->num_attrs = abbrev->num_attrs;
18216
18217 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
18218 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18219 info_ptr);
93311388
DE
18220
18221 *diep = die;
18222 *has_children = abbrev->has_children;
18223 return info_ptr;
18224}
18225
3019eac3
DE
18226/* Read a die and all its attributes.
18227 Set DIEP to point to a newly allocated die with its information,
18228 except for its child, sibling, and parent fields.
18229 Set HAS_CHILDREN to tell whether the die has children or not. */
18230
d521ce57 18231static const gdb_byte *
3019eac3 18232read_full_die (const struct die_reader_specs *reader,
d521ce57 18233 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
18234 int *has_children)
18235{
d521ce57 18236 const gdb_byte *result;
bf6af496
DE
18237
18238 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18239
b4f54984 18240 if (dwarf_die_debug)
bf6af496
DE
18241 {
18242 fprintf_unfiltered (gdb_stdlog,
18243 "Read die from %s@0x%x of %s:\n",
a32a8923 18244 get_section_name (reader->die_section),
bf6af496
DE
18245 (unsigned) (info_ptr - reader->die_section->buffer),
18246 bfd_get_filename (reader->abfd));
b4f54984 18247 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
18248 }
18249
18250 return result;
3019eac3 18251}
433df2d4
DE
18252\f
18253/* Abbreviation tables.
3019eac3 18254
433df2d4 18255 In DWARF version 2, the description of the debugging information is
c906108c
SS
18256 stored in a separate .debug_abbrev section. Before we read any
18257 dies from a section we read in all abbreviations and install them
433df2d4
DE
18258 in a hash table. */
18259
18260/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18261
685af9cd
TT
18262struct abbrev_info *
18263abbrev_table::alloc_abbrev ()
433df2d4
DE
18264{
18265 struct abbrev_info *abbrev;
18266
685af9cd 18267 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
433df2d4 18268 memset (abbrev, 0, sizeof (struct abbrev_info));
8d749320 18269
433df2d4
DE
18270 return abbrev;
18271}
18272
18273/* Add an abbreviation to the table. */
c906108c 18274
685af9cd
TT
18275void
18276abbrev_table::add_abbrev (unsigned int abbrev_number,
18277 struct abbrev_info *abbrev)
433df2d4
DE
18278{
18279 unsigned int hash_number;
18280
18281 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768
YQ
18282 abbrev->next = m_abbrevs[hash_number];
18283 m_abbrevs[hash_number] = abbrev;
433df2d4 18284}
dee91e82 18285
433df2d4
DE
18286/* Look up an abbrev in the table.
18287 Returns NULL if the abbrev is not found. */
18288
685af9cd
TT
18289struct abbrev_info *
18290abbrev_table::lookup_abbrev (unsigned int abbrev_number)
c906108c 18291{
433df2d4
DE
18292 unsigned int hash_number;
18293 struct abbrev_info *abbrev;
18294
18295 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768 18296 abbrev = m_abbrevs[hash_number];
433df2d4
DE
18297
18298 while (abbrev)
18299 {
18300 if (abbrev->number == abbrev_number)
18301 return abbrev;
18302 abbrev = abbrev->next;
18303 }
18304 return NULL;
18305}
18306
18307/* Read in an abbrev table. */
18308
685af9cd 18309static abbrev_table_up
ed2dc618
SM
18310abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18311 struct dwarf2_section_info *section,
9c541725 18312 sect_offset sect_off)
433df2d4
DE
18313{
18314 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 18315 bfd *abfd = get_section_bfd_owner (section);
d521ce57 18316 const gdb_byte *abbrev_ptr;
c906108c
SS
18317 struct abbrev_info *cur_abbrev;
18318 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 18319 unsigned int abbrev_form;
f3dd6933
DJ
18320 struct attr_abbrev *cur_attrs;
18321 unsigned int allocated_attrs;
c906108c 18322
685af9cd 18323 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
c906108c 18324
433df2d4 18325 dwarf2_read_section (objfile, section);
9c541725 18326 abbrev_ptr = section->buffer + to_underlying (sect_off);
c906108c
SS
18327 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18328 abbrev_ptr += bytes_read;
18329
f3dd6933 18330 allocated_attrs = ATTR_ALLOC_CHUNK;
8d749320 18331 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
6e70227d 18332
0963b4bd 18333 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
18334 while (abbrev_number)
18335 {
685af9cd 18336 cur_abbrev = abbrev_table->alloc_abbrev ();
c906108c
SS
18337
18338 /* read in abbrev header */
18339 cur_abbrev->number = abbrev_number;
aead7601
SM
18340 cur_abbrev->tag
18341 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
c906108c
SS
18342 abbrev_ptr += bytes_read;
18343 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18344 abbrev_ptr += 1;
18345
18346 /* now read in declarations */
22d2f3ab 18347 for (;;)
c906108c 18348 {
43988095
JK
18349 LONGEST implicit_const;
18350
22d2f3ab
JK
18351 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18352 abbrev_ptr += bytes_read;
18353 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18354 abbrev_ptr += bytes_read;
43988095
JK
18355 if (abbrev_form == DW_FORM_implicit_const)
18356 {
18357 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18358 &bytes_read);
18359 abbrev_ptr += bytes_read;
18360 }
18361 else
18362 {
18363 /* Initialize it due to a false compiler warning. */
18364 implicit_const = -1;
18365 }
22d2f3ab
JK
18366
18367 if (abbrev_name == 0)
18368 break;
18369
f3dd6933 18370 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 18371 {
f3dd6933
DJ
18372 allocated_attrs += ATTR_ALLOC_CHUNK;
18373 cur_attrs
224c3ddb 18374 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
c906108c 18375 }
ae038cb0 18376
aead7601
SM
18377 cur_attrs[cur_abbrev->num_attrs].name
18378 = (enum dwarf_attribute) abbrev_name;
22d2f3ab 18379 cur_attrs[cur_abbrev->num_attrs].form
aead7601 18380 = (enum dwarf_form) abbrev_form;
43988095 18381 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
22d2f3ab 18382 ++cur_abbrev->num_attrs;
c906108c
SS
18383 }
18384
8d749320
SM
18385 cur_abbrev->attrs =
18386 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18387 cur_abbrev->num_attrs);
f3dd6933
DJ
18388 memcpy (cur_abbrev->attrs, cur_attrs,
18389 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18390
685af9cd 18391 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
c906108c
SS
18392
18393 /* Get next abbreviation.
18394 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
18395 always properly terminated with an abbrev number of 0.
18396 Exit loop if we encounter an abbreviation which we have
18397 already read (which means we are about to read the abbreviations
18398 for the next compile unit) or if the end of the abbreviation
18399 table is reached. */
433df2d4 18400 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
18401 break;
18402 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18403 abbrev_ptr += bytes_read;
685af9cd 18404 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
c906108c
SS
18405 break;
18406 }
f3dd6933
DJ
18407
18408 xfree (cur_attrs);
433df2d4 18409 return abbrev_table;
c906108c
SS
18410}
18411
72bf9492
DJ
18412/* Returns nonzero if TAG represents a type that we might generate a partial
18413 symbol for. */
18414
18415static int
18416is_type_tag_for_partial (int tag)
18417{
18418 switch (tag)
18419 {
18420#if 0
18421 /* Some types that would be reasonable to generate partial symbols for,
18422 that we don't at present. */
18423 case DW_TAG_array_type:
18424 case DW_TAG_file_type:
18425 case DW_TAG_ptr_to_member_type:
18426 case DW_TAG_set_type:
18427 case DW_TAG_string_type:
18428 case DW_TAG_subroutine_type:
18429#endif
18430 case DW_TAG_base_type:
18431 case DW_TAG_class_type:
680b30c7 18432 case DW_TAG_interface_type:
72bf9492
DJ
18433 case DW_TAG_enumeration_type:
18434 case DW_TAG_structure_type:
18435 case DW_TAG_subrange_type:
18436 case DW_TAG_typedef:
18437 case DW_TAG_union_type:
18438 return 1;
18439 default:
18440 return 0;
18441 }
18442}
18443
18444/* Load all DIEs that are interesting for partial symbols into memory. */
18445
18446static struct partial_die_info *
dee91e82 18447load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18448 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18449{
dee91e82 18450 struct dwarf2_cu *cu = reader->cu;
518817b3 18451 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
72bf9492 18452 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18453 unsigned int bytes_read;
5afb4e99 18454 unsigned int load_all = 0;
72bf9492
DJ
18455 int nesting_level = 1;
18456
18457 parent_die = NULL;
18458 last_die = NULL;
18459
7adf1e79
DE
18460 gdb_assert (cu->per_cu != NULL);
18461 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18462 load_all = 1;
18463
72bf9492
DJ
18464 cu->partial_dies
18465 = htab_create_alloc_ex (cu->header.length / 12,
18466 partial_die_hash,
18467 partial_die_eq,
18468 NULL,
18469 &cu->comp_unit_obstack,
18470 hashtab_obstack_allocate,
18471 dummy_obstack_deallocate);
18472
72bf9492
DJ
18473 while (1)
18474 {
685af9cd 18475 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18476
18477 /* A NULL abbrev means the end of a series of children. */
18478 if (abbrev == NULL)
18479 {
18480 if (--nesting_level == 0)
cd9983dd
YQ
18481 return first_die;
18482
72bf9492
DJ
18483 info_ptr += bytes_read;
18484 last_die = parent_die;
18485 parent_die = parent_die->die_parent;
18486 continue;
18487 }
18488
98bfdba5
PA
18489 /* Check for template arguments. We never save these; if
18490 they're seen, we just mark the parent, and go on our way. */
18491 if (parent_die != NULL
18492 && cu->language == language_cplus
18493 && (abbrev->tag == DW_TAG_template_type_param
18494 || abbrev->tag == DW_TAG_template_value_param))
18495 {
18496 parent_die->has_template_arguments = 1;
18497
18498 if (!load_all)
18499 {
18500 /* We don't need a partial DIE for the template argument. */
dee91e82 18501 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18502 continue;
18503 }
18504 }
18505
0d99eb77 18506 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18507 Skip their other children. */
18508 if (!load_all
18509 && cu->language == language_cplus
18510 && parent_die != NULL
18511 && parent_die->tag == DW_TAG_subprogram)
18512 {
dee91e82 18513 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18514 continue;
18515 }
18516
5afb4e99
DJ
18517 /* Check whether this DIE is interesting enough to save. Normally
18518 we would not be interested in members here, but there may be
18519 later variables referencing them via DW_AT_specification (for
18520 static members). */
18521 if (!load_all
18522 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18523 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18524 && abbrev->tag != DW_TAG_enumerator
18525 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18526 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18527 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18528 && abbrev->tag != DW_TAG_variable
5afb4e99 18529 && abbrev->tag != DW_TAG_namespace
f55ee35c 18530 && abbrev->tag != DW_TAG_module
95554aad 18531 && abbrev->tag != DW_TAG_member
74921315
KS
18532 && abbrev->tag != DW_TAG_imported_unit
18533 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18534 {
18535 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18536 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18537 continue;
18538 }
18539
6f06d47b
YQ
18540 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18541 abbrev);
cd9983dd 18542
48fbe735 18543 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18544
18545 /* This two-pass algorithm for processing partial symbols has a
18546 high cost in cache pressure. Thus, handle some simple cases
18547 here which cover the majority of C partial symbols. DIEs
18548 which neither have specification tags in them, nor could have
18549 specification tags elsewhere pointing at them, can simply be
18550 processed and discarded.
18551
18552 This segment is also optional; scan_partial_symbols and
18553 add_partial_symbol will handle these DIEs if we chain
18554 them in normally. When compilers which do not emit large
18555 quantities of duplicate debug information are more common,
18556 this code can probably be removed. */
18557
18558 /* Any complete simple types at the top level (pretty much all
18559 of them, for a language without namespaces), can be processed
18560 directly. */
18561 if (parent_die == NULL
cd9983dd
YQ
18562 && pdi.has_specification == 0
18563 && pdi.is_declaration == 0
18564 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18565 || pdi.tag == DW_TAG_base_type
18566 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18567 {
cd9983dd 18568 if (building_psymtab && pdi.name != NULL)
67547d89 18569 add_psymbol_to_list (pdi.name, strlen (pdi.name), false,
79748972 18570 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 18571 psymbol_placement::STATIC,
1762568f 18572 0, cu->language, objfile);
cd9983dd 18573 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18574 continue;
18575 }
18576
d8228535
JK
18577 /* The exception for DW_TAG_typedef with has_children above is
18578 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 18579 type_name_or_error will error on such types later.
d8228535
JK
18580
18581 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18582 it could not find the child DIEs referenced later, this is checked
18583 above. In correct DWARF DW_TAG_typedef should have no children. */
18584
cd9983dd 18585 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 18586 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18587 "- DIE at %s [in module %s]"),
cd9983dd 18588 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18589
72bf9492
DJ
18590 /* If we're at the second level, and we're an enumerator, and
18591 our parent has no specification (meaning possibly lives in a
18592 namespace elsewhere), then we can add the partial symbol now
18593 instead of queueing it. */
cd9983dd 18594 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18595 && parent_die != NULL
18596 && parent_die->die_parent == NULL
18597 && parent_die->tag == DW_TAG_enumeration_type
18598 && parent_die->has_specification == 0)
18599 {
cd9983dd 18600 if (pdi.name == NULL)
b98664d3 18601 complaint (_("malformed enumerator DIE ignored"));
72bf9492 18602 else if (building_psymtab)
67547d89 18603 add_psymbol_to_list (pdi.name, strlen (pdi.name), false,
79748972 18604 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 18605 cu->language == language_cplus
75aedd27
TT
18606 ? psymbol_placement::GLOBAL
18607 : psymbol_placement::STATIC,
1762568f 18608 0, cu->language, objfile);
72bf9492 18609
cd9983dd 18610 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18611 continue;
18612 }
18613
cd9983dd 18614 struct partial_die_info *part_die
6f06d47b 18615 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18616
72bf9492
DJ
18617 /* We'll save this DIE so link it in. */
18618 part_die->die_parent = parent_die;
18619 part_die->die_sibling = NULL;
18620 part_die->die_child = NULL;
18621
18622 if (last_die && last_die == parent_die)
18623 last_die->die_child = part_die;
18624 else if (last_die)
18625 last_die->die_sibling = part_die;
18626
18627 last_die = part_die;
18628
18629 if (first_die == NULL)
18630 first_die = part_die;
18631
18632 /* Maybe add the DIE to the hash table. Not all DIEs that we
18633 find interesting need to be in the hash table, because we
18634 also have the parent/sibling/child chains; only those that we
18635 might refer to by offset later during partial symbol reading.
18636
18637 For now this means things that might have be the target of a
18638 DW_AT_specification, DW_AT_abstract_origin, or
18639 DW_AT_extension. DW_AT_extension will refer only to
18640 namespaces; DW_AT_abstract_origin refers to functions (and
18641 many things under the function DIE, but we do not recurse
18642 into function DIEs during partial symbol reading) and
18643 possibly variables as well; DW_AT_specification refers to
18644 declarations. Declarations ought to have the DW_AT_declaration
18645 flag. It happens that GCC forgets to put it in sometimes, but
18646 only for functions, not for types.
18647
18648 Adding more things than necessary to the hash table is harmless
18649 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18650 wasted time in find_partial_die, when we reread the compilation
18651 unit with load_all_dies set. */
72bf9492 18652
5afb4e99 18653 if (load_all
72929c62 18654 || abbrev->tag == DW_TAG_constant
5afb4e99 18655 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18656 || abbrev->tag == DW_TAG_variable
18657 || abbrev->tag == DW_TAG_namespace
18658 || part_die->is_declaration)
18659 {
18660 void **slot;
18661
18662 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18663 to_underlying (part_die->sect_off),
18664 INSERT);
72bf9492
DJ
18665 *slot = part_die;
18666 }
18667
72bf9492 18668 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18669 we have no reason to follow the children of structures; for other
98bfdba5
PA
18670 languages we have to, so that we can get at method physnames
18671 to infer fully qualified class names, for DW_AT_specification,
18672 and for C++ template arguments. For C++, we also look one level
18673 inside functions to find template arguments (if the name of the
18674 function does not already contain the template arguments).
bc30ff58 18675
0a4b0913
AB
18676 For Ada and Fortran, we need to scan the children of subprograms
18677 and lexical blocks as well because these languages allow the
18678 definition of nested entities that could be interesting for the
18679 debugger, such as nested subprograms for instance. */
72bf9492 18680 if (last_die->has_children
5afb4e99
DJ
18681 && (load_all
18682 || last_die->tag == DW_TAG_namespace
f55ee35c 18683 || last_die->tag == DW_TAG_module
72bf9492 18684 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18685 || (cu->language == language_cplus
18686 && last_die->tag == DW_TAG_subprogram
18687 && (last_die->name == NULL
18688 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
18689 || (cu->language != language_c
18690 && (last_die->tag == DW_TAG_class_type
680b30c7 18691 || last_die->tag == DW_TAG_interface_type
72bf9492 18692 || last_die->tag == DW_TAG_structure_type
bc30ff58 18693 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
18694 || ((cu->language == language_ada
18695 || cu->language == language_fortran)
bc30ff58
JB
18696 && (last_die->tag == DW_TAG_subprogram
18697 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18698 {
18699 nesting_level++;
18700 parent_die = last_die;
18701 continue;
18702 }
18703
18704 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18705 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18706
18707 /* Back to the top, do it again. */
18708 }
18709}
18710
6f06d47b
YQ
18711partial_die_info::partial_die_info (sect_offset sect_off_,
18712 struct abbrev_info *abbrev)
18713 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18714{
18715}
18716
35cc7ed7
YQ
18717/* Read a minimal amount of information into the minimal die structure.
18718 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 18719
48fbe735
YQ
18720const gdb_byte *
18721partial_die_info::read (const struct die_reader_specs *reader,
18722 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 18723{
dee91e82 18724 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18725 struct dwarf2_per_objfile *dwarf2_per_objfile
18726 = cu->per_cu->dwarf2_per_objfile;
fa238c03 18727 unsigned int i;
c5aa993b 18728 int has_low_pc_attr = 0;
c906108c 18729 int has_high_pc_attr = 0;
91da1414 18730 int high_pc_relative = 0;
c906108c 18731
fd0a254f 18732 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 18733 {
48fbe735
YQ
18734 struct attribute attr;
18735
fd0a254f 18736 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
c906108c
SS
18737
18738 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18739 partial symbol table. */
c906108c
SS
18740 switch (attr.name)
18741 {
18742 case DW_AT_name:
48fbe735 18743 switch (tag)
71c25dea
TT
18744 {
18745 case DW_TAG_compile_unit:
95554aad 18746 case DW_TAG_partial_unit:
348e048f 18747 case DW_TAG_type_unit:
71c25dea
TT
18748 /* Compilation units have a DW_AT_name that is a filename, not
18749 a source language identifier. */
18750 case DW_TAG_enumeration_type:
18751 case DW_TAG_enumerator:
18752 /* These tags always have simple identifiers already; no need
18753 to canonicalize them. */
48fbe735 18754 name = DW_STRING (&attr);
71c25dea
TT
18755 break;
18756 default:
48fbe735
YQ
18757 {
18758 struct objfile *objfile = dwarf2_per_objfile->objfile;
18759
18760 name
18761 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18762 &objfile->per_bfd->storage_obstack);
18763 }
71c25dea
TT
18764 break;
18765 }
c906108c 18766 break;
31ef98ae 18767 case DW_AT_linkage_name:
c906108c 18768 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18769 /* Note that both forms of linkage name might appear. We
18770 assume they will be the same, and we only store the last
18771 one we see. */
48fbe735 18772 linkage_name = DW_STRING (&attr);
c906108c
SS
18773 break;
18774 case DW_AT_low_pc:
18775 has_low_pc_attr = 1;
48fbe735 18776 lowpc = attr_value_as_address (&attr);
c906108c
SS
18777 break;
18778 case DW_AT_high_pc:
18779 has_high_pc_attr = 1;
48fbe735 18780 highpc = attr_value_as_address (&attr);
31aa7e4e
JB
18781 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18782 high_pc_relative = 1;
c906108c
SS
18783 break;
18784 case DW_AT_location:
0963b4bd 18785 /* Support the .debug_loc offsets. */
8e19ed76
PS
18786 if (attr_form_is_block (&attr))
18787 {
48fbe735 18788 d.locdesc = DW_BLOCK (&attr);
8e19ed76 18789 }
3690dd37 18790 else if (attr_form_is_section_offset (&attr))
8e19ed76 18791 {
4d3c2250 18792 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18793 }
18794 else
18795 {
4d3c2250
KB
18796 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18797 "partial symbol information");
8e19ed76 18798 }
c906108c 18799 break;
c906108c 18800 case DW_AT_external:
48fbe735 18801 is_external = DW_UNSND (&attr);
c906108c
SS
18802 break;
18803 case DW_AT_declaration:
48fbe735 18804 is_declaration = DW_UNSND (&attr);
c906108c
SS
18805 break;
18806 case DW_AT_type:
48fbe735 18807 has_type = 1;
c906108c
SS
18808 break;
18809 case DW_AT_abstract_origin:
18810 case DW_AT_specification:
72bf9492 18811 case DW_AT_extension:
48fbe735
YQ
18812 has_specification = 1;
18813 spec_offset = dwarf2_get_ref_die_offset (&attr);
18814 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 18815 || cu->per_cu->is_dwz);
c906108c
SS
18816 break;
18817 case DW_AT_sibling:
18818 /* Ignore absolute siblings, they might point outside of
18819 the current compile unit. */
18820 if (attr.form == DW_FORM_ref_addr)
b98664d3 18821 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 18822 else
b9502d3f 18823 {
48fbe735 18824 const gdb_byte *buffer = reader->buffer;
9c541725
PA
18825 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18826 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18827
18828 if (sibling_ptr < info_ptr)
b98664d3 18829 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
18830 else if (sibling_ptr > reader->buffer_end)
18831 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f 18832 else
48fbe735 18833 sibling = sibling_ptr;
b9502d3f 18834 }
c906108c 18835 break;
fa4028e9 18836 case DW_AT_byte_size:
48fbe735 18837 has_byte_size = 1;
fa4028e9 18838 break;
ff908ebf 18839 case DW_AT_const_value:
48fbe735 18840 has_const_value = 1;
ff908ebf 18841 break;
68511cec
CES
18842 case DW_AT_calling_convention:
18843 /* DWARF doesn't provide a way to identify a program's source-level
18844 entry point. DW_AT_calling_convention attributes are only meant
18845 to describe functions' calling conventions.
18846
18847 However, because it's a necessary piece of information in
0c1b455e
TT
18848 Fortran, and before DWARF 4 DW_CC_program was the only
18849 piece of debugging information whose definition refers to
18850 a 'main program' at all, several compilers marked Fortran
18851 main programs with DW_CC_program --- even when those
18852 functions use the standard calling conventions.
18853
18854 Although DWARF now specifies a way to provide this
18855 information, we support this practice for backward
18856 compatibility. */
68511cec 18857 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 18858 && cu->language == language_fortran)
48fbe735 18859 main_subprogram = 1;
68511cec 18860 break;
481860b3
GB
18861 case DW_AT_inline:
18862 if (DW_UNSND (&attr) == DW_INL_inlined
18863 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 18864 may_be_inlined = 1;
481860b3 18865 break;
95554aad
TT
18866
18867 case DW_AT_import:
48fbe735 18868 if (tag == DW_TAG_imported_unit)
36586728 18869 {
48fbe735
YQ
18870 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18871 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
18872 || cu->per_cu->is_dwz);
18873 }
95554aad
TT
18874 break;
18875
0c1b455e 18876 case DW_AT_main_subprogram:
48fbe735 18877 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
18878 break;
18879
05caa1d2
TT
18880 case DW_AT_ranges:
18881 {
18882 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18883 but that requires a full DIE, so instead we just
18884 reimplement it. */
18885 int need_ranges_base = tag != DW_TAG_compile_unit;
18886 unsigned int ranges_offset = (DW_UNSND (&attr)
18887 + (need_ranges_base
18888 ? cu->ranges_base
18889 : 0));
18890
18891 /* Value of the DW_AT_ranges attribute is the offset in the
18892 .debug_ranges section. */
18893 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18894 nullptr))
18895 has_pc_info = 1;
18896 }
18897 break;
18898
c906108c
SS
18899 default:
18900 break;
18901 }
18902 }
18903
10d06d82
TT
18904 /* For Ada, if both the name and the linkage name appear, we prefer
18905 the latter. This lets "catch exception" work better, regardless
18906 of the order in which the name and linkage name were emitted.
18907 Really, though, this is just a workaround for the fact that gdb
18908 doesn't store both the name and the linkage name. */
18909 if (cu->language == language_ada && linkage_name != nullptr)
18910 name = linkage_name;
18911
91da1414 18912 if (high_pc_relative)
48fbe735 18913 highpc += lowpc;
91da1414 18914
9373cf26
JK
18915 if (has_low_pc_attr && has_high_pc_attr)
18916 {
18917 /* When using the GNU linker, .gnu.linkonce. sections are used to
18918 eliminate duplicate copies of functions and vtables and such.
18919 The linker will arbitrarily choose one and discard the others.
18920 The AT_*_pc values for such functions refer to local labels in
18921 these sections. If the section from that file was discarded, the
18922 labels are not in the output, so the relocs get a value of 0.
18923 If this is a discarded function, mark the pc bounds as invalid,
18924 so that GDB will ignore it. */
48fbe735 18925 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9373cf26 18926 {
48fbe735 18927 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18928 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 18929
b98664d3 18930 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 18931 "for DIE at %s [in module %s]"),
48fbe735
YQ
18932 paddress (gdbarch, lowpc),
18933 sect_offset_str (sect_off),
9d8780f0 18934 objfile_name (objfile));
9373cf26
JK
18935 }
18936 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 18937 else if (lowpc >= highpc)
9373cf26 18938 {
48fbe735 18939 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18940 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 18941
b98664d3 18942 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 18943 "for DIE at %s [in module %s]"),
48fbe735
YQ
18944 paddress (gdbarch, lowpc),
18945 paddress (gdbarch, highpc),
18946 sect_offset_str (sect_off),
9c541725 18947 objfile_name (objfile));
9373cf26
JK
18948 }
18949 else
48fbe735 18950 has_pc_info = 1;
9373cf26 18951 }
85cbf3d3 18952
c906108c
SS
18953 return info_ptr;
18954}
18955
72bf9492
DJ
18956/* Find a cached partial DIE at OFFSET in CU. */
18957
d590ff25
YQ
18958struct partial_die_info *
18959dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
18960{
18961 struct partial_die_info *lookup_die = NULL;
6f06d47b 18962 struct partial_die_info part_die (sect_off);
72bf9492 18963
9a3c8263 18964 lookup_die = ((struct partial_die_info *)
d590ff25 18965 htab_find_with_hash (partial_dies, &part_die,
9c541725 18966 to_underlying (sect_off)));
72bf9492 18967
72bf9492
DJ
18968 return lookup_die;
18969}
18970
348e048f
DE
18971/* Find a partial DIE at OFFSET, which may or may not be in CU,
18972 except in the case of .debug_types DIEs which do not reference
18973 outside their CU (they do however referencing other types via
55f1336d 18974 DW_FORM_ref_sig8). */
72bf9492 18975
122cf0f2 18976static const struct cu_partial_die_info
9c541725 18977find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18978{
518817b3
SM
18979 struct dwarf2_per_objfile *dwarf2_per_objfile
18980 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18981 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
18982 struct dwarf2_per_cu_data *per_cu = NULL;
18983 struct partial_die_info *pd = NULL;
72bf9492 18984
36586728 18985 if (offset_in_dwz == cu->per_cu->is_dwz
9c541725 18986 && offset_in_cu_p (&cu->header, sect_off))
5afb4e99 18987 {
d590ff25 18988 pd = cu->find_partial_die (sect_off);
5afb4e99 18989 if (pd != NULL)
fb816e8b 18990 return { cu, pd };
0d99eb77
DE
18991 /* We missed recording what we needed.
18992 Load all dies and try again. */
18993 per_cu = cu->per_cu;
5afb4e99 18994 }
0d99eb77
DE
18995 else
18996 {
18997 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18998 if (cu->per_cu->is_debug_types)
0d99eb77 18999 {
9d8780f0
SM
19000 error (_("Dwarf Error: Type Unit at offset %s contains"
19001 " external reference to offset %s [in module %s].\n"),
19002 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
19003 bfd_get_filename (objfile->obfd));
19004 }
9c541725 19005 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 19006 dwarf2_per_objfile);
72bf9492 19007
0d99eb77
DE
19008 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
19009 load_partial_comp_unit (per_cu);
ae038cb0 19010
0d99eb77 19011 per_cu->cu->last_used = 0;
d590ff25 19012 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 19013 }
5afb4e99 19014
dee91e82
DE
19015 /* If we didn't find it, and not all dies have been loaded,
19016 load them all and try again. */
19017
5afb4e99
DJ
19018 if (pd == NULL && per_cu->load_all_dies == 0)
19019 {
5afb4e99 19020 per_cu->load_all_dies = 1;
fd820528
DE
19021
19022 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19023 THIS_CU->cu may already be in use. So we can't just free it and
19024 replace its DIEs with the ones we read in. Instead, we leave those
19025 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19026 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19027 set. */
dee91e82 19028 load_partial_comp_unit (per_cu);
5afb4e99 19029
d590ff25 19030 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
19031 }
19032
19033 if (pd == NULL)
19034 internal_error (__FILE__, __LINE__,
9d8780f0 19035 _("could not find partial DIE %s "
3e43a32a 19036 "in cache [from module %s]\n"),
9d8780f0 19037 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
fb816e8b 19038 return { per_cu->cu, pd };
72bf9492
DJ
19039}
19040
abc72ce4
DE
19041/* See if we can figure out if the class lives in a namespace. We do
19042 this by looking for a member function; its demangled name will
19043 contain namespace info, if there is any. */
19044
19045static void
19046guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19047 struct dwarf2_cu *cu)
19048{
19049 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19050 what template types look like, because the demangler
19051 frequently doesn't give the same name as the debug info. We
19052 could fix this by only using the demangled name to get the
19053 prefix (but see comment in read_structure_type). */
19054
19055 struct partial_die_info *real_pdi;
19056 struct partial_die_info *child_pdi;
19057
19058 /* If this DIE (this DIE's specification, if any) has a parent, then
19059 we should not do this. We'll prepend the parent's fully qualified
19060 name when we create the partial symbol. */
19061
19062 real_pdi = struct_pdi;
19063 while (real_pdi->has_specification)
fb816e8b 19064 {
122cf0f2
AB
19065 auto res = find_partial_die (real_pdi->spec_offset,
19066 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
19067 real_pdi = res.pdi;
19068 cu = res.cu;
19069 }
abc72ce4
DE
19070
19071 if (real_pdi->die_parent != NULL)
19072 return;
19073
19074 for (child_pdi = struct_pdi->die_child;
19075 child_pdi != NULL;
19076 child_pdi = child_pdi->die_sibling)
19077 {
19078 if (child_pdi->tag == DW_TAG_subprogram
19079 && child_pdi->linkage_name != NULL)
19080 {
19081 char *actual_class_name
19082 = language_class_name_from_physname (cu->language_defn,
19083 child_pdi->linkage_name);
19084 if (actual_class_name != NULL)
19085 {
518817b3 19086 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4 19087 struct_pdi->name
021887d8
TT
19088 = obstack_strdup (&objfile->per_bfd->storage_obstack,
19089 actual_class_name);
abc72ce4
DE
19090 xfree (actual_class_name);
19091 }
19092 break;
19093 }
19094 }
19095}
19096
52356b79
YQ
19097void
19098partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 19099{
abc72ce4
DE
19100 /* Once we've fixed up a die, there's no point in doing so again.
19101 This also avoids a memory leak if we were to call
19102 guess_partial_die_structure_name multiple times. */
52356b79 19103 if (fixup_called)
abc72ce4
DE
19104 return;
19105
72bf9492
DJ
19106 /* If we found a reference attribute and the DIE has no name, try
19107 to find a name in the referred to DIE. */
19108
52356b79 19109 if (name == NULL && has_specification)
72bf9492
DJ
19110 {
19111 struct partial_die_info *spec_die;
72bf9492 19112
122cf0f2 19113 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
19114 spec_die = res.pdi;
19115 cu = res.cu;
72bf9492 19116
52356b79 19117 spec_die->fixup (cu);
72bf9492
DJ
19118
19119 if (spec_die->name)
19120 {
52356b79 19121 name = spec_die->name;
72bf9492
DJ
19122
19123 /* Copy DW_AT_external attribute if it is set. */
19124 if (spec_die->is_external)
52356b79 19125 is_external = spec_die->is_external;
72bf9492
DJ
19126 }
19127 }
19128
19129 /* Set default names for some unnamed DIEs. */
72bf9492 19130
52356b79
YQ
19131 if (name == NULL && tag == DW_TAG_namespace)
19132 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 19133
abc72ce4
DE
19134 /* If there is no parent die to provide a namespace, and there are
19135 children, see if we can determine the namespace from their linkage
122d1940 19136 name. */
abc72ce4 19137 if (cu->language == language_cplus
fd5866f6 19138 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
52356b79
YQ
19139 && die_parent == NULL
19140 && has_children
19141 && (tag == DW_TAG_class_type
19142 || tag == DW_TAG_structure_type
19143 || tag == DW_TAG_union_type))
19144 guess_partial_die_structure_name (this, cu);
abc72ce4 19145
53832f31
TT
19146 /* GCC might emit a nameless struct or union that has a linkage
19147 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
19148 if (name == NULL
19149 && (tag == DW_TAG_class_type
19150 || tag == DW_TAG_interface_type
19151 || tag == DW_TAG_structure_type
19152 || tag == DW_TAG_union_type)
19153 && linkage_name != NULL)
53832f31
TT
19154 {
19155 char *demangled;
19156
52356b79 19157 demangled = gdb_demangle (linkage_name, DMGL_TYPES);
53832f31
TT
19158 if (demangled)
19159 {
96408a79
SA
19160 const char *base;
19161
19162 /* Strip any leading namespaces/classes, keep only the base name.
19163 DW_AT_name for named DIEs does not contain the prefixes. */
19164 base = strrchr (demangled, ':');
19165 if (base && base > demangled && base[-1] == ':')
19166 base++;
19167 else
19168 base = demangled;
19169
518817b3 19170 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
021887d8 19171 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
53832f31
TT
19172 xfree (demangled);
19173 }
19174 }
19175
52356b79 19176 fixup_called = 1;
72bf9492
DJ
19177}
19178
a8329558 19179/* Read an attribute value described by an attribute form. */
c906108c 19180
d521ce57 19181static const gdb_byte *
dee91e82
DE
19182read_attribute_value (const struct die_reader_specs *reader,
19183 struct attribute *attr, unsigned form,
43988095 19184 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 19185{
dee91e82 19186 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
19187 struct dwarf2_per_objfile *dwarf2_per_objfile
19188 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 19189 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 19190 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 19191 bfd *abfd = reader->abfd;
e7c27a73 19192 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19193 unsigned int bytes_read;
19194 struct dwarf_block *blk;
19195
aead7601 19196 attr->form = (enum dwarf_form) form;
a8329558 19197 switch (form)
c906108c 19198 {
c906108c 19199 case DW_FORM_ref_addr:
ae411497 19200 if (cu->header.version == 2)
4568ecf9 19201 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 19202 else
4568ecf9
DE
19203 DW_UNSND (attr) = read_offset (abfd, info_ptr,
19204 &cu->header, &bytes_read);
ae411497
TT
19205 info_ptr += bytes_read;
19206 break;
36586728
TT
19207 case DW_FORM_GNU_ref_alt:
19208 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19209 info_ptr += bytes_read;
19210 break;
ae411497 19211 case DW_FORM_addr:
e7c27a73 19212 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 19213 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 19214 info_ptr += bytes_read;
c906108c
SS
19215 break;
19216 case DW_FORM_block2:
7b5a2f43 19217 blk = dwarf_alloc_block (cu);
c906108c
SS
19218 blk->size = read_2_bytes (abfd, info_ptr);
19219 info_ptr += 2;
19220 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19221 info_ptr += blk->size;
19222 DW_BLOCK (attr) = blk;
19223 break;
19224 case DW_FORM_block4:
7b5a2f43 19225 blk = dwarf_alloc_block (cu);
c906108c
SS
19226 blk->size = read_4_bytes (abfd, info_ptr);
19227 info_ptr += 4;
19228 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19229 info_ptr += blk->size;
19230 DW_BLOCK (attr) = blk;
19231 break;
19232 case DW_FORM_data2:
19233 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19234 info_ptr += 2;
19235 break;
19236 case DW_FORM_data4:
19237 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19238 info_ptr += 4;
19239 break;
19240 case DW_FORM_data8:
19241 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19242 info_ptr += 8;
19243 break;
0224619f
JK
19244 case DW_FORM_data16:
19245 blk = dwarf_alloc_block (cu);
19246 blk->size = 16;
19247 blk->data = read_n_bytes (abfd, info_ptr, 16);
19248 info_ptr += 16;
19249 DW_BLOCK (attr) = blk;
19250 break;
2dc7f7b3
TT
19251 case DW_FORM_sec_offset:
19252 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19253 info_ptr += bytes_read;
19254 break;
c906108c 19255 case DW_FORM_string:
9b1c24c8 19256 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 19257 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
19258 info_ptr += bytes_read;
19259 break;
4bdf3d34 19260 case DW_FORM_strp:
36586728
TT
19261 if (!cu->per_cu->is_dwz)
19262 {
ed2dc618
SM
19263 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19264 abfd, info_ptr, cu_header,
36586728
TT
19265 &bytes_read);
19266 DW_STRING_IS_CANONICAL (attr) = 0;
19267 info_ptr += bytes_read;
19268 break;
19269 }
19270 /* FALLTHROUGH */
43988095
JK
19271 case DW_FORM_line_strp:
19272 if (!cu->per_cu->is_dwz)
19273 {
ed2dc618
SM
19274 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19275 abfd, info_ptr,
43988095
JK
19276 cu_header, &bytes_read);
19277 DW_STRING_IS_CANONICAL (attr) = 0;
19278 info_ptr += bytes_read;
19279 break;
19280 }
19281 /* FALLTHROUGH */
36586728
TT
19282 case DW_FORM_GNU_strp_alt:
19283 {
ed2dc618 19284 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
19285 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19286 &bytes_read);
19287
ed2dc618
SM
19288 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19289 dwz, str_offset);
36586728
TT
19290 DW_STRING_IS_CANONICAL (attr) = 0;
19291 info_ptr += bytes_read;
19292 }
4bdf3d34 19293 break;
2dc7f7b3 19294 case DW_FORM_exprloc:
c906108c 19295 case DW_FORM_block:
7b5a2f43 19296 blk = dwarf_alloc_block (cu);
c906108c
SS
19297 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19298 info_ptr += bytes_read;
19299 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19300 info_ptr += blk->size;
19301 DW_BLOCK (attr) = blk;
19302 break;
19303 case DW_FORM_block1:
7b5a2f43 19304 blk = dwarf_alloc_block (cu);
c906108c
SS
19305 blk->size = read_1_byte (abfd, info_ptr);
19306 info_ptr += 1;
19307 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19308 info_ptr += blk->size;
19309 DW_BLOCK (attr) = blk;
19310 break;
19311 case DW_FORM_data1:
19312 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19313 info_ptr += 1;
19314 break;
19315 case DW_FORM_flag:
19316 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19317 info_ptr += 1;
19318 break;
2dc7f7b3
TT
19319 case DW_FORM_flag_present:
19320 DW_UNSND (attr) = 1;
19321 break;
c906108c
SS
19322 case DW_FORM_sdata:
19323 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19324 info_ptr += bytes_read;
19325 break;
19326 case DW_FORM_udata:
19327 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19328 info_ptr += bytes_read;
19329 break;
19330 case DW_FORM_ref1:
9c541725 19331 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19332 + read_1_byte (abfd, info_ptr));
c906108c
SS
19333 info_ptr += 1;
19334 break;
19335 case DW_FORM_ref2:
9c541725 19336 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19337 + read_2_bytes (abfd, info_ptr));
c906108c
SS
19338 info_ptr += 2;
19339 break;
19340 case DW_FORM_ref4:
9c541725 19341 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19342 + read_4_bytes (abfd, info_ptr));
c906108c
SS
19343 info_ptr += 4;
19344 break;
613e1657 19345 case DW_FORM_ref8:
9c541725 19346 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19347 + read_8_bytes (abfd, info_ptr));
613e1657
KB
19348 info_ptr += 8;
19349 break;
55f1336d 19350 case DW_FORM_ref_sig8:
ac9ec31b 19351 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
19352 info_ptr += 8;
19353 break;
c906108c 19354 case DW_FORM_ref_udata:
9c541725 19355 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19356 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19357 info_ptr += bytes_read;
19358 break;
c906108c 19359 case DW_FORM_indirect:
a8329558
KW
19360 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19361 info_ptr += bytes_read;
43988095
JK
19362 if (form == DW_FORM_implicit_const)
19363 {
19364 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19365 info_ptr += bytes_read;
19366 }
19367 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19368 info_ptr);
19369 break;
19370 case DW_FORM_implicit_const:
19371 DW_SND (attr) = implicit_const;
a8329558 19372 break;
336d760d 19373 case DW_FORM_addrx:
3019eac3
DE
19374 case DW_FORM_GNU_addr_index:
19375 if (reader->dwo_file == NULL)
19376 {
19377 /* For now flag a hard error.
19378 Later we can turn this into a complaint. */
19379 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19380 dwarf_form_name (form),
19381 bfd_get_filename (abfd));
19382 }
19383 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19384 info_ptr += bytes_read;
19385 break;
cf532bd1 19386 case DW_FORM_strx:
15f18d14
AT
19387 case DW_FORM_strx1:
19388 case DW_FORM_strx2:
19389 case DW_FORM_strx3:
19390 case DW_FORM_strx4:
3019eac3
DE
19391 case DW_FORM_GNU_str_index:
19392 if (reader->dwo_file == NULL)
19393 {
19394 /* For now flag a hard error.
19395 Later we can turn this into a complaint if warranted. */
19396 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19397 dwarf_form_name (form),
19398 bfd_get_filename (abfd));
19399 }
19400 {
15f18d14
AT
19401 ULONGEST str_index;
19402 if (form == DW_FORM_strx1)
19403 {
19404 str_index = read_1_byte (abfd, info_ptr);
19405 info_ptr += 1;
19406 }
19407 else if (form == DW_FORM_strx2)
19408 {
19409 str_index = read_2_bytes (abfd, info_ptr);
19410 info_ptr += 2;
19411 }
19412 else if (form == DW_FORM_strx3)
19413 {
19414 str_index = read_3_bytes (abfd, info_ptr);
19415 info_ptr += 3;
19416 }
19417 else if (form == DW_FORM_strx4)
19418 {
19419 str_index = read_4_bytes (abfd, info_ptr);
19420 info_ptr += 4;
19421 }
19422 else
19423 {
19424 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19425 info_ptr += bytes_read;
19426 }
342587c4 19427 DW_STRING (attr) = read_str_index (reader, str_index);
3019eac3 19428 DW_STRING_IS_CANONICAL (attr) = 0;
3019eac3
DE
19429 }
19430 break;
c906108c 19431 default:
8a3fe4f8 19432 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19433 dwarf_form_name (form),
19434 bfd_get_filename (abfd));
c906108c 19435 }
28e94949 19436
36586728 19437 /* Super hack. */
7771576e 19438 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
19439 attr->form = DW_FORM_GNU_ref_alt;
19440
28e94949
JB
19441 /* We have seen instances where the compiler tried to emit a byte
19442 size attribute of -1 which ended up being encoded as an unsigned
19443 0xffffffff. Although 0xffffffff is technically a valid size value,
19444 an object of this size seems pretty unlikely so we can relatively
19445 safely treat these cases as if the size attribute was invalid and
19446 treat them as zero by default. */
19447 if (attr->name == DW_AT_byte_size
19448 && form == DW_FORM_data4
19449 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19450 {
19451 complaint
b98664d3 19452 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 19453 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19454 DW_UNSND (attr) = 0;
19455 }
28e94949 19456
c906108c
SS
19457 return info_ptr;
19458}
19459
a8329558
KW
19460/* Read an attribute described by an abbreviated attribute. */
19461
d521ce57 19462static const gdb_byte *
dee91e82
DE
19463read_attribute (const struct die_reader_specs *reader,
19464 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 19465 const gdb_byte *info_ptr)
a8329558
KW
19466{
19467 attr->name = abbrev->name;
43988095
JK
19468 return read_attribute_value (reader, attr, abbrev->form,
19469 abbrev->implicit_const, info_ptr);
a8329558
KW
19470}
19471
0963b4bd 19472/* Read dwarf information from a buffer. */
c906108c
SS
19473
19474static unsigned int
a1855c1d 19475read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19476{
fe1b8b76 19477 return bfd_get_8 (abfd, buf);
c906108c
SS
19478}
19479
19480static int
a1855c1d 19481read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19482{
fe1b8b76 19483 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
19484}
19485
19486static unsigned int
a1855c1d 19487read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19488{
fe1b8b76 19489 return bfd_get_16 (abfd, buf);
c906108c
SS
19490}
19491
21ae7a4d 19492static int
a1855c1d 19493read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19494{
19495 return bfd_get_signed_16 (abfd, buf);
19496}
19497
15f18d14
AT
19498static unsigned int
19499read_3_bytes (bfd *abfd, const gdb_byte *buf)
19500{
19501 unsigned int result = 0;
19502 for (int i = 0; i < 3; ++i)
19503 {
19504 unsigned char byte = bfd_get_8 (abfd, buf);
19505 buf++;
19506 result |= ((unsigned int) byte << (i * 8));
19507 }
19508 return result;
19509}
19510
c906108c 19511static unsigned int
a1855c1d 19512read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19513{
fe1b8b76 19514 return bfd_get_32 (abfd, buf);
c906108c
SS
19515}
19516
21ae7a4d 19517static int
a1855c1d 19518read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19519{
19520 return bfd_get_signed_32 (abfd, buf);
19521}
19522
93311388 19523static ULONGEST
a1855c1d 19524read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19525{
fe1b8b76 19526 return bfd_get_64 (abfd, buf);
c906108c
SS
19527}
19528
19529static CORE_ADDR
d521ce57 19530read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 19531 unsigned int *bytes_read)
c906108c 19532{
e7c27a73 19533 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19534 CORE_ADDR retval = 0;
19535
107d2387 19536 if (cu_header->signed_addr_p)
c906108c 19537 {
107d2387
AC
19538 switch (cu_header->addr_size)
19539 {
19540 case 2:
fe1b8b76 19541 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
19542 break;
19543 case 4:
fe1b8b76 19544 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
19545 break;
19546 case 8:
fe1b8b76 19547 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
19548 break;
19549 default:
8e65ff28 19550 internal_error (__FILE__, __LINE__,
e2e0b3e5 19551 _("read_address: bad switch, signed [in module %s]"),
659b0389 19552 bfd_get_filename (abfd));
107d2387
AC
19553 }
19554 }
19555 else
19556 {
19557 switch (cu_header->addr_size)
19558 {
19559 case 2:
fe1b8b76 19560 retval = bfd_get_16 (abfd, buf);
107d2387
AC
19561 break;
19562 case 4:
fe1b8b76 19563 retval = bfd_get_32 (abfd, buf);
107d2387
AC
19564 break;
19565 case 8:
fe1b8b76 19566 retval = bfd_get_64 (abfd, buf);
107d2387
AC
19567 break;
19568 default:
8e65ff28 19569 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
19570 _("read_address: bad switch, "
19571 "unsigned [in module %s]"),
659b0389 19572 bfd_get_filename (abfd));
107d2387 19573 }
c906108c 19574 }
64367e0a 19575
107d2387
AC
19576 *bytes_read = cu_header->addr_size;
19577 return retval;
c906108c
SS
19578}
19579
f7ef9339 19580/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
19581 specification allows the initial length to take up either 4 bytes
19582 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19583 bytes describe the length and all offsets will be 8 bytes in length
19584 instead of 4.
19585
f7ef9339
KB
19586 An older, non-standard 64-bit format is also handled by this
19587 function. The older format in question stores the initial length
19588 as an 8-byte quantity without an escape value. Lengths greater
19589 than 2^32 aren't very common which means that the initial 4 bytes
19590 is almost always zero. Since a length value of zero doesn't make
19591 sense for the 32-bit format, this initial zero can be considered to
19592 be an escape value which indicates the presence of the older 64-bit
19593 format. As written, the code can't detect (old format) lengths
917c78fc
MK
19594 greater than 4GB. If it becomes necessary to handle lengths
19595 somewhat larger than 4GB, we could allow other small values (such
19596 as the non-sensical values of 1, 2, and 3) to also be used as
19597 escape values indicating the presence of the old format.
f7ef9339 19598
917c78fc
MK
19599 The value returned via bytes_read should be used to increment the
19600 relevant pointer after calling read_initial_length().
c764a876 19601
613e1657
KB
19602 [ Note: read_initial_length() and read_offset() are based on the
19603 document entitled "DWARF Debugging Information Format", revision
f7ef9339 19604 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
19605 from:
19606
f7ef9339 19607 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 19608
613e1657
KB
19609 This document is only a draft and is subject to change. (So beware.)
19610
f7ef9339 19611 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
19612 determined empirically by examining 64-bit ELF files produced by
19613 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
19614
19615 - Kevin, July 16, 2002
613e1657
KB
19616 ] */
19617
19618static LONGEST
d521ce57 19619read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 19620{
fe1b8b76 19621 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 19622
dd373385 19623 if (length == 0xffffffff)
613e1657 19624 {
fe1b8b76 19625 length = bfd_get_64 (abfd, buf + 4);
613e1657 19626 *bytes_read = 12;
613e1657 19627 }
dd373385 19628 else if (length == 0)
f7ef9339 19629 {
dd373385 19630 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 19631 length = bfd_get_64 (abfd, buf);
f7ef9339 19632 *bytes_read = 8;
f7ef9339 19633 }
613e1657
KB
19634 else
19635 {
19636 *bytes_read = 4;
613e1657
KB
19637 }
19638
c764a876
DE
19639 return length;
19640}
dd373385 19641
c764a876
DE
19642/* Cover function for read_initial_length.
19643 Returns the length of the object at BUF, and stores the size of the
19644 initial length in *BYTES_READ and stores the size that offsets will be in
19645 *OFFSET_SIZE.
19646 If the initial length size is not equivalent to that specified in
19647 CU_HEADER then issue a complaint.
19648 This is useful when reading non-comp-unit headers. */
dd373385 19649
c764a876 19650static LONGEST
d521ce57 19651read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
19652 const struct comp_unit_head *cu_header,
19653 unsigned int *bytes_read,
19654 unsigned int *offset_size)
19655{
19656 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19657
19658 gdb_assert (cu_header->initial_length_size == 4
19659 || cu_header->initial_length_size == 8
19660 || cu_header->initial_length_size == 12);
19661
19662 if (cu_header->initial_length_size != *bytes_read)
b98664d3 19663 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 19664
c764a876 19665 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 19666 return length;
613e1657
KB
19667}
19668
19669/* Read an offset from the data stream. The size of the offset is
917c78fc 19670 given by cu_header->offset_size. */
613e1657
KB
19671
19672static LONGEST
d521ce57
TT
19673read_offset (bfd *abfd, const gdb_byte *buf,
19674 const struct comp_unit_head *cu_header,
891d2f0b 19675 unsigned int *bytes_read)
c764a876
DE
19676{
19677 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 19678
c764a876
DE
19679 *bytes_read = cu_header->offset_size;
19680 return offset;
19681}
19682
19683/* Read an offset from the data stream. */
19684
19685static LONGEST
d521ce57 19686read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
19687{
19688 LONGEST retval = 0;
19689
c764a876 19690 switch (offset_size)
613e1657
KB
19691 {
19692 case 4:
fe1b8b76 19693 retval = bfd_get_32 (abfd, buf);
613e1657
KB
19694 break;
19695 case 8:
fe1b8b76 19696 retval = bfd_get_64 (abfd, buf);
613e1657
KB
19697 break;
19698 default:
8e65ff28 19699 internal_error (__FILE__, __LINE__,
c764a876 19700 _("read_offset_1: bad switch [in module %s]"),
659b0389 19701 bfd_get_filename (abfd));
613e1657
KB
19702 }
19703
917c78fc 19704 return retval;
613e1657
KB
19705}
19706
d521ce57
TT
19707static const gdb_byte *
19708read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
19709{
19710 /* If the size of a host char is 8 bits, we can return a pointer
19711 to the buffer, otherwise we have to copy the data to a buffer
19712 allocated on the temporary obstack. */
4bdf3d34 19713 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 19714 return buf;
c906108c
SS
19715}
19716
d521ce57
TT
19717static const char *
19718read_direct_string (bfd *abfd, const gdb_byte *buf,
19719 unsigned int *bytes_read_ptr)
c906108c
SS
19720{
19721 /* If the size of a host char is 8 bits, we can return a pointer
19722 to the string, otherwise we have to copy the string to a buffer
19723 allocated on the temporary obstack. */
4bdf3d34 19724 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
19725 if (*buf == '\0')
19726 {
19727 *bytes_read_ptr = 1;
19728 return NULL;
19729 }
d521ce57
TT
19730 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19731 return (const char *) buf;
4bdf3d34
JJ
19732}
19733
43988095
JK
19734/* Return pointer to string at section SECT offset STR_OFFSET with error
19735 reporting strings FORM_NAME and SECT_NAME. */
19736
d521ce57 19737static const char *
ed2dc618
SM
19738read_indirect_string_at_offset_from (struct objfile *objfile,
19739 bfd *abfd, LONGEST str_offset,
43988095
JK
19740 struct dwarf2_section_info *sect,
19741 const char *form_name,
19742 const char *sect_name)
19743{
ed2dc618 19744 dwarf2_read_section (objfile, sect);
43988095
JK
19745 if (sect->buffer == NULL)
19746 error (_("%s used without %s section [in module %s]"),
19747 form_name, sect_name, bfd_get_filename (abfd));
19748 if (str_offset >= sect->size)
19749 error (_("%s pointing outside of %s section [in module %s]"),
19750 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 19751 gdb_assert (HOST_CHAR_BIT == 8);
43988095 19752 if (sect->buffer[str_offset] == '\0')
4bdf3d34 19753 return NULL;
43988095
JK
19754 return (const char *) (sect->buffer + str_offset);
19755}
19756
19757/* Return pointer to string at .debug_str offset STR_OFFSET. */
19758
19759static const char *
ed2dc618
SM
19760read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19761 bfd *abfd, LONGEST str_offset)
43988095 19762{
ed2dc618
SM
19763 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19764 abfd, str_offset,
43988095
JK
19765 &dwarf2_per_objfile->str,
19766 "DW_FORM_strp", ".debug_str");
19767}
19768
19769/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19770
19771static const char *
ed2dc618
SM
19772read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19773 bfd *abfd, LONGEST str_offset)
43988095 19774{
ed2dc618
SM
19775 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19776 abfd, str_offset,
43988095
JK
19777 &dwarf2_per_objfile->line_str,
19778 "DW_FORM_line_strp",
19779 ".debug_line_str");
c906108c
SS
19780}
19781
36586728
TT
19782/* Read a string at offset STR_OFFSET in the .debug_str section from
19783 the .dwz file DWZ. Throw an error if the offset is too large. If
19784 the string consists of a single NUL byte, return NULL; otherwise
19785 return a pointer to the string. */
19786
d521ce57 19787static const char *
ed2dc618
SM
19788read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19789 LONGEST str_offset)
36586728 19790{
ed2dc618 19791 dwarf2_read_section (objfile, &dwz->str);
36586728
TT
19792
19793 if (dwz->str.buffer == NULL)
19794 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19795 "section [in module %s]"),
00f93c44 19796 bfd_get_filename (dwz->dwz_bfd.get ()));
36586728
TT
19797 if (str_offset >= dwz->str.size)
19798 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19799 ".debug_str section [in module %s]"),
00f93c44 19800 bfd_get_filename (dwz->dwz_bfd.get ()));
36586728
TT
19801 gdb_assert (HOST_CHAR_BIT == 8);
19802 if (dwz->str.buffer[str_offset] == '\0')
19803 return NULL;
d521ce57 19804 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
19805}
19806
43988095
JK
19807/* Return pointer to string at .debug_str offset as read from BUF.
19808 BUF is assumed to be in a compilation unit described by CU_HEADER.
19809 Return *BYTES_READ_PTR count of bytes read from BUF. */
19810
d521ce57 19811static const char *
ed2dc618
SM
19812read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19813 const gdb_byte *buf,
cf2c3c16
TT
19814 const struct comp_unit_head *cu_header,
19815 unsigned int *bytes_read_ptr)
19816{
19817 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19818
ed2dc618 19819 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
cf2c3c16
TT
19820}
19821
43988095
JK
19822/* Return pointer to string at .debug_line_str offset as read from BUF.
19823 BUF is assumed to be in a compilation unit described by CU_HEADER.
19824 Return *BYTES_READ_PTR count of bytes read from BUF. */
19825
19826static const char *
ed2dc618
SM
19827read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19828 bfd *abfd, const gdb_byte *buf,
43988095
JK
19829 const struct comp_unit_head *cu_header,
19830 unsigned int *bytes_read_ptr)
19831{
19832 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19833
ed2dc618
SM
19834 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19835 str_offset);
43988095
JK
19836}
19837
19838ULONGEST
d521ce57 19839read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
43988095 19840 unsigned int *bytes_read_ptr)
c906108c 19841{
12df843f 19842 ULONGEST result;
ce5d95e1 19843 unsigned int num_read;
870f88f7 19844 int shift;
c906108c
SS
19845 unsigned char byte;
19846
19847 result = 0;
19848 shift = 0;
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++;
12df843f 19855 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
19856 if ((byte & 128) == 0)
19857 {
19858 break;
19859 }
19860 shift += 7;
19861 }
19862 *bytes_read_ptr = num_read;
19863 return result;
19864}
19865
12df843f 19866static LONGEST
d521ce57
TT
19867read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19868 unsigned int *bytes_read_ptr)
c906108c 19869{
4dd1b460 19870 ULONGEST result;
870f88f7 19871 int shift, num_read;
c906108c
SS
19872 unsigned char byte;
19873
19874 result = 0;
19875 shift = 0;
c906108c 19876 num_read = 0;
c906108c
SS
19877 while (1)
19878 {
fe1b8b76 19879 byte = bfd_get_8 (abfd, buf);
c906108c
SS
19880 buf++;
19881 num_read++;
4dd1b460 19882 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
19883 shift += 7;
19884 if ((byte & 128) == 0)
19885 {
19886 break;
19887 }
19888 }
77e0b926 19889 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
4dd1b460 19890 result |= -(((ULONGEST) 1) << shift);
c906108c
SS
19891 *bytes_read_ptr = num_read;
19892 return result;
19893}
19894
3019eac3
DE
19895/* Given index ADDR_INDEX in .debug_addr, fetch the value.
19896 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
19897 ADDR_SIZE is the size of addresses from the CU header. */
19898
19899static CORE_ADDR
ed2dc618
SM
19900read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19901 unsigned int addr_index, ULONGEST addr_base, int addr_size)
3019eac3
DE
19902{
19903 struct objfile *objfile = dwarf2_per_objfile->objfile;
19904 bfd *abfd = objfile->obfd;
19905 const gdb_byte *info_ptr;
19906
19907 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
19908 if (dwarf2_per_objfile->addr.buffer == NULL)
19909 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 19910 objfile_name (objfile));
3019eac3
DE
19911 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
19912 error (_("DW_FORM_addr_index pointing outside of "
19913 ".debug_addr section [in module %s]"),
4262abfb 19914 objfile_name (objfile));
3019eac3
DE
19915 info_ptr = (dwarf2_per_objfile->addr.buffer
19916 + addr_base + addr_index * addr_size);
19917 if (addr_size == 4)
19918 return bfd_get_32 (abfd, info_ptr);
19919 else
19920 return bfd_get_64 (abfd, info_ptr);
19921}
19922
19923/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19924
19925static CORE_ADDR
19926read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19927{
518817b3
SM
19928 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19929 cu->addr_base, cu->header.addr_size);
3019eac3
DE
19930}
19931
19932/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19933
19934static CORE_ADDR
d521ce57 19935read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
19936 unsigned int *bytes_read)
19937{
518817b3 19938 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
3019eac3
DE
19939 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19940
19941 return read_addr_index (cu, addr_index);
19942}
19943
19944/* Data structure to pass results from dwarf2_read_addr_index_reader
19945 back to dwarf2_read_addr_index. */
19946
19947struct dwarf2_read_addr_index_data
19948{
19949 ULONGEST addr_base;
19950 int addr_size;
19951};
19952
19953/* die_reader_func for dwarf2_read_addr_index. */
19954
19955static void
19956dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 19957 const gdb_byte *info_ptr,
3019eac3
DE
19958 struct die_info *comp_unit_die,
19959 int has_children,
19960 void *data)
19961{
19962 struct dwarf2_cu *cu = reader->cu;
19963 struct dwarf2_read_addr_index_data *aidata =
19964 (struct dwarf2_read_addr_index_data *) data;
19965
19966 aidata->addr_base = cu->addr_base;
19967 aidata->addr_size = cu->header.addr_size;
19968}
19969
19970/* Given an index in .debug_addr, fetch the value.
19971 NOTE: This can be called during dwarf expression evaluation,
19972 long after the debug information has been read, and thus per_cu->cu
19973 may no longer exist. */
19974
19975CORE_ADDR
19976dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19977 unsigned int addr_index)
19978{
ed2dc618 19979 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3
DE
19980 struct dwarf2_cu *cu = per_cu->cu;
19981 ULONGEST addr_base;
19982 int addr_size;
19983
3019eac3
DE
19984 /* We need addr_base and addr_size.
19985 If we don't have PER_CU->cu, we have to get it.
19986 Nasty, but the alternative is storing the needed info in PER_CU,
19987 which at this point doesn't seem justified: it's not clear how frequently
19988 it would get used and it would increase the size of every PER_CU.
19989 Entry points like dwarf2_per_cu_addr_size do a similar thing
19990 so we're not in uncharted territory here.
19991 Alas we need to be a bit more complicated as addr_base is contained
19992 in the DIE.
19993
19994 We don't need to read the entire CU(/TU).
19995 We just need the header and top level die.
a1b64ce1 19996
3019eac3 19997 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 19998 For now we skip this optimization. */
3019eac3
DE
19999
20000 if (cu != NULL)
20001 {
20002 addr_base = cu->addr_base;
20003 addr_size = cu->header.addr_size;
20004 }
20005 else
20006 {
20007 struct dwarf2_read_addr_index_data aidata;
20008
a1b64ce1
DE
20009 /* Note: We can't use init_cutu_and_read_dies_simple here,
20010 we need addr_base. */
58f0c718 20011 init_cutu_and_read_dies (per_cu, NULL, 0, 0, false,
a1b64ce1 20012 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
20013 addr_base = aidata.addr_base;
20014 addr_size = aidata.addr_size;
20015 }
20016
ed2dc618
SM
20017 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
20018 addr_size);
3019eac3
DE
20019}
20020
cf532bd1 20021/* Given a DW_FORM_GNU_str_index or DW_FORM_strx, fetch the string.
57d63ce2 20022 This is only used by the Fission support. */
3019eac3 20023
d521ce57 20024static const char *
342587c4 20025read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
3019eac3 20026{
ed2dc618 20027 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
20028 struct dwarf2_per_objfile *dwarf2_per_objfile
20029 = cu->per_cu->dwarf2_per_objfile;
3019eac3 20030 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 20031 const char *objf_name = objfile_name (objfile);
3019eac3 20032 bfd *abfd = objfile->obfd;
73869dc2
DE
20033 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
20034 struct dwarf2_section_info *str_offsets_section =
20035 &reader->dwo_file->sections.str_offsets;
d521ce57 20036 const gdb_byte *info_ptr;
3019eac3 20037 ULONGEST str_offset;
cf532bd1 20038 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 20039
73869dc2
DE
20040 dwarf2_read_section (objfile, str_section);
20041 dwarf2_read_section (objfile, str_offsets_section);
20042 if (str_section->buffer == NULL)
57d63ce2 20043 error (_("%s used without .debug_str.dwo section"
9d8780f0
SM
20044 " in CU at offset %s [in module %s]"),
20045 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20046 if (str_offsets_section->buffer == NULL)
57d63ce2 20047 error (_("%s used without .debug_str_offsets.dwo section"
9d8780f0
SM
20048 " in CU at offset %s [in module %s]"),
20049 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20050 if (str_index * cu->header.offset_size >= str_offsets_section->size)
57d63ce2 20051 error (_("%s pointing outside of .debug_str_offsets.dwo"
9d8780f0
SM
20052 " section in CU at offset %s [in module %s]"),
20053 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20054 info_ptr = (str_offsets_section->buffer
3019eac3
DE
20055 + str_index * cu->header.offset_size);
20056 if (cu->header.offset_size == 4)
20057 str_offset = bfd_get_32 (abfd, info_ptr);
20058 else
20059 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 20060 if (str_offset >= str_section->size)
57d63ce2 20061 error (_("Offset from %s pointing outside of"
9d8780f0
SM
20062 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20063 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20064 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
20065}
20066
3019eac3
DE
20067/* Return the length of an LEB128 number in BUF. */
20068
20069static int
20070leb128_size (const gdb_byte *buf)
20071{
20072 const gdb_byte *begin = buf;
20073 gdb_byte byte;
20074
20075 while (1)
20076 {
20077 byte = *buf++;
20078 if ((byte & 128) == 0)
20079 return buf - begin;
20080 }
20081}
20082
c906108c 20083static void
e142c38c 20084set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
20085{
20086 switch (lang)
20087 {
20088 case DW_LANG_C89:
76bee0cc 20089 case DW_LANG_C99:
0cfd832f 20090 case DW_LANG_C11:
c906108c 20091 case DW_LANG_C:
d1be3247 20092 case DW_LANG_UPC:
e142c38c 20093 cu->language = language_c;
c906108c 20094 break;
9c37b5ae 20095 case DW_LANG_Java:
c906108c 20096 case DW_LANG_C_plus_plus:
0cfd832f
MW
20097 case DW_LANG_C_plus_plus_11:
20098 case DW_LANG_C_plus_plus_14:
e142c38c 20099 cu->language = language_cplus;
c906108c 20100 break;
6aecb9c2
JB
20101 case DW_LANG_D:
20102 cu->language = language_d;
20103 break;
c906108c
SS
20104 case DW_LANG_Fortran77:
20105 case DW_LANG_Fortran90:
b21b22e0 20106 case DW_LANG_Fortran95:
f7de9aab
MW
20107 case DW_LANG_Fortran03:
20108 case DW_LANG_Fortran08:
e142c38c 20109 cu->language = language_fortran;
c906108c 20110 break;
a766d390
DE
20111 case DW_LANG_Go:
20112 cu->language = language_go;
20113 break;
c906108c 20114 case DW_LANG_Mips_Assembler:
e142c38c 20115 cu->language = language_asm;
c906108c
SS
20116 break;
20117 case DW_LANG_Ada83:
8aaf0b47 20118 case DW_LANG_Ada95:
bc5f45f8
JB
20119 cu->language = language_ada;
20120 break;
72019c9c
GM
20121 case DW_LANG_Modula2:
20122 cu->language = language_m2;
20123 break;
fe8e67fd
PM
20124 case DW_LANG_Pascal83:
20125 cu->language = language_pascal;
20126 break;
22566fbd
DJ
20127 case DW_LANG_ObjC:
20128 cu->language = language_objc;
20129 break;
c44af4eb
TT
20130 case DW_LANG_Rust:
20131 case DW_LANG_Rust_old:
20132 cu->language = language_rust;
20133 break;
c906108c
SS
20134 case DW_LANG_Cobol74:
20135 case DW_LANG_Cobol85:
c906108c 20136 default:
e142c38c 20137 cu->language = language_minimal;
c906108c
SS
20138 break;
20139 }
e142c38c 20140 cu->language_defn = language_def (cu->language);
c906108c
SS
20141}
20142
20143/* Return the named attribute or NULL if not there. */
20144
20145static struct attribute *
e142c38c 20146dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 20147{
a48e046c 20148 for (;;)
c906108c 20149 {
a48e046c
TT
20150 unsigned int i;
20151 struct attribute *spec = NULL;
20152
20153 for (i = 0; i < die->num_attrs; ++i)
20154 {
20155 if (die->attrs[i].name == name)
20156 return &die->attrs[i];
20157 if (die->attrs[i].name == DW_AT_specification
20158 || die->attrs[i].name == DW_AT_abstract_origin)
20159 spec = &die->attrs[i];
20160 }
20161
20162 if (!spec)
20163 break;
c906108c 20164
f2f0e013 20165 die = follow_die_ref (die, spec, &cu);
f2f0e013 20166 }
c5aa993b 20167
c906108c
SS
20168 return NULL;
20169}
20170
348e048f
DE
20171/* Return the named attribute or NULL if not there,
20172 but do not follow DW_AT_specification, etc.
20173 This is for use in contexts where we're reading .debug_types dies.
20174 Following DW_AT_specification, DW_AT_abstract_origin will take us
20175 back up the chain, and we want to go down. */
20176
20177static struct attribute *
45e58e77 20178dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
20179{
20180 unsigned int i;
20181
20182 for (i = 0; i < die->num_attrs; ++i)
20183 if (die->attrs[i].name == name)
20184 return &die->attrs[i];
20185
20186 return NULL;
20187}
20188
7d45c7c3
KB
20189/* Return the string associated with a string-typed attribute, or NULL if it
20190 is either not found or is of an incorrect type. */
20191
20192static const char *
20193dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20194{
20195 struct attribute *attr;
20196 const char *str = NULL;
20197
20198 attr = dwarf2_attr (die, name, cu);
20199
20200 if (attr != NULL)
20201 {
43988095 20202 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438 20203 || attr->form == DW_FORM_string
cf532bd1 20204 || attr->form == DW_FORM_strx
8fe0f950
AT
20205 || attr->form == DW_FORM_strx1
20206 || attr->form == DW_FORM_strx2
20207 || attr->form == DW_FORM_strx3
20208 || attr->form == DW_FORM_strx4
b3340438 20209 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 20210 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
20211 str = DW_STRING (attr);
20212 else
b98664d3 20213 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
20214 "DIE at %s in module %s"),
20215 dwarf_attr_name (name), sect_offset_str (die->sect_off),
518817b3 20216 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7d45c7c3
KB
20217 }
20218
20219 return str;
20220}
20221
a084a2a6 20222/* Return the dwo name or NULL if not present. If present, it is in either
85102364 20223 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
20224static const char *
20225dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20226{
20227 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20228 if (dwo_name == nullptr)
20229 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20230 return dwo_name;
20231}
20232
05cf31d1
JB
20233/* Return non-zero iff the attribute NAME is defined for the given DIE,
20234 and holds a non-zero value. This function should only be used for
2dc7f7b3 20235 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
20236
20237static int
20238dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20239{
20240 struct attribute *attr = dwarf2_attr (die, name, cu);
20241
20242 return (attr && DW_UNSND (attr));
20243}
20244
3ca72b44 20245static int
e142c38c 20246die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 20247{
05cf31d1
JB
20248 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20249 which value is non-zero. However, we have to be careful with
20250 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20251 (via dwarf2_flag_true_p) follows this attribute. So we may
20252 end up accidently finding a declaration attribute that belongs
20253 to a different DIE referenced by the specification attribute,
20254 even though the given DIE does not have a declaration attribute. */
20255 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20256 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
20257}
20258
63d06c5c 20259/* Return the die giving the specification for DIE, if there is
f2f0e013 20260 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
20261 containing the return value on output. If there is no
20262 specification, but there is an abstract origin, that is
20263 returned. */
63d06c5c
DC
20264
20265static struct die_info *
f2f0e013 20266die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 20267{
f2f0e013
DJ
20268 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20269 *spec_cu);
63d06c5c 20270
edb3359d
DJ
20271 if (spec_attr == NULL)
20272 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20273
63d06c5c
DC
20274 if (spec_attr == NULL)
20275 return NULL;
20276 else
f2f0e013 20277 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 20278}
c906108c 20279
527f3840
JK
20280/* Stub for free_line_header to match void * callback types. */
20281
20282static void
20283free_line_header_voidp (void *arg)
20284{
9a3c8263 20285 struct line_header *lh = (struct line_header *) arg;
527f3840 20286
fff8551c 20287 delete lh;
527f3840
JK
20288}
20289
fff8551c
PA
20290void
20291line_header::add_include_dir (const char *include_dir)
c906108c 20292{
27e0867f 20293 if (dwarf_line_debug >= 2)
7ba99d21
AT
20294 {
20295 size_t new_size;
20296 if (version >= 5)
20297 new_size = m_include_dirs.size ();
20298 else
20299 new_size = m_include_dirs.size () + 1;
20300 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20301 new_size, include_dir);
20302 }
20303 m_include_dirs.push_back (include_dir);
debd256d 20304}
6e70227d 20305
fff8551c
PA
20306void
20307line_header::add_file_name (const char *name,
ecfb656c 20308 dir_index d_index,
fff8551c
PA
20309 unsigned int mod_time,
20310 unsigned int length)
debd256d 20311{
27e0867f 20312 if (dwarf_line_debug >= 2)
7ba99d21
AT
20313 {
20314 size_t new_size;
20315 if (version >= 5)
20316 new_size = file_names_size ();
20317 else
20318 new_size = file_names_size () + 1;
20319 fprintf_unfiltered (gdb_stdlog, "Adding file %zu: %s\n",
20320 new_size, name);
20321 }
20322 m_file_names.emplace_back (name, d_index, mod_time, length);
debd256d 20323}
6e70227d 20324
83769d0b 20325/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
20326
20327static struct dwarf2_section_info *
20328get_debug_line_section (struct dwarf2_cu *cu)
20329{
20330 struct dwarf2_section_info *section;
518817b3
SM
20331 struct dwarf2_per_objfile *dwarf2_per_objfile
20332 = cu->per_cu->dwarf2_per_objfile;
36586728
TT
20333
20334 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20335 DWO file. */
20336 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20337 section = &cu->dwo_unit->dwo_file->sections.line;
20338 else if (cu->per_cu->is_dwz)
20339 {
ed2dc618 20340 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
20341
20342 section = &dwz->line;
20343 }
20344 else
20345 section = &dwarf2_per_objfile->line;
20346
20347 return section;
20348}
20349
43988095
JK
20350/* Read directory or file name entry format, starting with byte of
20351 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20352 entries count and the entries themselves in the described entry
20353 format. */
20354
20355static void
ed2dc618
SM
20356read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20357 bfd *abfd, const gdb_byte **bufp,
43988095
JK
20358 struct line_header *lh,
20359 const struct comp_unit_head *cu_header,
20360 void (*callback) (struct line_header *lh,
20361 const char *name,
ecfb656c 20362 dir_index d_index,
43988095
JK
20363 unsigned int mod_time,
20364 unsigned int length))
20365{
20366 gdb_byte format_count, formati;
20367 ULONGEST data_count, datai;
20368 const gdb_byte *buf = *bufp;
20369 const gdb_byte *format_header_data;
43988095
JK
20370 unsigned int bytes_read;
20371
20372 format_count = read_1_byte (abfd, buf);
20373 buf += 1;
20374 format_header_data = buf;
20375 for (formati = 0; formati < format_count; formati++)
20376 {
20377 read_unsigned_leb128 (abfd, buf, &bytes_read);
20378 buf += bytes_read;
20379 read_unsigned_leb128 (abfd, buf, &bytes_read);
20380 buf += bytes_read;
20381 }
20382
20383 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20384 buf += bytes_read;
20385 for (datai = 0; datai < data_count; datai++)
20386 {
20387 const gdb_byte *format = format_header_data;
20388 struct file_entry fe;
20389
43988095
JK
20390 for (formati = 0; formati < format_count; formati++)
20391 {
ecfb656c 20392 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20393 format += bytes_read;
43988095 20394
ecfb656c 20395 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20396 format += bytes_read;
ecfb656c
PA
20397
20398 gdb::optional<const char *> string;
20399 gdb::optional<unsigned int> uint;
20400
43988095
JK
20401 switch (form)
20402 {
20403 case DW_FORM_string:
ecfb656c 20404 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
20405 buf += bytes_read;
20406 break;
20407
20408 case DW_FORM_line_strp:
ed2dc618
SM
20409 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20410 abfd, buf,
ecfb656c
PA
20411 cu_header,
20412 &bytes_read));
43988095
JK
20413 buf += bytes_read;
20414 break;
20415
20416 case DW_FORM_data1:
ecfb656c 20417 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
20418 buf += 1;
20419 break;
20420
20421 case DW_FORM_data2:
ecfb656c 20422 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
20423 buf += 2;
20424 break;
20425
20426 case DW_FORM_data4:
ecfb656c 20427 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
20428 buf += 4;
20429 break;
20430
20431 case DW_FORM_data8:
ecfb656c 20432 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
20433 buf += 8;
20434 break;
20435
7ba99d21
AT
20436 case DW_FORM_data16:
20437 /* This is used for MD5, but file_entry does not record MD5s. */
20438 buf += 16;
20439 break;
20440
43988095 20441 case DW_FORM_udata:
ecfb656c 20442 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
20443 buf += bytes_read;
20444 break;
20445
20446 case DW_FORM_block:
20447 /* It is valid only for DW_LNCT_timestamp which is ignored by
20448 current GDB. */
20449 break;
20450 }
ecfb656c
PA
20451
20452 switch (content_type)
20453 {
20454 case DW_LNCT_path:
20455 if (string.has_value ())
20456 fe.name = *string;
20457 break;
20458 case DW_LNCT_directory_index:
20459 if (uint.has_value ())
20460 fe.d_index = (dir_index) *uint;
20461 break;
20462 case DW_LNCT_timestamp:
20463 if (uint.has_value ())
20464 fe.mod_time = *uint;
20465 break;
20466 case DW_LNCT_size:
20467 if (uint.has_value ())
20468 fe.length = *uint;
20469 break;
20470 case DW_LNCT_MD5:
20471 break;
20472 default:
b98664d3 20473 complaint (_("Unknown format content type %s"),
ecfb656c
PA
20474 pulongest (content_type));
20475 }
43988095
JK
20476 }
20477
ecfb656c 20478 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
20479 }
20480
20481 *bufp = buf;
20482}
20483
debd256d 20484/* Read the statement program header starting at OFFSET in
3019eac3 20485 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 20486 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
20487 Returns NULL if there is a problem reading the header, e.g., if it
20488 has a version we don't understand.
debd256d
JB
20489
20490 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
20491 the returned object point into the dwarf line section buffer,
20492 and must not be freed. */
ae2de4f8 20493
fff8551c 20494static line_header_up
9c541725 20495dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 20496{
d521ce57 20497 const gdb_byte *line_ptr;
c764a876 20498 unsigned int bytes_read, offset_size;
debd256d 20499 int i;
d521ce57 20500 const char *cur_dir, *cur_file;
3019eac3
DE
20501 struct dwarf2_section_info *section;
20502 bfd *abfd;
518817b3
SM
20503 struct dwarf2_per_objfile *dwarf2_per_objfile
20504 = cu->per_cu->dwarf2_per_objfile;
3019eac3 20505
36586728 20506 section = get_debug_line_section (cu);
3019eac3
DE
20507 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20508 if (section->buffer == NULL)
debd256d 20509 {
3019eac3 20510 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 20511 complaint (_("missing .debug_line.dwo section"));
3019eac3 20512 else
b98664d3 20513 complaint (_("missing .debug_line section"));
debd256d
JB
20514 return 0;
20515 }
20516
fceca515
DE
20517 /* We can't do this until we know the section is non-empty.
20518 Only then do we know we have such a section. */
a32a8923 20519 abfd = get_section_bfd_owner (section);
fceca515 20520
a738430d
MK
20521 /* Make sure that at least there's room for the total_length field.
20522 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 20523 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 20524 {
4d3c2250 20525 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20526 return 0;
20527 }
20528
fff8551c 20529 line_header_up lh (new line_header ());
debd256d 20530
9c541725 20531 lh->sect_off = sect_off;
527f3840
JK
20532 lh->offset_in_dwz = cu->per_cu->is_dwz;
20533
9c541725 20534 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 20535
a738430d 20536 /* Read in the header. */
6e70227d 20537 lh->total_length =
c764a876
DE
20538 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20539 &bytes_read, &offset_size);
debd256d 20540 line_ptr += bytes_read;
7ba99d21
AT
20541
20542 const gdb_byte *start_here = line_ptr;
20543
3019eac3 20544 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 20545 {
4d3c2250 20546 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20547 return 0;
20548 }
7ba99d21 20549 lh->statement_program_end = start_here + lh->total_length;
debd256d
JB
20550 lh->version = read_2_bytes (abfd, line_ptr);
20551 line_ptr += 2;
43988095 20552 if (lh->version > 5)
cd366ee8
DE
20553 {
20554 /* This is a version we don't understand. The format could have
20555 changed in ways we don't handle properly so just punt. */
b98664d3 20556 complaint (_("unsupported version in .debug_line section"));
cd366ee8
DE
20557 return NULL;
20558 }
43988095
JK
20559 if (lh->version >= 5)
20560 {
20561 gdb_byte segment_selector_size;
20562
20563 /* Skip address size. */
20564 read_1_byte (abfd, line_ptr);
20565 line_ptr += 1;
20566
20567 segment_selector_size = read_1_byte (abfd, line_ptr);
20568 line_ptr += 1;
20569 if (segment_selector_size != 0)
20570 {
b98664d3 20571 complaint (_("unsupported segment selector size %u "
43988095
JK
20572 "in .debug_line section"),
20573 segment_selector_size);
20574 return NULL;
20575 }
20576 }
c764a876
DE
20577 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20578 line_ptr += offset_size;
7ba99d21 20579 lh->statement_program_start = line_ptr + lh->header_length;
debd256d
JB
20580 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20581 line_ptr += 1;
2dc7f7b3
TT
20582 if (lh->version >= 4)
20583 {
20584 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20585 line_ptr += 1;
20586 }
20587 else
20588 lh->maximum_ops_per_instruction = 1;
20589
20590 if (lh->maximum_ops_per_instruction == 0)
20591 {
20592 lh->maximum_ops_per_instruction = 1;
b98664d3 20593 complaint (_("invalid maximum_ops_per_instruction "
3e43a32a 20594 "in `.debug_line' section"));
2dc7f7b3
TT
20595 }
20596
debd256d
JB
20597 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20598 line_ptr += 1;
20599 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20600 line_ptr += 1;
20601 lh->line_range = read_1_byte (abfd, line_ptr);
20602 line_ptr += 1;
20603 lh->opcode_base = read_1_byte (abfd, line_ptr);
20604 line_ptr += 1;
fff8551c 20605 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
20606
20607 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20608 for (i = 1; i < lh->opcode_base; ++i)
20609 {
20610 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20611 line_ptr += 1;
20612 }
20613
43988095 20614 if (lh->version >= 5)
debd256d 20615 {
43988095 20616 /* Read directory table. */
ed2dc618
SM
20617 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20618 &cu->header,
b926417a 20619 [] (struct line_header *header, const char *name,
ecfb656c 20620 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20621 unsigned int length)
20622 {
b926417a 20623 header->add_include_dir (name);
fff8551c 20624 });
debd256d 20625
43988095 20626 /* Read file name table. */
ed2dc618
SM
20627 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20628 &cu->header,
b926417a 20629 [] (struct line_header *header, const char *name,
ecfb656c 20630 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20631 unsigned int length)
20632 {
b926417a 20633 header->add_file_name (name, d_index, mod_time, length);
fff8551c 20634 });
43988095
JK
20635 }
20636 else
debd256d 20637 {
43988095
JK
20638 /* Read directory table. */
20639 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20640 {
20641 line_ptr += bytes_read;
fff8551c 20642 lh->add_include_dir (cur_dir);
43988095 20643 }
debd256d
JB
20644 line_ptr += bytes_read;
20645
43988095
JK
20646 /* Read file name table. */
20647 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20648 {
ecfb656c
PA
20649 unsigned int mod_time, length;
20650 dir_index d_index;
43988095
JK
20651
20652 line_ptr += bytes_read;
ecfb656c 20653 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
20654 line_ptr += bytes_read;
20655 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20656 line_ptr += bytes_read;
20657 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20658 line_ptr += bytes_read;
20659
ecfb656c 20660 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
20661 }
20662 line_ptr += bytes_read;
debd256d 20663 }
debd256d 20664
3019eac3 20665 if (line_ptr > (section->buffer + section->size))
b98664d3 20666 complaint (_("line number info header doesn't "
3e43a32a 20667 "fit in `.debug_line' section"));
debd256d 20668
debd256d
JB
20669 return lh;
20670}
c906108c 20671
c6da4cef 20672/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 20673 Return the file name of the psymtab for the given file_entry.
c6da4cef 20674 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
20675 If space for the result is malloc'd, *NAME_HOLDER will be set.
20676 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 20677
d521ce57 20678static const char *
7ba99d21 20679psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
c6da4cef 20680 const struct partial_symtab *pst,
c89b44cd
TT
20681 const char *comp_dir,
20682 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 20683{
d521ce57
TT
20684 const char *include_name = fe.name;
20685 const char *include_name_to_compare = include_name;
72b9f47f 20686 const char *pst_filename;
c6da4cef
DE
20687 int file_is_pst;
20688
8c43009f 20689 const char *dir_name = fe.include_dir (lh);
c6da4cef 20690
c89b44cd 20691 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
20692 if (!IS_ABSOLUTE_PATH (include_name)
20693 && (dir_name != NULL || comp_dir != NULL))
20694 {
20695 /* Avoid creating a duplicate psymtab for PST.
20696 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20697 Before we do the comparison, however, we need to account
20698 for DIR_NAME and COMP_DIR.
20699 First prepend dir_name (if non-NULL). If we still don't
20700 have an absolute path prepend comp_dir (if non-NULL).
20701 However, the directory we record in the include-file's
20702 psymtab does not contain COMP_DIR (to match the
20703 corresponding symtab(s)).
20704
20705 Example:
20706
20707 bash$ cd /tmp
20708 bash$ gcc -g ./hello.c
20709 include_name = "hello.c"
20710 dir_name = "."
20711 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
20712 DW_AT_name = "./hello.c"
20713
20714 */
c6da4cef
DE
20715
20716 if (dir_name != NULL)
20717 {
c89b44cd
TT
20718 name_holder->reset (concat (dir_name, SLASH_STRING,
20719 include_name, (char *) NULL));
20720 include_name = name_holder->get ();
c6da4cef 20721 include_name_to_compare = include_name;
c6da4cef
DE
20722 }
20723 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20724 {
c89b44cd
TT
20725 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20726 include_name, (char *) NULL));
20727 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
20728 }
20729 }
20730
20731 pst_filename = pst->filename;
c89b44cd 20732 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
20733 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20734 {
c89b44cd
TT
20735 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20736 pst_filename, (char *) NULL));
20737 pst_filename = copied_name.get ();
c6da4cef
DE
20738 }
20739
1e3fad37 20740 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 20741
c6da4cef
DE
20742 if (file_is_pst)
20743 return NULL;
20744 return include_name;
20745}
20746
d9b3de22
DE
20747/* State machine to track the state of the line number program. */
20748
6f77053d 20749class lnp_state_machine
d9b3de22 20750{
6f77053d
PA
20751public:
20752 /* Initialize a machine state for the start of a line number
20753 program. */
804d2729
TT
20754 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20755 bool record_lines_p);
6f77053d 20756
8c43009f
PA
20757 file_entry *current_file ()
20758 {
20759 /* lh->file_names is 0-based, but the file name numbers in the
20760 statement program are 1-based. */
6f77053d
PA
20761 return m_line_header->file_name_at (m_file);
20762 }
20763
20764 /* Record the line in the state machine. END_SEQUENCE is true if
20765 we're processing the end of a sequence. */
20766 void record_line (bool end_sequence);
20767
7ab6656f
OJ
20768 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
20769 nop-out rest of the lines in this sequence. */
6f77053d
PA
20770 void check_line_address (struct dwarf2_cu *cu,
20771 const gdb_byte *line_ptr,
7ab6656f 20772 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
20773
20774 void handle_set_discriminator (unsigned int discriminator)
20775 {
20776 m_discriminator = discriminator;
20777 m_line_has_non_zero_discriminator |= discriminator != 0;
20778 }
20779
20780 /* Handle DW_LNE_set_address. */
20781 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20782 {
20783 m_op_index = 0;
20784 address += baseaddr;
20785 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20786 }
20787
20788 /* Handle DW_LNS_advance_pc. */
20789 void handle_advance_pc (CORE_ADDR adjust);
20790
20791 /* Handle a special opcode. */
20792 void handle_special_opcode (unsigned char op_code);
20793
20794 /* Handle DW_LNS_advance_line. */
20795 void handle_advance_line (int line_delta)
20796 {
20797 advance_line (line_delta);
20798 }
20799
20800 /* Handle DW_LNS_set_file. */
20801 void handle_set_file (file_name_index file);
20802
20803 /* Handle DW_LNS_negate_stmt. */
20804 void handle_negate_stmt ()
20805 {
20806 m_is_stmt = !m_is_stmt;
20807 }
20808
20809 /* Handle DW_LNS_const_add_pc. */
20810 void handle_const_add_pc ();
20811
20812 /* Handle DW_LNS_fixed_advance_pc. */
20813 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20814 {
20815 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20816 m_op_index = 0;
20817 }
20818
20819 /* Handle DW_LNS_copy. */
20820 void handle_copy ()
20821 {
20822 record_line (false);
20823 m_discriminator = 0;
20824 }
20825
20826 /* Handle DW_LNE_end_sequence. */
20827 void handle_end_sequence ()
20828 {
804d2729 20829 m_currently_recording_lines = true;
6f77053d
PA
20830 }
20831
20832private:
20833 /* Advance the line by LINE_DELTA. */
20834 void advance_line (int line_delta)
20835 {
20836 m_line += line_delta;
20837
20838 if (line_delta != 0)
20839 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
20840 }
20841
804d2729
TT
20842 struct dwarf2_cu *m_cu;
20843
6f77053d
PA
20844 gdbarch *m_gdbarch;
20845
20846 /* True if we're recording lines.
20847 Otherwise we're building partial symtabs and are just interested in
20848 finding include files mentioned by the line number program. */
20849 bool m_record_lines_p;
20850
8c43009f 20851 /* The line number header. */
6f77053d 20852 line_header *m_line_header;
8c43009f 20853
6f77053d
PA
20854 /* These are part of the standard DWARF line number state machine,
20855 and initialized according to the DWARF spec. */
d9b3de22 20856
6f77053d 20857 unsigned char m_op_index = 0;
7ba99d21
AT
20858 /* The line table index of the current file. */
20859 file_name_index m_file = 1;
6f77053d
PA
20860 unsigned int m_line = 1;
20861
20862 /* These are initialized in the constructor. */
20863
20864 CORE_ADDR m_address;
20865 bool m_is_stmt;
20866 unsigned int m_discriminator;
d9b3de22
DE
20867
20868 /* Additional bits of state we need to track. */
20869
20870 /* The last file that we called dwarf2_start_subfile for.
20871 This is only used for TLLs. */
6f77053d 20872 unsigned int m_last_file = 0;
d9b3de22 20873 /* The last file a line number was recorded for. */
6f77053d 20874 struct subfile *m_last_subfile = NULL;
d9b3de22 20875
804d2729
TT
20876 /* When true, record the lines we decode. */
20877 bool m_currently_recording_lines = false;
d9b3de22
DE
20878
20879 /* The last line number that was recorded, used to coalesce
20880 consecutive entries for the same line. This can happen, for
20881 example, when discriminators are present. PR 17276. */
6f77053d
PA
20882 unsigned int m_last_line = 0;
20883 bool m_line_has_non_zero_discriminator = false;
8c43009f 20884};
d9b3de22 20885
6f77053d
PA
20886void
20887lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20888{
20889 CORE_ADDR addr_adj = (((m_op_index + adjust)
20890 / m_line_header->maximum_ops_per_instruction)
20891 * m_line_header->minimum_instruction_length);
20892 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20893 m_op_index = ((m_op_index + adjust)
20894 % m_line_header->maximum_ops_per_instruction);
20895}
d9b3de22 20896
6f77053d
PA
20897void
20898lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 20899{
6f77053d
PA
20900 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20901 CORE_ADDR addr_adj = (((m_op_index
20902 + (adj_opcode / m_line_header->line_range))
20903 / m_line_header->maximum_ops_per_instruction)
20904 * m_line_header->minimum_instruction_length);
20905 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20906 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20907 % m_line_header->maximum_ops_per_instruction);
d9b3de22 20908
6f77053d
PA
20909 int line_delta = (m_line_header->line_base
20910 + (adj_opcode % m_line_header->line_range));
20911 advance_line (line_delta);
20912 record_line (false);
20913 m_discriminator = 0;
20914}
d9b3de22 20915
6f77053d
PA
20916void
20917lnp_state_machine::handle_set_file (file_name_index file)
20918{
20919 m_file = file;
20920
20921 const file_entry *fe = current_file ();
20922 if (fe == NULL)
20923 dwarf2_debug_line_missing_file_complaint ();
20924 else if (m_record_lines_p)
20925 {
20926 const char *dir = fe->include_dir (m_line_header);
20927
c24bdb02 20928 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20929 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 20930 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
20931 }
20932}
20933
20934void
20935lnp_state_machine::handle_const_add_pc ()
20936{
20937 CORE_ADDR adjust
20938 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20939
20940 CORE_ADDR addr_adj
20941 = (((m_op_index + adjust)
20942 / m_line_header->maximum_ops_per_instruction)
20943 * m_line_header->minimum_instruction_length);
20944
20945 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20946 m_op_index = ((m_op_index + adjust)
20947 % m_line_header->maximum_ops_per_instruction);
20948}
d9b3de22 20949
a05a36a5
DE
20950/* Return non-zero if we should add LINE to the line number table.
20951 LINE is the line to add, LAST_LINE is the last line that was added,
20952 LAST_SUBFILE is the subfile for LAST_LINE.
20953 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20954 had a non-zero discriminator.
20955
20956 We have to be careful in the presence of discriminators.
20957 E.g., for this line:
20958
20959 for (i = 0; i < 100000; i++);
20960
20961 clang can emit four line number entries for that one line,
20962 each with a different discriminator.
20963 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20964
20965 However, we want gdb to coalesce all four entries into one.
20966 Otherwise the user could stepi into the middle of the line and
20967 gdb would get confused about whether the pc really was in the
20968 middle of the line.
20969
20970 Things are further complicated by the fact that two consecutive
20971 line number entries for the same line is a heuristic used by gcc
20972 to denote the end of the prologue. So we can't just discard duplicate
20973 entries, we have to be selective about it. The heuristic we use is
20974 that we only collapse consecutive entries for the same line if at least
20975 one of those entries has a non-zero discriminator. PR 17276.
20976
20977 Note: Addresses in the line number state machine can never go backwards
20978 within one sequence, thus this coalescing is ok. */
20979
20980static int
804d2729
TT
20981dwarf_record_line_p (struct dwarf2_cu *cu,
20982 unsigned int line, unsigned int last_line,
a05a36a5
DE
20983 int line_has_non_zero_discriminator,
20984 struct subfile *last_subfile)
20985{
c24bdb02 20986 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
20987 return 1;
20988 if (line != last_line)
20989 return 1;
20990 /* Same line for the same file that we've seen already.
20991 As a last check, for pr 17276, only record the line if the line
20992 has never had a non-zero discriminator. */
20993 if (!line_has_non_zero_discriminator)
20994 return 1;
20995 return 0;
20996}
20997
804d2729
TT
20998/* Use the CU's builder to record line number LINE beginning at
20999 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
21000
21001static void
d9b3de22
DE
21002dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
21003 unsigned int line, CORE_ADDR address,
804d2729 21004 struct dwarf2_cu *cu)
252a6764
DE
21005{
21006 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21007
27e0867f
DE
21008 if (dwarf_line_debug)
21009 {
21010 fprintf_unfiltered (gdb_stdlog,
21011 "Recording line %u, file %s, address %s\n",
21012 line, lbasename (subfile->name),
21013 paddress (gdbarch, address));
21014 }
21015
804d2729 21016 if (cu != nullptr)
c24bdb02 21017 cu->get_builder ()->record_line (subfile, line, addr);
252a6764
DE
21018}
21019
21020/* Subroutine of dwarf_decode_lines_1 to simplify it.
21021 Mark the end of a set of line number records.
d9b3de22 21022 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
21023 If SUBFILE is NULL the request is ignored. */
21024
21025static void
21026dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 21027 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 21028{
27e0867f
DE
21029 if (subfile == NULL)
21030 return;
21031
21032 if (dwarf_line_debug)
21033 {
21034 fprintf_unfiltered (gdb_stdlog,
21035 "Finishing current line, file %s, address %s\n",
21036 lbasename (subfile->name),
21037 paddress (gdbarch, address));
21038 }
21039
804d2729 21040 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
d9b3de22
DE
21041}
21042
6f77053d
PA
21043void
21044lnp_state_machine::record_line (bool end_sequence)
d9b3de22 21045{
d9b3de22
DE
21046 if (dwarf_line_debug)
21047 {
21048 fprintf_unfiltered (gdb_stdlog,
21049 "Processing actual line %u: file %u,"
21050 " address %s, is_stmt %u, discrim %u\n",
7ba99d21 21051 m_line, m_file,
6f77053d
PA
21052 paddress (m_gdbarch, m_address),
21053 m_is_stmt, m_discriminator);
d9b3de22
DE
21054 }
21055
6f77053d 21056 file_entry *fe = current_file ();
8c43009f
PA
21057
21058 if (fe == NULL)
d9b3de22
DE
21059 dwarf2_debug_line_missing_file_complaint ();
21060 /* For now we ignore lines not starting on an instruction boundary.
21061 But not when processing end_sequence for compatibility with the
21062 previous version of the code. */
6f77053d 21063 else if (m_op_index == 0 || end_sequence)
d9b3de22 21064 {
8c43009f 21065 fe->included_p = 1;
c258c396 21066 if (m_record_lines_p && (producer_is_codewarrior (m_cu) || m_is_stmt))
d9b3de22 21067 {
c24bdb02 21068 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
804d2729 21069 || end_sequence)
d9b3de22 21070 {
804d2729
TT
21071 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21072 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
21073 }
21074
21075 if (!end_sequence)
21076 {
804d2729 21077 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
21078 m_line_has_non_zero_discriminator,
21079 m_last_subfile))
d9b3de22 21080 {
c24bdb02 21081 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 21082 dwarf_record_line_1 (m_gdbarch,
c24bdb02 21083 builder->get_current_subfile (),
6f77053d 21084 m_line, m_address,
804d2729 21085 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 21086 }
c24bdb02 21087 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21088 m_last_line = m_line;
d9b3de22
DE
21089 }
21090 }
21091 }
21092}
21093
804d2729
TT
21094lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21095 line_header *lh, bool record_lines_p)
d9b3de22 21096{
804d2729 21097 m_cu = cu;
6f77053d
PA
21098 m_gdbarch = arch;
21099 m_record_lines_p = record_lines_p;
21100 m_line_header = lh;
d9b3de22 21101
804d2729 21102 m_currently_recording_lines = true;
d9b3de22 21103
d9b3de22
DE
21104 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21105 was a line entry for it so that the backend has a chance to adjust it
21106 and also record it in case it needs it. This is currently used by MIPS
21107 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
21108 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21109 m_is_stmt = lh->default_is_stmt;
21110 m_discriminator = 0;
252a6764
DE
21111}
21112
6f77053d
PA
21113void
21114lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21115 const gdb_byte *line_ptr,
7ab6656f 21116 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 21117{
7ab6656f
OJ
21118 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
21119 the pc range of the CU. However, we restrict the test to only ADDRESS
21120 values of zero to preserve GDB's previous behaviour which is to handle
21121 the specific case of a function being GC'd by the linker. */
924c2928 21122
7ab6656f 21123 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
21124 {
21125 /* This line table is for a function which has been
21126 GCd by the linker. Ignore it. PR gdb/12528 */
21127
518817b3 21128 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
924c2928
DE
21129 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21130
b98664d3 21131 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 21132 line_offset, objfile_name (objfile));
804d2729
TT
21133 m_currently_recording_lines = false;
21134 /* Note: m_currently_recording_lines is left as false until we see
21135 DW_LNE_end_sequence. */
924c2928
DE
21136 }
21137}
21138
f3f5162e 21139/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
21140 Process the line number information in LH.
21141 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21142 program in order to set included_p for every referenced header. */
debd256d 21143
c906108c 21144static void
43f3e411
DE
21145dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21146 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 21147{
d521ce57
TT
21148 const gdb_byte *line_ptr, *extended_end;
21149 const gdb_byte *line_end;
a8c50c1f 21150 unsigned int bytes_read, extended_len;
699ca60a 21151 unsigned char op_code, extended_op;
e142c38c 21152 CORE_ADDR baseaddr;
518817b3 21153 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 21154 bfd *abfd = objfile->obfd;
fbf65064 21155 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
21156 /* True if we're recording line info (as opposed to building partial
21157 symtabs and just interested in finding include files mentioned by
21158 the line number program). */
21159 bool record_lines_p = !decode_for_pst_p;
e142c38c
DJ
21160
21161 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 21162
debd256d
JB
21163 line_ptr = lh->statement_program_start;
21164 line_end = lh->statement_program_end;
c906108c
SS
21165
21166 /* Read the statement sequences until there's nothing left. */
21167 while (line_ptr < line_end)
21168 {
6f77053d
PA
21169 /* The DWARF line number program state machine. Reset the state
21170 machine at the start of each sequence. */
804d2729 21171 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 21172 bool end_sequence = false;
d9b3de22 21173
8c43009f 21174 if (record_lines_p)
c906108c 21175 {
8c43009f
PA
21176 /* Start a subfile for the current file of the state
21177 machine. */
21178 const file_entry *fe = state_machine.current_file ();
21179
21180 if (fe != NULL)
804d2729 21181 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
21182 }
21183
a738430d 21184 /* Decode the table. */
d9b3de22 21185 while (line_ptr < line_end && !end_sequence)
c906108c
SS
21186 {
21187 op_code = read_1_byte (abfd, line_ptr);
21188 line_ptr += 1;
9aa1fe7e 21189
debd256d 21190 if (op_code >= lh->opcode_base)
6e70227d 21191 {
8e07a239 21192 /* Special opcode. */
6f77053d 21193 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
21194 }
21195 else switch (op_code)
c906108c
SS
21196 {
21197 case DW_LNS_extended_op:
3e43a32a
MS
21198 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21199 &bytes_read);
473b7be6 21200 line_ptr += bytes_read;
a8c50c1f 21201 extended_end = line_ptr + extended_len;
c906108c
SS
21202 extended_op = read_1_byte (abfd, line_ptr);
21203 line_ptr += 1;
21204 switch (extended_op)
21205 {
21206 case DW_LNE_end_sequence:
6f77053d
PA
21207 state_machine.handle_end_sequence ();
21208 end_sequence = true;
c906108c
SS
21209 break;
21210 case DW_LNE_set_address:
d9b3de22
DE
21211 {
21212 CORE_ADDR address
21213 = read_address (abfd, line_ptr, cu, &bytes_read);
d9b3de22 21214 line_ptr += bytes_read;
6f77053d
PA
21215
21216 state_machine.check_line_address (cu, line_ptr,
7ab6656f 21217 lowpc - baseaddr, address);
6f77053d 21218 state_machine.handle_set_address (baseaddr, address);
d9b3de22 21219 }
c906108c
SS
21220 break;
21221 case DW_LNE_define_file:
debd256d 21222 {
d521ce57 21223 const char *cur_file;
ecfb656c
PA
21224 unsigned int mod_time, length;
21225 dir_index dindex;
6e70227d 21226
3e43a32a
MS
21227 cur_file = read_direct_string (abfd, line_ptr,
21228 &bytes_read);
debd256d 21229 line_ptr += bytes_read;
ecfb656c 21230 dindex = (dir_index)
debd256d
JB
21231 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21232 line_ptr += bytes_read;
21233 mod_time =
21234 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21235 line_ptr += bytes_read;
21236 length =
21237 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21238 line_ptr += bytes_read;
ecfb656c 21239 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 21240 }
c906108c 21241 break;
d0c6ba3d 21242 case DW_LNE_set_discriminator:
6f77053d
PA
21243 {
21244 /* The discriminator is not interesting to the
21245 debugger; just ignore it. We still need to
21246 check its value though:
21247 if there are consecutive entries for the same
21248 (non-prologue) line we want to coalesce them.
21249 PR 17276. */
21250 unsigned int discr
21251 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21252 line_ptr += bytes_read;
21253
21254 state_machine.handle_set_discriminator (discr);
21255 }
d0c6ba3d 21256 break;
c906108c 21257 default:
b98664d3 21258 complaint (_("mangled .debug_line section"));
debd256d 21259 return;
c906108c 21260 }
a8c50c1f
DJ
21261 /* Make sure that we parsed the extended op correctly. If e.g.
21262 we expected a different address size than the producer used,
21263 we may have read the wrong number of bytes. */
21264 if (line_ptr != extended_end)
21265 {
b98664d3 21266 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
21267 return;
21268 }
c906108c
SS
21269 break;
21270 case DW_LNS_copy:
6f77053d 21271 state_machine.handle_copy ();
c906108c
SS
21272 break;
21273 case DW_LNS_advance_pc:
2dc7f7b3
TT
21274 {
21275 CORE_ADDR adjust
21276 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 21277 line_ptr += bytes_read;
6f77053d
PA
21278
21279 state_machine.handle_advance_pc (adjust);
2dc7f7b3 21280 }
c906108c
SS
21281 break;
21282 case DW_LNS_advance_line:
a05a36a5
DE
21283 {
21284 int line_delta
21285 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 21286 line_ptr += bytes_read;
6f77053d
PA
21287
21288 state_machine.handle_advance_line (line_delta);
a05a36a5 21289 }
c906108c
SS
21290 break;
21291 case DW_LNS_set_file:
d9b3de22 21292 {
6f77053d 21293 file_name_index file
ecfb656c
PA
21294 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21295 &bytes_read);
d9b3de22 21296 line_ptr += bytes_read;
8c43009f 21297
6f77053d 21298 state_machine.handle_set_file (file);
d9b3de22 21299 }
c906108c
SS
21300 break;
21301 case DW_LNS_set_column:
0ad93d4f 21302 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
21303 line_ptr += bytes_read;
21304 break;
21305 case DW_LNS_negate_stmt:
6f77053d 21306 state_machine.handle_negate_stmt ();
c906108c
SS
21307 break;
21308 case DW_LNS_set_basic_block:
c906108c 21309 break;
c2c6d25f
JM
21310 /* Add to the address register of the state machine the
21311 address increment value corresponding to special opcode
a738430d
MK
21312 255. I.e., this value is scaled by the minimum
21313 instruction length since special opcode 255 would have
b021a221 21314 scaled the increment. */
c906108c 21315 case DW_LNS_const_add_pc:
6f77053d 21316 state_machine.handle_const_add_pc ();
c906108c
SS
21317 break;
21318 case DW_LNS_fixed_advance_pc:
3e29f34a 21319 {
6f77053d 21320 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 21321 line_ptr += 2;
6f77053d
PA
21322
21323 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 21324 }
c906108c 21325 break;
9aa1fe7e 21326 default:
a738430d
MK
21327 {
21328 /* Unknown standard opcode, ignore it. */
9aa1fe7e 21329 int i;
a738430d 21330
debd256d 21331 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
21332 {
21333 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21334 line_ptr += bytes_read;
21335 }
21336 }
c906108c
SS
21337 }
21338 }
d9b3de22
DE
21339
21340 if (!end_sequence)
21341 dwarf2_debug_line_missing_end_sequence_complaint ();
21342
21343 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21344 in which case we still finish recording the last line). */
6f77053d 21345 state_machine.record_line (true);
c906108c 21346 }
f3f5162e
DE
21347}
21348
21349/* Decode the Line Number Program (LNP) for the given line_header
21350 structure and CU. The actual information extracted and the type
21351 of structures created from the LNP depends on the value of PST.
21352
21353 1. If PST is NULL, then this procedure uses the data from the program
21354 to create all necessary symbol tables, and their linetables.
21355
21356 2. If PST is not NULL, this procedure reads the program to determine
21357 the list of files included by the unit represented by PST, and
21358 builds all the associated partial symbol tables.
21359
21360 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21361 It is used for relative paths in the line table.
21362 NOTE: When processing partial symtabs (pst != NULL),
21363 comp_dir == pst->dirname.
21364
21365 NOTE: It is important that psymtabs have the same file name (via strcmp)
21366 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21367 symtab we don't use it in the name of the psymtabs we create.
21368 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
21369 A good testcase for this is mb-inline.exp.
21370
527f3840
JK
21371 LOWPC is the lowest address in CU (or 0 if not known).
21372
21373 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21374 for its PC<->lines mapping information. Otherwise only the filename
21375 table is read in. */
f3f5162e
DE
21376
21377static void
21378dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
c3b7b696 21379 struct dwarf2_cu *cu, struct partial_symtab *pst,
527f3840 21380 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 21381{
518817b3 21382 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 21383 const int decode_for_pst_p = (pst != NULL);
f3f5162e 21384
527f3840
JK
21385 if (decode_mapping)
21386 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
21387
21388 if (decode_for_pst_p)
21389 {
aaa75496
JB
21390 /* Now that we're done scanning the Line Header Program, we can
21391 create the psymtab of each included file. */
7ba99d21
AT
21392 for (auto &file_entry : lh->file_names ())
21393 if (file_entry.included_p == 1)
aaa75496 21394 {
c89b44cd 21395 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 21396 const char *include_name =
7ba99d21
AT
21397 psymtab_include_file_name (lh, file_entry, pst,
21398 comp_dir, &name_holder);
c6da4cef 21399 if (include_name != NULL)
aaa75496
JB
21400 dwarf2_create_include_psymtab (include_name, pst, objfile);
21401 }
21402 }
cb1df416
DJ
21403 else
21404 {
21405 /* Make sure a symtab is created for every file, even files
21406 which contain only variables (i.e. no code with associated
21407 line numbers). */
c24bdb02
KS
21408 buildsym_compunit *builder = cu->get_builder ();
21409 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 21410
7ba99d21 21411 for (auto &fe : lh->file_names ())
cb1df416 21412 {
804d2729 21413 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 21414 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 21415 {
c24bdb02 21416 builder->get_current_subfile ()->symtab
804d2729 21417 = allocate_symtab (cust,
c24bdb02 21418 builder->get_current_subfile ()->name);
43f3e411 21419 }
c24bdb02 21420 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
21421 }
21422 }
c906108c
SS
21423}
21424
21425/* Start a subfile for DWARF. FILENAME is the name of the file and
21426 DIRNAME the name of the source directory which contains FILENAME
4d663531 21427 or NULL if not known.
c906108c
SS
21428 This routine tries to keep line numbers from identical absolute and
21429 relative file names in a common subfile.
21430
21431 Using the `list' example from the GDB testsuite, which resides in
21432 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21433 of /srcdir/list0.c yields the following debugging information for list0.c:
21434
c5aa993b 21435 DW_AT_name: /srcdir/list0.c
4d663531 21436 DW_AT_comp_dir: /compdir
357e46e7 21437 files.files[0].name: list0.h
c5aa993b 21438 files.files[0].dir: /srcdir
357e46e7 21439 files.files[1].name: list0.c
c5aa993b 21440 files.files[1].dir: /srcdir
c906108c
SS
21441
21442 The line number information for list0.c has to end up in a single
4f1520fb
FR
21443 subfile, so that `break /srcdir/list0.c:1' works as expected.
21444 start_subfile will ensure that this happens provided that we pass the
21445 concatenation of files.files[1].dir and files.files[1].name as the
21446 subfile's name. */
c906108c
SS
21447
21448static void
804d2729
TT
21449dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21450 const char *dirname)
c906108c 21451{
d521ce57 21452 char *copy = NULL;
4f1520fb 21453
4d663531 21454 /* In order not to lose the line information directory,
4f1520fb
FR
21455 we concatenate it to the filename when it makes sense.
21456 Note that the Dwarf3 standard says (speaking of filenames in line
21457 information): ``The directory index is ignored for file names
21458 that represent full path names''. Thus ignoring dirname in the
21459 `else' branch below isn't an issue. */
c906108c 21460
d5166ae1 21461 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
21462 {
21463 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21464 filename = copy;
21465 }
c906108c 21466
c24bdb02 21467 cu->get_builder ()->start_subfile (filename);
4f1520fb 21468
d521ce57
TT
21469 if (copy != NULL)
21470 xfree (copy);
c906108c
SS
21471}
21472
804d2729
TT
21473/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21474 buildsym_compunit constructor. */
f4dc4d17 21475
c24bdb02
KS
21476struct compunit_symtab *
21477dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21478 CORE_ADDR low_pc)
f4dc4d17 21479{
c24bdb02 21480 gdb_assert (m_builder == nullptr);
43f3e411 21481
c24bdb02
KS
21482 m_builder.reset (new struct buildsym_compunit
21483 (per_cu->dwarf2_per_objfile->objfile,
21484 name, comp_dir, language, low_pc));
93b8bea4 21485
c24bdb02 21486 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 21487
c24bdb02
KS
21488 get_builder ()->record_debugformat ("DWARF 2");
21489 get_builder ()->record_producer (producer);
f4dc4d17 21490
c24bdb02 21491 processing_has_namespace_info = false;
43f3e411 21492
c24bdb02 21493 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
21494}
21495
4c2df51b
DJ
21496static void
21497var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 21498 struct dwarf2_cu *cu)
4c2df51b 21499{
518817b3 21500 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e7c27a73
DJ
21501 struct comp_unit_head *cu_header = &cu->header;
21502
4c2df51b
DJ
21503 /* NOTE drow/2003-01-30: There used to be a comment and some special
21504 code here to turn a symbol with DW_AT_external and a
21505 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21506 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21507 with some versions of binutils) where shared libraries could have
21508 relocations against symbols in their debug information - the
21509 minimal symbol would have the right address, but the debug info
21510 would not. It's no longer necessary, because we will explicitly
21511 apply relocations when we read in the debug information now. */
21512
21513 /* A DW_AT_location attribute with no contents indicates that a
21514 variable has been optimized away. */
21515 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21516 {
f1e6e072 21517 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
21518 return;
21519 }
21520
21521 /* Handle one degenerate form of location expression specially, to
21522 preserve GDB's previous behavior when section offsets are
336d760d
AT
21523 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21524 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
21525
21526 if (attr_form_is_block (attr)
3019eac3
DE
21527 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21528 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
336d760d
AT
21529 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21530 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
3019eac3
DE
21531 && (DW_BLOCK (attr)->size
21532 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 21533 {
891d2f0b 21534 unsigned int dummy;
4c2df51b 21535
3019eac3 21536 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
38583298
TT
21537 SET_SYMBOL_VALUE_ADDRESS (sym,
21538 read_address (objfile->obfd,
21539 DW_BLOCK (attr)->data + 1,
21540 cu, &dummy));
3019eac3 21541 else
38583298
TT
21542 SET_SYMBOL_VALUE_ADDRESS
21543 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
21544 &dummy));
f1e6e072 21545 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b 21546 fixup_symbol_section (sym, objfile);
38583298
TT
21547 SET_SYMBOL_VALUE_ADDRESS (sym,
21548 SYMBOL_VALUE_ADDRESS (sym)
21549 + ANOFFSET (objfile->section_offsets,
21550 SYMBOL_SECTION (sym)));
4c2df51b
DJ
21551 return;
21552 }
21553
21554 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21555 expression evaluator, and use LOC_COMPUTED only when necessary
21556 (i.e. when the value of a register or memory location is
21557 referenced, or a thread-local block, etc.). Then again, it might
21558 not be worthwhile. I'm assuming that it isn't unless performance
21559 or memory numbers show me otherwise. */
21560
f1e6e072 21561 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 21562
f1e6e072 21563 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 21564 cu->has_loclist = true;
4c2df51b
DJ
21565}
21566
c906108c
SS
21567/* Given a pointer to a DWARF information entry, figure out if we need
21568 to make a symbol table entry for it, and if so, create a new entry
21569 and return a pointer to it.
21570 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
21571 used the passed type.
21572 If SPACE is not NULL, use it to hold the new symbol. If it is
21573 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
21574
21575static struct symbol *
5e2db402
TT
21576new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21577 struct symbol *space)
c906108c 21578{
518817b3
SM
21579 struct dwarf2_per_objfile *dwarf2_per_objfile
21580 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21581 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 21582 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 21583 struct symbol *sym = NULL;
15d034d0 21584 const char *name;
c906108c
SS
21585 struct attribute *attr = NULL;
21586 struct attribute *attr2 = NULL;
e142c38c 21587 CORE_ADDR baseaddr;
e37fd15a
SW
21588 struct pending **list_to_add = NULL;
21589
edb3359d 21590 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
21591
21592 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 21593
94af9270 21594 name = dwarf2_name (die, cu);
c906108c
SS
21595 if (name)
21596 {
94af9270 21597 const char *linkagename;
34eaf542 21598 int suppress_add = 0;
94af9270 21599
34eaf542
TT
21600 if (space)
21601 sym = space;
21602 else
e623cf5d 21603 sym = allocate_symbol (objfile);
c906108c 21604 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
21605
21606 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 21607 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
21608 linkagename = dwarf2_physname (name, die, cu);
21609 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 21610
f55ee35c
JK
21611 /* Fortran does not have mangling standard and the mangling does differ
21612 between gfortran, iFort etc. */
21613 if (cu->language == language_fortran
b250c185 21614 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 21615 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 21616 dwarf2_full_name (name, die, cu),
29df156d 21617 NULL);
f55ee35c 21618
c906108c 21619 /* Default assumptions.
c5aa993b 21620 Use the passed type or decode it from the die. */
176620f1 21621 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 21622 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
21623 if (type != NULL)
21624 SYMBOL_TYPE (sym) = type;
21625 else
e7c27a73 21626 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
21627 attr = dwarf2_attr (die,
21628 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21629 cu);
c906108c
SS
21630 if (attr)
21631 {
21632 SYMBOL_LINE (sym) = DW_UNSND (attr);
21633 }
cb1df416 21634
edb3359d
DJ
21635 attr = dwarf2_attr (die,
21636 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21637 cu);
cb1df416
DJ
21638 if (attr)
21639 {
ecfb656c 21640 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 21641 struct file_entry *fe;
9a619af0 21642
ecfb656c
PA
21643 if (cu->line_header != NULL)
21644 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
21645 else
21646 fe = NULL;
21647
21648 if (fe == NULL)
b98664d3 21649 complaint (_("file index out of range"));
8c43009f
PA
21650 else
21651 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
21652 }
21653
c906108c
SS
21654 switch (die->tag)
21655 {
21656 case DW_TAG_label:
e142c38c 21657 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c 21658 if (attr)
3e29f34a
MR
21659 {
21660 CORE_ADDR addr;
21661
21662 addr = attr_value_as_address (attr);
21663 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 21664 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
3e29f34a 21665 }
0f5238ed
TT
21666 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21667 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 21668 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
d3cb6808 21669 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
21670 break;
21671 case DW_TAG_subprogram:
21672 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21673 finish_block. */
f1e6e072 21674 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 21675 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d 21676 if ((attr2 && (DW_UNSND (attr2) != 0))
0a4b0913
AB
21677 || cu->language == language_ada
21678 || cu->language == language_fortran)
c906108c 21679 {
2cfa0c8d 21680 /* Subprograms marked external are stored as a global symbol.
0a4b0913
AB
21681 Ada and Fortran subprograms, whether marked external or
21682 not, are always stored as a global symbol, because we want
21683 to be able to access them globally. For instance, we want
21684 to be able to break on a nested subprogram without having
21685 to specify the context. */
c24bdb02 21686 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
21687 }
21688 else
21689 {
e37fd15a 21690 list_to_add = cu->list_in_scope;
c906108c
SS
21691 }
21692 break;
edb3359d
DJ
21693 case DW_TAG_inlined_subroutine:
21694 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21695 finish_block. */
f1e6e072 21696 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 21697 SYMBOL_INLINED (sym) = 1;
481860b3 21698 list_to_add = cu->list_in_scope;
edb3359d 21699 break;
34eaf542
TT
21700 case DW_TAG_template_value_param:
21701 suppress_add = 1;
21702 /* Fall through. */
72929c62 21703 case DW_TAG_constant:
c906108c 21704 case DW_TAG_variable:
254e6b9e 21705 case DW_TAG_member:
0963b4bd
MS
21706 /* Compilation with minimal debug info may result in
21707 variables with missing type entries. Change the
21708 misleading `void' type to something sensible. */
c906108c 21709 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 21710 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 21711
e142c38c 21712 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
21713 /* In the case of DW_TAG_member, we should only be called for
21714 static const members. */
21715 if (die->tag == DW_TAG_member)
21716 {
3863f96c
DE
21717 /* dwarf2_add_field uses die_is_declaration,
21718 so we do the same. */
254e6b9e
DE
21719 gdb_assert (die_is_declaration (die, cu));
21720 gdb_assert (attr);
21721 }
c906108c
SS
21722 if (attr)
21723 {
e7c27a73 21724 dwarf2_const_value (attr, sym, cu);
e142c38c 21725 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 21726 if (!suppress_add)
34eaf542
TT
21727 {
21728 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 21729 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 21730 else
e37fd15a 21731 list_to_add = cu->list_in_scope;
34eaf542 21732 }
c906108c
SS
21733 break;
21734 }
e142c38c 21735 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
21736 if (attr)
21737 {
e7c27a73 21738 var_decode_location (attr, sym, cu);
e142c38c 21739 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
21740
21741 /* Fortran explicitly imports any global symbols to the local
21742 scope by DW_TAG_common_block. */
21743 if (cu->language == language_fortran && die->parent
21744 && die->parent->tag == DW_TAG_common_block)
21745 attr2 = NULL;
21746
caac4577
JG
21747 if (SYMBOL_CLASS (sym) == LOC_STATIC
21748 && SYMBOL_VALUE_ADDRESS (sym) == 0
21749 && !dwarf2_per_objfile->has_section_at_zero)
21750 {
21751 /* When a static variable is eliminated by the linker,
21752 the corresponding debug information is not stripped
21753 out, but the variable address is set to null;
21754 do not add such variables into symbol table. */
21755 }
21756 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 21757 {
4b610737
TT
21758 if (SYMBOL_CLASS (sym) == LOC_STATIC
21759 && (objfile->flags & OBJF_MAINLINE) == 0
21760 && dwarf2_per_objfile->can_copy)
21761 {
21762 /* A global static variable might be subject to
21763 copy relocation. We first check for a local
21764 minsym, though, because maybe the symbol was
21765 marked hidden, in which case this would not
21766 apply. */
21767 bound_minimal_symbol found
21768 = (lookup_minimal_symbol_linkage
21769 (SYMBOL_LINKAGE_NAME (sym), objfile));
21770 if (found.minsym != nullptr)
21771 sym->maybe_copied = 1;
21772 }
f55ee35c 21773
1c809c68
TT
21774 /* A variable with DW_AT_external is never static,
21775 but it may be block-scoped. */
804d2729 21776 list_to_add
c24bdb02
KS
21777 = ((cu->list_in_scope
21778 == cu->get_builder ()->get_file_symbols ())
21779 ? cu->get_builder ()->get_global_symbols ()
804d2729 21780 : cu->list_in_scope);
1c809c68 21781 }
c906108c 21782 else
e37fd15a 21783 list_to_add = cu->list_in_scope;
c906108c
SS
21784 }
21785 else
21786 {
21787 /* We do not know the address of this symbol.
c5aa993b
JM
21788 If it is an external symbol and we have type information
21789 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21790 The address of the variable will then be determined from
21791 the minimal symbol table whenever the variable is
21792 referenced. */
e142c38c 21793 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
21794
21795 /* Fortran explicitly imports any global symbols to the local
21796 scope by DW_TAG_common_block. */
21797 if (cu->language == language_fortran && die->parent
21798 && die->parent->tag == DW_TAG_common_block)
21799 {
21800 /* SYMBOL_CLASS doesn't matter here because
21801 read_common_block is going to reset it. */
21802 if (!suppress_add)
21803 list_to_add = cu->list_in_scope;
21804 }
21805 else if (attr2 && (DW_UNSND (attr2) != 0)
21806 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 21807 {
0fe7935b
DJ
21808 /* A variable with DW_AT_external is never static, but it
21809 may be block-scoped. */
804d2729 21810 list_to_add
c24bdb02
KS
21811 = ((cu->list_in_scope
21812 == cu->get_builder ()->get_file_symbols ())
21813 ? cu->get_builder ()->get_global_symbols ()
804d2729 21814 : cu->list_in_scope);
0fe7935b 21815
f1e6e072 21816 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 21817 }
442ddf59
JK
21818 else if (!die_is_declaration (die, cu))
21819 {
21820 /* Use the default LOC_OPTIMIZED_OUT class. */
21821 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
21822 if (!suppress_add)
21823 list_to_add = cu->list_in_scope;
442ddf59 21824 }
c906108c
SS
21825 }
21826 break;
21827 case DW_TAG_formal_parameter:
a60f3166
TT
21828 {
21829 /* If we are inside a function, mark this as an argument. If
21830 not, we might be looking at an argument to an inlined function
21831 when we do not have enough information to show inlined frames;
21832 pretend it's a local variable in that case so that the user can
21833 still see it. */
804d2729 21834 struct context_stack *curr
c24bdb02 21835 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
21836 if (curr != nullptr && curr->name != nullptr)
21837 SYMBOL_IS_ARGUMENT (sym) = 1;
21838 attr = dwarf2_attr (die, DW_AT_location, cu);
21839 if (attr)
21840 {
21841 var_decode_location (attr, sym, cu);
21842 }
21843 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21844 if (attr)
21845 {
21846 dwarf2_const_value (attr, sym, cu);
21847 }
f346a30d 21848
a60f3166
TT
21849 list_to_add = cu->list_in_scope;
21850 }
c906108c
SS
21851 break;
21852 case DW_TAG_unspecified_parameters:
21853 /* From varargs functions; gdb doesn't seem to have any
21854 interest in this information, so just ignore it for now.
21855 (FIXME?) */
21856 break;
34eaf542
TT
21857 case DW_TAG_template_type_param:
21858 suppress_add = 1;
21859 /* Fall through. */
c906108c 21860 case DW_TAG_class_type:
680b30c7 21861 case DW_TAG_interface_type:
c906108c
SS
21862 case DW_TAG_structure_type:
21863 case DW_TAG_union_type:
72019c9c 21864 case DW_TAG_set_type:
c906108c 21865 case DW_TAG_enumeration_type:
f1e6e072 21866 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21867 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 21868
63d06c5c 21869 {
9c37b5ae 21870 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
21871 really ever be static objects: otherwise, if you try
21872 to, say, break of a class's method and you're in a file
21873 which doesn't mention that class, it won't work unless
21874 the check for all static symbols in lookup_symbol_aux
21875 saves you. See the OtherFileClass tests in
21876 gdb.c++/namespace.exp. */
21877
e37fd15a 21878 if (!suppress_add)
34eaf542 21879 {
c24bdb02 21880 buildsym_compunit *builder = cu->get_builder ();
804d2729 21881 list_to_add
c24bdb02 21882 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 21883 && cu->language == language_cplus
c24bdb02 21884 ? builder->get_global_symbols ()
804d2729 21885 : cu->list_in_scope);
63d06c5c 21886
64382290 21887 /* The semantics of C++ state that "struct foo {
9c37b5ae 21888 ... }" also defines a typedef for "foo". */
64382290 21889 if (cu->language == language_cplus
45280282 21890 || cu->language == language_ada
c44af4eb
TT
21891 || cu->language == language_d
21892 || cu->language == language_rust)
64382290
TT
21893 {
21894 /* The symbol's name is already allocated along
21895 with this objfile, so we don't need to
21896 duplicate it for the type. */
21897 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21898 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21899 }
63d06c5c
DC
21900 }
21901 }
c906108c
SS
21902 break;
21903 case DW_TAG_typedef:
f1e6e072 21904 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 21905 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21906 list_to_add = cu->list_in_scope;
63d06c5c 21907 break;
c906108c 21908 case DW_TAG_base_type:
a02abb62 21909 case DW_TAG_subrange_type:
f1e6e072 21910 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21911 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21912 list_to_add = cu->list_in_scope;
c906108c
SS
21913 break;
21914 case DW_TAG_enumerator:
e142c38c 21915 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
21916 if (attr)
21917 {
e7c27a73 21918 dwarf2_const_value (attr, sym, cu);
c906108c 21919 }
63d06c5c
DC
21920 {
21921 /* NOTE: carlton/2003-11-10: See comment above in the
21922 DW_TAG_class_type, etc. block. */
21923
804d2729 21924 list_to_add
c24bdb02 21925 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 21926 && cu->language == language_cplus
c24bdb02 21927 ? cu->get_builder ()->get_global_symbols ()
804d2729 21928 : cu->list_in_scope);
63d06c5c 21929 }
c906108c 21930 break;
74921315 21931 case DW_TAG_imported_declaration:
5c4e30ca 21932 case DW_TAG_namespace:
f1e6e072 21933 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 21934 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 21935 break;
530e8392
KB
21936 case DW_TAG_module:
21937 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21938 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 21939 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 21940 break;
4357ac6c 21941 case DW_TAG_common_block:
f1e6e072 21942 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 21943 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 21944 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 21945 break;
c906108c
SS
21946 default:
21947 /* Not a tag we recognize. Hopefully we aren't processing
21948 trash data, but since we must specifically ignore things
21949 we don't recognize, there is nothing else we should do at
0963b4bd 21950 this point. */
b98664d3 21951 complaint (_("unsupported tag: '%s'"),
4d3c2250 21952 dwarf_tag_name (die->tag));
c906108c
SS
21953 break;
21954 }
df8a16a1 21955
e37fd15a
SW
21956 if (suppress_add)
21957 {
21958 sym->hash_next = objfile->template_symbols;
21959 objfile->template_symbols = sym;
21960 list_to_add = NULL;
21961 }
21962
21963 if (list_to_add != NULL)
d3cb6808 21964 add_symbol_to_list (sym, list_to_add);
e37fd15a 21965
df8a16a1
DJ
21966 /* For the benefit of old versions of GCC, check for anonymous
21967 namespaces based on the demangled name. */
4d4ec4e5 21968 if (!cu->processing_has_namespace_info
94af9270 21969 && cu->language == language_cplus)
c24bdb02 21970 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
21971 }
21972 return (sym);
21973}
21974
98bfdba5
PA
21975/* Given an attr with a DW_FORM_dataN value in host byte order,
21976 zero-extend it as appropriate for the symbol's type. The DWARF
21977 standard (v4) is not entirely clear about the meaning of using
21978 DW_FORM_dataN for a constant with a signed type, where the type is
21979 wider than the data. The conclusion of a discussion on the DWARF
21980 list was that this is unspecified. We choose to always zero-extend
21981 because that is the interpretation long in use by GCC. */
c906108c 21982
98bfdba5 21983static gdb_byte *
ff39bb5e 21984dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 21985 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 21986{
518817b3 21987 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e17a4113
UW
21988 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21989 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
21990 LONGEST l = DW_UNSND (attr);
21991
21992 if (bits < sizeof (*value) * 8)
21993 {
21994 l &= ((LONGEST) 1 << bits) - 1;
21995 *value = l;
21996 }
21997 else if (bits == sizeof (*value) * 8)
21998 *value = l;
21999 else
22000 {
224c3ddb 22001 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
22002 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22003 return bytes;
22004 }
22005
22006 return NULL;
22007}
22008
22009/* Read a constant value from an attribute. Either set *VALUE, or if
22010 the value does not fit in *VALUE, set *BYTES - either already
22011 allocated on the objfile obstack, or newly allocated on OBSTACK,
22012 or, set *BATON, if we translated the constant to a location
22013 expression. */
22014
22015static void
ff39bb5e 22016dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
22017 const char *name, struct obstack *obstack,
22018 struct dwarf2_cu *cu,
d521ce57 22019 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
22020 struct dwarf2_locexpr_baton **baton)
22021{
518817b3 22022 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
98bfdba5 22023 struct comp_unit_head *cu_header = &cu->header;
c906108c 22024 struct dwarf_block *blk;
98bfdba5
PA
22025 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22026 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22027
22028 *value = 0;
22029 *bytes = NULL;
22030 *baton = NULL;
c906108c
SS
22031
22032 switch (attr->form)
22033 {
22034 case DW_FORM_addr:
336d760d 22035 case DW_FORM_addrx:
3019eac3 22036 case DW_FORM_GNU_addr_index:
ac56253d 22037 {
ac56253d
TT
22038 gdb_byte *data;
22039
98bfdba5
PA
22040 if (TYPE_LENGTH (type) != cu_header->addr_size)
22041 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 22042 cu_header->addr_size,
98bfdba5 22043 TYPE_LENGTH (type));
ac56253d
TT
22044 /* Symbols of this form are reasonably rare, so we just
22045 piggyback on the existing location code rather than writing
22046 a new implementation of symbol_computed_ops. */
8d749320 22047 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
22048 (*baton)->per_cu = cu->per_cu;
22049 gdb_assert ((*baton)->per_cu);
ac56253d 22050
98bfdba5 22051 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 22052 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 22053 (*baton)->data = data;
ac56253d
TT
22054
22055 data[0] = DW_OP_addr;
22056 store_unsigned_integer (&data[1], cu_header->addr_size,
22057 byte_order, DW_ADDR (attr));
22058 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 22059 }
c906108c 22060 break;
4ac36638 22061 case DW_FORM_string:
93b5768b 22062 case DW_FORM_strp:
cf532bd1 22063 case DW_FORM_strx:
3019eac3 22064 case DW_FORM_GNU_str_index:
36586728 22065 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
22066 /* DW_STRING is already allocated on the objfile obstack, point
22067 directly to it. */
d521ce57 22068 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 22069 break;
c906108c
SS
22070 case DW_FORM_block1:
22071 case DW_FORM_block2:
22072 case DW_FORM_block4:
22073 case DW_FORM_block:
2dc7f7b3 22074 case DW_FORM_exprloc:
0224619f 22075 case DW_FORM_data16:
c906108c 22076 blk = DW_BLOCK (attr);
98bfdba5
PA
22077 if (TYPE_LENGTH (type) != blk->size)
22078 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22079 TYPE_LENGTH (type));
22080 *bytes = blk->data;
c906108c 22081 break;
2df3850c
JM
22082
22083 /* The DW_AT_const_value attributes are supposed to carry the
22084 symbol's value "represented as it would be on the target
22085 architecture." By the time we get here, it's already been
22086 converted to host endianness, so we just need to sign- or
22087 zero-extend it as appropriate. */
22088 case DW_FORM_data1:
3aef2284 22089 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 22090 break;
c906108c 22091 case DW_FORM_data2:
3aef2284 22092 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 22093 break;
c906108c 22094 case DW_FORM_data4:
3aef2284 22095 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 22096 break;
c906108c 22097 case DW_FORM_data8:
3aef2284 22098 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
22099 break;
22100
c906108c 22101 case DW_FORM_sdata:
663c44ac 22102 case DW_FORM_implicit_const:
98bfdba5 22103 *value = DW_SND (attr);
2df3850c
JM
22104 break;
22105
c906108c 22106 case DW_FORM_udata:
98bfdba5 22107 *value = DW_UNSND (attr);
c906108c 22108 break;
2df3850c 22109
c906108c 22110 default:
b98664d3 22111 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 22112 dwarf_form_name (attr->form));
98bfdba5 22113 *value = 0;
c906108c
SS
22114 break;
22115 }
22116}
22117
2df3850c 22118
98bfdba5
PA
22119/* Copy constant value from an attribute to a symbol. */
22120
2df3850c 22121static void
ff39bb5e 22122dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 22123 struct dwarf2_cu *cu)
2df3850c 22124{
518817b3 22125 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12df843f 22126 LONGEST value;
d521ce57 22127 const gdb_byte *bytes;
98bfdba5 22128 struct dwarf2_locexpr_baton *baton;
2df3850c 22129
98bfdba5
PA
22130 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22131 SYMBOL_PRINT_NAME (sym),
22132 &objfile->objfile_obstack, cu,
22133 &value, &bytes, &baton);
2df3850c 22134
98bfdba5
PA
22135 if (baton != NULL)
22136 {
98bfdba5 22137 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 22138 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
22139 }
22140 else if (bytes != NULL)
22141 {
22142 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 22143 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
22144 }
22145 else
22146 {
22147 SYMBOL_VALUE (sym) = value;
f1e6e072 22148 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 22149 }
2df3850c
JM
22150}
22151
c906108c
SS
22152/* Return the type of the die in question using its DW_AT_type attribute. */
22153
22154static struct type *
e7c27a73 22155die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22156{
c906108c 22157 struct attribute *type_attr;
c906108c 22158
e142c38c 22159 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
22160 if (!type_attr)
22161 {
518817b3 22162 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 22163 /* A missing DW_AT_type represents a void type. */
518817b3 22164 return objfile_type (objfile)->builtin_void;
c906108c 22165 }
348e048f 22166
673bfd45 22167 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22168}
22169
b4ba55a1
JB
22170/* True iff CU's producer generates GNAT Ada auxiliary information
22171 that allows to find parallel types through that information instead
22172 of having to do expensive parallel lookups by type name. */
22173
22174static int
22175need_gnat_info (struct dwarf2_cu *cu)
22176{
de4cb04a
JB
22177 /* Assume that the Ada compiler was GNAT, which always produces
22178 the auxiliary information. */
22179 return (cu->language == language_ada);
b4ba55a1
JB
22180}
22181
b4ba55a1
JB
22182/* Return the auxiliary type of the die in question using its
22183 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22184 attribute is not present. */
22185
22186static struct type *
22187die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22188{
b4ba55a1 22189 struct attribute *type_attr;
b4ba55a1
JB
22190
22191 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22192 if (!type_attr)
22193 return NULL;
22194
673bfd45 22195 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
22196}
22197
22198/* If DIE has a descriptive_type attribute, then set the TYPE's
22199 descriptive type accordingly. */
22200
22201static void
22202set_descriptive_type (struct type *type, struct die_info *die,
22203 struct dwarf2_cu *cu)
22204{
22205 struct type *descriptive_type = die_descriptive_type (die, cu);
22206
22207 if (descriptive_type)
22208 {
22209 ALLOCATE_GNAT_AUX_TYPE (type);
22210 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22211 }
22212}
22213
c906108c
SS
22214/* Return the containing type of the die in question using its
22215 DW_AT_containing_type attribute. */
22216
22217static struct type *
e7c27a73 22218die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22219{
c906108c 22220 struct attribute *type_attr;
518817b3 22221 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 22222
e142c38c 22223 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
22224 if (!type_attr)
22225 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 22226 "[in module %s]"), objfile_name (objfile));
33ac96f0 22227
673bfd45 22228 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22229}
22230
ac9ec31b
DE
22231/* Return an error marker type to use for the ill formed type in DIE/CU. */
22232
22233static struct type *
22234build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22235{
518817b3
SM
22236 struct dwarf2_per_objfile *dwarf2_per_objfile
22237 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b 22238 struct objfile *objfile = dwarf2_per_objfile->objfile;
528e1572 22239 char *saved;
ac9ec31b 22240
528e1572
SM
22241 std::string message
22242 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22243 objfile_name (objfile),
22244 sect_offset_str (cu->header.sect_off),
22245 sect_offset_str (die->sect_off));
efba19b0 22246 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 22247
19f392bc 22248 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
22249}
22250
673bfd45 22251/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
22252 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22253 DW_AT_containing_type.
673bfd45
DE
22254 If there is no type substitute an error marker. */
22255
c906108c 22256static struct type *
ff39bb5e 22257lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 22258 struct dwarf2_cu *cu)
c906108c 22259{
518817b3
SM
22260 struct dwarf2_per_objfile *dwarf2_per_objfile
22261 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 22262 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
22263 struct type *this_type;
22264
ac9ec31b
DE
22265 gdb_assert (attr->name == DW_AT_type
22266 || attr->name == DW_AT_GNAT_descriptive_type
22267 || attr->name == DW_AT_containing_type);
22268
673bfd45
DE
22269 /* First see if we have it cached. */
22270
36586728
TT
22271 if (attr->form == DW_FORM_GNU_ref_alt)
22272 {
22273 struct dwarf2_per_cu_data *per_cu;
9c541725 22274 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 22275
ed2dc618
SM
22276 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22277 dwarf2_per_objfile);
9c541725 22278 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 22279 }
7771576e 22280 else if (attr_form_is_ref (attr))
673bfd45 22281 {
9c541725 22282 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 22283
9c541725 22284 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 22285 }
55f1336d 22286 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 22287 {
ac9ec31b 22288 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 22289
ac9ec31b 22290 return get_signatured_type (die, signature, cu);
673bfd45
DE
22291 }
22292 else
22293 {
b98664d3 22294 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
22295 " at %s [in module %s]"),
22296 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 22297 objfile_name (objfile));
ac9ec31b 22298 return build_error_marker_type (cu, die);
673bfd45
DE
22299 }
22300
22301 /* If not cached we need to read it in. */
22302
22303 if (this_type == NULL)
22304 {
ac9ec31b 22305 struct die_info *type_die = NULL;
673bfd45
DE
22306 struct dwarf2_cu *type_cu = cu;
22307
7771576e 22308 if (attr_form_is_ref (attr))
ac9ec31b
DE
22309 type_die = follow_die_ref (die, attr, &type_cu);
22310 if (type_die == NULL)
22311 return build_error_marker_type (cu, die);
22312 /* If we find the type now, it's probably because the type came
3019eac3
DE
22313 from an inter-CU reference and the type's CU got expanded before
22314 ours. */
ac9ec31b 22315 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
22316 }
22317
22318 /* If we still don't have a type use an error marker. */
22319
22320 if (this_type == NULL)
ac9ec31b 22321 return build_error_marker_type (cu, die);
673bfd45 22322
f792889a 22323 return this_type;
c906108c
SS
22324}
22325
673bfd45
DE
22326/* Return the type in DIE, CU.
22327 Returns NULL for invalid types.
22328
02142a6c 22329 This first does a lookup in die_type_hash,
673bfd45
DE
22330 and only reads the die in if necessary.
22331
22332 NOTE: This can be called when reading in partial or full symbols. */
22333
f792889a 22334static struct type *
e7c27a73 22335read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22336{
f792889a
DJ
22337 struct type *this_type;
22338
22339 this_type = get_die_type (die, cu);
22340 if (this_type)
22341 return this_type;
22342
673bfd45
DE
22343 return read_type_die_1 (die, cu);
22344}
22345
22346/* Read the type in DIE, CU.
22347 Returns NULL for invalid types. */
22348
22349static struct type *
22350read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22351{
22352 struct type *this_type = NULL;
22353
c906108c
SS
22354 switch (die->tag)
22355 {
22356 case DW_TAG_class_type:
680b30c7 22357 case DW_TAG_interface_type:
c906108c
SS
22358 case DW_TAG_structure_type:
22359 case DW_TAG_union_type:
f792889a 22360 this_type = read_structure_type (die, cu);
c906108c
SS
22361 break;
22362 case DW_TAG_enumeration_type:
f792889a 22363 this_type = read_enumeration_type (die, cu);
c906108c
SS
22364 break;
22365 case DW_TAG_subprogram:
22366 case DW_TAG_subroutine_type:
edb3359d 22367 case DW_TAG_inlined_subroutine:
f792889a 22368 this_type = read_subroutine_type (die, cu);
c906108c
SS
22369 break;
22370 case DW_TAG_array_type:
f792889a 22371 this_type = read_array_type (die, cu);
c906108c 22372 break;
72019c9c 22373 case DW_TAG_set_type:
f792889a 22374 this_type = read_set_type (die, cu);
72019c9c 22375 break;
c906108c 22376 case DW_TAG_pointer_type:
f792889a 22377 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
22378 break;
22379 case DW_TAG_ptr_to_member_type:
f792889a 22380 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
22381 break;
22382 case DW_TAG_reference_type:
4297a3f0
AV
22383 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22384 break;
22385 case DW_TAG_rvalue_reference_type:
22386 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
22387 break;
22388 case DW_TAG_const_type:
f792889a 22389 this_type = read_tag_const_type (die, cu);
c906108c
SS
22390 break;
22391 case DW_TAG_volatile_type:
f792889a 22392 this_type = read_tag_volatile_type (die, cu);
c906108c 22393 break;
06d66ee9
TT
22394 case DW_TAG_restrict_type:
22395 this_type = read_tag_restrict_type (die, cu);
22396 break;
c906108c 22397 case DW_TAG_string_type:
f792889a 22398 this_type = read_tag_string_type (die, cu);
c906108c
SS
22399 break;
22400 case DW_TAG_typedef:
f792889a 22401 this_type = read_typedef (die, cu);
c906108c 22402 break;
a02abb62 22403 case DW_TAG_subrange_type:
f792889a 22404 this_type = read_subrange_type (die, cu);
a02abb62 22405 break;
c906108c 22406 case DW_TAG_base_type:
f792889a 22407 this_type = read_base_type (die, cu);
c906108c 22408 break;
81a17f79 22409 case DW_TAG_unspecified_type:
f792889a 22410 this_type = read_unspecified_type (die, cu);
81a17f79 22411 break;
0114d602
DJ
22412 case DW_TAG_namespace:
22413 this_type = read_namespace_type (die, cu);
22414 break;
f55ee35c
JK
22415 case DW_TAG_module:
22416 this_type = read_module_type (die, cu);
22417 break;
a2c2acaf
MW
22418 case DW_TAG_atomic_type:
22419 this_type = read_tag_atomic_type (die, cu);
22420 break;
c906108c 22421 default:
b98664d3 22422 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 22423 dwarf_tag_name (die->tag));
c906108c
SS
22424 break;
22425 }
63d06c5c 22426
f792889a 22427 return this_type;
63d06c5c
DC
22428}
22429
abc72ce4
DE
22430/* See if we can figure out if the class lives in a namespace. We do
22431 this by looking for a member function; its demangled name will
22432 contain namespace info, if there is any.
22433 Return the computed name or NULL.
22434 Space for the result is allocated on the objfile's obstack.
22435 This is the full-die version of guess_partial_die_structure_name.
22436 In this case we know DIE has no useful parent. */
22437
22438static char *
22439guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22440{
22441 struct die_info *spec_die;
22442 struct dwarf2_cu *spec_cu;
22443 struct die_info *child;
518817b3 22444 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4
DE
22445
22446 spec_cu = cu;
22447 spec_die = die_specification (die, &spec_cu);
22448 if (spec_die != NULL)
22449 {
22450 die = spec_die;
22451 cu = spec_cu;
22452 }
22453
22454 for (child = die->child;
22455 child != NULL;
22456 child = child->sibling)
22457 {
22458 if (child->tag == DW_TAG_subprogram)
22459 {
73b9be8b 22460 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 22461
7d45c7c3 22462 if (linkage_name != NULL)
abc72ce4
DE
22463 {
22464 char *actual_name
22465 = language_class_name_from_physname (cu->language_defn,
7d45c7c3 22466 linkage_name);
abc72ce4
DE
22467 char *name = NULL;
22468
22469 if (actual_name != NULL)
22470 {
15d034d0 22471 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
22472
22473 if (die_name != NULL
22474 && strcmp (die_name, actual_name) != 0)
22475 {
22476 /* Strip off the class name from the full name.
22477 We want the prefix. */
22478 int die_name_len = strlen (die_name);
22479 int actual_name_len = strlen (actual_name);
22480
22481 /* Test for '::' as a sanity check. */
22482 if (actual_name_len > die_name_len + 2
3e43a32a
MS
22483 && actual_name[actual_name_len
22484 - die_name_len - 1] == ':')
0cf9feb9 22485 name = obstack_strndup (
e3b94546 22486 &objfile->per_bfd->storage_obstack,
224c3ddb 22487 actual_name, actual_name_len - die_name_len - 2);
abc72ce4
DE
22488 }
22489 }
22490 xfree (actual_name);
22491 return name;
22492 }
22493 }
22494 }
22495
22496 return NULL;
22497}
22498
96408a79
SA
22499/* GCC might emit a nameless typedef that has a linkage name. Determine the
22500 prefix part in such case. See
22501 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22502
a121b7c1 22503static const char *
96408a79
SA
22504anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22505{
22506 struct attribute *attr;
e6a959d6 22507 const char *base;
96408a79
SA
22508
22509 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22510 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22511 return NULL;
22512
7d45c7c3 22513 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
22514 return NULL;
22515
73b9be8b 22516 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
22517 if (attr == NULL || DW_STRING (attr) == NULL)
22518 return NULL;
22519
22520 /* dwarf2_name had to be already called. */
22521 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22522
22523 /* Strip the base name, keep any leading namespaces/classes. */
22524 base = strrchr (DW_STRING (attr), ':');
22525 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22526 return "";
22527
518817b3 22528 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0cf9feb9
TT
22529 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22530 DW_STRING (attr),
22531 &base[-1] - DW_STRING (attr));
96408a79
SA
22532}
22533
fdde2d81 22534/* Return the name of the namespace/class that DIE is defined within,
0114d602 22535 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 22536
0114d602
DJ
22537 For example, if we're within the method foo() in the following
22538 code:
22539
22540 namespace N {
22541 class C {
22542 void foo () {
22543 }
22544 };
22545 }
22546
22547 then determine_prefix on foo's die will return "N::C". */
fdde2d81 22548
0d5cff50 22549static const char *
e142c38c 22550determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 22551{
518817b3
SM
22552 struct dwarf2_per_objfile *dwarf2_per_objfile
22553 = cu->per_cu->dwarf2_per_objfile;
0114d602
DJ
22554 struct die_info *parent, *spec_die;
22555 struct dwarf2_cu *spec_cu;
22556 struct type *parent_type;
a121b7c1 22557 const char *retval;
63d06c5c 22558
9c37b5ae 22559 if (cu->language != language_cplus
c44af4eb
TT
22560 && cu->language != language_fortran && cu->language != language_d
22561 && cu->language != language_rust)
0114d602
DJ
22562 return "";
22563
96408a79
SA
22564 retval = anonymous_struct_prefix (die, cu);
22565 if (retval)
22566 return retval;
22567
0114d602
DJ
22568 /* We have to be careful in the presence of DW_AT_specification.
22569 For example, with GCC 3.4, given the code
22570
22571 namespace N {
22572 void foo() {
22573 // Definition of N::foo.
22574 }
22575 }
22576
22577 then we'll have a tree of DIEs like this:
22578
22579 1: DW_TAG_compile_unit
22580 2: DW_TAG_namespace // N
22581 3: DW_TAG_subprogram // declaration of N::foo
22582 4: DW_TAG_subprogram // definition of N::foo
22583 DW_AT_specification // refers to die #3
22584
22585 Thus, when processing die #4, we have to pretend that we're in
22586 the context of its DW_AT_specification, namely the contex of die
22587 #3. */
22588 spec_cu = cu;
22589 spec_die = die_specification (die, &spec_cu);
22590 if (spec_die == NULL)
22591 parent = die->parent;
22592 else
63d06c5c 22593 {
0114d602
DJ
22594 parent = spec_die->parent;
22595 cu = spec_cu;
63d06c5c 22596 }
0114d602
DJ
22597
22598 if (parent == NULL)
22599 return "";
98bfdba5
PA
22600 else if (parent->building_fullname)
22601 {
22602 const char *name;
22603 const char *parent_name;
22604
22605 /* It has been seen on RealView 2.2 built binaries,
22606 DW_TAG_template_type_param types actually _defined_ as
22607 children of the parent class:
22608
22609 enum E {};
22610 template class <class Enum> Class{};
22611 Class<enum E> class_e;
22612
22613 1: DW_TAG_class_type (Class)
22614 2: DW_TAG_enumeration_type (E)
22615 3: DW_TAG_enumerator (enum1:0)
22616 3: DW_TAG_enumerator (enum2:1)
22617 ...
22618 2: DW_TAG_template_type_param
22619 DW_AT_type DW_FORM_ref_udata (E)
22620
22621 Besides being broken debug info, it can put GDB into an
22622 infinite loop. Consider:
22623
22624 When we're building the full name for Class<E>, we'll start
22625 at Class, and go look over its template type parameters,
22626 finding E. We'll then try to build the full name of E, and
22627 reach here. We're now trying to build the full name of E,
22628 and look over the parent DIE for containing scope. In the
22629 broken case, if we followed the parent DIE of E, we'd again
22630 find Class, and once again go look at its template type
22631 arguments, etc., etc. Simply don't consider such parent die
22632 as source-level parent of this die (it can't be, the language
22633 doesn't allow it), and break the loop here. */
22634 name = dwarf2_name (die, cu);
22635 parent_name = dwarf2_name (parent, cu);
b98664d3 22636 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
22637 name ? name : "<unknown>",
22638 parent_name ? parent_name : "<unknown>");
22639 return "";
22640 }
63d06c5c 22641 else
0114d602
DJ
22642 switch (parent->tag)
22643 {
63d06c5c 22644 case DW_TAG_namespace:
0114d602 22645 parent_type = read_type_die (parent, cu);
acebe513
UW
22646 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22647 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22648 Work around this problem here. */
22649 if (cu->language == language_cplus
e86ca25f 22650 && strcmp (TYPE_NAME (parent_type), "::") == 0)
acebe513 22651 return "";
0114d602 22652 /* We give a name to even anonymous namespaces. */
e86ca25f 22653 return TYPE_NAME (parent_type);
63d06c5c 22654 case DW_TAG_class_type:
680b30c7 22655 case DW_TAG_interface_type:
63d06c5c 22656 case DW_TAG_structure_type:
0114d602 22657 case DW_TAG_union_type:
f55ee35c 22658 case DW_TAG_module:
0114d602 22659 parent_type = read_type_die (parent, cu);
e86ca25f
TT
22660 if (TYPE_NAME (parent_type) != NULL)
22661 return TYPE_NAME (parent_type);
0114d602
DJ
22662 else
22663 /* An anonymous structure is only allowed non-static data
22664 members; no typedefs, no member functions, et cetera.
22665 So it does not need a prefix. */
22666 return "";
abc72ce4 22667 case DW_TAG_compile_unit:
95554aad 22668 case DW_TAG_partial_unit:
abc72ce4
DE
22669 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22670 if (cu->language == language_cplus
fd5866f6 22671 && !dwarf2_per_objfile->types.empty ()
abc72ce4
DE
22672 && die->child != NULL
22673 && (die->tag == DW_TAG_class_type
22674 || die->tag == DW_TAG_structure_type
22675 || die->tag == DW_TAG_union_type))
22676 {
22677 char *name = guess_full_die_structure_name (die, cu);
22678 if (name != NULL)
22679 return name;
22680 }
22681 return "";
0a4b0913
AB
22682 case DW_TAG_subprogram:
22683 /* Nested subroutines in Fortran get a prefix with the name
22684 of the parent's subroutine. */
22685 if (cu->language == language_fortran)
22686 {
22687 if ((die->tag == DW_TAG_subprogram)
22688 && (dwarf2_name (parent, cu) != NULL))
22689 return dwarf2_name (parent, cu);
22690 }
22691 return determine_prefix (parent, cu);
3d567982
TT
22692 case DW_TAG_enumeration_type:
22693 parent_type = read_type_die (parent, cu);
22694 if (TYPE_DECLARED_CLASS (parent_type))
22695 {
e86ca25f
TT
22696 if (TYPE_NAME (parent_type) != NULL)
22697 return TYPE_NAME (parent_type);
3d567982
TT
22698 return "";
22699 }
22700 /* Fall through. */
63d06c5c 22701 default:
8176b9b8 22702 return determine_prefix (parent, cu);
63d06c5c 22703 }
63d06c5c
DC
22704}
22705
3e43a32a
MS
22706/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22707 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22708 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22709 an obconcat, otherwise allocate storage for the result. The CU argument is
22710 used to determine the language and hence, the appropriate separator. */
987504bb 22711
f55ee35c 22712#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
22713
22714static char *
f55ee35c
JK
22715typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22716 int physname, struct dwarf2_cu *cu)
63d06c5c 22717{
f55ee35c 22718 const char *lead = "";
5c315b68 22719 const char *sep;
63d06c5c 22720
3e43a32a
MS
22721 if (suffix == NULL || suffix[0] == '\0'
22722 || prefix == NULL || prefix[0] == '\0')
987504bb 22723 sep = "";
45280282
IB
22724 else if (cu->language == language_d)
22725 {
22726 /* For D, the 'main' function could be defined in any module, but it
22727 should never be prefixed. */
22728 if (strcmp (suffix, "D main") == 0)
22729 {
22730 prefix = "";
22731 sep = "";
22732 }
22733 else
22734 sep = ".";
22735 }
f55ee35c
JK
22736 else if (cu->language == language_fortran && physname)
22737 {
22738 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22739 DW_AT_MIPS_linkage_name is preferred and used instead. */
22740
22741 lead = "__";
22742 sep = "_MOD_";
22743 }
987504bb
JJ
22744 else
22745 sep = "::";
63d06c5c 22746
6dd47d34
DE
22747 if (prefix == NULL)
22748 prefix = "";
22749 if (suffix == NULL)
22750 suffix = "";
22751
987504bb
JJ
22752 if (obs == NULL)
22753 {
3e43a32a 22754 char *retval
224c3ddb
SM
22755 = ((char *)
22756 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 22757
f55ee35c
JK
22758 strcpy (retval, lead);
22759 strcat (retval, prefix);
6dd47d34
DE
22760 strcat (retval, sep);
22761 strcat (retval, suffix);
63d06c5c
DC
22762 return retval;
22763 }
987504bb
JJ
22764 else
22765 {
22766 /* We have an obstack. */
f55ee35c 22767 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 22768 }
63d06c5c
DC
22769}
22770
c906108c
SS
22771/* Return sibling of die, NULL if no sibling. */
22772
f9aca02d 22773static struct die_info *
fba45db2 22774sibling_die (struct die_info *die)
c906108c 22775{
639d11d3 22776 return die->sibling;
c906108c
SS
22777}
22778
71c25dea
TT
22779/* Get name of a die, return NULL if not found. */
22780
15d034d0
TT
22781static const char *
22782dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
22783 struct obstack *obstack)
22784{
22785 if (name && cu->language == language_cplus)
22786 {
2f408ecb 22787 std::string canon_name = cp_canonicalize_string (name);
71c25dea 22788
2f408ecb 22789 if (!canon_name.empty ())
71c25dea 22790 {
2f408ecb 22791 if (canon_name != name)
efba19b0 22792 name = obstack_strdup (obstack, canon_name);
71c25dea
TT
22793 }
22794 }
22795
22796 return name;
c906108c
SS
22797}
22798
96553a0c
DE
22799/* Get name of a die, return NULL if not found.
22800 Anonymous namespaces are converted to their magic string. */
9219021c 22801
15d034d0 22802static const char *
e142c38c 22803dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
22804{
22805 struct attribute *attr;
518817b3 22806 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9219021c 22807
e142c38c 22808 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 22809 if ((!attr || !DW_STRING (attr))
96553a0c 22810 && die->tag != DW_TAG_namespace
53832f31
TT
22811 && die->tag != DW_TAG_class_type
22812 && die->tag != DW_TAG_interface_type
22813 && die->tag != DW_TAG_structure_type
22814 && die->tag != DW_TAG_union_type)
71c25dea
TT
22815 return NULL;
22816
22817 switch (die->tag)
22818 {
22819 case DW_TAG_compile_unit:
95554aad 22820 case DW_TAG_partial_unit:
71c25dea
TT
22821 /* Compilation units have a DW_AT_name that is a filename, not
22822 a source language identifier. */
22823 case DW_TAG_enumeration_type:
22824 case DW_TAG_enumerator:
22825 /* These tags always have simple identifiers already; no need
22826 to canonicalize them. */
22827 return DW_STRING (attr);
907af001 22828
96553a0c
DE
22829 case DW_TAG_namespace:
22830 if (attr != NULL && DW_STRING (attr) != NULL)
22831 return DW_STRING (attr);
22832 return CP_ANONYMOUS_NAMESPACE_STR;
22833
907af001
UW
22834 case DW_TAG_class_type:
22835 case DW_TAG_interface_type:
22836 case DW_TAG_structure_type:
22837 case DW_TAG_union_type:
22838 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22839 structures or unions. These were of the form "._%d" in GCC 4.1,
22840 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22841 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 22842 if (attr && DW_STRING (attr)
61012eef
GB
22843 && (startswith (DW_STRING (attr), "._")
22844 || startswith (DW_STRING (attr), "<anonymous")))
907af001 22845 return NULL;
53832f31
TT
22846
22847 /* GCC might emit a nameless typedef that has a linkage name. See
22848 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22849 if (!attr || DW_STRING (attr) == NULL)
22850 {
df5c6c50 22851 char *demangled = NULL;
53832f31 22852
73b9be8b 22853 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
22854 if (attr == NULL || DW_STRING (attr) == NULL)
22855 return NULL;
22856
df5c6c50
JK
22857 /* Avoid demangling DW_STRING (attr) the second time on a second
22858 call for the same DIE. */
22859 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 22860 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
22861
22862 if (demangled)
22863 {
e6a959d6 22864 const char *base;
96408a79 22865
53832f31 22866 /* FIXME: we already did this for the partial symbol... */
34a68019 22867 DW_STRING (attr)
021887d8
TT
22868 = obstack_strdup (&objfile->per_bfd->storage_obstack,
22869 demangled);
53832f31
TT
22870 DW_STRING_IS_CANONICAL (attr) = 1;
22871 xfree (demangled);
96408a79
SA
22872
22873 /* Strip any leading namespaces/classes, keep only the base name.
22874 DW_AT_name for named DIEs does not contain the prefixes. */
22875 base = strrchr (DW_STRING (attr), ':');
22876 if (base && base > DW_STRING (attr) && base[-1] == ':')
22877 return &base[1];
22878 else
22879 return DW_STRING (attr);
53832f31
TT
22880 }
22881 }
907af001
UW
22882 break;
22883
71c25dea 22884 default:
907af001
UW
22885 break;
22886 }
22887
22888 if (!DW_STRING_IS_CANONICAL (attr))
22889 {
22890 DW_STRING (attr)
22891 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
e3b94546 22892 &objfile->per_bfd->storage_obstack);
907af001 22893 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 22894 }
907af001 22895 return DW_STRING (attr);
9219021c
DC
22896}
22897
22898/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
22899 is none. *EXT_CU is the CU containing DIE on input, and the CU
22900 containing the return value on output. */
9219021c
DC
22901
22902static struct die_info *
f2f0e013 22903dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
22904{
22905 struct attribute *attr;
9219021c 22906
f2f0e013 22907 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
22908 if (attr == NULL)
22909 return NULL;
22910
f2f0e013 22911 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
22912}
22913
fa9c3fa0
TT
22914/* A convenience function that returns an "unknown" DWARF name,
22915 including the value of V. STR is the name of the entity being
22916 printed, e.g., "TAG". */
22917
22918static const char *
22919dwarf_unknown (const char *str, unsigned v)
22920{
22921 char *cell = get_print_cell ();
22922 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
22923 return cell;
22924}
22925
c906108c
SS
22926/* Convert a DIE tag into its string name. */
22927
f39c6ffd 22928static const char *
aa1ee363 22929dwarf_tag_name (unsigned tag)
c906108c 22930{
f39c6ffd
TT
22931 const char *name = get_DW_TAG_name (tag);
22932
22933 if (name == NULL)
fa9c3fa0 22934 return dwarf_unknown ("TAG", tag);
f39c6ffd
TT
22935
22936 return name;
c906108c
SS
22937}
22938
22939/* Convert a DWARF attribute code into its string name. */
22940
f39c6ffd 22941static const char *
aa1ee363 22942dwarf_attr_name (unsigned attr)
c906108c 22943{
f39c6ffd
TT
22944 const char *name;
22945
c764a876 22946#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
22947 if (attr == DW_AT_MIPS_fde)
22948 return "DW_AT_MIPS_fde";
22949#else
22950 if (attr == DW_AT_HP_block_index)
22951 return "DW_AT_HP_block_index";
c764a876 22952#endif
f39c6ffd
TT
22953
22954 name = get_DW_AT_name (attr);
22955
22956 if (name == NULL)
fa9c3fa0 22957 return dwarf_unknown ("AT", attr);
f39c6ffd
TT
22958
22959 return name;
c906108c
SS
22960}
22961
a084a2a6
AT
22962/* Convert a unit type to corresponding DW_UT name. */
22963
22964static const char *
22965dwarf_unit_type_name (int unit_type) {
22966 switch (unit_type)
22967 {
22968 case 0x01:
22969 return "DW_UT_compile (0x01)";
22970 case 0x02:
22971 return "DW_UT_type (0x02)";
22972 case 0x03:
22973 return "DW_UT_partial (0x03)";
22974 case 0x04:
22975 return "DW_UT_skeleton (0x04)";
22976 case 0x05:
22977 return "DW_UT_split_compile (0x05)";
22978 case 0x06:
22979 return "DW_UT_split_type (0x06)";
22980 case 0x80:
22981 return "DW_UT_lo_user (0x80)";
22982 case 0xff:
22983 return "DW_UT_hi_user (0xff)";
22984 default:
22985 return nullptr;
22986 }
22987}
22988
c906108c
SS
22989/* Convert a DWARF value form code into its string name. */
22990
f39c6ffd 22991static const char *
aa1ee363 22992dwarf_form_name (unsigned form)
c906108c 22993{
f39c6ffd
TT
22994 const char *name = get_DW_FORM_name (form);
22995
22996 if (name == NULL)
fa9c3fa0 22997 return dwarf_unknown ("FORM", form);
f39c6ffd
TT
22998
22999 return name;
c906108c
SS
23000}
23001
a121b7c1 23002static const char *
fba45db2 23003dwarf_bool_name (unsigned mybool)
c906108c
SS
23004{
23005 if (mybool)
23006 return "TRUE";
23007 else
23008 return "FALSE";
23009}
23010
23011/* Convert a DWARF type code into its string name. */
23012
f39c6ffd 23013static const char *
aa1ee363 23014dwarf_type_encoding_name (unsigned enc)
c906108c 23015{
f39c6ffd 23016 const char *name = get_DW_ATE_name (enc);
c906108c 23017
f39c6ffd 23018 if (name == NULL)
fa9c3fa0 23019 return dwarf_unknown ("ATE", enc);
c906108c 23020
f39c6ffd 23021 return name;
c906108c 23022}
c906108c 23023
f9aca02d 23024static void
d97bc12b 23025dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
23026{
23027 unsigned int i;
23028
d97bc12b 23029 print_spaces (indent, f);
9d8780f0 23030 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 23031 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 23032 sect_offset_str (die->sect_off));
d97bc12b
DE
23033
23034 if (die->parent != NULL)
23035 {
23036 print_spaces (indent, f);
9d8780f0
SM
23037 fprintf_unfiltered (f, " parent at offset: %s\n",
23038 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
23039 }
23040
23041 print_spaces (indent, f);
23042 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 23043 dwarf_bool_name (die->child != NULL));
c906108c 23044
d97bc12b
DE
23045 print_spaces (indent, f);
23046 fprintf_unfiltered (f, " attributes:\n");
23047
c906108c
SS
23048 for (i = 0; i < die->num_attrs; ++i)
23049 {
d97bc12b
DE
23050 print_spaces (indent, f);
23051 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
23052 dwarf_attr_name (die->attrs[i].name),
23053 dwarf_form_name (die->attrs[i].form));
d97bc12b 23054
c906108c
SS
23055 switch (die->attrs[i].form)
23056 {
c906108c 23057 case DW_FORM_addr:
336d760d 23058 case DW_FORM_addrx:
3019eac3 23059 case DW_FORM_GNU_addr_index:
d97bc12b 23060 fprintf_unfiltered (f, "address: ");
5af949e3 23061 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
23062 break;
23063 case DW_FORM_block2:
23064 case DW_FORM_block4:
23065 case DW_FORM_block:
23066 case DW_FORM_block1:
56eb65bd
SP
23067 fprintf_unfiltered (f, "block: size %s",
23068 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 23069 break;
2dc7f7b3 23070 case DW_FORM_exprloc:
56eb65bd
SP
23071 fprintf_unfiltered (f, "expression: size %s",
23072 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 23073 break;
0224619f
JK
23074 case DW_FORM_data16:
23075 fprintf_unfiltered (f, "constant of 16 bytes");
23076 break;
4568ecf9
DE
23077 case DW_FORM_ref_addr:
23078 fprintf_unfiltered (f, "ref address: ");
23079 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23080 break;
36586728
TT
23081 case DW_FORM_GNU_ref_alt:
23082 fprintf_unfiltered (f, "alt ref address: ");
23083 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
23084 break;
10b3939b
DJ
23085 case DW_FORM_ref1:
23086 case DW_FORM_ref2:
23087 case DW_FORM_ref4:
4568ecf9
DE
23088 case DW_FORM_ref8:
23089 case DW_FORM_ref_udata:
d97bc12b 23090 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 23091 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 23092 break;
c906108c
SS
23093 case DW_FORM_data1:
23094 case DW_FORM_data2:
23095 case DW_FORM_data4:
ce5d95e1 23096 case DW_FORM_data8:
c906108c
SS
23097 case DW_FORM_udata:
23098 case DW_FORM_sdata:
43bbcdc2
PH
23099 fprintf_unfiltered (f, "constant: %s",
23100 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 23101 break;
2dc7f7b3
TT
23102 case DW_FORM_sec_offset:
23103 fprintf_unfiltered (f, "section offset: %s",
23104 pulongest (DW_UNSND (&die->attrs[i])));
23105 break;
55f1336d 23106 case DW_FORM_ref_sig8:
ac9ec31b
DE
23107 fprintf_unfiltered (f, "signature: %s",
23108 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 23109 break;
c906108c 23110 case DW_FORM_string:
4bdf3d34 23111 case DW_FORM_strp:
43988095 23112 case DW_FORM_line_strp:
cf532bd1 23113 case DW_FORM_strx:
3019eac3 23114 case DW_FORM_GNU_str_index:
36586728 23115 case DW_FORM_GNU_strp_alt:
8285870a 23116 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 23117 DW_STRING (&die->attrs[i])
8285870a
JK
23118 ? DW_STRING (&die->attrs[i]) : "",
23119 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
23120 break;
23121 case DW_FORM_flag:
23122 if (DW_UNSND (&die->attrs[i]))
d97bc12b 23123 fprintf_unfiltered (f, "flag: TRUE");
c906108c 23124 else
d97bc12b 23125 fprintf_unfiltered (f, "flag: FALSE");
c906108c 23126 break;
2dc7f7b3
TT
23127 case DW_FORM_flag_present:
23128 fprintf_unfiltered (f, "flag: TRUE");
23129 break;
a8329558 23130 case DW_FORM_indirect:
0963b4bd
MS
23131 /* The reader will have reduced the indirect form to
23132 the "base form" so this form should not occur. */
5f48f8f3 23133 fprintf_unfiltered (f,
3e43a32a 23134 "unexpected attribute form: DW_FORM_indirect");
a8329558 23135 break;
663c44ac
JK
23136 case DW_FORM_implicit_const:
23137 fprintf_unfiltered (f, "constant: %s",
23138 plongest (DW_SND (&die->attrs[i])));
23139 break;
c906108c 23140 default:
d97bc12b 23141 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 23142 die->attrs[i].form);
d97bc12b 23143 break;
c906108c 23144 }
d97bc12b 23145 fprintf_unfiltered (f, "\n");
c906108c
SS
23146 }
23147}
23148
f9aca02d 23149static void
d97bc12b 23150dump_die_for_error (struct die_info *die)
c906108c 23151{
d97bc12b
DE
23152 dump_die_shallow (gdb_stderr, 0, die);
23153}
23154
23155static void
23156dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23157{
23158 int indent = level * 4;
23159
23160 gdb_assert (die != NULL);
23161
23162 if (level >= max_level)
23163 return;
23164
23165 dump_die_shallow (f, indent, die);
23166
23167 if (die->child != NULL)
c906108c 23168 {
d97bc12b
DE
23169 print_spaces (indent, f);
23170 fprintf_unfiltered (f, " Children:");
23171 if (level + 1 < max_level)
23172 {
23173 fprintf_unfiltered (f, "\n");
23174 dump_die_1 (f, level + 1, max_level, die->child);
23175 }
23176 else
23177 {
3e43a32a
MS
23178 fprintf_unfiltered (f,
23179 " [not printed, max nesting level reached]\n");
d97bc12b
DE
23180 }
23181 }
23182
23183 if (die->sibling != NULL && level > 0)
23184 {
23185 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
23186 }
23187}
23188
d97bc12b
DE
23189/* This is called from the pdie macro in gdbinit.in.
23190 It's not static so gcc will keep a copy callable from gdb. */
23191
23192void
23193dump_die (struct die_info *die, int max_level)
23194{
23195 dump_die_1 (gdb_stdlog, 0, max_level, die);
23196}
23197
f9aca02d 23198static void
51545339 23199store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 23200{
51545339 23201 void **slot;
c906108c 23202
9c541725
PA
23203 slot = htab_find_slot_with_hash (cu->die_hash, die,
23204 to_underlying (die->sect_off),
b64f50a1 23205 INSERT);
51545339
DJ
23206
23207 *slot = die;
c906108c
SS
23208}
23209
b64f50a1
JK
23210/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
23211 required kind. */
23212
23213static sect_offset
ff39bb5e 23214dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 23215{
7771576e 23216 if (attr_form_is_ref (attr))
9c541725 23217 return (sect_offset) DW_UNSND (attr);
93311388 23218
b98664d3 23219 complaint (_("unsupported die ref attribute form: '%s'"),
93311388 23220 dwarf_form_name (attr->form));
9c541725 23221 return {};
c906108c
SS
23222}
23223
43bbcdc2
PH
23224/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
23225 * the value held by the attribute is not constant. */
a02abb62 23226
43bbcdc2 23227static LONGEST
ff39bb5e 23228dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 23229{
663c44ac 23230 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
23231 return DW_SND (attr);
23232 else if (attr->form == DW_FORM_udata
23233 || attr->form == DW_FORM_data1
23234 || attr->form == DW_FORM_data2
23235 || attr->form == DW_FORM_data4
23236 || attr->form == DW_FORM_data8)
23237 return DW_UNSND (attr);
23238 else
23239 {
0224619f 23240 /* For DW_FORM_data16 see attr_form_is_constant. */
b98664d3 23241 complaint (_("Attribute value is not a constant (%s)"),
a02abb62
JB
23242 dwarf_form_name (attr->form));
23243 return default_value;
23244 }
23245}
23246
348e048f
DE
23247/* Follow reference or signature attribute ATTR of SRC_DIE.
23248 On entry *REF_CU is the CU of SRC_DIE.
23249 On exit *REF_CU is the CU of the result. */
23250
23251static struct die_info *
ff39bb5e 23252follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
23253 struct dwarf2_cu **ref_cu)
23254{
23255 struct die_info *die;
23256
7771576e 23257 if (attr_form_is_ref (attr))
348e048f 23258 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 23259 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
23260 die = follow_die_sig (src_die, attr, ref_cu);
23261 else
23262 {
23263 dump_die_for_error (src_die);
23264 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
518817b3 23265 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
348e048f
DE
23266 }
23267
23268 return die;
03dd20cc
DJ
23269}
23270
5c631832 23271/* Follow reference OFFSET.
673bfd45
DE
23272 On entry *REF_CU is the CU of the source die referencing OFFSET.
23273 On exit *REF_CU is the CU of the result.
23274 Returns NULL if OFFSET is invalid. */
f504f079 23275
f9aca02d 23276static struct die_info *
9c541725 23277follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 23278 struct dwarf2_cu **ref_cu)
c906108c 23279{
10b3939b 23280 struct die_info temp_die;
f2f0e013 23281 struct dwarf2_cu *target_cu, *cu = *ref_cu;
518817b3
SM
23282 struct dwarf2_per_objfile *dwarf2_per_objfile
23283 = cu->per_cu->dwarf2_per_objfile;
10b3939b 23284
348e048f
DE
23285 gdb_assert (cu->per_cu != NULL);
23286
98bfdba5
PA
23287 target_cu = cu;
23288
3019eac3 23289 if (cu->per_cu->is_debug_types)
348e048f
DE
23290 {
23291 /* .debug_types CUs cannot reference anything outside their CU.
23292 If they need to, they have to reference a signatured type via
55f1336d 23293 DW_FORM_ref_sig8. */
9c541725 23294 if (!offset_in_cu_p (&cu->header, sect_off))
5c631832 23295 return NULL;
348e048f 23296 }
36586728 23297 else if (offset_in_dwz != cu->per_cu->is_dwz
9c541725 23298 || !offset_in_cu_p (&cu->header, sect_off))
10b3939b
DJ
23299 {
23300 struct dwarf2_per_cu_data *per_cu;
9a619af0 23301
9c541725 23302 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 23303 dwarf2_per_objfile);
03dd20cc
DJ
23304
23305 /* If necessary, add it to the queue and load its DIEs. */
95554aad 23306 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 23307 load_full_comp_unit (per_cu, false, cu->language);
03dd20cc 23308
10b3939b
DJ
23309 target_cu = per_cu->cu;
23310 }
98bfdba5
PA
23311 else if (cu->dies == NULL)
23312 {
23313 /* We're loading full DIEs during partial symbol reading. */
23314 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
58f0c718 23315 load_full_comp_unit (cu->per_cu, false, language_minimal);
98bfdba5 23316 }
c906108c 23317
f2f0e013 23318 *ref_cu = target_cu;
9c541725 23319 temp_die.sect_off = sect_off;
c24bdb02
KS
23320
23321 if (target_cu != cu)
23322 target_cu->ancestor = cu;
23323
9a3c8263 23324 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
23325 &temp_die,
23326 to_underlying (sect_off));
5c631832 23327}
10b3939b 23328
5c631832
JK
23329/* Follow reference attribute ATTR of SRC_DIE.
23330 On entry *REF_CU is the CU of SRC_DIE.
23331 On exit *REF_CU is the CU of the result. */
23332
23333static struct die_info *
ff39bb5e 23334follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
23335 struct dwarf2_cu **ref_cu)
23336{
9c541725 23337 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
23338 struct dwarf2_cu *cu = *ref_cu;
23339 struct die_info *die;
23340
9c541725 23341 die = follow_die_offset (sect_off,
36586728
TT
23342 (attr->form == DW_FORM_GNU_ref_alt
23343 || cu->per_cu->is_dwz),
23344 ref_cu);
5c631832 23345 if (!die)
9d8780f0
SM
23346 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23347 "at %s [in module %s]"),
23348 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
518817b3 23349 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
348e048f 23350
5c631832
JK
23351 return die;
23352}
23353
9c541725 23354/* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
d83e736b 23355 Returned value is intended for DW_OP_call*. Returned
e3b94546
SM
23356 dwarf2_locexpr_baton->data has lifetime of
23357 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
5c631832
JK
23358
23359struct dwarf2_locexpr_baton
9c541725 23360dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
8b9737bf
TT
23361 struct dwarf2_per_cu_data *per_cu,
23362 CORE_ADDR (*get_frame_pc) (void *baton),
e4a62c65 23363 void *baton, bool resolve_abstract_p)
5c631832 23364{
918dd910 23365 struct dwarf2_cu *cu;
5c631832
JK
23366 struct die_info *die;
23367 struct attribute *attr;
23368 struct dwarf2_locexpr_baton retval;
12359b5e
SM
23369 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23370 struct objfile *objfile = dwarf2_per_objfile->objfile;
8cf6f0b1 23371
918dd910 23372 if (per_cu->cu == NULL)
58f0c718 23373 load_cu (per_cu, false);
918dd910 23374 cu = per_cu->cu;
cc12ce38
DE
23375 if (cu == NULL)
23376 {
23377 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23378 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23379 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23380 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23381 }
918dd910 23382
9c541725 23383 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 23384 if (!die)
9d8780f0
SM
23385 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23386 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23387
23388 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 23389 if (!attr && resolve_abstract_p
3360b6e7 23390 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
e4a62c65
TV
23391 != dwarf2_per_objfile->abstract_to_concrete.end ()))
23392 {
23393 CORE_ADDR pc = (*get_frame_pc) (baton);
eba4caf2
TV
23394 CORE_ADDR baseaddr
23395 = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23396 struct gdbarch *gdbarch = get_objfile_arch (objfile);
e4a62c65 23397
3360b6e7
TV
23398 for (const auto &cand_off
23399 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
e4a62c65 23400 {
3360b6e7
TV
23401 struct dwarf2_cu *cand_cu = cu;
23402 struct die_info *cand
23403 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23404 if (!cand
23405 || !cand->parent
e4a62c65
TV
23406 || cand->parent->tag != DW_TAG_subprogram)
23407 continue;
23408
23409 CORE_ADDR pc_low, pc_high;
23410 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
23411 if (pc_low == ((CORE_ADDR) -1))
23412 continue;
23413 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23414 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23415 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
23416 continue;
23417
23418 die = cand;
23419 attr = dwarf2_attr (die, DW_AT_location, cu);
23420 break;
23421 }
23422 }
23423
5c631832
JK
23424 if (!attr)
23425 {
e103e986
JK
23426 /* DWARF: "If there is no such attribute, then there is no effect.".
23427 DATA is ignored if SIZE is 0. */
5c631832 23428
e103e986 23429 retval.data = NULL;
5c631832
JK
23430 retval.size = 0;
23431 }
8cf6f0b1
TT
23432 else if (attr_form_is_section_offset (attr))
23433 {
23434 struct dwarf2_loclist_baton loclist_baton;
23435 CORE_ADDR pc = (*get_frame_pc) (baton);
23436 size_t size;
23437
23438 fill_in_loclist_baton (cu, &loclist_baton, attr);
23439
23440 retval.data = dwarf2_find_location_expression (&loclist_baton,
23441 &size, pc);
23442 retval.size = size;
23443 }
5c631832
JK
23444 else
23445 {
23446 if (!attr_form_is_block (attr))
9d8780f0 23447 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 23448 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 23449 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23450
23451 retval.data = DW_BLOCK (attr)->data;
23452 retval.size = DW_BLOCK (attr)->size;
23453 }
23454 retval.per_cu = cu->per_cu;
918dd910 23455
ed2dc618 23456 age_cached_comp_units (dwarf2_per_objfile);
918dd910 23457
5c631832 23458 return retval;
348e048f
DE
23459}
23460
8b9737bf
TT
23461/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23462 offset. */
23463
23464struct dwarf2_locexpr_baton
23465dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23466 struct dwarf2_per_cu_data *per_cu,
23467 CORE_ADDR (*get_frame_pc) (void *baton),
23468 void *baton)
23469{
9c541725 23470 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 23471
9c541725 23472 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
23473}
23474
b6807d98
TT
23475/* Write a constant of a given type as target-ordered bytes into
23476 OBSTACK. */
23477
23478static const gdb_byte *
23479write_constant_as_bytes (struct obstack *obstack,
23480 enum bfd_endian byte_order,
23481 struct type *type,
23482 ULONGEST value,
23483 LONGEST *len)
23484{
23485 gdb_byte *result;
23486
23487 *len = TYPE_LENGTH (type);
224c3ddb 23488 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23489 store_unsigned_integer (result, *len, byte_order, value);
23490
23491 return result;
23492}
23493
23494/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23495 pointer to the constant bytes and set LEN to the length of the
23496 data. If memory is needed, allocate it on OBSTACK. If the DIE
23497 does not have a DW_AT_const_value, return NULL. */
23498
23499const gdb_byte *
9c541725 23500dwarf2_fetch_constant_bytes (sect_offset sect_off,
b6807d98
TT
23501 struct dwarf2_per_cu_data *per_cu,
23502 struct obstack *obstack,
23503 LONGEST *len)
23504{
23505 struct dwarf2_cu *cu;
23506 struct die_info *die;
23507 struct attribute *attr;
23508 const gdb_byte *result = NULL;
23509 struct type *type;
23510 LONGEST value;
23511 enum bfd_endian byte_order;
e3b94546 23512 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 23513
b6807d98 23514 if (per_cu->cu == NULL)
58f0c718 23515 load_cu (per_cu, false);
b6807d98 23516 cu = per_cu->cu;
cc12ce38
DE
23517 if (cu == NULL)
23518 {
23519 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23520 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23521 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23522 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23523 }
b6807d98 23524
9c541725 23525 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 23526 if (!die)
9d8780f0
SM
23527 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23528 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
23529
23530 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23531 if (attr == NULL)
23532 return NULL;
23533
e3b94546 23534 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
23535 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23536
23537 switch (attr->form)
23538 {
23539 case DW_FORM_addr:
336d760d 23540 case DW_FORM_addrx:
b6807d98
TT
23541 case DW_FORM_GNU_addr_index:
23542 {
23543 gdb_byte *tem;
23544
23545 *len = cu->header.addr_size;
224c3ddb 23546 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23547 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23548 result = tem;
23549 }
23550 break;
23551 case DW_FORM_string:
23552 case DW_FORM_strp:
cf532bd1 23553 case DW_FORM_strx:
b6807d98
TT
23554 case DW_FORM_GNU_str_index:
23555 case DW_FORM_GNU_strp_alt:
23556 /* DW_STRING is already allocated on the objfile obstack, point
23557 directly to it. */
23558 result = (const gdb_byte *) DW_STRING (attr);
23559 *len = strlen (DW_STRING (attr));
23560 break;
23561 case DW_FORM_block1:
23562 case DW_FORM_block2:
23563 case DW_FORM_block4:
23564 case DW_FORM_block:
23565 case DW_FORM_exprloc:
0224619f 23566 case DW_FORM_data16:
b6807d98
TT
23567 result = DW_BLOCK (attr)->data;
23568 *len = DW_BLOCK (attr)->size;
23569 break;
23570
23571 /* The DW_AT_const_value attributes are supposed to carry the
23572 symbol's value "represented as it would be on the target
23573 architecture." By the time we get here, it's already been
23574 converted to host endianness, so we just need to sign- or
23575 zero-extend it as appropriate. */
23576 case DW_FORM_data1:
23577 type = die_type (die, cu);
23578 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23579 if (result == NULL)
23580 result = write_constant_as_bytes (obstack, byte_order,
23581 type, value, len);
23582 break;
23583 case DW_FORM_data2:
23584 type = die_type (die, cu);
23585 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23586 if (result == NULL)
23587 result = write_constant_as_bytes (obstack, byte_order,
23588 type, value, len);
23589 break;
23590 case DW_FORM_data4:
23591 type = die_type (die, cu);
23592 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23593 if (result == NULL)
23594 result = write_constant_as_bytes (obstack, byte_order,
23595 type, value, len);
23596 break;
23597 case DW_FORM_data8:
23598 type = die_type (die, cu);
23599 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23600 if (result == NULL)
23601 result = write_constant_as_bytes (obstack, byte_order,
23602 type, value, len);
23603 break;
23604
23605 case DW_FORM_sdata:
663c44ac 23606 case DW_FORM_implicit_const:
b6807d98
TT
23607 type = die_type (die, cu);
23608 result = write_constant_as_bytes (obstack, byte_order,
23609 type, DW_SND (attr), len);
23610 break;
23611
23612 case DW_FORM_udata:
23613 type = die_type (die, cu);
23614 result = write_constant_as_bytes (obstack, byte_order,
23615 type, DW_UNSND (attr), len);
23616 break;
23617
23618 default:
b98664d3 23619 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
23620 dwarf_form_name (attr->form));
23621 break;
23622 }
23623
23624 return result;
23625}
23626
7942e96e
AA
23627/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23628 valid type for this die is found. */
23629
23630struct type *
9c541725 23631dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
7942e96e
AA
23632 struct dwarf2_per_cu_data *per_cu)
23633{
23634 struct dwarf2_cu *cu;
23635 struct die_info *die;
23636
7942e96e 23637 if (per_cu->cu == NULL)
58f0c718 23638 load_cu (per_cu, false);
7942e96e
AA
23639 cu = per_cu->cu;
23640 if (!cu)
23641 return NULL;
23642
9c541725 23643 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
23644 if (!die)
23645 return NULL;
23646
23647 return die_type (die, cu);
23648}
23649
8a9b8146
TT
23650/* Return the type of the DIE at DIE_OFFSET in the CU named by
23651 PER_CU. */
23652
23653struct type *
b64f50a1 23654dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
23655 struct dwarf2_per_cu_data *per_cu)
23656{
9c541725 23657 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 23658 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
23659}
23660
ac9ec31b 23661/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 23662 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
23663 On exit *REF_CU is the CU of the result.
23664 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
23665
23666static struct die_info *
ac9ec31b
DE
23667follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23668 struct dwarf2_cu **ref_cu)
348e048f 23669{
348e048f 23670 struct die_info temp_die;
c24bdb02 23671 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f
DE
23672 struct die_info *die;
23673
ac9ec31b
DE
23674 /* While it might be nice to assert sig_type->type == NULL here,
23675 we can get here for DW_AT_imported_declaration where we need
23676 the DIE not the type. */
348e048f
DE
23677
23678 /* If necessary, add it to the queue and load its DIEs. */
23679
95554aad 23680 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 23681 read_signatured_type (sig_type);
348e048f 23682
348e048f 23683 sig_cu = sig_type->per_cu.cu;
69d751e3 23684 gdb_assert (sig_cu != NULL);
9c541725
PA
23685 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23686 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 23687 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 23688 to_underlying (temp_die.sect_off));
348e048f
DE
23689 if (die)
23690 {
ed2dc618 23691 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 23692 = (*ref_cu)->per_cu->dwarf2_per_objfile;
ed2dc618 23693
796a7ff8
DE
23694 /* For .gdb_index version 7 keep track of included TUs.
23695 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23696 if (dwarf2_per_objfile->index_table != NULL
23697 && dwarf2_per_objfile->index_table->version <= 7)
23698 {
ae640021 23699 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
23700 }
23701
348e048f 23702 *ref_cu = sig_cu;
c24bdb02
KS
23703 if (sig_cu != cu)
23704 sig_cu->ancestor = cu;
23705
348e048f
DE
23706 return die;
23707 }
23708
ac9ec31b
DE
23709 return NULL;
23710}
23711
23712/* Follow signatured type referenced by ATTR in SRC_DIE.
23713 On entry *REF_CU is the CU of SRC_DIE.
23714 On exit *REF_CU is the CU of the result.
23715 The result is the DIE of the type.
23716 If the referenced type cannot be found an error is thrown. */
23717
23718static struct die_info *
ff39bb5e 23719follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
23720 struct dwarf2_cu **ref_cu)
23721{
23722 ULONGEST signature = DW_SIGNATURE (attr);
23723 struct signatured_type *sig_type;
23724 struct die_info *die;
23725
23726 gdb_assert (attr->form == DW_FORM_ref_sig8);
23727
a2ce51a0 23728 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
23729 /* sig_type will be NULL if the signatured type is missing from
23730 the debug info. */
23731 if (sig_type == NULL)
23732 {
23733 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23734 " from DIE at %s [in module %s]"),
23735 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23736 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23737 }
23738
23739 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23740 if (die == NULL)
23741 {
23742 dump_die_for_error (src_die);
23743 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23744 " from DIE at %s [in module %s]"),
23745 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23746 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23747 }
23748
23749 return die;
23750}
23751
23752/* Get the type specified by SIGNATURE referenced in DIE/CU,
23753 reading in and processing the type unit if necessary. */
23754
23755static struct type *
23756get_signatured_type (struct die_info *die, ULONGEST signature,
23757 struct dwarf2_cu *cu)
23758{
518817b3
SM
23759 struct dwarf2_per_objfile *dwarf2_per_objfile
23760 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
23761 struct signatured_type *sig_type;
23762 struct dwarf2_cu *type_cu;
23763 struct die_info *type_die;
23764 struct type *type;
23765
a2ce51a0 23766 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
23767 /* sig_type will be NULL if the signatured type is missing from
23768 the debug info. */
23769 if (sig_type == NULL)
23770 {
b98664d3 23771 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23772 " from DIE at %s [in module %s]"),
23773 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23774 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23775 return build_error_marker_type (cu, die);
23776 }
23777
23778 /* If we already know the type we're done. */
23779 if (sig_type->type != NULL)
23780 return sig_type->type;
23781
23782 type_cu = cu;
23783 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23784 if (type_die != NULL)
23785 {
23786 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23787 is created. This is important, for example, because for c++ classes
23788 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23789 type = read_type_die (type_die, type_cu);
23790 if (type == NULL)
23791 {
b98664d3 23792 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
23793 " referenced from DIE at %s [in module %s]"),
23794 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23795 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23796 type = build_error_marker_type (cu, die);
23797 }
23798 }
23799 else
23800 {
b98664d3 23801 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23802 " from DIE at %s [in module %s]"),
23803 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23804 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23805 type = build_error_marker_type (cu, die);
23806 }
23807 sig_type->type = type;
23808
23809 return type;
23810}
23811
23812/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23813 reading in and processing the type unit if necessary. */
23814
23815static struct type *
ff39bb5e 23816get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 23817 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
23818{
23819 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 23820 if (attr_form_is_ref (attr))
ac9ec31b
DE
23821 {
23822 struct dwarf2_cu *type_cu = cu;
23823 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23824
23825 return read_type_die (type_die, type_cu);
23826 }
23827 else if (attr->form == DW_FORM_ref_sig8)
23828 {
23829 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23830 }
23831 else
23832 {
518817b3
SM
23833 struct dwarf2_per_objfile *dwarf2_per_objfile
23834 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 23835
b98664d3 23836 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
23837 " at %s [in module %s]"),
23838 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 23839 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23840 return build_error_marker_type (cu, die);
23841 }
348e048f
DE
23842}
23843
e5fe5e75 23844/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
23845
23846static void
e5fe5e75 23847load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 23848{
52dc124a 23849 struct signatured_type *sig_type;
348e048f 23850
f4dc4d17
DE
23851 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23852 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23853
6721b2ec
DE
23854 /* We have the per_cu, but we need the signatured_type.
23855 Fortunately this is an easy translation. */
23856 gdb_assert (per_cu->is_debug_types);
23857 sig_type = (struct signatured_type *) per_cu;
348e048f 23858
6721b2ec 23859 gdb_assert (per_cu->cu == NULL);
348e048f 23860
52dc124a 23861 read_signatured_type (sig_type);
348e048f 23862
6721b2ec 23863 gdb_assert (per_cu->cu != NULL);
348e048f
DE
23864}
23865
dee91e82
DE
23866/* die_reader_func for read_signatured_type.
23867 This is identical to load_full_comp_unit_reader,
23868 but is kept separate for now. */
348e048f
DE
23869
23870static void
dee91e82 23871read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 23872 const gdb_byte *info_ptr,
dee91e82
DE
23873 struct die_info *comp_unit_die,
23874 int has_children,
23875 void *data)
348e048f 23876{
dee91e82 23877 struct dwarf2_cu *cu = reader->cu;
348e048f 23878
dee91e82
DE
23879 gdb_assert (cu->die_hash == NULL);
23880 cu->die_hash =
23881 htab_create_alloc_ex (cu->header.length / 12,
23882 die_hash,
23883 die_eq,
23884 NULL,
23885 &cu->comp_unit_obstack,
23886 hashtab_obstack_allocate,
23887 dummy_obstack_deallocate);
348e048f 23888
dee91e82
DE
23889 if (has_children)
23890 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23891 &info_ptr, comp_unit_die);
23892 cu->dies = comp_unit_die;
23893 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
23894
23895 /* We try not to read any attributes in this function, because not
9cdd5dbd 23896 all CUs needed for references have been loaded yet, and symbol
348e048f 23897 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
23898 or we won't be able to build types correctly.
23899 Similarly, if we do not read the producer, we can not apply
23900 producer-specific interpretation. */
95554aad 23901 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 23902}
348e048f 23903
3019eac3
DE
23904/* Read in a signatured type and build its CU and DIEs.
23905 If the type is a stub for the real type in a DWO file,
23906 read in the real type from the DWO file as well. */
dee91e82
DE
23907
23908static void
23909read_signatured_type (struct signatured_type *sig_type)
23910{
23911 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 23912
3019eac3 23913 gdb_assert (per_cu->is_debug_types);
dee91e82 23914 gdb_assert (per_cu->cu == NULL);
348e048f 23915
58f0c718 23916 init_cutu_and_read_dies (per_cu, NULL, 0, 1, false,
f4dc4d17 23917 read_signatured_type_reader, NULL);
7ee85ab1 23918 sig_type->per_cu.tu_read = 1;
c906108c
SS
23919}
23920
c906108c
SS
23921/* Decode simple location descriptions.
23922 Given a pointer to a dwarf block that defines a location, compute
23923 the location and return the value.
23924
4cecd739
DJ
23925 NOTE drow/2003-11-18: This function is called in two situations
23926 now: for the address of static or global variables (partial symbols
23927 only) and for offsets into structures which are expected to be
23928 (more or less) constant. The partial symbol case should go away,
23929 and only the constant case should remain. That will let this
23930 function complain more accurately. A few special modes are allowed
23931 without complaint for global variables (for instance, global
23932 register values and thread-local values).
c906108c
SS
23933
23934 A location description containing no operations indicates that the
4cecd739 23935 object is optimized out. The return value is 0 for that case.
6b992462
DJ
23936 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23937 callers will only want a very basic result and this can become a
21ae7a4d
JK
23938 complaint.
23939
23940 Note that stack[0] is unused except as a default error return. */
c906108c
SS
23941
23942static CORE_ADDR
e7c27a73 23943decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 23944{
518817b3 23945 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
56eb65bd
SP
23946 size_t i;
23947 size_t size = blk->size;
d521ce57 23948 const gdb_byte *data = blk->data;
21ae7a4d
JK
23949 CORE_ADDR stack[64];
23950 int stacki;
23951 unsigned int bytes_read, unsnd;
23952 gdb_byte op;
c906108c 23953
21ae7a4d
JK
23954 i = 0;
23955 stacki = 0;
23956 stack[stacki] = 0;
23957 stack[++stacki] = 0;
23958
23959 while (i < size)
23960 {
23961 op = data[i++];
23962 switch (op)
23963 {
23964 case DW_OP_lit0:
23965 case DW_OP_lit1:
23966 case DW_OP_lit2:
23967 case DW_OP_lit3:
23968 case DW_OP_lit4:
23969 case DW_OP_lit5:
23970 case DW_OP_lit6:
23971 case DW_OP_lit7:
23972 case DW_OP_lit8:
23973 case DW_OP_lit9:
23974 case DW_OP_lit10:
23975 case DW_OP_lit11:
23976 case DW_OP_lit12:
23977 case DW_OP_lit13:
23978 case DW_OP_lit14:
23979 case DW_OP_lit15:
23980 case DW_OP_lit16:
23981 case DW_OP_lit17:
23982 case DW_OP_lit18:
23983 case DW_OP_lit19:
23984 case DW_OP_lit20:
23985 case DW_OP_lit21:
23986 case DW_OP_lit22:
23987 case DW_OP_lit23:
23988 case DW_OP_lit24:
23989 case DW_OP_lit25:
23990 case DW_OP_lit26:
23991 case DW_OP_lit27:
23992 case DW_OP_lit28:
23993 case DW_OP_lit29:
23994 case DW_OP_lit30:
23995 case DW_OP_lit31:
23996 stack[++stacki] = op - DW_OP_lit0;
23997 break;
f1bea926 23998
21ae7a4d
JK
23999 case DW_OP_reg0:
24000 case DW_OP_reg1:
24001 case DW_OP_reg2:
24002 case DW_OP_reg3:
24003 case DW_OP_reg4:
24004 case DW_OP_reg5:
24005 case DW_OP_reg6:
24006 case DW_OP_reg7:
24007 case DW_OP_reg8:
24008 case DW_OP_reg9:
24009 case DW_OP_reg10:
24010 case DW_OP_reg11:
24011 case DW_OP_reg12:
24012 case DW_OP_reg13:
24013 case DW_OP_reg14:
24014 case DW_OP_reg15:
24015 case DW_OP_reg16:
24016 case DW_OP_reg17:
24017 case DW_OP_reg18:
24018 case DW_OP_reg19:
24019 case DW_OP_reg20:
24020 case DW_OP_reg21:
24021 case DW_OP_reg22:
24022 case DW_OP_reg23:
24023 case DW_OP_reg24:
24024 case DW_OP_reg25:
24025 case DW_OP_reg26:
24026 case DW_OP_reg27:
24027 case DW_OP_reg28:
24028 case DW_OP_reg29:
24029 case DW_OP_reg30:
24030 case DW_OP_reg31:
24031 stack[++stacki] = op - DW_OP_reg0;
24032 if (i < size)
24033 dwarf2_complex_location_expr_complaint ();
24034 break;
c906108c 24035
21ae7a4d
JK
24036 case DW_OP_regx:
24037 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24038 i += bytes_read;
24039 stack[++stacki] = unsnd;
24040 if (i < size)
24041 dwarf2_complex_location_expr_complaint ();
24042 break;
c906108c 24043
21ae7a4d
JK
24044 case DW_OP_addr:
24045 stack[++stacki] = read_address (objfile->obfd, &data[i],
24046 cu, &bytes_read);
24047 i += bytes_read;
24048 break;
d53d4ac5 24049
21ae7a4d
JK
24050 case DW_OP_const1u:
24051 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24052 i += 1;
24053 break;
24054
24055 case DW_OP_const1s:
24056 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24057 i += 1;
24058 break;
24059
24060 case DW_OP_const2u:
24061 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24062 i += 2;
24063 break;
24064
24065 case DW_OP_const2s:
24066 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24067 i += 2;
24068 break;
d53d4ac5 24069
21ae7a4d
JK
24070 case DW_OP_const4u:
24071 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24072 i += 4;
24073 break;
24074
24075 case DW_OP_const4s:
24076 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24077 i += 4;
24078 break;
24079
585861ea
JK
24080 case DW_OP_const8u:
24081 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24082 i += 8;
24083 break;
24084
21ae7a4d
JK
24085 case DW_OP_constu:
24086 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24087 &bytes_read);
24088 i += bytes_read;
24089 break;
24090
24091 case DW_OP_consts:
24092 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24093 i += bytes_read;
24094 break;
24095
24096 case DW_OP_dup:
24097 stack[stacki + 1] = stack[stacki];
24098 stacki++;
24099 break;
24100
24101 case DW_OP_plus:
24102 stack[stacki - 1] += stack[stacki];
24103 stacki--;
24104 break;
24105
24106 case DW_OP_plus_uconst:
24107 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24108 &bytes_read);
24109 i += bytes_read;
24110 break;
24111
24112 case DW_OP_minus:
24113 stack[stacki - 1] -= stack[stacki];
24114 stacki--;
24115 break;
24116
24117 case DW_OP_deref:
24118 /* If we're not the last op, then we definitely can't encode
24119 this using GDB's address_class enum. This is valid for partial
24120 global symbols, although the variable's address will be bogus
24121 in the psymtab. */
24122 if (i < size)
24123 dwarf2_complex_location_expr_complaint ();
24124 break;
24125
24126 case DW_OP_GNU_push_tls_address:
4aa4e28b 24127 case DW_OP_form_tls_address:
21ae7a4d
JK
24128 /* The top of the stack has the offset from the beginning
24129 of the thread control block at which the variable is located. */
24130 /* Nothing should follow this operator, so the top of stack would
24131 be returned. */
24132 /* This is valid for partial global symbols, but the variable's
585861ea
JK
24133 address will be bogus in the psymtab. Make it always at least
24134 non-zero to not look as a variable garbage collected by linker
24135 which have DW_OP_addr 0. */
21ae7a4d
JK
24136 if (i < size)
24137 dwarf2_complex_location_expr_complaint ();
585861ea 24138 stack[stacki]++;
21ae7a4d
JK
24139 break;
24140
24141 case DW_OP_GNU_uninit:
24142 break;
24143
336d760d 24144 case DW_OP_addrx:
3019eac3 24145 case DW_OP_GNU_addr_index:
49f6c839 24146 case DW_OP_GNU_const_index:
3019eac3
DE
24147 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24148 &bytes_read);
24149 i += bytes_read;
24150 break;
24151
21ae7a4d
JK
24152 default:
24153 {
f39c6ffd 24154 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
24155
24156 if (name)
b98664d3 24157 complaint (_("unsupported stack op: '%s'"),
21ae7a4d
JK
24158 name);
24159 else
b98664d3 24160 complaint (_("unsupported stack op: '%02x'"),
21ae7a4d
JK
24161 op);
24162 }
24163
24164 return (stack[stacki]);
d53d4ac5 24165 }
3c6e0cb3 24166
21ae7a4d
JK
24167 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24168 outside of the allocated space. Also enforce minimum>0. */
24169 if (stacki >= ARRAY_SIZE (stack) - 1)
24170 {
b98664d3 24171 complaint (_("location description stack overflow"));
21ae7a4d
JK
24172 return 0;
24173 }
24174
24175 if (stacki <= 0)
24176 {
b98664d3 24177 complaint (_("location description stack underflow"));
21ae7a4d
JK
24178 return 0;
24179 }
24180 }
24181 return (stack[stacki]);
c906108c
SS
24182}
24183
24184/* memory allocation interface */
24185
c906108c 24186static struct dwarf_block *
7b5a2f43 24187dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 24188{
8d749320 24189 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
24190}
24191
c906108c 24192static struct die_info *
b60c80d6 24193dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
24194{
24195 struct die_info *die;
b60c80d6
DJ
24196 size_t size = sizeof (struct die_info);
24197
24198 if (num_attrs > 1)
24199 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 24200
b60c80d6 24201 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
24202 memset (die, 0, sizeof (struct die_info));
24203 return (die);
24204}
2e276125
JB
24205
24206\f
24207/* Macro support. */
24208
233d95b5
JK
24209/* Return file name relative to the compilation directory of file number I in
24210 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 24211 responsible for freeing it. */
233d95b5 24212
2e276125 24213static char *
233d95b5 24214file_file_name (int file, struct line_header *lh)
2e276125 24215{
6a83a1e6
EZ
24216 /* Is the file number a valid index into the line header's file name
24217 table? Remember that file numbers start with one, not zero. */
7ba99d21 24218 if (lh->is_valid_file_index (file))
6a83a1e6 24219 {
7ba99d21 24220 const file_entry *fe = lh->file_name_at (file);
6e70227d 24221
7ba99d21 24222 if (!IS_ABSOLUTE_PATH (fe->name))
8c43009f 24223 {
7ba99d21 24224 const char *dir = fe->include_dir (lh);
8c43009f 24225 if (dir != NULL)
7ba99d21 24226 return concat (dir, SLASH_STRING, fe->name, (char *) NULL);
8c43009f 24227 }
7ba99d21 24228 return xstrdup (fe->name);
6a83a1e6 24229 }
2e276125
JB
24230 else
24231 {
6a83a1e6
EZ
24232 /* The compiler produced a bogus file number. We can at least
24233 record the macro definitions made in the file, even if we
24234 won't be able to find the file by name. */
24235 char fake_name[80];
9a619af0 24236
8c042590
PM
24237 xsnprintf (fake_name, sizeof (fake_name),
24238 "<bad macro file number %d>", file);
2e276125 24239
b98664d3 24240 complaint (_("bad file number in macro information (%d)"),
6a83a1e6 24241 file);
2e276125 24242
6a83a1e6 24243 return xstrdup (fake_name);
2e276125
JB
24244 }
24245}
24246
233d95b5
JK
24247/* Return the full name of file number I in *LH's file name table.
24248 Use COMP_DIR as the name of the current directory of the
24249 compilation. The result is allocated using xmalloc; the caller is
24250 responsible for freeing it. */
24251static char *
24252file_full_name (int file, struct line_header *lh, const char *comp_dir)
24253{
24254 /* Is the file number a valid index into the line header's file name
24255 table? Remember that file numbers start with one, not zero. */
7ba99d21 24256 if (lh->is_valid_file_index (file))
233d95b5
JK
24257 {
24258 char *relative = file_file_name (file, lh);
24259
24260 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
24261 return relative;
b36cec19
PA
24262 return reconcat (relative, comp_dir, SLASH_STRING,
24263 relative, (char *) NULL);
233d95b5
JK
24264 }
24265 else
24266 return file_file_name (file, lh);
24267}
24268
2e276125
JB
24269
24270static struct macro_source_file *
804d2729
TT
24271macro_start_file (struct dwarf2_cu *cu,
24272 int file, int line,
2e276125 24273 struct macro_source_file *current_file,
43f3e411 24274 struct line_header *lh)
2e276125 24275{
233d95b5
JK
24276 /* File name relative to the compilation directory of this source file. */
24277 char *file_name = file_file_name (file, lh);
2e276125 24278
2e276125 24279 if (! current_file)
abc9d0dc 24280 {
fc474241
DE
24281 /* Note: We don't create a macro table for this compilation unit
24282 at all until we actually get a filename. */
c24bdb02 24283 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
fc474241 24284
abc9d0dc
TT
24285 /* If we have no current file, then this must be the start_file
24286 directive for the compilation unit's main source file. */
fc474241
DE
24287 current_file = macro_set_main (macro_table, file_name);
24288 macro_define_special (macro_table);
abc9d0dc 24289 }
2e276125 24290 else
233d95b5 24291 current_file = macro_include (current_file, line, file_name);
2e276125 24292
233d95b5 24293 xfree (file_name);
6e70227d 24294
2e276125
JB
24295 return current_file;
24296}
24297
2e276125
JB
24298static const char *
24299consume_improper_spaces (const char *p, const char *body)
24300{
24301 if (*p == ' ')
24302 {
b98664d3 24303 complaint (_("macro definition contains spaces "
3e43a32a 24304 "in formal argument list:\n`%s'"),
4d3c2250 24305 body);
2e276125
JB
24306
24307 while (*p == ' ')
24308 p++;
24309 }
24310
24311 return p;
24312}
24313
24314
24315static void
24316parse_macro_definition (struct macro_source_file *file, int line,
24317 const char *body)
24318{
24319 const char *p;
24320
24321 /* The body string takes one of two forms. For object-like macro
24322 definitions, it should be:
24323
24324 <macro name> " " <definition>
24325
24326 For function-like macro definitions, it should be:
24327
24328 <macro name> "() " <definition>
24329 or
24330 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
24331
24332 Spaces may appear only where explicitly indicated, and in the
24333 <definition>.
24334
24335 The Dwarf 2 spec says that an object-like macro's name is always
24336 followed by a space, but versions of GCC around March 2002 omit
6e70227d 24337 the space when the macro's definition is the empty string.
2e276125
JB
24338
24339 The Dwarf 2 spec says that there should be no spaces between the
24340 formal arguments in a function-like macro's formal argument list,
24341 but versions of GCC around March 2002 include spaces after the
24342 commas. */
24343
24344
24345 /* Find the extent of the macro name. The macro name is terminated
24346 by either a space or null character (for an object-like macro) or
24347 an opening paren (for a function-like macro). */
24348 for (p = body; *p; p++)
24349 if (*p == ' ' || *p == '(')
24350 break;
24351
24352 if (*p == ' ' || *p == '\0')
24353 {
24354 /* It's an object-like macro. */
24355 int name_len = p - body;
3f8a7804 24356 char *name = savestring (body, name_len);
2e276125
JB
24357 const char *replacement;
24358
24359 if (*p == ' ')
24360 replacement = body + name_len + 1;
24361 else
24362 {
4d3c2250 24363 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24364 replacement = body + name_len;
24365 }
6e70227d 24366
2e276125
JB
24367 macro_define_object (file, line, name, replacement);
24368
24369 xfree (name);
24370 }
24371 else if (*p == '(')
24372 {
24373 /* It's a function-like macro. */
3f8a7804 24374 char *name = savestring (body, p - body);
2e276125
JB
24375 int argc = 0;
24376 int argv_size = 1;
8d749320 24377 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
24378
24379 p++;
24380
24381 p = consume_improper_spaces (p, body);
24382
24383 /* Parse the formal argument list. */
24384 while (*p && *p != ')')
24385 {
24386 /* Find the extent of the current argument name. */
24387 const char *arg_start = p;
24388
24389 while (*p && *p != ',' && *p != ')' && *p != ' ')
24390 p++;
24391
24392 if (! *p || p == arg_start)
4d3c2250 24393 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24394 else
24395 {
24396 /* Make sure argv has room for the new argument. */
24397 if (argc >= argv_size)
24398 {
24399 argv_size *= 2;
224c3ddb 24400 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
24401 }
24402
3f8a7804 24403 argv[argc++] = savestring (arg_start, p - arg_start);
2e276125
JB
24404 }
24405
24406 p = consume_improper_spaces (p, body);
24407
24408 /* Consume the comma, if present. */
24409 if (*p == ',')
24410 {
24411 p++;
24412
24413 p = consume_improper_spaces (p, body);
24414 }
24415 }
24416
24417 if (*p == ')')
24418 {
24419 p++;
24420
24421 if (*p == ' ')
24422 /* Perfectly formed definition, no complaints. */
24423 macro_define_function (file, line, name,
6e70227d 24424 argc, (const char **) argv,
2e276125
JB
24425 p + 1);
24426 else if (*p == '\0')
24427 {
24428 /* Complain, but do define it. */
4d3c2250 24429 dwarf2_macro_malformed_definition_complaint (body);
2e276125 24430 macro_define_function (file, line, name,
6e70227d 24431 argc, (const char **) argv,
2e276125
JB
24432 p);
24433 }
24434 else
24435 /* Just complain. */
4d3c2250 24436 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24437 }
24438 else
24439 /* Just complain. */
4d3c2250 24440 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24441
24442 xfree (name);
24443 {
24444 int i;
24445
24446 for (i = 0; i < argc; i++)
24447 xfree (argv[i]);
24448 }
24449 xfree (argv);
24450 }
24451 else
4d3c2250 24452 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24453}
24454
cf2c3c16
TT
24455/* Skip some bytes from BYTES according to the form given in FORM.
24456 Returns the new pointer. */
2e276125 24457
d521ce57
TT
24458static const gdb_byte *
24459skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
24460 enum dwarf_form form,
24461 unsigned int offset_size,
24462 struct dwarf2_section_info *section)
2e276125 24463{
cf2c3c16 24464 unsigned int bytes_read;
2e276125 24465
cf2c3c16 24466 switch (form)
2e276125 24467 {
cf2c3c16
TT
24468 case DW_FORM_data1:
24469 case DW_FORM_flag:
24470 ++bytes;
24471 break;
24472
24473 case DW_FORM_data2:
24474 bytes += 2;
24475 break;
24476
24477 case DW_FORM_data4:
24478 bytes += 4;
24479 break;
24480
24481 case DW_FORM_data8:
24482 bytes += 8;
24483 break;
24484
0224619f
JK
24485 case DW_FORM_data16:
24486 bytes += 16;
24487 break;
24488
cf2c3c16
TT
24489 case DW_FORM_string:
24490 read_direct_string (abfd, bytes, &bytes_read);
24491 bytes += bytes_read;
24492 break;
24493
24494 case DW_FORM_sec_offset:
24495 case DW_FORM_strp:
36586728 24496 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
24497 bytes += offset_size;
24498 break;
24499
24500 case DW_FORM_block:
24501 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24502 bytes += bytes_read;
24503 break;
24504
24505 case DW_FORM_block1:
24506 bytes += 1 + read_1_byte (abfd, bytes);
24507 break;
24508 case DW_FORM_block2:
24509 bytes += 2 + read_2_bytes (abfd, bytes);
24510 break;
24511 case DW_FORM_block4:
24512 bytes += 4 + read_4_bytes (abfd, bytes);
24513 break;
24514
336d760d 24515 case DW_FORM_addrx:
cf2c3c16 24516 case DW_FORM_sdata:
cf532bd1 24517 case DW_FORM_strx:
cf2c3c16 24518 case DW_FORM_udata:
3019eac3
DE
24519 case DW_FORM_GNU_addr_index:
24520 case DW_FORM_GNU_str_index:
d521ce57 24521 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
24522 if (bytes == NULL)
24523 {
24524 dwarf2_section_buffer_overflow_complaint (section);
24525 return NULL;
24526 }
cf2c3c16
TT
24527 break;
24528
663c44ac
JK
24529 case DW_FORM_implicit_const:
24530 break;
24531
cf2c3c16
TT
24532 default:
24533 {
b98664d3 24534 complaint (_("invalid form 0x%x in `%s'"),
a32a8923 24535 form, get_section_name (section));
cf2c3c16
TT
24536 return NULL;
24537 }
2e276125
JB
24538 }
24539
cf2c3c16
TT
24540 return bytes;
24541}
757a13d0 24542
cf2c3c16
TT
24543/* A helper for dwarf_decode_macros that handles skipping an unknown
24544 opcode. Returns an updated pointer to the macro data buffer; or,
24545 on error, issues a complaint and returns NULL. */
757a13d0 24546
d521ce57 24547static const gdb_byte *
cf2c3c16 24548skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
24549 const gdb_byte **opcode_definitions,
24550 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
24551 bfd *abfd,
24552 unsigned int offset_size,
24553 struct dwarf2_section_info *section)
24554{
24555 unsigned int bytes_read, i;
24556 unsigned long arg;
d521ce57 24557 const gdb_byte *defn;
2e276125 24558
cf2c3c16 24559 if (opcode_definitions[opcode] == NULL)
2e276125 24560 {
b98664d3 24561 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
cf2c3c16
TT
24562 opcode);
24563 return NULL;
24564 }
2e276125 24565
cf2c3c16
TT
24566 defn = opcode_definitions[opcode];
24567 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24568 defn += bytes_read;
2e276125 24569
cf2c3c16
TT
24570 for (i = 0; i < arg; ++i)
24571 {
aead7601
SM
24572 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24573 (enum dwarf_form) defn[i], offset_size,
f664829e 24574 section);
cf2c3c16
TT
24575 if (mac_ptr == NULL)
24576 {
24577 /* skip_form_bytes already issued the complaint. */
24578 return NULL;
24579 }
24580 }
757a13d0 24581
cf2c3c16
TT
24582 return mac_ptr;
24583}
757a13d0 24584
cf2c3c16
TT
24585/* A helper function which parses the header of a macro section.
24586 If the macro section is the extended (for now called "GNU") type,
24587 then this updates *OFFSET_SIZE. Returns a pointer to just after
24588 the header, or issues a complaint and returns NULL on error. */
757a13d0 24589
d521ce57
TT
24590static const gdb_byte *
24591dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 24592 bfd *abfd,
d521ce57 24593 const gdb_byte *mac_ptr,
cf2c3c16
TT
24594 unsigned int *offset_size,
24595 int section_is_gnu)
24596{
24597 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 24598
cf2c3c16
TT
24599 if (section_is_gnu)
24600 {
24601 unsigned int version, flags;
757a13d0 24602
cf2c3c16 24603 version = read_2_bytes (abfd, mac_ptr);
0af92d60 24604 if (version != 4 && version != 5)
cf2c3c16 24605 {
b98664d3 24606 complaint (_("unrecognized version `%d' in .debug_macro section"),
cf2c3c16
TT
24607 version);
24608 return NULL;
24609 }
24610 mac_ptr += 2;
757a13d0 24611
cf2c3c16
TT
24612 flags = read_1_byte (abfd, mac_ptr);
24613 ++mac_ptr;
24614 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 24615
cf2c3c16
TT
24616 if ((flags & 2) != 0)
24617 /* We don't need the line table offset. */
24618 mac_ptr += *offset_size;
757a13d0 24619
cf2c3c16
TT
24620 /* Vendor opcode descriptions. */
24621 if ((flags & 4) != 0)
24622 {
24623 unsigned int i, count;
757a13d0 24624
cf2c3c16
TT
24625 count = read_1_byte (abfd, mac_ptr);
24626 ++mac_ptr;
24627 for (i = 0; i < count; ++i)
24628 {
24629 unsigned int opcode, bytes_read;
24630 unsigned long arg;
24631
24632 opcode = read_1_byte (abfd, mac_ptr);
24633 ++mac_ptr;
24634 opcode_definitions[opcode] = mac_ptr;
24635 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24636 mac_ptr += bytes_read;
24637 mac_ptr += arg;
24638 }
757a13d0 24639 }
cf2c3c16 24640 }
757a13d0 24641
cf2c3c16
TT
24642 return mac_ptr;
24643}
757a13d0 24644
cf2c3c16 24645/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 24646 including DW_MACRO_import. */
cf2c3c16
TT
24647
24648static void
804d2729 24649dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
ed2dc618 24650 bfd *abfd,
d521ce57 24651 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 24652 struct macro_source_file *current_file,
43f3e411 24653 struct line_header *lh,
cf2c3c16 24654 struct dwarf2_section_info *section,
36586728 24655 int section_is_gnu, int section_is_dwz,
cf2c3c16 24656 unsigned int offset_size,
8fc3fc34 24657 htab_t include_hash)
cf2c3c16 24658{
804d2729
TT
24659 struct dwarf2_per_objfile *dwarf2_per_objfile
24660 = cu->per_cu->dwarf2_per_objfile;
4d663531 24661 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
24662 enum dwarf_macro_record_type macinfo_type;
24663 int at_commandline;
d521ce57 24664 const gdb_byte *opcode_definitions[256];
757a13d0 24665
cf2c3c16
TT
24666 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24667 &offset_size, section_is_gnu);
24668 if (mac_ptr == NULL)
24669 {
24670 /* We already issued a complaint. */
24671 return;
24672 }
757a13d0
JK
24673
24674 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24675 GDB is still reading the definitions from command line. First
24676 DW_MACINFO_start_file will need to be ignored as it was already executed
24677 to create CURRENT_FILE for the main source holding also the command line
24678 definitions. On first met DW_MACINFO_start_file this flag is reset to
24679 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24680
24681 at_commandline = 1;
24682
24683 do
24684 {
24685 /* Do we at least have room for a macinfo type byte? */
24686 if (mac_ptr >= mac_end)
24687 {
f664829e 24688 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
24689 break;
24690 }
24691
aead7601 24692 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
24693 mac_ptr++;
24694
cf2c3c16
TT
24695 /* Note that we rely on the fact that the corresponding GNU and
24696 DWARF constants are the same. */
132448f8
SM
24697 DIAGNOSTIC_PUSH
24698 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
757a13d0
JK
24699 switch (macinfo_type)
24700 {
24701 /* A zero macinfo type indicates the end of the macro
24702 information. */
24703 case 0:
24704 break;
2e276125 24705
0af92d60
JK
24706 case DW_MACRO_define:
24707 case DW_MACRO_undef:
24708 case DW_MACRO_define_strp:
24709 case DW_MACRO_undef_strp:
24710 case DW_MACRO_define_sup:
24711 case DW_MACRO_undef_sup:
2e276125 24712 {
891d2f0b 24713 unsigned int bytes_read;
2e276125 24714 int line;
d521ce57 24715 const char *body;
cf2c3c16 24716 int is_define;
2e276125 24717
cf2c3c16
TT
24718 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24719 mac_ptr += bytes_read;
24720
0af92d60
JK
24721 if (macinfo_type == DW_MACRO_define
24722 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
24723 {
24724 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24725 mac_ptr += bytes_read;
24726 }
24727 else
24728 {
24729 LONGEST str_offset;
24730
24731 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24732 mac_ptr += offset_size;
2e276125 24733
0af92d60
JK
24734 if (macinfo_type == DW_MACRO_define_sup
24735 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 24736 || section_is_dwz)
36586728 24737 {
ed2dc618
SM
24738 struct dwz_file *dwz
24739 = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728 24740
ed2dc618
SM
24741 body = read_indirect_string_from_dwz (objfile,
24742 dwz, str_offset);
36586728
TT
24743 }
24744 else
ed2dc618
SM
24745 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24746 abfd, str_offset);
cf2c3c16
TT
24747 }
24748
0af92d60
JK
24749 is_define = (macinfo_type == DW_MACRO_define
24750 || macinfo_type == DW_MACRO_define_strp
24751 || macinfo_type == DW_MACRO_define_sup);
2e276125 24752 if (! current_file)
757a13d0
JK
24753 {
24754 /* DWARF violation as no main source is present. */
b98664d3 24755 complaint (_("debug info with no main source gives macro %s "
757a13d0 24756 "on line %d: %s"),
cf2c3c16
TT
24757 is_define ? _("definition") : _("undefinition"),
24758 line, body);
757a13d0
JK
24759 break;
24760 }
3e43a32a
MS
24761 if ((line == 0 && !at_commandline)
24762 || (line != 0 && at_commandline))
b98664d3 24763 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
757a13d0 24764 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 24765 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
24766 line == 0 ? _("zero") : _("non-zero"), line, body);
24767
955b06fa 24768 if (body == NULL)
7bede828 24769 {
955b06fa
SDJ
24770 /* Fedora's rpm-build's "debugedit" binary
24771 corrupted .debug_macro sections.
24772
24773 For more info, see
24774 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
24775 complaint (_("debug info gives %s invalid macro %s "
24776 "without body (corrupted?) at line %d "
24777 "on file %s"),
24778 at_commandline ? _("command-line") : _("in-file"),
24779 is_define ? _("definition") : _("undefinition"),
24780 line, current_file->filename);
7bede828 24781 }
955b06fa
SDJ
24782 else if (is_define)
24783 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
24784 else
24785 {
0af92d60
JK
24786 gdb_assert (macinfo_type == DW_MACRO_undef
24787 || macinfo_type == DW_MACRO_undef_strp
24788 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
24789 macro_undef (current_file, line, body);
24790 }
2e276125
JB
24791 }
24792 break;
24793
0af92d60 24794 case DW_MACRO_start_file:
2e276125 24795 {
891d2f0b 24796 unsigned int bytes_read;
2e276125
JB
24797 int line, file;
24798
24799 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24800 mac_ptr += bytes_read;
24801 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24802 mac_ptr += bytes_read;
24803
3e43a32a
MS
24804 if ((line == 0 && !at_commandline)
24805 || (line != 0 && at_commandline))
b98664d3 24806 complaint (_("debug info gives source %d included "
757a13d0
JK
24807 "from %s at %s line %d"),
24808 file, at_commandline ? _("command-line") : _("file"),
24809 line == 0 ? _("zero") : _("non-zero"), line);
24810
24811 if (at_commandline)
24812 {
0af92d60 24813 /* This DW_MACRO_start_file was executed in the
cf2c3c16 24814 pass one. */
757a13d0
JK
24815 at_commandline = 0;
24816 }
24817 else
804d2729
TT
24818 current_file = macro_start_file (cu, file, line, current_file,
24819 lh);
2e276125
JB
24820 }
24821 break;
24822
0af92d60 24823 case DW_MACRO_end_file:
2e276125 24824 if (! current_file)
b98664d3 24825 complaint (_("macro debug info has an unmatched "
3e43a32a 24826 "`close_file' directive"));
2e276125
JB
24827 else
24828 {
24829 current_file = current_file->included_by;
24830 if (! current_file)
24831 {
cf2c3c16 24832 enum dwarf_macro_record_type next_type;
2e276125
JB
24833
24834 /* GCC circa March 2002 doesn't produce the zero
24835 type byte marking the end of the compilation
24836 unit. Complain if it's not there, but exit no
24837 matter what. */
24838
24839 /* Do we at least have room for a macinfo type byte? */
24840 if (mac_ptr >= mac_end)
24841 {
f664829e 24842 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
24843 return;
24844 }
24845
24846 /* We don't increment mac_ptr here, so this is just
24847 a look-ahead. */
aead7601
SM
24848 next_type
24849 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24850 mac_ptr);
2e276125 24851 if (next_type != 0)
b98664d3 24852 complaint (_("no terminating 0-type entry for "
3e43a32a 24853 "macros in `.debug_macinfo' section"));
2e276125
JB
24854
24855 return;
24856 }
24857 }
24858 break;
24859
0af92d60
JK
24860 case DW_MACRO_import:
24861 case DW_MACRO_import_sup:
cf2c3c16
TT
24862 {
24863 LONGEST offset;
8fc3fc34 24864 void **slot;
a036ba48
TT
24865 bfd *include_bfd = abfd;
24866 struct dwarf2_section_info *include_section = section;
d521ce57 24867 const gdb_byte *include_mac_end = mac_end;
a036ba48 24868 int is_dwz = section_is_dwz;
d521ce57 24869 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
24870
24871 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24872 mac_ptr += offset_size;
24873
0af92d60 24874 if (macinfo_type == DW_MACRO_import_sup)
a036ba48 24875 {
ed2dc618 24876 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
a036ba48 24877
4d663531 24878 dwarf2_read_section (objfile, &dwz->macro);
a036ba48 24879
a036ba48 24880 include_section = &dwz->macro;
a32a8923 24881 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
24882 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24883 is_dwz = 1;
24884 }
24885
24886 new_mac_ptr = include_section->buffer + offset;
24887 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24888
8fc3fc34
TT
24889 if (*slot != NULL)
24890 {
24891 /* This has actually happened; see
24892 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
b98664d3 24893 complaint (_("recursive DW_MACRO_import in "
8fc3fc34
TT
24894 ".debug_macro section"));
24895 }
24896 else
24897 {
d521ce57 24898 *slot = (void *) new_mac_ptr;
36586728 24899
804d2729 24900 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
43f3e411 24901 include_mac_end, current_file, lh,
36586728 24902 section, section_is_gnu, is_dwz,
4d663531 24903 offset_size, include_hash);
8fc3fc34 24904
d521ce57 24905 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 24906 }
cf2c3c16
TT
24907 }
24908 break;
24909
2e276125 24910 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
24911 if (!section_is_gnu)
24912 {
24913 unsigned int bytes_read;
2e276125 24914
ac298888
TT
24915 /* This reads the constant, but since we don't recognize
24916 any vendor extensions, we ignore it. */
24917 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
24918 mac_ptr += bytes_read;
24919 read_direct_string (abfd, mac_ptr, &bytes_read);
24920 mac_ptr += bytes_read;
2e276125 24921
cf2c3c16
TT
24922 /* We don't recognize any vendor extensions. */
24923 break;
24924 }
24925 /* FALLTHROUGH */
24926
24927 default:
24928 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 24929 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
24930 section);
24931 if (mac_ptr == NULL)
24932 return;
24933 break;
2e276125 24934 }
132448f8 24935 DIAGNOSTIC_POP
757a13d0 24936 } while (macinfo_type != 0);
2e276125 24937}
8e19ed76 24938
cf2c3c16 24939static void
09262596 24940dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 24941 int section_is_gnu)
cf2c3c16 24942{
518817b3
SM
24943 struct dwarf2_per_objfile *dwarf2_per_objfile
24944 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 24945 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
24946 struct line_header *lh = cu->line_header;
24947 bfd *abfd;
d521ce57 24948 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
24949 struct macro_source_file *current_file = 0;
24950 enum dwarf_macro_record_type macinfo_type;
24951 unsigned int offset_size = cu->header.offset_size;
d521ce57 24952 const gdb_byte *opcode_definitions[256];
8fc3fc34 24953 void **slot;
09262596
DE
24954 struct dwarf2_section_info *section;
24955 const char *section_name;
24956
24957 if (cu->dwo_unit != NULL)
24958 {
24959 if (section_is_gnu)
24960 {
24961 section = &cu->dwo_unit->dwo_file->sections.macro;
24962 section_name = ".debug_macro.dwo";
24963 }
24964 else
24965 {
24966 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24967 section_name = ".debug_macinfo.dwo";
24968 }
24969 }
24970 else
24971 {
24972 if (section_is_gnu)
24973 {
24974 section = &dwarf2_per_objfile->macro;
24975 section_name = ".debug_macro";
24976 }
24977 else
24978 {
24979 section = &dwarf2_per_objfile->macinfo;
24980 section_name = ".debug_macinfo";
24981 }
24982 }
cf2c3c16 24983
bb5ed363 24984 dwarf2_read_section (objfile, section);
cf2c3c16
TT
24985 if (section->buffer == NULL)
24986 {
b98664d3 24987 complaint (_("missing %s section"), section_name);
cf2c3c16
TT
24988 return;
24989 }
a32a8923 24990 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
24991
24992 /* First pass: Find the name of the base filename.
24993 This filename is needed in order to process all macros whose definition
24994 (or undefinition) comes from the command line. These macros are defined
24995 before the first DW_MACINFO_start_file entry, and yet still need to be
24996 associated to the base file.
24997
24998 To determine the base file name, we scan the macro definitions until we
24999 reach the first DW_MACINFO_start_file entry. We then initialize
25000 CURRENT_FILE accordingly so that any macro definition found before the
25001 first DW_MACINFO_start_file can still be associated to the base file. */
25002
25003 mac_ptr = section->buffer + offset;
25004 mac_end = section->buffer + section->size;
25005
25006 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
25007 &offset_size, section_is_gnu);
25008 if (mac_ptr == NULL)
25009 {
25010 /* We already issued a complaint. */
25011 return;
25012 }
25013
25014 do
25015 {
25016 /* Do we at least have room for a macinfo type byte? */
25017 if (mac_ptr >= mac_end)
25018 {
25019 /* Complaint is printed during the second pass as GDB will probably
25020 stop the first pass earlier upon finding
25021 DW_MACINFO_start_file. */
25022 break;
25023 }
25024
aead7601 25025 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
25026 mac_ptr++;
25027
25028 /* Note that we rely on the fact that the corresponding GNU and
25029 DWARF constants are the same. */
132448f8
SM
25030 DIAGNOSTIC_PUSH
25031 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
cf2c3c16
TT
25032 switch (macinfo_type)
25033 {
25034 /* A zero macinfo type indicates the end of the macro
25035 information. */
25036 case 0:
25037 break;
25038
0af92d60
JK
25039 case DW_MACRO_define:
25040 case DW_MACRO_undef:
cf2c3c16
TT
25041 /* Only skip the data by MAC_PTR. */
25042 {
25043 unsigned int bytes_read;
25044
25045 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25046 mac_ptr += bytes_read;
25047 read_direct_string (abfd, mac_ptr, &bytes_read);
25048 mac_ptr += bytes_read;
25049 }
25050 break;
25051
0af92d60 25052 case DW_MACRO_start_file:
cf2c3c16
TT
25053 {
25054 unsigned int bytes_read;
25055 int line, file;
25056
25057 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25058 mac_ptr += bytes_read;
25059 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25060 mac_ptr += bytes_read;
25061
804d2729 25062 current_file = macro_start_file (cu, file, line, current_file, lh);
cf2c3c16
TT
25063 }
25064 break;
25065
0af92d60 25066 case DW_MACRO_end_file:
cf2c3c16
TT
25067 /* No data to skip by MAC_PTR. */
25068 break;
25069
0af92d60
JK
25070 case DW_MACRO_define_strp:
25071 case DW_MACRO_undef_strp:
25072 case DW_MACRO_define_sup:
25073 case DW_MACRO_undef_sup:
cf2c3c16
TT
25074 {
25075 unsigned int bytes_read;
25076
25077 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25078 mac_ptr += bytes_read;
25079 mac_ptr += offset_size;
25080 }
25081 break;
25082
0af92d60
JK
25083 case DW_MACRO_import:
25084 case DW_MACRO_import_sup:
cf2c3c16 25085 /* Note that, according to the spec, a transparent include
0af92d60 25086 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
25087 skip this opcode. */
25088 mac_ptr += offset_size;
25089 break;
25090
25091 case DW_MACINFO_vendor_ext:
25092 /* Only skip the data by MAC_PTR. */
25093 if (!section_is_gnu)
25094 {
25095 unsigned int bytes_read;
25096
25097 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
25098 mac_ptr += bytes_read;
25099 read_direct_string (abfd, mac_ptr, &bytes_read);
25100 mac_ptr += bytes_read;
25101 }
25102 /* FALLTHROUGH */
25103
25104 default:
25105 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 25106 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
25107 section);
25108 if (mac_ptr == NULL)
25109 return;
25110 break;
25111 }
132448f8 25112 DIAGNOSTIC_POP
cf2c3c16
TT
25113 } while (macinfo_type != 0 && current_file == NULL);
25114
25115 /* Second pass: Process all entries.
25116
25117 Use the AT_COMMAND_LINE flag to determine whether we are still processing
25118 command-line macro definitions/undefinitions. This flag is unset when we
25119 reach the first DW_MACINFO_start_file entry. */
25120
fc4007c9
TT
25121 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
25122 htab_eq_pointer,
25123 NULL, xcalloc, xfree));
8fc3fc34 25124 mac_ptr = section->buffer + offset;
fc4007c9 25125 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 25126 *slot = (void *) mac_ptr;
804d2729 25127 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
43f3e411 25128 current_file, lh, section,
fc4007c9
TT
25129 section_is_gnu, 0, offset_size,
25130 include_hash.get ());
cf2c3c16
TT
25131}
25132
8e19ed76 25133/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 25134 if so return true else false. */
380bca97 25135
8e19ed76 25136static int
6e5a29e1 25137attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
25138{
25139 return (attr == NULL ? 0 :
25140 attr->form == DW_FORM_block1
25141 || attr->form == DW_FORM_block2
25142 || attr->form == DW_FORM_block4
2dc7f7b3
TT
25143 || attr->form == DW_FORM_block
25144 || attr->form == DW_FORM_exprloc);
8e19ed76 25145}
4c2df51b 25146
c6a0999f
JB
25147/* Return non-zero if ATTR's value is a section offset --- classes
25148 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
25149 You may use DW_UNSND (attr) to retrieve such offsets.
25150
25151 Section 7.5.4, "Attribute Encodings", explains that no attribute
25152 may have a value that belongs to more than one of these classes; it
25153 would be ambiguous if we did, because we use the same forms for all
25154 of them. */
380bca97 25155
3690dd37 25156static int
6e5a29e1 25157attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
25158{
25159 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
25160 || attr->form == DW_FORM_data8
25161 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
25162}
25163
3690dd37
JB
25164/* Return non-zero if ATTR's value falls in the 'constant' class, or
25165 zero otherwise. When this function returns true, you can apply
25166 dwarf2_get_attr_constant_value to it.
25167
25168 However, note that for some attributes you must check
25169 attr_form_is_section_offset before using this test. DW_FORM_data4
25170 and DW_FORM_data8 are members of both the constant class, and of
25171 the classes that contain offsets into other debug sections
25172 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
25173 that, if an attribute's can be either a constant or one of the
25174 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
0224619f
JK
25175 taken as section offsets, not constants.
25176
25177 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
25178 cannot handle that. */
380bca97 25179
3690dd37 25180static int
6e5a29e1 25181attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
25182{
25183 switch (attr->form)
25184 {
25185 case DW_FORM_sdata:
25186 case DW_FORM_udata:
25187 case DW_FORM_data1:
25188 case DW_FORM_data2:
25189 case DW_FORM_data4:
25190 case DW_FORM_data8:
663c44ac 25191 case DW_FORM_implicit_const:
3690dd37
JB
25192 return 1;
25193 default:
25194 return 0;
25195 }
25196}
25197
7771576e
SA
25198
25199/* DW_ADDR is always stored already as sect_offset; despite for the forms
25200 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
25201
25202static int
6e5a29e1 25203attr_form_is_ref (const struct attribute *attr)
7771576e
SA
25204{
25205 switch (attr->form)
25206 {
25207 case DW_FORM_ref_addr:
25208 case DW_FORM_ref1:
25209 case DW_FORM_ref2:
25210 case DW_FORM_ref4:
25211 case DW_FORM_ref8:
25212 case DW_FORM_ref_udata:
25213 case DW_FORM_GNU_ref_alt:
25214 return 1;
25215 default:
25216 return 0;
25217 }
25218}
25219
3019eac3
DE
25220/* Return the .debug_loc section to use for CU.
25221 For DWO files use .debug_loc.dwo. */
25222
25223static struct dwarf2_section_info *
25224cu_debug_loc_section (struct dwarf2_cu *cu)
25225{
518817b3
SM
25226 struct dwarf2_per_objfile *dwarf2_per_objfile
25227 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 25228
3019eac3 25229 if (cu->dwo_unit)
43988095
JK
25230 {
25231 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 25232
43988095
JK
25233 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
25234 }
25235 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
25236 : &dwarf2_per_objfile->loc);
3019eac3
DE
25237}
25238
8cf6f0b1
TT
25239/* A helper function that fills in a dwarf2_loclist_baton. */
25240
25241static void
25242fill_in_loclist_baton (struct dwarf2_cu *cu,
25243 struct dwarf2_loclist_baton *baton,
ff39bb5e 25244 const struct attribute *attr)
8cf6f0b1 25245{
518817b3
SM
25246 struct dwarf2_per_objfile *dwarf2_per_objfile
25247 = cu->per_cu->dwarf2_per_objfile;
3019eac3
DE
25248 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25249
25250 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
25251
25252 baton->per_cu = cu->per_cu;
25253 gdb_assert (baton->per_cu);
25254 /* We don't know how long the location list is, but make sure we
25255 don't run off the edge of the section. */
3019eac3
DE
25256 baton->size = section->size - DW_UNSND (attr);
25257 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 25258 baton->base_address = cu->base_address;
f664829e 25259 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
25260}
25261
4c2df51b 25262static void
ff39bb5e 25263dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 25264 struct dwarf2_cu *cu, int is_block)
4c2df51b 25265{
518817b3
SM
25266 struct dwarf2_per_objfile *dwarf2_per_objfile
25267 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 25268 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 25269 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 25270
3690dd37 25271 if (attr_form_is_section_offset (attr)
3019eac3 25272 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
25273 the section. If so, fall through to the complaint in the
25274 other branch. */
3019eac3 25275 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 25276 {
0d53c4c4 25277 struct dwarf2_loclist_baton *baton;
4c2df51b 25278
8d749320 25279 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 25280
8cf6f0b1 25281 fill_in_loclist_baton (cu, baton, attr);
be391dca 25282
d00adf39 25283 if (cu->base_known == 0)
b98664d3 25284 complaint (_("Location list used without "
3e43a32a 25285 "specifying the CU base address."));
4c2df51b 25286
f1e6e072
TT
25287 SYMBOL_ACLASS_INDEX (sym) = (is_block
25288 ? dwarf2_loclist_block_index
25289 : dwarf2_loclist_index);
0d53c4c4
DJ
25290 SYMBOL_LOCATION_BATON (sym) = baton;
25291 }
25292 else
25293 {
25294 struct dwarf2_locexpr_baton *baton;
25295
8d749320 25296 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
25297 baton->per_cu = cu->per_cu;
25298 gdb_assert (baton->per_cu);
0d53c4c4
DJ
25299
25300 if (attr_form_is_block (attr))
25301 {
25302 /* Note that we're just copying the block's data pointer
25303 here, not the actual data. We're still pointing into the
6502dd73
DJ
25304 info_buffer for SYM's objfile; right now we never release
25305 that buffer, but when we do clean up properly this may
25306 need to change. */
0d53c4c4
DJ
25307 baton->size = DW_BLOCK (attr)->size;
25308 baton->data = DW_BLOCK (attr)->data;
25309 }
25310 else
25311 {
25312 dwarf2_invalid_attrib_class_complaint ("location description",
25313 SYMBOL_NATURAL_NAME (sym));
25314 baton->size = 0;
0d53c4c4 25315 }
6e70227d 25316
f1e6e072
TT
25317 SYMBOL_ACLASS_INDEX (sym) = (is_block
25318 ? dwarf2_locexpr_block_index
25319 : dwarf2_locexpr_index);
0d53c4c4
DJ
25320 SYMBOL_LOCATION_BATON (sym) = baton;
25321 }
4c2df51b 25322}
6502dd73 25323
9aa1f1e3
TT
25324/* Return the OBJFILE associated with the compilation unit CU. If CU
25325 came from a separate debuginfo file, then the master objfile is
25326 returned. */
ae0d2f24
UW
25327
25328struct objfile *
25329dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
25330{
e3b94546 25331 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
ae0d2f24
UW
25332
25333 /* Return the master objfile, so that we can report and look up the
25334 correct file containing this variable. */
25335 if (objfile->separate_debug_objfile_backlink)
25336 objfile = objfile->separate_debug_objfile_backlink;
25337
25338 return objfile;
25339}
25340
96408a79
SA
25341/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
25342 (CU_HEADERP is unused in such case) or prepare a temporary copy at
25343 CU_HEADERP first. */
25344
25345static const struct comp_unit_head *
25346per_cu_header_read_in (struct comp_unit_head *cu_headerp,
25347 struct dwarf2_per_cu_data *per_cu)
25348{
d521ce57 25349 const gdb_byte *info_ptr;
96408a79
SA
25350
25351 if (per_cu->cu)
25352 return &per_cu->cu->header;
25353
9c541725 25354 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
25355
25356 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
25357 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
25358 rcuh_kind::COMPILE);
96408a79
SA
25359
25360 return cu_headerp;
25361}
25362
ae0d2f24
UW
25363/* Return the address size given in the compilation unit header for CU. */
25364
98714339 25365int
ae0d2f24
UW
25366dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
25367{
96408a79
SA
25368 struct comp_unit_head cu_header_local;
25369 const struct comp_unit_head *cu_headerp;
c471e790 25370
96408a79
SA
25371 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25372
25373 return cu_headerp->addr_size;
ae0d2f24
UW
25374}
25375
9eae7c52
TT
25376/* Return the offset size given in the compilation unit header for CU. */
25377
25378int
25379dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
25380{
96408a79
SA
25381 struct comp_unit_head cu_header_local;
25382 const struct comp_unit_head *cu_headerp;
9c6c53f7 25383
96408a79
SA
25384 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25385
25386 return cu_headerp->offset_size;
25387}
25388
25389/* See its dwarf2loc.h declaration. */
25390
25391int
25392dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
25393{
25394 struct comp_unit_head cu_header_local;
25395 const struct comp_unit_head *cu_headerp;
25396
25397 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25398
25399 if (cu_headerp->version == 2)
25400 return cu_headerp->addr_size;
25401 else
25402 return cu_headerp->offset_size;
181cebd4
JK
25403}
25404
9aa1f1e3
TT
25405/* Return the text offset of the CU. The returned offset comes from
25406 this CU's objfile. If this objfile came from a separate debuginfo
25407 file, then the offset may be different from the corresponding
25408 offset in the parent objfile. */
25409
25410CORE_ADDR
25411dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25412{
e3b94546 25413 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
9aa1f1e3
TT
25414
25415 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25416}
25417
9a49df9d
AB
25418/* Return a type that is a generic pointer type, the size of which matches
25419 the address size given in the compilation unit header for PER_CU. */
25420static struct type *
25421dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu)
25422{
25423 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25424 struct type *void_type = objfile_type (objfile)->builtin_void;
25425 struct type *addr_type = lookup_pointer_type (void_type);
25426 int addr_size = dwarf2_per_cu_addr_size (per_cu);
25427
25428 if (TYPE_LENGTH (addr_type) == addr_size)
25429 return addr_type;
25430
25431 addr_type
25432 = dwarf2_per_cu_addr_sized_int_type (per_cu, TYPE_UNSIGNED (addr_type));
25433 return addr_type;
25434}
25435
43988095
JK
25436/* Return DWARF version number of PER_CU. */
25437
25438short
25439dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25440{
25441 return per_cu->dwarf_version;
25442}
25443
348e048f
DE
25444/* Locate the .debug_info compilation unit from CU's objfile which contains
25445 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
25446
25447static struct dwarf2_per_cu_data *
9c541725 25448dwarf2_find_containing_comp_unit (sect_offset sect_off,
36586728 25449 unsigned int offset_in_dwz,
ed2dc618 25450 struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
25451{
25452 struct dwarf2_per_cu_data *this_cu;
25453 int low, high;
25454
ae038cb0 25455 low = 0;
b76e467d 25456 high = dwarf2_per_objfile->all_comp_units.size () - 1;
ae038cb0
DJ
25457 while (high > low)
25458 {
36586728 25459 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 25460 int mid = low + (high - low) / 2;
9a619af0 25461
36586728 25462 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
36586728 25463 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 25464 || (mid_cu->is_dwz == offset_in_dwz
45b8ae0c 25465 && mid_cu->sect_off + mid_cu->length >= sect_off))
ae038cb0
DJ
25466 high = mid;
25467 else
25468 low = mid + 1;
25469 }
25470 gdb_assert (low == high);
36586728 25471 this_cu = dwarf2_per_objfile->all_comp_units[low];
45b8ae0c 25472 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 25473 {
36586728 25474 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 25475 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
25476 "offset %s [in module %s]"),
25477 sect_offset_str (sect_off),
ed2dc618 25478 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 25479
9c541725
PA
25480 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25481 <= sect_off);
ae038cb0
DJ
25482 return dwarf2_per_objfile->all_comp_units[low-1];
25483 }
25484 else
25485 {
b76e467d 25486 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
9c541725 25487 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 25488 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 25489 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
25490 return this_cu;
25491 }
25492}
25493
23745b47 25494/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 25495
fcd3b13d
SM
25496dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25497 : per_cu (per_cu_),
9068261f
AB
25498 mark (false),
25499 has_loclist (false),
25500 checked_producer (false),
25501 producer_is_gxx_lt_4_6 (false),
25502 producer_is_gcc_lt_4_3 (false),
eb77c9df 25503 producer_is_icc (false),
9068261f 25504 producer_is_icc_lt_14 (false),
c258c396 25505 producer_is_codewarrior (false),
9068261f 25506 processing_has_namespace_info (false)
93311388 25507{
fcd3b13d
SM
25508 per_cu->cu = this;
25509}
25510
25511/* Destroy a dwarf2_cu. */
25512
25513dwarf2_cu::~dwarf2_cu ()
25514{
25515 per_cu->cu = NULL;
9816fde3
JK
25516}
25517
25518/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25519
25520static void
95554aad
TT
25521prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25522 enum language pretend_language)
9816fde3
JK
25523{
25524 struct attribute *attr;
25525
25526 /* Set the language we're debugging. */
25527 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25528 if (attr)
25529 set_cu_language (DW_UNSND (attr), cu);
25530 else
9cded63f 25531 {
95554aad 25532 cu->language = pretend_language;
9cded63f
TT
25533 cu->language_defn = language_def (cu->language);
25534 }
dee91e82 25535
7d45c7c3 25536 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
25537}
25538
ae038cb0
DJ
25539/* Increase the age counter on each cached compilation unit, and free
25540 any that are too old. */
25541
25542static void
ed2dc618 25543age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
25544{
25545 struct dwarf2_per_cu_data *per_cu, **last_chain;
25546
25547 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25548 per_cu = dwarf2_per_objfile->read_in_chain;
25549 while (per_cu != NULL)
25550 {
25551 per_cu->cu->last_used ++;
b4f54984 25552 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
25553 dwarf2_mark (per_cu->cu);
25554 per_cu = per_cu->cu->read_in_chain;
25555 }
25556
25557 per_cu = dwarf2_per_objfile->read_in_chain;
25558 last_chain = &dwarf2_per_objfile->read_in_chain;
25559 while (per_cu != NULL)
25560 {
25561 struct dwarf2_per_cu_data *next_cu;
25562
25563 next_cu = per_cu->cu->read_in_chain;
25564
25565 if (!per_cu->cu->mark)
25566 {
fcd3b13d 25567 delete per_cu->cu;
ae038cb0
DJ
25568 *last_chain = next_cu;
25569 }
25570 else
25571 last_chain = &per_cu->cu->read_in_chain;
25572
25573 per_cu = next_cu;
25574 }
25575}
25576
25577/* Remove a single compilation unit from the cache. */
25578
25579static void
dee91e82 25580free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
25581{
25582 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
25583 struct dwarf2_per_objfile *dwarf2_per_objfile
25584 = target_per_cu->dwarf2_per_objfile;
ae038cb0
DJ
25585
25586 per_cu = dwarf2_per_objfile->read_in_chain;
25587 last_chain = &dwarf2_per_objfile->read_in_chain;
25588 while (per_cu != NULL)
25589 {
25590 struct dwarf2_per_cu_data *next_cu;
25591
25592 next_cu = per_cu->cu->read_in_chain;
25593
dee91e82 25594 if (per_cu == target_per_cu)
ae038cb0 25595 {
fcd3b13d 25596 delete per_cu->cu;
dee91e82 25597 per_cu->cu = NULL;
ae038cb0
DJ
25598 *last_chain = next_cu;
25599 break;
25600 }
25601 else
25602 last_chain = &per_cu->cu->read_in_chain;
25603
25604 per_cu = next_cu;
25605 }
25606}
25607
dee91e82
DE
25608/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25609 We store these in a hash table separate from the DIEs, and preserve them
25610 when the DIEs are flushed out of cache.
25611
25612 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 25613 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
25614 or the type may come from a DWO file. Furthermore, while it's more logical
25615 to use per_cu->section+offset, with Fission the section with the data is in
25616 the DWO file but we don't know that section at the point we need it.
25617 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25618 because we can enter the lookup routine, get_die_type_at_offset, from
25619 outside this file, and thus won't necessarily have PER_CU->cu.
25620 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 25621
dee91e82 25622struct dwarf2_per_cu_offset_and_type
1c379e20 25623{
dee91e82 25624 const struct dwarf2_per_cu_data *per_cu;
9c541725 25625 sect_offset sect_off;
1c379e20
DJ
25626 struct type *type;
25627};
25628
dee91e82 25629/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25630
25631static hashval_t
dee91e82 25632per_cu_offset_and_type_hash (const void *item)
1c379e20 25633{
9a3c8263
SM
25634 const struct dwarf2_per_cu_offset_and_type *ofs
25635 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 25636
9c541725 25637 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
25638}
25639
dee91e82 25640/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25641
25642static int
dee91e82 25643per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 25644{
9a3c8263
SM
25645 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25646 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25647 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25648 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 25649
dee91e82 25650 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 25651 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
25652}
25653
25654/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
25655 table if necessary. For convenience, return TYPE.
25656
25657 The DIEs reading must have careful ordering to:
85102364 25658 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
25659 reading current DIE.
25660 * Not trying to dereference contents of still incompletely read in types
25661 while reading in other DIEs.
25662 * Enable referencing still incompletely read in types just by a pointer to
25663 the type without accessing its fields.
25664
25665 Therefore caller should follow these rules:
25666 * Try to fetch any prerequisite types we may need to build this DIE type
25667 before building the type and calling set_die_type.
e71ec853 25668 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
25669 possible before fetching more types to complete the current type.
25670 * Make the type as complete as possible before fetching more types. */
1c379e20 25671
f792889a 25672static struct type *
1c379e20
DJ
25673set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25674{
518817b3
SM
25675 struct dwarf2_per_objfile *dwarf2_per_objfile
25676 = cu->per_cu->dwarf2_per_objfile;
dee91e82 25677 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 25678 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
25679 struct attribute *attr;
25680 struct dynamic_prop prop;
1c379e20 25681
b4ba55a1
JB
25682 /* For Ada types, make sure that the gnat-specific data is always
25683 initialized (if not already set). There are a few types where
25684 we should not be doing so, because the type-specific area is
25685 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25686 where the type-specific area is used to store the floatformat).
25687 But this is not a problem, because the gnat-specific information
25688 is actually not needed for these types. */
25689 if (need_gnat_info (cu)
25690 && TYPE_CODE (type) != TYPE_CODE_FUNC
25691 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
25692 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25693 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25694 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
25695 && !HAVE_GNAT_AUX_INFO (type))
25696 INIT_GNAT_SPECIFIC (type);
25697
3f2f83dd
KB
25698 /* Read DW_AT_allocated and set in type. */
25699 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25700 if (attr_form_is_block (attr))
25701 {
9a49df9d
AB
25702 struct type *prop_type
25703 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25704 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 25705 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
3f2f83dd
KB
25706 }
25707 else if (attr != NULL)
25708 {
b98664d3 25709 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 25710 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25711 sect_offset_str (die->sect_off));
3f2f83dd
KB
25712 }
25713
25714 /* Read DW_AT_associated and set in type. */
25715 attr = dwarf2_attr (die, DW_AT_associated, cu);
25716 if (attr_form_is_block (attr))
25717 {
9a49df9d
AB
25718 struct type *prop_type
25719 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25720 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 25721 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
3f2f83dd
KB
25722 }
25723 else if (attr != NULL)
25724 {
b98664d3 25725 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 25726 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25727 sect_offset_str (die->sect_off));
3f2f83dd
KB
25728 }
25729
3cdcd0ce
JB
25730 /* Read DW_AT_data_location and set in type. */
25731 attr = dwarf2_attr (die, DW_AT_data_location, cu);
9a49df9d
AB
25732 if (attr_to_dynamic_prop (attr, die, cu, &prop,
25733 dwarf2_per_cu_addr_type (cu->per_cu)))
50a82047 25734 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
3cdcd0ce 25735
dee91e82 25736 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25737 {
dee91e82
DE
25738 dwarf2_per_objfile->die_type_hash =
25739 htab_create_alloc_ex (127,
25740 per_cu_offset_and_type_hash,
25741 per_cu_offset_and_type_eq,
25742 NULL,
25743 &objfile->objfile_obstack,
25744 hashtab_obstack_allocate,
25745 dummy_obstack_deallocate);
f792889a 25746 }
1c379e20 25747
dee91e82 25748 ofs.per_cu = cu->per_cu;
9c541725 25749 ofs.sect_off = die->sect_off;
1c379e20 25750 ofs.type = type;
dee91e82
DE
25751 slot = (struct dwarf2_per_cu_offset_and_type **)
25752 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57 25753 if (*slot)
b98664d3 25754 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 25755 sect_offset_str (die->sect_off));
8d749320
SM
25756 *slot = XOBNEW (&objfile->objfile_obstack,
25757 struct dwarf2_per_cu_offset_and_type);
1c379e20 25758 **slot = ofs;
f792889a 25759 return type;
1c379e20
DJ
25760}
25761
9c541725 25762/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 25763 or return NULL if the die does not have a saved type. */
1c379e20
DJ
25764
25765static struct type *
9c541725 25766get_die_type_at_offset (sect_offset sect_off,
673bfd45 25767 struct dwarf2_per_cu_data *per_cu)
1c379e20 25768{
dee91e82 25769 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 25770 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 25771
dee91e82 25772 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25773 return NULL;
1c379e20 25774
dee91e82 25775 ofs.per_cu = per_cu;
9c541725 25776 ofs.sect_off = sect_off;
9a3c8263
SM
25777 slot = ((struct dwarf2_per_cu_offset_and_type *)
25778 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
25779 if (slot)
25780 return slot->type;
25781 else
25782 return NULL;
25783}
25784
02142a6c 25785/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
25786 or return NULL if DIE does not have a saved type. */
25787
25788static struct type *
25789get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25790{
9c541725 25791 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
25792}
25793
10b3939b
DJ
25794/* Add a dependence relationship from CU to REF_PER_CU. */
25795
25796static void
25797dwarf2_add_dependence (struct dwarf2_cu *cu,
25798 struct dwarf2_per_cu_data *ref_per_cu)
25799{
25800 void **slot;
25801
25802 if (cu->dependencies == NULL)
25803 cu->dependencies
25804 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25805 NULL, &cu->comp_unit_obstack,
25806 hashtab_obstack_allocate,
25807 dummy_obstack_deallocate);
25808
25809 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25810 if (*slot == NULL)
25811 *slot = ref_per_cu;
25812}
1c379e20 25813
f504f079
DE
25814/* Subroutine of dwarf2_mark to pass to htab_traverse.
25815 Set the mark field in every compilation unit in the
ae038cb0
DJ
25816 cache that we must keep because we are keeping CU. */
25817
10b3939b
DJ
25818static int
25819dwarf2_mark_helper (void **slot, void *data)
25820{
25821 struct dwarf2_per_cu_data *per_cu;
25822
25823 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
25824
25825 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25826 reading of the chain. As such dependencies remain valid it is not much
25827 useful to track and undo them during QUIT cleanups. */
25828 if (per_cu->cu == NULL)
25829 return 1;
25830
10b3939b
DJ
25831 if (per_cu->cu->mark)
25832 return 1;
9068261f 25833 per_cu->cu->mark = true;
10b3939b
DJ
25834
25835 if (per_cu->cu->dependencies != NULL)
25836 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25837
25838 return 1;
25839}
25840
f504f079
DE
25841/* Set the mark field in CU and in every other compilation unit in the
25842 cache that we must keep because we are keeping CU. */
25843
ae038cb0
DJ
25844static void
25845dwarf2_mark (struct dwarf2_cu *cu)
25846{
25847 if (cu->mark)
25848 return;
9068261f 25849 cu->mark = true;
10b3939b
DJ
25850 if (cu->dependencies != NULL)
25851 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
25852}
25853
25854static void
25855dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25856{
25857 while (per_cu)
25858 {
9068261f 25859 per_cu->cu->mark = false;
ae038cb0
DJ
25860 per_cu = per_cu->cu->read_in_chain;
25861 }
72bf9492
DJ
25862}
25863
72bf9492
DJ
25864/* Trivial hash function for partial_die_info: the hash value of a DIE
25865 is its offset in .debug_info for this objfile. */
25866
25867static hashval_t
25868partial_die_hash (const void *item)
25869{
9a3c8263
SM
25870 const struct partial_die_info *part_die
25871 = (const struct partial_die_info *) item;
9a619af0 25872
9c541725 25873 return to_underlying (part_die->sect_off);
72bf9492
DJ
25874}
25875
25876/* Trivial comparison function for partial_die_info structures: two DIEs
25877 are equal if they have the same offset. */
25878
25879static int
25880partial_die_eq (const void *item_lhs, const void *item_rhs)
25881{
9a3c8263
SM
25882 const struct partial_die_info *part_die_lhs
25883 = (const struct partial_die_info *) item_lhs;
25884 const struct partial_die_info *part_die_rhs
25885 = (const struct partial_die_info *) item_rhs;
9a619af0 25886
9c541725 25887 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
25888}
25889
3c3bb058
AB
25890struct cmd_list_element *set_dwarf_cmdlist;
25891struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
25892
25893static void
981a3fb3 25894set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 25895{
b4f54984 25896 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 25897 gdb_stdout);
ae038cb0
DJ
25898}
25899
25900static void
981a3fb3 25901show_dwarf_cmd (const char *args, int from_tty)
6e70227d 25902{
b4f54984 25903 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
25904}
25905
491144b5 25906bool dwarf_always_disassemble;
437afbb8 25907
437afbb8 25908static void
cd4fb1b2
SM
25909show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
25910 struct cmd_list_element *c, const char *value)
9291a0cd 25911{
cd4fb1b2
SM
25912 fprintf_filtered (file,
25913 _("Whether to always disassemble "
25914 "DWARF expressions is %s.\n"),
25915 value);
9291a0cd
TT
25916}
25917
9291a0cd 25918static void
cd4fb1b2
SM
25919show_check_physname (struct ui_file *file, int from_tty,
25920 struct cmd_list_element *c, const char *value)
9291a0cd 25921{
cd4fb1b2
SM
25922 fprintf_filtered (file,
25923 _("Whether to check \"physname\" is %s.\n"),
25924 value);
9291a0cd
TT
25925}
25926
cd4fb1b2
SM
25927void
25928_initialize_dwarf2_read (void)
9291a0cd 25929{
cd4fb1b2
SM
25930 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
25931Set DWARF specific variables.\n\
590042fc 25932Configure DWARF variables such as the cache size."),
cd4fb1b2
SM
25933 &set_dwarf_cmdlist, "maintenance set dwarf ",
25934 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 25935
cd4fb1b2 25936 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
590042fc
PW
25937Show DWARF specific variables.\n\
25938Show DWARF variables such as the cache size."),
cd4fb1b2
SM
25939 &show_dwarf_cmdlist, "maintenance show dwarf ",
25940 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 25941
cd4fb1b2
SM
25942 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25943 &dwarf_max_cache_age, _("\
25944Set the upper bound on the age of cached DWARF compilation units."), _("\
25945Show the upper bound on the age of cached DWARF compilation units."), _("\
25946A higher limit means that cached compilation units will be stored\n\
25947in memory longer, and more total memory will be used. Zero disables\n\
25948caching, which can slow down startup."),
25949 NULL,
25950 show_dwarf_max_cache_age,
25951 &set_dwarf_cmdlist,
25952 &show_dwarf_cmdlist);
156942c7 25953
cd4fb1b2
SM
25954 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
25955 &dwarf_always_disassemble, _("\
25956Set whether `info address' always disassembles DWARF expressions."), _("\
25957Show whether `info address' always disassembles DWARF expressions."), _("\
25958When enabled, DWARF expressions are always printed in an assembly-like\n\
25959syntax. When disabled, expressions will be printed in a more\n\
25960conversational style, when possible."),
25961 NULL,
25962 show_dwarf_always_disassemble,
25963 &set_dwarf_cmdlist,
25964 &show_dwarf_cmdlist);
9291a0cd 25965
cd4fb1b2
SM
25966 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25967Set debugging of the DWARF reader."), _("\
25968Show debugging of the DWARF reader."), _("\
25969When enabled (non-zero), debugging messages are printed during DWARF\n\
25970reading and symtab expansion. A value of 1 (one) provides basic\n\
25971information. A value greater than 1 provides more verbose information."),
25972 NULL,
25973 NULL,
25974 &setdebuglist, &showdebuglist);
9291a0cd 25975
cd4fb1b2
SM
25976 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25977Set debugging of the DWARF DIE reader."), _("\
25978Show debugging of the DWARF DIE reader."), _("\
25979When enabled (non-zero), DIEs are dumped after they are read in.\n\
25980The value is the maximum depth to print."),
25981 NULL,
25982 NULL,
25983 &setdebuglist, &showdebuglist);
9291a0cd 25984
cd4fb1b2
SM
25985 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25986Set debugging of the dwarf line reader."), _("\
25987Show debugging of the dwarf line reader."), _("\
25988When enabled (non-zero), line number entries are dumped as they are read in.\n\
25989A value of 1 (one) provides basic information.\n\
25990A value greater than 1 provides more verbose information."),
25991 NULL,
25992 NULL,
25993 &setdebuglist, &showdebuglist);
437afbb8 25994
cd4fb1b2
SM
25995 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25996Set cross-checking of \"physname\" code against demangler."), _("\
25997Show cross-checking of \"physname\" code against demangler."), _("\
25998When enabled, GDB's internal \"physname\" code is checked against\n\
25999the demangler."),
26000 NULL, show_check_physname,
26001 &setdebuglist, &showdebuglist);
900e11f9 26002
e615022a
DE
26003 add_setshow_boolean_cmd ("use-deprecated-index-sections",
26004 no_class, &use_deprecated_index_sections, _("\
26005Set whether to use deprecated gdb_index sections."), _("\
26006Show whether to use deprecated gdb_index sections."), _("\
26007When enabled, deprecated .gdb_index sections are used anyway.\n\
26008Normally they are ignored either because of a missing feature or\n\
26009performance issue.\n\
26010Warning: This option must be enabled before gdb reads the file."),
26011 NULL,
26012 NULL,
26013 &setlist, &showlist);
26014
f1e6e072
TT
26015 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
26016 &dwarf2_locexpr_funcs);
26017 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
26018 &dwarf2_loclist_funcs);
26019
26020 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
26021 &dwarf2_block_frame_base_locexpr_funcs);
26022 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
26023 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
26024
26025#if GDB_SELF_TEST
26026 selftests::register_test ("dw2_expand_symtabs_matching",
26027 selftests::dw2_expand_symtabs_matching::run_test);
26028#endif
6502dd73 26029}
This page took 4.992338 seconds and 4 git commands to generate.