Change map_matching_symbols to take a lookup_name_info
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
42a4f53d 3 Copyright (C) 1994-2019 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c 31#include "defs.h"
4de283e4 32#include "dwarf2read.h"
d55e5aa6
TT
33#include "dwarf-index-cache.h"
34#include "dwarf-index-common.h"
4de283e4
TT
35#include "bfd.h"
36#include "elf-bfd.h"
37#include "symtab.h"
38#include "gdbtypes.h"
39#include "objfiles.h"
d55e5aa6 40#include "dwarf2.h"
4de283e4
TT
41#include "buildsym.h"
42#include "demangle.h"
43#include "gdb-demangle.h"
44#include "expression.h"
45#include "filenames.h" /* for DOSish file names */
46#include "macrotab.h"
47#include "language.h"
48#include "complaints.h"
d55e5aa6
TT
49#include "dwarf2expr.h"
50#include "dwarf2loc.h"
4de283e4
TT
51#include "cp-support.h"
52#include "hashtab.h"
53#include "command.h"
d55e5aa6 54#include "gdbcmd.h"
4de283e4
TT
55#include "block.h"
56#include "addrmap.h"
57#include "typeprint.h"
58#include "psympriv.h"
59#include <sys/stat.h>
60#include "completer.h"
268a13a5 61#include "gdbsupport/vec.h"
4de283e4 62#include "c-lang.h"
d55e5aa6 63#include "go-lang.h"
4de283e4
TT
64#include "valprint.h"
65#include "gdbcore.h" /* for gnutarget */
66#include "gdb/gdb-index.h"
67#include <ctype.h>
68#include "gdb_bfd.h"
69#include "f-lang.h"
70#include "source.h"
268a13a5 71#include "gdbsupport/filestuff.h"
4de283e4 72#include "build-id.h"
d55e5aa6 73#include "namespace.h"
268a13a5
TT
74#include "gdbsupport/gdb_unlinker.h"
75#include "gdbsupport/function-view.h"
76#include "gdbsupport/gdb_optional.h"
77#include "gdbsupport/underlying.h"
78#include "gdbsupport/byte-vector.h"
79#include "gdbsupport/hash_enum.h"
4de283e4 80#include "filename-seen-cache.h"
b32b108a 81#include "producer.h"
4de283e4
TT
82#include <fcntl.h>
83#include <sys/types.h>
84#include <algorithm>
85#include <unordered_set>
86#include <unordered_map>
268a13a5 87#include "gdbsupport/selftest.h"
4de283e4
TT
88#include <cmath>
89#include <set>
90#include <forward_list>
c9317f21 91#include "rust-lang.h"
268a13a5 92#include "gdbsupport/pathstuff.h"
437afbb8 93
73be47f5
DE
94/* When == 1, print basic high level tracing messages.
95 When > 1, be more verbose.
b4f54984
DE
96 This is in contrast to the low level DIE reading of dwarf_die_debug. */
97static unsigned int dwarf_read_debug = 0;
45cfd468 98
d97bc12b 99/* When non-zero, dump DIEs after they are read in. */
b4f54984 100static unsigned int dwarf_die_debug = 0;
d97bc12b 101
27e0867f
DE
102/* When non-zero, dump line number entries as they are read in. */
103static unsigned int dwarf_line_debug = 0;
104
900e11f9
JK
105/* When non-zero, cross-check physname against demangler. */
106static int check_physname = 0;
107
481860b3 108/* When non-zero, do not reject deprecated .gdb_index sections. */
e615022a 109static int use_deprecated_index_sections = 0;
481860b3 110
5bfd760d 111static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
6502dd73 112
f1e6e072
TT
113/* The "aclass" indices for various kinds of computed DWARF symbols. */
114
115static int dwarf2_locexpr_index;
116static int dwarf2_loclist_index;
117static int dwarf2_locexpr_block_index;
118static int dwarf2_loclist_block_index;
119
3f563c84
PA
120/* An index into a (C++) symbol name component in a symbol name as
121 recorded in the mapped_index's symbol table. For each C++ symbol
122 in the symbol table, we record one entry for the start of each
123 component in the symbol in a table of name components, and then
124 sort the table, in order to be able to binary search symbol names,
125 ignoring leading namespaces, both completion and regular look up.
126 For example, for symbol "A::B::C", we'll have an entry that points
127 to "A::B::C", another that points to "B::C", and another for "C".
128 Note that function symbols in GDB index have no parameter
129 information, just the function/method names. You can convert a
130 name_component to a "const char *" using the
131 'mapped_index::symbol_name_at(offset_type)' method. */
132
133struct name_component
134{
135 /* Offset in the symbol name where the component starts. Stored as
136 a (32-bit) offset instead of a pointer to save memory and improve
137 locality on 64-bit architectures. */
138 offset_type name_offset;
139
140 /* The symbol's index in the symbol and constant pool tables of a
141 mapped_index. */
142 offset_type idx;
143};
144
44ed8f3e
PA
145/* Base class containing bits shared by both .gdb_index and
146 .debug_name indexes. */
147
148struct mapped_index_base
149{
22ca247e
TT
150 mapped_index_base () = default;
151 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
152
44ed8f3e
PA
153 /* The name_component table (a sorted vector). See name_component's
154 description above. */
155 std::vector<name_component> name_components;
156
157 /* How NAME_COMPONENTS is sorted. */
158 enum case_sensitivity name_components_casing;
159
160 /* Return the number of names in the symbol table. */
161 virtual size_t symbol_name_count () const = 0;
162
163 /* Get the name of the symbol at IDX in the symbol table. */
164 virtual const char *symbol_name_at (offset_type idx) const = 0;
165
166 /* Return whether the name at IDX in the symbol table should be
167 ignored. */
168 virtual bool symbol_name_slot_invalid (offset_type idx) const
169 {
170 return false;
171 }
172
173 /* Build the symbol name component sorted vector, if we haven't
174 yet. */
175 void build_name_components ();
176
177 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
178 possible matches for LN_NO_PARAMS in the name component
179 vector. */
180 std::pair<std::vector<name_component>::const_iterator,
181 std::vector<name_component>::const_iterator>
182 find_name_components_bounds (const lookup_name_info &ln_no_params) const;
183
184 /* Prevent deleting/destroying via a base class pointer. */
185protected:
186 ~mapped_index_base() = default;
187};
188
9291a0cd
TT
189/* A description of the mapped index. The file format is described in
190 a comment by the code that writes the index. */
fc898b42 191struct mapped_index final : public mapped_index_base
9291a0cd 192{
f00a2de2
PA
193 /* A slot/bucket in the symbol table hash. */
194 struct symbol_table_slot
195 {
196 const offset_type name;
197 const offset_type vec;
198 };
199
559a7a62 200 /* Index data format version. */
3063847f 201 int version = 0;
559a7a62 202
f00a2de2
PA
203 /* The address table data. */
204 gdb::array_view<const gdb_byte> address_table;
b11b1f88 205
3876f04e 206 /* The symbol table, implemented as a hash table. */
f00a2de2 207 gdb::array_view<symbol_table_slot> symbol_table;
b11b1f88 208
9291a0cd 209 /* A pointer to the constant pool. */
3063847f 210 const char *constant_pool = nullptr;
3f563c84 211
44ed8f3e
PA
212 bool symbol_name_slot_invalid (offset_type idx) const override
213 {
214 const auto &bucket = this->symbol_table[idx];
9ab08412 215 return bucket.name == 0 && bucket.vec == 0;
44ed8f3e 216 }
5c58de74 217
3f563c84
PA
218 /* Convenience method to get at the name of the symbol at IDX in the
219 symbol table. */
44ed8f3e 220 const char *symbol_name_at (offset_type idx) const override
f00a2de2 221 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
5c58de74 222
44ed8f3e
PA
223 size_t symbol_name_count () const override
224 { return this->symbol_table.size (); }
9291a0cd
TT
225};
226
927aa2e7
JK
227/* A description of the mapped .debug_names.
228 Uninitialized map has CU_COUNT 0. */
fc898b42 229struct mapped_debug_names final : public mapped_index_base
927aa2e7 230{
ed2dc618
SM
231 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
232 : dwarf2_per_objfile (dwarf2_per_objfile_)
233 {}
234
235 struct dwarf2_per_objfile *dwarf2_per_objfile;
927aa2e7
JK
236 bfd_endian dwarf5_byte_order;
237 bool dwarf5_is_dwarf64;
238 bool augmentation_is_gdb;
239 uint8_t offset_size;
240 uint32_t cu_count = 0;
241 uint32_t tu_count, bucket_count, name_count;
242 const gdb_byte *cu_table_reordered, *tu_table_reordered;
243 const uint32_t *bucket_table_reordered, *hash_table_reordered;
244 const gdb_byte *name_table_string_offs_reordered;
245 const gdb_byte *name_table_entry_offs_reordered;
246 const gdb_byte *entry_pool;
247
248 struct index_val
249 {
250 ULONGEST dwarf_tag;
251 struct attr
252 {
253 /* Attribute name DW_IDX_*. */
254 ULONGEST dw_idx;
255
256 /* Attribute form DW_FORM_*. */
257 ULONGEST form;
258
259 /* Value if FORM is DW_FORM_implicit_const. */
260 LONGEST implicit_const;
261 };
262 std::vector<attr> attr_vec;
263 };
264
265 std::unordered_map<ULONGEST, index_val> abbrev_map;
266
267 const char *namei_to_name (uint32_t namei) const;
44ed8f3e
PA
268
269 /* Implementation of the mapped_index_base virtual interface, for
270 the name_components cache. */
271
272 const char *symbol_name_at (offset_type idx) const override
273 { return namei_to_name (idx); }
274
275 size_t symbol_name_count () const override
276 { return this->name_count; }
927aa2e7
JK
277};
278
cd4fb1b2 279/* See dwarf2read.h. */
ed2dc618 280
cd4fb1b2 281dwarf2_per_objfile *
ed2dc618
SM
282get_dwarf2_per_objfile (struct objfile *objfile)
283{
5bfd760d 284 return dwarf2_objfile_data_key.get (objfile);
ed2dc618 285}
c906108c 286
251d32d9 287/* Default names of the debugging sections. */
c906108c 288
233a11ab
CS
289/* Note that if the debugging section has been compressed, it might
290 have a name like .zdebug_info. */
291
9cdd5dbd
DE
292static const struct dwarf2_debug_sections dwarf2_elf_names =
293{
251d32d9
TG
294 { ".debug_info", ".zdebug_info" },
295 { ".debug_abbrev", ".zdebug_abbrev" },
296 { ".debug_line", ".zdebug_line" },
297 { ".debug_loc", ".zdebug_loc" },
43988095 298 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 299 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 300 { ".debug_macro", ".zdebug_macro" },
251d32d9 301 { ".debug_str", ".zdebug_str" },
43988095 302 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 303 { ".debug_ranges", ".zdebug_ranges" },
43988095 304 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 305 { ".debug_types", ".zdebug_types" },
3019eac3 306 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
307 { ".debug_frame", ".zdebug_frame" },
308 { ".eh_frame", NULL },
24d3216f 309 { ".gdb_index", ".zgdb_index" },
927aa2e7
JK
310 { ".debug_names", ".zdebug_names" },
311 { ".debug_aranges", ".zdebug_aranges" },
24d3216f 312 23
251d32d9 313};
c906108c 314
80626a55 315/* List of DWO/DWP sections. */
3019eac3 316
80626a55 317static const struct dwop_section_names
3019eac3
DE
318{
319 struct dwarf2_section_names abbrev_dwo;
320 struct dwarf2_section_names info_dwo;
321 struct dwarf2_section_names line_dwo;
322 struct dwarf2_section_names loc_dwo;
43988095 323 struct dwarf2_section_names loclists_dwo;
09262596
DE
324 struct dwarf2_section_names macinfo_dwo;
325 struct dwarf2_section_names macro_dwo;
3019eac3
DE
326 struct dwarf2_section_names str_dwo;
327 struct dwarf2_section_names str_offsets_dwo;
328 struct dwarf2_section_names types_dwo;
80626a55
DE
329 struct dwarf2_section_names cu_index;
330 struct dwarf2_section_names tu_index;
3019eac3 331}
80626a55 332dwop_section_names =
3019eac3
DE
333{
334 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
335 { ".debug_info.dwo", ".zdebug_info.dwo" },
336 { ".debug_line.dwo", ".zdebug_line.dwo" },
337 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 338 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
339 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
340 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
341 { ".debug_str.dwo", ".zdebug_str.dwo" },
342 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
343 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
344 { ".debug_cu_index", ".zdebug_cu_index" },
345 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
346};
347
c906108c
SS
348/* local data types */
349
107d2387
AC
350/* The data in a compilation unit header, after target2host
351 translation, looks like this. */
c906108c 352struct comp_unit_head
a738430d 353{
c764a876 354 unsigned int length;
a738430d 355 short version;
a738430d
MK
356 unsigned char addr_size;
357 unsigned char signed_addr_p;
9c541725 358 sect_offset abbrev_sect_off;
57349743 359
a738430d
MK
360 /* Size of file offsets; either 4 or 8. */
361 unsigned int offset_size;
57349743 362
a738430d
MK
363 /* Size of the length field; either 4 or 12. */
364 unsigned int initial_length_size;
57349743 365
43988095
JK
366 enum dwarf_unit_type unit_type;
367
a738430d
MK
368 /* Offset to the first byte of this compilation unit header in the
369 .debug_info section, for resolving relative reference dies. */
9c541725 370 sect_offset sect_off;
57349743 371
d00adf39
DE
372 /* Offset to first die in this cu from the start of the cu.
373 This will be the first byte following the compilation unit header. */
9c541725 374 cu_offset first_die_cu_offset;
43988095 375
a084a2a6
AT
376
377 /* 64-bit signature of this unit. For type units, it denotes the signature of
378 the type (DW_UT_type in DWARF 4, additionally DW_UT_split_type in DWARF 5).
379 Also used in DWARF 5, to denote the dwo id when the unit type is
380 DW_UT_skeleton or DW_UT_split_compile. */
43988095
JK
381 ULONGEST signature;
382
383 /* For types, offset in the type's DIE of the type defined by this TU. */
9c541725 384 cu_offset type_cu_offset_in_tu;
a738430d 385};
c906108c 386
3da10d80
KS
387/* Type used for delaying computation of method physnames.
388 See comments for compute_delayed_physnames. */
389struct delayed_method_info
390{
391 /* The type to which the method is attached, i.e., its parent class. */
392 struct type *type;
393
394 /* The index of the method in the type's function fieldlists. */
395 int fnfield_index;
396
397 /* The index of the method in the fieldlist. */
398 int index;
399
400 /* The name of the DIE. */
401 const char *name;
402
403 /* The DIE associated with this method. */
404 struct die_info *die;
405};
406
e7c27a73
DJ
407/* Internal state when decoding a particular compilation unit. */
408struct dwarf2_cu
409{
fcd3b13d
SM
410 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
411 ~dwarf2_cu ();
412
413 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
414
c24bdb02
KS
415 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
416 Create the set of symtabs used by this TU, or if this TU is sharing
417 symtabs with another TU and the symtabs have already been created
418 then restore those symtabs in the line header.
419 We don't need the pc/line-number mapping for type units. */
420 void setup_type_unit_groups (struct die_info *die);
421
422 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
423 buildsym_compunit constructor. */
424 struct compunit_symtab *start_symtab (const char *name,
425 const char *comp_dir,
426 CORE_ADDR low_pc);
427
428 /* Reset the builder. */
429 void reset_builder () { m_builder.reset (); }
430
d00adf39 431 /* The header of the compilation unit. */
fcd3b13d 432 struct comp_unit_head header {};
e142c38c 433
d00adf39 434 /* Base address of this compilation unit. */
fcd3b13d 435 CORE_ADDR base_address = 0;
d00adf39
DE
436
437 /* Non-zero if base_address has been set. */
fcd3b13d 438 int base_known = 0;
d00adf39 439
e142c38c 440 /* The language we are debugging. */
fcd3b13d
SM
441 enum language language = language_unknown;
442 const struct language_defn *language_defn = nullptr;
e142c38c 443
fcd3b13d 444 const char *producer = nullptr;
b0f35d58 445
c24bdb02 446private:
804d2729
TT
447 /* The symtab builder for this CU. This is only non-NULL when full
448 symbols are being read. */
c24bdb02 449 std::unique_ptr<buildsym_compunit> m_builder;
804d2729 450
c24bdb02 451public:
e142c38c
DJ
452 /* The generic symbol table building routines have separate lists for
453 file scope symbols and all all other scopes (local scopes). So
454 we need to select the right one to pass to add_symbol_to_list().
455 We do it by keeping a pointer to the correct list in list_in_scope.
456
457 FIXME: The original dwarf code just treated the file scope as the
458 first local scope, and all other local scopes as nested local
459 scopes, and worked fine. Check to see if we really need to
460 distinguish these in buildsym.c. */
fcd3b13d 461 struct pending **list_in_scope = nullptr;
e142c38c 462
b64f50a1
JK
463 /* Hash table holding all the loaded partial DIEs
464 with partial_die->offset.SECT_OFF as hash. */
fcd3b13d 465 htab_t partial_dies = nullptr;
72bf9492
DJ
466
467 /* Storage for things with the same lifetime as this read-in compilation
468 unit, including partial DIEs. */
fcd3b13d 469 auto_obstack comp_unit_obstack;
72bf9492 470
ae038cb0
DJ
471 /* When multiple dwarf2_cu structures are living in memory, this field
472 chains them all together, so that they can be released efficiently.
473 We will probably also want a generation counter so that most-recently-used
474 compilation units are cached... */
fcd3b13d 475 struct dwarf2_per_cu_data *read_in_chain = nullptr;
ae038cb0 476
69d751e3 477 /* Backlink to our per_cu entry. */
ae038cb0
DJ
478 struct dwarf2_per_cu_data *per_cu;
479
480 /* How many compilation units ago was this CU last referenced? */
fcd3b13d 481 int last_used = 0;
ae038cb0 482
b64f50a1
JK
483 /* A hash table of DIE cu_offset for following references with
484 die_info->offset.sect_off as hash. */
fcd3b13d 485 htab_t die_hash = nullptr;
10b3939b
DJ
486
487 /* Full DIEs if read in. */
fcd3b13d 488 struct die_info *dies = nullptr;
10b3939b
DJ
489
490 /* A set of pointers to dwarf2_per_cu_data objects for compilation
491 units referenced by this one. Only set during full symbol processing;
492 partial symbol tables do not have dependencies. */
fcd3b13d 493 htab_t dependencies = nullptr;
10b3939b 494
cb1df416 495 /* Header data from the line table, during full symbol processing. */
fcd3b13d 496 struct line_header *line_header = nullptr;
4c8aa72d
PA
497 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
498 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
499 this is the DW_TAG_compile_unit die for this CU. We'll hold on
500 to the line header as long as this DIE is being processed. See
501 process_die_scope. */
fcd3b13d 502 die_info *line_header_die_owner = nullptr;
cb1df416 503
3da10d80
KS
504 /* A list of methods which need to have physnames computed
505 after all type information has been read. */
c89b44cd 506 std::vector<delayed_method_info> method_list;
3da10d80 507
96408a79 508 /* To be copied to symtab->call_site_htab. */
fcd3b13d 509 htab_t call_site_htab = nullptr;
96408a79 510
034e5797
DE
511 /* Non-NULL if this CU came from a DWO file.
512 There is an invariant here that is important to remember:
513 Except for attributes copied from the top level DIE in the "main"
514 (or "stub") file in preparation for reading the DWO file
515 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
516 Either there isn't a DWO file (in which case this is NULL and the point
517 is moot), or there is and either we're not going to read it (in which
518 case this is NULL) or there is and we are reading it (in which case this
519 is non-NULL). */
fcd3b13d 520 struct dwo_unit *dwo_unit = nullptr;
3019eac3
DE
521
522 /* The DW_AT_addr_base attribute if present, zero otherwise
523 (zero is a valid value though).
1dbab08b 524 Note this value comes from the Fission stub CU/TU's DIE. */
fcd3b13d 525 ULONGEST addr_base = 0;
3019eac3 526
2e3cf129
DE
527 /* The DW_AT_ranges_base attribute if present, zero otherwise
528 (zero is a valid value though).
1dbab08b 529 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 530 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
531 be used without needing to know whether DWO files are in use or not.
532 N.B. This does not apply to DW_AT_ranges appearing in
533 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
534 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
535 DW_AT_ranges_base *would* have to be applied, and we'd have to care
536 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
fcd3b13d 537 ULONGEST ranges_base = 0;
2e3cf129 538
c9317f21
TT
539 /* When reading debug info generated by older versions of rustc, we
540 have to rewrite some union types to be struct types with a
541 variant part. This rewriting must be done after the CU is fully
542 read in, because otherwise at the point of rewriting some struct
543 type might not have been fully processed. So, we keep a list of
544 all such types here and process them after expansion. */
545 std::vector<struct type *> rust_unions;
546
ae038cb0 547 /* Mark used when releasing cached dies. */
9068261f 548 bool mark : 1;
ae038cb0 549
8be455d7
JK
550 /* This CU references .debug_loc. See the symtab->locations_valid field.
551 This test is imperfect as there may exist optimized debug code not using
552 any location list and still facing inlining issues if handled as
553 unoptimized code. For a future better test see GCC PR other/32998. */
9068261f 554 bool has_loclist : 1;
ba919b58 555
9068261f 556 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
1b80a9fa
JK
557 if all the producer_is_* fields are valid. This information is cached
558 because profiling CU expansion showed excessive time spent in
559 producer_is_gxx_lt_4_6. */
9068261f
AB
560 bool checked_producer : 1;
561 bool producer_is_gxx_lt_4_6 : 1;
562 bool producer_is_gcc_lt_4_3 : 1;
eb77c9df 563 bool producer_is_icc : 1;
9068261f 564 bool producer_is_icc_lt_14 : 1;
c258c396 565 bool producer_is_codewarrior : 1;
4d4ec4e5 566
9068261f 567 /* When true, the file that we're processing is known to have
4d4ec4e5
TT
568 debugging info for C++ namespaces. GCC 3.3.x did not produce
569 this information, but later versions do. */
570
9068261f 571 bool processing_has_namespace_info : 1;
d590ff25
YQ
572
573 struct partial_die_info *find_partial_die (sect_offset sect_off);
c24bdb02
KS
574
575 /* If this CU was inherited by another CU (via specification,
576 abstract_origin, etc), this is the ancestor CU. */
577 dwarf2_cu *ancestor;
578
579 /* Get the buildsym_compunit for this CU. */
580 buildsym_compunit *get_builder ()
581 {
582 /* If this CU has a builder associated with it, use that. */
583 if (m_builder != nullptr)
584 return m_builder.get ();
585
586 /* Otherwise, search ancestors for a valid builder. */
587 if (ancestor != nullptr)
588 return ancestor->get_builder ();
589
590 return nullptr;
591 }
e7c27a73
DJ
592};
593
094b34ac
DE
594/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
595 This includes type_unit_group and quick_file_names. */
596
597struct stmt_list_hash
598{
599 /* The DWO unit this table is from or NULL if there is none. */
600 struct dwo_unit *dwo_unit;
601
602 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 603 sect_offset line_sect_off;
094b34ac
DE
604};
605
f4dc4d17
DE
606/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
607 an object of this type. */
608
609struct type_unit_group
610{
0186c6a7 611 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
612 To simplify things we create an artificial CU that "includes" all the
613 type units using this stmt_list so that the rest of the code still has
614 a "per_cu" handle on the symtab.
615 This PER_CU is recognized by having no section. */
8a0459fd 616#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
617 struct dwarf2_per_cu_data per_cu;
618
0186c6a7
DE
619 /* The TUs that share this DW_AT_stmt_list entry.
620 This is added to while parsing type units to build partial symtabs,
621 and is deleted afterwards and not used again. */
622 VEC (sig_type_ptr) *tus;
f4dc4d17 623
43f3e411 624 /* The compunit symtab.
094b34ac 625 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
626 so we create an essentially anonymous symtab as the compunit symtab. */
627 struct compunit_symtab *compunit_symtab;
f4dc4d17 628
094b34ac
DE
629 /* The data used to construct the hash key. */
630 struct stmt_list_hash hash;
f4dc4d17
DE
631
632 /* The number of symtabs from the line header.
633 The value here must match line_header.num_file_names. */
634 unsigned int num_symtabs;
635
636 /* The symbol tables for this TU (obtained from the files listed in
637 DW_AT_stmt_list).
638 WARNING: The order of entries here must match the order of entries
639 in the line header. After the first TU using this type_unit_group, the
640 line header for the subsequent TUs is recreated from this. This is done
641 because we need to use the same symtabs for each TU using the same
642 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
643 there's no guarantee the line header doesn't have duplicate entries. */
644 struct symtab **symtabs;
645};
646
73869dc2 647/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
648
649struct dwo_sections
650{
651 struct dwarf2_section_info abbrev;
3019eac3
DE
652 struct dwarf2_section_info line;
653 struct dwarf2_section_info loc;
43988095 654 struct dwarf2_section_info loclists;
09262596
DE
655 struct dwarf2_section_info macinfo;
656 struct dwarf2_section_info macro;
3019eac3
DE
657 struct dwarf2_section_info str;
658 struct dwarf2_section_info str_offsets;
80626a55
DE
659 /* In the case of a virtual DWO file, these two are unused. */
660 struct dwarf2_section_info info;
fd5866f6 661 std::vector<dwarf2_section_info> types;
3019eac3
DE
662};
663
c88ee1f0 664/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
665
666struct dwo_unit
667{
668 /* Backlink to the containing struct dwo_file. */
669 struct dwo_file *dwo_file;
670
671 /* The "id" that distinguishes this CU/TU.
672 .debug_info calls this "dwo_id", .debug_types calls this "signature".
673 Since signatures came first, we stick with it for consistency. */
674 ULONGEST signature;
675
676 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 677 struct dwarf2_section_info *section;
3019eac3 678
9c541725
PA
679 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
680 sect_offset sect_off;
3019eac3
DE
681 unsigned int length;
682
683 /* For types, offset in the type's DIE of the type defined by this TU. */
684 cu_offset type_offset_in_tu;
685};
686
73869dc2
DE
687/* include/dwarf2.h defines the DWP section codes.
688 It defines a max value but it doesn't define a min value, which we
689 use for error checking, so provide one. */
690
691enum dwp_v2_section_ids
692{
693 DW_SECT_MIN = 1
694};
695
80626a55 696/* Data for one DWO file.
57d63ce2
DE
697
698 This includes virtual DWO files (a virtual DWO file is a DWO file as it
699 appears in a DWP file). DWP files don't really have DWO files per se -
700 comdat folding of types "loses" the DWO file they came from, and from
701 a high level view DWP files appear to contain a mass of random types.
702 However, to maintain consistency with the non-DWP case we pretend DWP
703 files contain virtual DWO files, and we assign each TU with one virtual
704 DWO file (generally based on the line and abbrev section offsets -
705 a heuristic that seems to work in practice). */
3019eac3
DE
706
707struct dwo_file
708{
51ac9db5
SM
709 dwo_file () = default;
710 DISABLE_COPY_AND_ASSIGN (dwo_file);
711
0ac5b59e 712 /* The DW_AT_GNU_dwo_name attribute.
80626a55
DE
713 For virtual DWO files the name is constructed from the section offsets
714 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
715 from related CU+TUs. */
51ac9db5 716 const char *dwo_name = nullptr;
0ac5b59e
DE
717
718 /* The DW_AT_comp_dir attribute. */
51ac9db5 719 const char *comp_dir = nullptr;
3019eac3 720
80626a55
DE
721 /* The bfd, when the file is open. Otherwise this is NULL.
722 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
fb1eb2f9 723 gdb_bfd_ref_ptr dbfd;
3019eac3 724
73869dc2
DE
725 /* The sections that make up this DWO file.
726 Remember that for virtual DWO files in DWP V2, these are virtual
727 sections (for lack of a better name). */
51ac9db5 728 struct dwo_sections sections {};
3019eac3 729
33c5cd75
DB
730 /* The CUs in the file.
731 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
732 an extension to handle LLVM's Link Time Optimization output (where
733 multiple source files may be compiled into a single object/dwo pair). */
51ac9db5 734 htab_t cus {};
3019eac3
DE
735
736 /* Table of TUs in the file.
737 Each element is a struct dwo_unit. */
51ac9db5 738 htab_t tus {};
3019eac3
DE
739};
740
80626a55
DE
741/* These sections are what may appear in a DWP file. */
742
743struct dwp_sections
744{
73869dc2 745 /* These are used by both DWP version 1 and 2. */
80626a55
DE
746 struct dwarf2_section_info str;
747 struct dwarf2_section_info cu_index;
748 struct dwarf2_section_info tu_index;
73869dc2
DE
749
750 /* These are only used by DWP version 2 files.
751 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
752 sections are referenced by section number, and are not recorded here.
753 In DWP version 2 there is at most one copy of all these sections, each
754 section being (effectively) comprised of the concatenation of all of the
755 individual sections that exist in the version 1 format.
756 To keep the code simple we treat each of these concatenated pieces as a
757 section itself (a virtual section?). */
758 struct dwarf2_section_info abbrev;
759 struct dwarf2_section_info info;
760 struct dwarf2_section_info line;
761 struct dwarf2_section_info loc;
762 struct dwarf2_section_info macinfo;
763 struct dwarf2_section_info macro;
764 struct dwarf2_section_info str_offsets;
765 struct dwarf2_section_info types;
80626a55
DE
766};
767
73869dc2
DE
768/* These sections are what may appear in a virtual DWO file in DWP version 1.
769 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 770
73869dc2 771struct virtual_v1_dwo_sections
80626a55
DE
772{
773 struct dwarf2_section_info abbrev;
774 struct dwarf2_section_info line;
775 struct dwarf2_section_info loc;
776 struct dwarf2_section_info macinfo;
777 struct dwarf2_section_info macro;
778 struct dwarf2_section_info str_offsets;
779 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 780 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
781 struct dwarf2_section_info info_or_types;
782};
783
73869dc2
DE
784/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
785 In version 2, the sections of the DWO files are concatenated together
786 and stored in one section of that name. Thus each ELF section contains
787 several "virtual" sections. */
788
789struct virtual_v2_dwo_sections
790{
791 bfd_size_type abbrev_offset;
792 bfd_size_type abbrev_size;
793
794 bfd_size_type line_offset;
795 bfd_size_type line_size;
796
797 bfd_size_type loc_offset;
798 bfd_size_type loc_size;
799
800 bfd_size_type macinfo_offset;
801 bfd_size_type macinfo_size;
802
803 bfd_size_type macro_offset;
804 bfd_size_type macro_size;
805
806 bfd_size_type str_offsets_offset;
807 bfd_size_type str_offsets_size;
808
809 /* Each DWP hash table entry records one CU or one TU.
810 That is recorded here, and copied to dwo_unit.section. */
811 bfd_size_type info_or_types_offset;
812 bfd_size_type info_or_types_size;
813};
814
80626a55
DE
815/* Contents of DWP hash tables. */
816
817struct dwp_hash_table
818{
73869dc2 819 uint32_t version, nr_columns;
80626a55 820 uint32_t nr_units, nr_slots;
73869dc2
DE
821 const gdb_byte *hash_table, *unit_table;
822 union
823 {
824 struct
825 {
826 const gdb_byte *indices;
827 } v1;
828 struct
829 {
830 /* This is indexed by column number and gives the id of the section
831 in that column. */
832#define MAX_NR_V2_DWO_SECTIONS \
833 (1 /* .debug_info or .debug_types */ \
834 + 1 /* .debug_abbrev */ \
835 + 1 /* .debug_line */ \
836 + 1 /* .debug_loc */ \
837 + 1 /* .debug_str_offsets */ \
838 + 1 /* .debug_macro or .debug_macinfo */)
839 int section_ids[MAX_NR_V2_DWO_SECTIONS];
840 const gdb_byte *offsets;
841 const gdb_byte *sizes;
842 } v2;
843 } section_pool;
80626a55
DE
844};
845
846/* Data for one DWP file. */
847
848struct dwp_file
849{
400174b1
TT
850 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
851 : name (name_),
852 dbfd (std::move (abfd))
853 {
854 }
855
80626a55
DE
856 /* Name of the file. */
857 const char *name;
858
73869dc2 859 /* File format version. */
400174b1 860 int version = 0;
73869dc2 861
93417882 862 /* The bfd. */
400174b1 863 gdb_bfd_ref_ptr dbfd;
80626a55
DE
864
865 /* Section info for this file. */
400174b1 866 struct dwp_sections sections {};
80626a55 867
57d63ce2 868 /* Table of CUs in the file. */
400174b1 869 const struct dwp_hash_table *cus = nullptr;
80626a55
DE
870
871 /* Table of TUs in the file. */
400174b1 872 const struct dwp_hash_table *tus = nullptr;
80626a55 873
19ac8c2e 874 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
400174b1
TT
875 htab_t loaded_cus {};
876 htab_t loaded_tus {};
80626a55 877
73869dc2
DE
878 /* Table to map ELF section numbers to their sections.
879 This is only needed for the DWP V1 file format. */
400174b1
TT
880 unsigned int num_sections = 0;
881 asection **elf_sections = nullptr;
80626a55
DE
882};
883
0963b4bd
MS
884/* Struct used to pass misc. parameters to read_die_and_children, et
885 al. which are used for both .debug_info and .debug_types dies.
886 All parameters here are unchanging for the life of the call. This
dee91e82 887 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
888
889struct die_reader_specs
890{
a32a8923 891 /* The bfd of die_section. */
93311388
DE
892 bfd* abfd;
893
894 /* The CU of the DIE we are parsing. */
895 struct dwarf2_cu *cu;
896
80626a55 897 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
898 struct dwo_file *dwo_file;
899
dee91e82 900 /* The section the die comes from.
3019eac3 901 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
902 struct dwarf2_section_info *die_section;
903
904 /* die_section->buffer. */
d521ce57 905 const gdb_byte *buffer;
f664829e
DE
906
907 /* The end of the buffer. */
908 const gdb_byte *buffer_end;
a2ce51a0
DE
909
910 /* The value of the DW_AT_comp_dir attribute. */
911 const char *comp_dir;
685af9cd
TT
912
913 /* The abbreviation table to use when reading the DIEs. */
914 struct abbrev_table *abbrev_table;
93311388
DE
915};
916
fd820528 917/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82 918typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
d521ce57 919 const gdb_byte *info_ptr,
dee91e82
DE
920 struct die_info *comp_unit_die,
921 int has_children,
922 void *data);
923
ecfb656c
PA
924/* A 1-based directory index. This is a strong typedef to prevent
925 accidentally using a directory index as a 0-based index into an
926 array/vector. */
927enum class dir_index : unsigned int {};
928
929/* Likewise, a 1-based file name index. */
930enum class file_name_index : unsigned int {};
931
52059ffd
TT
932struct file_entry
933{
fff8551c
PA
934 file_entry () = default;
935
ecfb656c 936 file_entry (const char *name_, dir_index d_index_,
fff8551c
PA
937 unsigned int mod_time_, unsigned int length_)
938 : name (name_),
ecfb656c 939 d_index (d_index_),
fff8551c
PA
940 mod_time (mod_time_),
941 length (length_)
942 {}
943
ecfb656c
PA
944 /* Return the include directory at D_INDEX stored in LH. Returns
945 NULL if D_INDEX is out of bounds. */
8c43009f
PA
946 const char *include_dir (const line_header *lh) const;
947
fff8551c
PA
948 /* The file name. Note this is an observing pointer. The memory is
949 owned by debug_line_buffer. */
950 const char *name {};
951
8c43009f 952 /* The directory index (1-based). */
ecfb656c 953 dir_index d_index {};
fff8551c
PA
954
955 unsigned int mod_time {};
956
957 unsigned int length {};
958
959 /* True if referenced by the Line Number Program. */
960 bool included_p {};
961
83769d0b 962 /* The associated symbol table, if any. */
fff8551c 963 struct symtab *symtab {};
52059ffd
TT
964};
965
debd256d
JB
966/* The line number information for a compilation unit (found in the
967 .debug_line section) begins with a "statement program header",
968 which contains the following information. */
969struct line_header
970{
fff8551c
PA
971 line_header ()
972 : offset_in_dwz {}
973 {}
974
975 /* Add an entry to the include directory table. */
976 void add_include_dir (const char *include_dir);
977
978 /* Add an entry to the file name table. */
ecfb656c 979 void add_file_name (const char *name, dir_index d_index,
fff8551c
PA
980 unsigned int mod_time, unsigned int length);
981
ecfb656c 982 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
8c43009f 983 is out of bounds. */
ecfb656c 984 const char *include_dir_at (dir_index index) const
8c43009f 985 {
ecfb656c
PA
986 /* Convert directory index number (1-based) to vector index
987 (0-based). */
988 size_t vec_index = to_underlying (index) - 1;
989
990 if (vec_index >= include_dirs.size ())
8c43009f 991 return NULL;
ecfb656c 992 return include_dirs[vec_index];
8c43009f
PA
993 }
994
ecfb656c 995 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
8c43009f 996 is out of bounds. */
ecfb656c 997 file_entry *file_name_at (file_name_index index)
8c43009f 998 {
ecfb656c
PA
999 /* Convert file name index number (1-based) to vector index
1000 (0-based). */
1001 size_t vec_index = to_underlying (index) - 1;
1002
1003 if (vec_index >= file_names.size ())
fff8551c 1004 return NULL;
ecfb656c 1005 return &file_names[vec_index];
fff8551c
PA
1006 }
1007
527f3840 1008 /* Offset of line number information in .debug_line section. */
9c541725 1009 sect_offset sect_off {};
527f3840
JK
1010
1011 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
fff8551c
PA
1012 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1013
1014 unsigned int total_length {};
1015 unsigned short version {};
1016 unsigned int header_length {};
1017 unsigned char minimum_instruction_length {};
1018 unsigned char maximum_ops_per_instruction {};
1019 unsigned char default_is_stmt {};
1020 int line_base {};
1021 unsigned char line_range {};
1022 unsigned char opcode_base {};
debd256d
JB
1023
1024 /* standard_opcode_lengths[i] is the number of operands for the
1025 standard opcode whose value is i. This means that
1026 standard_opcode_lengths[0] is unused, and the last meaningful
1027 element is standard_opcode_lengths[opcode_base - 1]. */
fff8551c 1028 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
debd256d 1029
fff8551c
PA
1030 /* The include_directories table. Note these are observing
1031 pointers. The memory is owned by debug_line_buffer. */
1032 std::vector<const char *> include_dirs;
debd256d 1033
fff8551c
PA
1034 /* The file_names table. */
1035 std::vector<file_entry> file_names;
debd256d
JB
1036
1037 /* The start and end of the statement program following this
6502dd73 1038 header. These point into dwarf2_per_objfile->line_buffer. */
fff8551c 1039 const gdb_byte *statement_program_start {}, *statement_program_end {};
debd256d 1040};
c906108c 1041
fff8551c
PA
1042typedef std::unique_ptr<line_header> line_header_up;
1043
8c43009f
PA
1044const char *
1045file_entry::include_dir (const line_header *lh) const
1046{
ecfb656c 1047 return lh->include_dir_at (d_index);
8c43009f
PA
1048}
1049
c906108c 1050/* When we construct a partial symbol table entry we only
0963b4bd 1051 need this much information. */
6f06d47b 1052struct partial_die_info : public allocate_on_obstack
c906108c 1053 {
6f06d47b
YQ
1054 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1055
1056 /* Disable assign but still keep copy ctor, which is needed
1057 load_partial_dies. */
1058 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1059
52356b79
YQ
1060 /* Adjust the partial die before generating a symbol for it. This
1061 function may set the is_external flag or change the DIE's
1062 name. */
1063 void fixup (struct dwarf2_cu *cu);
1064
48fbe735
YQ
1065 /* Read a minimal amount of information into the minimal die
1066 structure. */
1067 const gdb_byte *read (const struct die_reader_specs *reader,
1068 const struct abbrev_info &abbrev,
1069 const gdb_byte *info_ptr);
1070
72bf9492 1071 /* Offset of this DIE. */
6f06d47b 1072 const sect_offset sect_off;
72bf9492
DJ
1073
1074 /* DWARF-2 tag for this DIE. */
6f06d47b 1075 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 1076
72bf9492 1077 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
1078 const unsigned int has_children : 1;
1079
72bf9492
DJ
1080 unsigned int is_external : 1;
1081 unsigned int is_declaration : 1;
1082 unsigned int has_type : 1;
1083 unsigned int has_specification : 1;
1084 unsigned int has_pc_info : 1;
481860b3 1085 unsigned int may_be_inlined : 1;
72bf9492 1086
0c1b455e
TT
1087 /* This DIE has been marked DW_AT_main_subprogram. */
1088 unsigned int main_subprogram : 1;
1089
72bf9492
DJ
1090 /* Flag set if the SCOPE field of this structure has been
1091 computed. */
1092 unsigned int scope_set : 1;
1093
fa4028e9
JB
1094 /* Flag set if the DIE has a byte_size attribute. */
1095 unsigned int has_byte_size : 1;
1096
ff908ebf
AW
1097 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1098 unsigned int has_const_value : 1;
1099
98bfdba5
PA
1100 /* Flag set if any of the DIE's children are template arguments. */
1101 unsigned int has_template_arguments : 1;
1102
52356b79 1103 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
1104 unsigned int fixup_called : 1;
1105
36586728
TT
1106 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1107 unsigned int is_dwz : 1;
1108
1109 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1110 unsigned int spec_is_dwz : 1;
1111
72bf9492 1112 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1113 sometimes a default name for unnamed DIEs. */
6f06d47b 1114 const char *name = nullptr;
72bf9492 1115
abc72ce4 1116 /* The linkage name, if present. */
6f06d47b 1117 const char *linkage_name = nullptr;
abc72ce4 1118
72bf9492
DJ
1119 /* The scope to prepend to our children. This is generally
1120 allocated on the comp_unit_obstack, so will disappear
1121 when this compilation unit leaves the cache. */
6f06d47b 1122 const char *scope = nullptr;
72bf9492 1123
95554aad
TT
1124 /* Some data associated with the partial DIE. The tag determines
1125 which field is live. */
1126 union
1127 {
1128 /* The location description associated with this DIE, if any. */
1129 struct dwarf_block *locdesc;
1130 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1131 sect_offset sect_off;
6f06d47b 1132 } d {};
72bf9492
DJ
1133
1134 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
1135 CORE_ADDR lowpc = 0;
1136 CORE_ADDR highpc = 0;
72bf9492 1137
93311388 1138 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1139 DW_AT_sibling, if any. */
48fbe735
YQ
1140 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1141 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1142 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1143
1144 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1145 DW_AT_specification (or DW_AT_abstract_origin or
1146 DW_AT_extension). */
6f06d47b 1147 sect_offset spec_offset {};
72bf9492
DJ
1148
1149 /* Pointers to this DIE's parent, first child, and next sibling,
1150 if any. */
6f06d47b
YQ
1151 struct partial_die_info *die_parent = nullptr;
1152 struct partial_die_info *die_child = nullptr;
1153 struct partial_die_info *die_sibling = nullptr;
1154
1155 friend struct partial_die_info *
1156 dwarf2_cu::find_partial_die (sect_offset sect_off);
1157
1158 private:
1159 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1160 partial_die_info (sect_offset sect_off)
1161 : partial_die_info (sect_off, DW_TAG_padding, 0)
1162 {
1163 }
1164
1165 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1166 int has_children_)
1167 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1168 {
1169 is_external = 0;
1170 is_declaration = 0;
1171 has_type = 0;
1172 has_specification = 0;
1173 has_pc_info = 0;
1174 may_be_inlined = 0;
1175 main_subprogram = 0;
1176 scope_set = 0;
1177 has_byte_size = 0;
1178 has_const_value = 0;
1179 has_template_arguments = 0;
1180 fixup_called = 0;
1181 is_dwz = 0;
1182 spec_is_dwz = 0;
1183 }
c906108c
SS
1184 };
1185
0963b4bd 1186/* This data structure holds the information of an abbrev. */
c906108c
SS
1187struct abbrev_info
1188 {
1189 unsigned int number; /* number identifying abbrev */
1190 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
1191 unsigned short has_children; /* boolean */
1192 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1193 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1194 struct abbrev_info *next; /* next in chain */
1195 };
1196
1197struct attr_abbrev
1198 {
9d25dd43
DE
1199 ENUM_BITFIELD(dwarf_attribute) name : 16;
1200 ENUM_BITFIELD(dwarf_form) form : 16;
43988095
JK
1201
1202 /* It is valid only if FORM is DW_FORM_implicit_const. */
1203 LONGEST implicit_const;
c906108c
SS
1204 };
1205
433df2d4
DE
1206/* Size of abbrev_table.abbrev_hash_table. */
1207#define ABBREV_HASH_SIZE 121
1208
1209/* Top level data structure to contain an abbreviation table. */
1210
1211struct abbrev_table
1212{
685af9cd
TT
1213 explicit abbrev_table (sect_offset off)
1214 : sect_off (off)
1215 {
4a17f768 1216 m_abbrevs =
685af9cd 1217 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
4a17f768 1218 memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
685af9cd
TT
1219 }
1220
1221 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1222
1223 /* Allocate space for a struct abbrev_info object in
1224 ABBREV_TABLE. */
1225 struct abbrev_info *alloc_abbrev ();
1226
1227 /* Add an abbreviation to the table. */
1228 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1229
1230 /* Look up an abbrev in the table.
1231 Returns NULL if the abbrev is not found. */
1232
1233 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1234
1235
f4dc4d17
DE
1236 /* Where the abbrev table came from.
1237 This is used as a sanity check when the table is used. */
685af9cd 1238 const sect_offset sect_off;
433df2d4
DE
1239
1240 /* Storage for the abbrev table. */
685af9cd 1241 auto_obstack abbrev_obstack;
433df2d4 1242
4a17f768
YQ
1243private:
1244
433df2d4
DE
1245 /* Hash table of abbrevs.
1246 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1247 It could be statically allocated, but the previous code didn't so we
1248 don't either. */
4a17f768 1249 struct abbrev_info **m_abbrevs;
433df2d4
DE
1250};
1251
685af9cd
TT
1252typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
1253
0963b4bd 1254/* Attributes have a name and a value. */
b60c80d6
DJ
1255struct attribute
1256 {
9d25dd43 1257 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1258 ENUM_BITFIELD(dwarf_form) form : 15;
1259
1260 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1261 field should be in u.str (existing only for DW_STRING) but it is kept
1262 here for better struct attribute alignment. */
1263 unsigned int string_is_canonical : 1;
1264
b60c80d6
DJ
1265 union
1266 {
15d034d0 1267 const char *str;
b60c80d6 1268 struct dwarf_block *blk;
43bbcdc2
PH
1269 ULONGEST unsnd;
1270 LONGEST snd;
b60c80d6 1271 CORE_ADDR addr;
ac9ec31b 1272 ULONGEST signature;
b60c80d6
DJ
1273 }
1274 u;
1275 };
1276
0963b4bd 1277/* This data structure holds a complete die structure. */
c906108c
SS
1278struct die_info
1279 {
76815b17
DE
1280 /* DWARF-2 tag for this DIE. */
1281 ENUM_BITFIELD(dwarf_tag) tag : 16;
1282
1283 /* Number of attributes */
98bfdba5
PA
1284 unsigned char num_attrs;
1285
1286 /* True if we're presently building the full type name for the
1287 type derived from this DIE. */
1288 unsigned char building_fullname : 1;
76815b17 1289
adde2bff
DE
1290 /* True if this die is in process. PR 16581. */
1291 unsigned char in_process : 1;
1292
76815b17
DE
1293 /* Abbrev number */
1294 unsigned int abbrev;
1295
93311388 1296 /* Offset in .debug_info or .debug_types section. */
9c541725 1297 sect_offset sect_off;
78ba4af6
JB
1298
1299 /* The dies in a compilation unit form an n-ary tree. PARENT
1300 points to this die's parent; CHILD points to the first child of
1301 this node; and all the children of a given node are chained
4950bc1c 1302 together via their SIBLING fields. */
639d11d3
DC
1303 struct die_info *child; /* Its first child, if any. */
1304 struct die_info *sibling; /* Its next sibling, if any. */
1305 struct die_info *parent; /* Its parent, if any. */
c906108c 1306
b60c80d6
DJ
1307 /* An array of attributes, with NUM_ATTRS elements. There may be
1308 zero, but it's not common and zero-sized arrays are not
1309 sufficiently portable C. */
1310 struct attribute attrs[1];
c906108c
SS
1311 };
1312
0963b4bd 1313/* Get at parts of an attribute structure. */
c906108c
SS
1314
1315#define DW_STRING(attr) ((attr)->u.str)
8285870a 1316#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1317#define DW_UNSND(attr) ((attr)->u.unsnd)
1318#define DW_BLOCK(attr) ((attr)->u.blk)
1319#define DW_SND(attr) ((attr)->u.snd)
1320#define DW_ADDR(attr) ((attr)->u.addr)
ac9ec31b 1321#define DW_SIGNATURE(attr) ((attr)->u.signature)
c906108c 1322
0963b4bd 1323/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1324struct dwarf_block
1325 {
56eb65bd 1326 size_t size;
1d6edc3c
JK
1327
1328 /* Valid only if SIZE is not zero. */
d521ce57 1329 const gdb_byte *data;
c906108c
SS
1330 };
1331
c906108c
SS
1332#ifndef ATTR_ALLOC_CHUNK
1333#define ATTR_ALLOC_CHUNK 4
1334#endif
1335
c906108c
SS
1336/* Allocate fields for structs, unions and enums in this size. */
1337#ifndef DW_FIELD_ALLOC_CHUNK
1338#define DW_FIELD_ALLOC_CHUNK 4
1339#endif
1340
c906108c
SS
1341/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1342 but this would require a corresponding change in unpack_field_as_long
1343 and friends. */
1344static int bits_per_byte = 8;
1345
2ddeaf8a
TT
1346/* When reading a variant or variant part, we track a bit more
1347 information about the field, and store it in an object of this
1348 type. */
1349
1350struct variant_field
1351{
1352 /* If we see a DW_TAG_variant, then this will be the discriminant
1353 value. */
1354 ULONGEST discriminant_value;
1355 /* If we see a DW_TAG_variant, then this will be set if this is the
1356 default branch. */
1357 bool default_branch;
1358 /* While reading a DW_TAG_variant_part, this will be set if this
1359 field is the discriminant. */
1360 bool is_discriminant;
1361};
1362
52059ffd
TT
1363struct nextfield
1364{
be2daae6
TT
1365 int accessibility = 0;
1366 int virtuality = 0;
2ddeaf8a 1367 /* Extra information to describe a variant or variant part. */
be2daae6
TT
1368 struct variant_field variant {};
1369 struct field field {};
52059ffd
TT
1370};
1371
1372struct fnfieldlist
1373{
be2daae6
TT
1374 const char *name = nullptr;
1375 std::vector<struct fn_field> fnfields;
52059ffd
TT
1376};
1377
c906108c
SS
1378/* The routines that read and process dies for a C struct or C++ class
1379 pass lists of data member fields and lists of member function fields
1380 in an instance of a field_info structure, as defined below. */
1381struct field_info
c5aa993b 1382 {
0963b4bd 1383 /* List of data member and baseclasses fields. */
be2daae6
TT
1384 std::vector<struct nextfield> fields;
1385 std::vector<struct nextfield> baseclasses;
c906108c 1386
7d0ccb61 1387 /* Number of fields (including baseclasses). */
be2daae6 1388 int nfields = 0;
c906108c 1389
c5aa993b 1390 /* Set if the accesibility of one of the fields is not public. */
be2daae6 1391 int non_public_fields = 0;
c906108c 1392
c5aa993b
JM
1393 /* Member function fieldlist array, contains name of possibly overloaded
1394 member function, number of overloaded member functions and a pointer
1395 to the head of the member function field chain. */
be2daae6 1396 std::vector<struct fnfieldlist> fnfieldlists;
98751a41
JK
1397
1398 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1399 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
be2daae6 1400 std::vector<struct decl_field> typedef_field_list;
883fd55a
KS
1401
1402 /* Nested types defined by this class and the number of elements in this
1403 list. */
be2daae6 1404 std::vector<struct decl_field> nested_types_list;
c5aa993b 1405 };
c906108c 1406
10b3939b
DJ
1407/* One item on the queue of compilation units to read in full symbols
1408 for. */
1409struct dwarf2_queue_item
1410{
1411 struct dwarf2_per_cu_data *per_cu;
95554aad 1412 enum language pretend_language;
10b3939b
DJ
1413 struct dwarf2_queue_item *next;
1414};
1415
1416/* The current queue. */
1417static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1418
ae038cb0
DJ
1419/* Loaded secondary compilation units are kept in memory until they
1420 have not been referenced for the processing of this many
1421 compilation units. Set this to zero to disable caching. Cache
1422 sizes of up to at least twenty will improve startup time for
1423 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1424static int dwarf_max_cache_age = 5;
920d2a44 1425static void
b4f54984
DE
1426show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1427 struct cmd_list_element *c, const char *value)
920d2a44 1428{
3e43a32a 1429 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1430 "DWARF compilation units is %s.\n"),
920d2a44
AC
1431 value);
1432}
4390d890 1433\f
c906108c
SS
1434/* local function prototypes */
1435
a32a8923
DE
1436static const char *get_section_name (const struct dwarf2_section_info *);
1437
1438static const char *get_section_file_name (const struct dwarf2_section_info *);
1439
918dd910
JK
1440static void dwarf2_find_base_address (struct die_info *die,
1441 struct dwarf2_cu *cu);
1442
0018ea6f
DE
1443static struct partial_symtab *create_partial_symtab
1444 (struct dwarf2_per_cu_data *per_cu, const char *name);
1445
f1902523
JK
1446static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1447 const gdb_byte *info_ptr,
1448 struct die_info *type_unit_die,
1449 int has_children, void *data);
1450
ed2dc618
SM
1451static void dwarf2_build_psymtabs_hard
1452 (struct dwarf2_per_objfile *dwarf2_per_objfile);
c906108c 1453
72bf9492
DJ
1454static void scan_partial_symbols (struct partial_die_info *,
1455 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1456 int, struct dwarf2_cu *);
c906108c 1457
72bf9492
DJ
1458static void add_partial_symbol (struct partial_die_info *,
1459 struct dwarf2_cu *);
63d06c5c 1460
72bf9492
DJ
1461static void add_partial_namespace (struct partial_die_info *pdi,
1462 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1463 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1464
5d7cb8df 1465static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1466 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1467 struct dwarf2_cu *cu);
1468
72bf9492
DJ
1469static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1470 struct dwarf2_cu *cu);
91c24f0a 1471
bc30ff58
JB
1472static void add_partial_subprogram (struct partial_die_info *pdi,
1473 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1474 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1475
257e7a09
YQ
1476static void dwarf2_read_symtab (struct partial_symtab *,
1477 struct objfile *);
c906108c 1478
a14ed312 1479static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1480
685af9cd 1481static abbrev_table_up abbrev_table_read_table
ed2dc618
SM
1482 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1483 sect_offset);
433df2d4 1484
d521ce57 1485static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1486
dee91e82 1487static struct partial_die_info *load_partial_dies
d521ce57 1488 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1489
fb816e8b
TV
1490/* A pair of partial_die_info and compilation unit. */
1491struct cu_partial_die_info
1492{
1493 /* The compilation unit of the partial_die_info. */
1494 struct dwarf2_cu *cu;
1495 /* A partial_die_info. */
1496 struct partial_die_info *pdi;
122cf0f2
AB
1497
1498 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1499 : cu (cu),
1500 pdi (pdi)
1501 { /* Nothhing. */ }
1502
1503private:
1504 cu_partial_die_info () = delete;
fb816e8b
TV
1505};
1506
122cf0f2
AB
1507static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1508 struct dwarf2_cu *);
72bf9492 1509
d521ce57
TT
1510static const gdb_byte *read_attribute (const struct die_reader_specs *,
1511 struct attribute *, struct attr_abbrev *,
1512 const gdb_byte *);
a8329558 1513
a1855c1d 1514static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1515
a1855c1d 1516static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1517
a1855c1d 1518static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1519
15f18d14
AT
1520/* Read the next three bytes (little-endian order) as an unsigned integer. */
1521static unsigned int read_3_bytes (bfd *, const gdb_byte *);
1522
a1855c1d 1523static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1524
a1855c1d 1525static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1526
d521ce57 1527static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1528 unsigned int *);
c906108c 1529
d521ce57 1530static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1531
1532static LONGEST read_checked_initial_length_and_offset
d521ce57 1533 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1534 unsigned int *, unsigned int *);
613e1657 1535
d521ce57
TT
1536static LONGEST read_offset (bfd *, const gdb_byte *,
1537 const struct comp_unit_head *,
c764a876
DE
1538 unsigned int *);
1539
d521ce57 1540static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1541
ed2dc618
SM
1542static sect_offset read_abbrev_offset
1543 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1544 struct dwarf2_section_info *, sect_offset);
f4dc4d17 1545
d521ce57 1546static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1547
d521ce57 1548static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1549
ed2dc618
SM
1550static const char *read_indirect_string
1551 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1552 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1553
ed2dc618
SM
1554static const char *read_indirect_line_string
1555 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1556 const struct comp_unit_head *, unsigned int *);
36586728 1557
ed2dc618
SM
1558static const char *read_indirect_string_at_offset
1559 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1560 LONGEST str_offset);
927aa2e7 1561
ed2dc618
SM
1562static const char *read_indirect_string_from_dwz
1563 (struct objfile *objfile, struct dwz_file *, LONGEST);
c906108c 1564
d521ce57 1565static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1566
d521ce57
TT
1567static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1568 const gdb_byte *,
3019eac3
DE
1569 unsigned int *);
1570
d521ce57 1571static const char *read_str_index (const struct die_reader_specs *reader,
342587c4 1572 ULONGEST str_index);
3019eac3 1573
e142c38c 1574static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1575
e142c38c
DJ
1576static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1577 struct dwarf2_cu *);
c906108c 1578
348e048f 1579static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1580 unsigned int);
348e048f 1581
7d45c7c3
KB
1582static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1583 struct dwarf2_cu *cu);
1584
a084a2a6
AT
1585static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1586
05cf31d1
JB
1587static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1588 struct dwarf2_cu *cu);
1589
e142c38c 1590static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1591
e142c38c 1592static struct die_info *die_specification (struct die_info *die,
f2f0e013 1593 struct dwarf2_cu **);
63d06c5c 1594
9c541725 1595static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1596 struct dwarf2_cu *cu);
debd256d 1597
f3f5162e 1598static void dwarf_decode_lines (struct line_header *, const char *,
c3b7b696 1599 struct dwarf2_cu *, struct partial_symtab *,
527f3840 1600 CORE_ADDR, int decode_mapping);
c906108c 1601
804d2729
TT
1602static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1603 const char *);
c906108c 1604
a14ed312 1605static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1606 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1607
ff39bb5e 1608static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1609 struct dwarf2_cu *);
c906108c 1610
ff39bb5e 1611static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1612 struct type *type,
1613 const char *name,
1614 struct obstack *obstack,
12df843f 1615 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1616 const gdb_byte **bytes,
98bfdba5 1617 struct dwarf2_locexpr_baton **baton);
2df3850c 1618
e7c27a73 1619static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1620
b4ba55a1
JB
1621static int need_gnat_info (struct dwarf2_cu *);
1622
3e43a32a
MS
1623static struct type *die_descriptive_type (struct die_info *,
1624 struct dwarf2_cu *);
b4ba55a1
JB
1625
1626static void set_descriptive_type (struct type *, struct die_info *,
1627 struct dwarf2_cu *);
1628
e7c27a73
DJ
1629static struct type *die_containing_type (struct die_info *,
1630 struct dwarf2_cu *);
c906108c 1631
ff39bb5e 1632static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1633 struct dwarf2_cu *);
c906108c 1634
f792889a 1635static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1636
673bfd45
DE
1637static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1638
0d5cff50 1639static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1640
6e70227d 1641static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1642 const char *suffix, int physname,
1643 struct dwarf2_cu *cu);
63d06c5c 1644
e7c27a73 1645static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1646
348e048f
DE
1647static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1648
e7c27a73 1649static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1650
e7c27a73 1651static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1652
96408a79
SA
1653static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1654
71a3c369
TT
1655static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1656
ff013f42
JK
1657static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1658 struct dwarf2_cu *, struct partial_symtab *);
1659
3a2b436a 1660/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1661 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1662enum pc_bounds_kind
1663{
e385593e 1664 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1665 PC_BOUNDS_NOT_PRESENT,
1666
e385593e
JK
1667 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1668 were present but they do not form a valid range of PC addresses. */
1669 PC_BOUNDS_INVALID,
1670
3a2b436a
JK
1671 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1672 PC_BOUNDS_RANGES,
1673
1674 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1675 PC_BOUNDS_HIGH_LOW,
1676};
1677
1678static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1679 CORE_ADDR *, CORE_ADDR *,
1680 struct dwarf2_cu *,
1681 struct partial_symtab *);
c906108c 1682
fae299cd
DC
1683static void get_scope_pc_bounds (struct die_info *,
1684 CORE_ADDR *, CORE_ADDR *,
1685 struct dwarf2_cu *);
1686
801e3a5b
JB
1687static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1688 CORE_ADDR, struct dwarf2_cu *);
1689
a14ed312 1690static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1691 struct dwarf2_cu *);
c906108c 1692
a14ed312 1693static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1694 struct type *, struct dwarf2_cu *);
c906108c 1695
a14ed312 1696static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1697 struct die_info *, struct type *,
e7c27a73 1698 struct dwarf2_cu *);
c906108c 1699
a14ed312 1700static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1701 struct type *,
1702 struct dwarf2_cu *);
c906108c 1703
134d01f1 1704static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1705
e7c27a73 1706static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1707
e7c27a73 1708static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1709
5d7cb8df
JK
1710static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1711
804d2729 1712static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1713
27aa8d6a
SW
1714static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1715
74921315
KS
1716static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1717
f55ee35c
JK
1718static struct type *read_module_type (struct die_info *die,
1719 struct dwarf2_cu *cu);
1720
38d518c9 1721static const char *namespace_name (struct die_info *die,
e142c38c 1722 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1723
134d01f1 1724static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1725
e7c27a73 1726static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1727
6e70227d 1728static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1729 struct dwarf2_cu *);
1730
bf6af496 1731static struct die_info *read_die_and_siblings_1
d521ce57 1732 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1733 struct die_info *);
639d11d3 1734
dee91e82 1735static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1736 const gdb_byte *info_ptr,
1737 const gdb_byte **new_info_ptr,
639d11d3
DC
1738 struct die_info *parent);
1739
d521ce57
TT
1740static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1741 struct die_info **, const gdb_byte *,
1742 int *, int);
3019eac3 1743
d521ce57
TT
1744static const gdb_byte *read_full_die (const struct die_reader_specs *,
1745 struct die_info **, const gdb_byte *,
1746 int *);
93311388 1747
e7c27a73 1748static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1749
15d034d0
TT
1750static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1751 struct obstack *);
71c25dea 1752
15d034d0 1753static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1754
15d034d0 1755static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1756 struct die_info *die,
1757 struct dwarf2_cu *cu);
1758
ca69b9e6
DE
1759static const char *dwarf2_physname (const char *name, struct die_info *die,
1760 struct dwarf2_cu *cu);
1761
e142c38c 1762static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1763 struct dwarf2_cu **);
9219021c 1764
f39c6ffd 1765static const char *dwarf_tag_name (unsigned int);
c906108c 1766
f39c6ffd 1767static const char *dwarf_attr_name (unsigned int);
c906108c 1768
a084a2a6
AT
1769static const char *dwarf_unit_type_name (int unit_type);
1770
f39c6ffd 1771static const char *dwarf_form_name (unsigned int);
c906108c 1772
a121b7c1 1773static const char *dwarf_bool_name (unsigned int);
c906108c 1774
f39c6ffd 1775static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1776
f9aca02d 1777static struct die_info *sibling_die (struct die_info *);
c906108c 1778
d97bc12b
DE
1779static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1780
1781static void dump_die_for_error (struct die_info *);
1782
1783static void dump_die_1 (struct ui_file *, int level, int max_level,
1784 struct die_info *);
c906108c 1785
d97bc12b 1786/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1787
51545339 1788static void store_in_ref_table (struct die_info *,
10b3939b 1789 struct dwarf2_cu *);
c906108c 1790
ff39bb5e 1791static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1792
ff39bb5e 1793static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1794
348e048f 1795static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1796 const struct attribute *,
348e048f
DE
1797 struct dwarf2_cu **);
1798
10b3939b 1799static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1800 const struct attribute *,
f2f0e013 1801 struct dwarf2_cu **);
c906108c 1802
348e048f 1803static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1804 const struct attribute *,
348e048f
DE
1805 struct dwarf2_cu **);
1806
ac9ec31b
DE
1807static struct type *get_signatured_type (struct die_info *, ULONGEST,
1808 struct dwarf2_cu *);
1809
1810static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1811 const struct attribute *,
ac9ec31b
DE
1812 struct dwarf2_cu *);
1813
e5fe5e75 1814static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1815
52dc124a 1816static void read_signatured_type (struct signatured_type *);
348e048f 1817
63e43d3a
PMR
1818static int attr_to_dynamic_prop (const struct attribute *attr,
1819 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1820 struct dynamic_prop *prop, struct type *type);
63e43d3a 1821
c906108c
SS
1822/* memory allocation interface */
1823
7b5a2f43 1824static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1825
b60c80d6 1826static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1827
43f3e411 1828static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1829
6e5a29e1 1830static int attr_form_is_block (const struct attribute *);
8e19ed76 1831
6e5a29e1 1832static int attr_form_is_section_offset (const struct attribute *);
3690dd37 1833
6e5a29e1 1834static int attr_form_is_constant (const struct attribute *);
3690dd37 1835
6e5a29e1 1836static int attr_form_is_ref (const struct attribute *);
7771576e 1837
8cf6f0b1
TT
1838static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1839 struct dwarf2_loclist_baton *baton,
ff39bb5e 1840 const struct attribute *attr);
8cf6f0b1 1841
ff39bb5e 1842static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1843 struct symbol *sym,
f1e6e072
TT
1844 struct dwarf2_cu *cu,
1845 int is_block);
4c2df51b 1846
d521ce57
TT
1847static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1848 const gdb_byte *info_ptr,
1849 struct abbrev_info *abbrev);
4bb7a0a7 1850
72bf9492
DJ
1851static hashval_t partial_die_hash (const void *item);
1852
1853static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1854
ae038cb0 1855static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618
SM
1856 (sect_offset sect_off, unsigned int offset_in_dwz,
1857 struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1858
9816fde3 1859static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1860 struct die_info *comp_unit_die,
1861 enum language pretend_language);
93311388 1862
ed2dc618 1863static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1864
dee91e82 1865static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1866
f792889a
DJ
1867static struct type *set_die_type (struct die_info *, struct type *,
1868 struct dwarf2_cu *);
1c379e20 1869
ed2dc618 1870static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1871
ed2dc618 1872static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 1873
58f0c718 1874static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
95554aad 1875 enum language);
10b3939b 1876
95554aad
TT
1877static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1878 enum language);
10b3939b 1879
f4dc4d17
DE
1880static void process_full_type_unit (struct dwarf2_per_cu_data *,
1881 enum language);
1882
10b3939b
DJ
1883static void dwarf2_add_dependence (struct dwarf2_cu *,
1884 struct dwarf2_per_cu_data *);
1885
ae038cb0
DJ
1886static void dwarf2_mark (struct dwarf2_cu *);
1887
1888static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1889
b64f50a1 1890static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1891 struct dwarf2_per_cu_data *);
673bfd45 1892
f792889a 1893static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1894
95554aad
TT
1895static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1896 enum language pretend_language);
1897
ed2dc618 1898static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 1899
9a49df9d
AB
1900static struct type *dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu);
1901static struct type *dwarf2_per_cu_addr_sized_int_type
1902 (struct dwarf2_per_cu_data *per_cu, bool unsigned_p);
1903
b303c6f6
AB
1904/* Class, the destructor of which frees all allocated queue entries. This
1905 will only have work to do if an error was thrown while processing the
1906 dwarf. If no error was thrown then the queue entries should have all
1907 been processed, and freed, as we went along. */
1908
1909class dwarf2_queue_guard
1910{
1911public:
1912 dwarf2_queue_guard () = default;
1913
1914 /* Free any entries remaining on the queue. There should only be
1915 entries left if we hit an error while processing the dwarf. */
1916 ~dwarf2_queue_guard ()
1917 {
1918 struct dwarf2_queue_item *item, *last;
1919
1920 item = dwarf2_queue;
1921 while (item)
1922 {
1923 /* Anything still marked queued is likely to be in an
1924 inconsistent state, so discard it. */
1925 if (item->per_cu->queued)
1926 {
1927 if (item->per_cu->cu != NULL)
1928 free_one_cached_comp_unit (item->per_cu);
1929 item->per_cu->queued = 0;
1930 }
1931
1932 last = item;
1933 item = item->next;
1934 xfree (last);
1935 }
1936
1937 dwarf2_queue = dwarf2_queue_tail = NULL;
1938 }
1939};
1940
d721ba37
PA
1941/* The return type of find_file_and_directory. Note, the enclosed
1942 string pointers are only valid while this object is valid. */
1943
1944struct file_and_directory
1945{
1946 /* The filename. This is never NULL. */
1947 const char *name;
1948
1949 /* The compilation directory. NULL if not known. If we needed to
1950 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1951 points directly to the DW_AT_comp_dir string attribute owned by
1952 the obstack that owns the DIE. */
1953 const char *comp_dir;
1954
1955 /* If we needed to build a new string for comp_dir, this is what
1956 owns the storage. */
1957 std::string comp_dir_storage;
1958};
1959
1960static file_and_directory find_file_and_directory (struct die_info *die,
1961 struct dwarf2_cu *cu);
9291a0cd
TT
1962
1963static char *file_full_name (int file, struct line_header *lh,
1964 const char *comp_dir);
1965
43988095
JK
1966/* Expected enum dwarf_unit_type for read_comp_unit_head. */
1967enum class rcuh_kind { COMPILE, TYPE };
1968
d521ce57 1969static const gdb_byte *read_and_check_comp_unit_head
ed2dc618
SM
1970 (struct dwarf2_per_objfile* dwarf2_per_objfile,
1971 struct comp_unit_head *header,
36586728 1972 struct dwarf2_section_info *section,
d521ce57 1973 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
43988095 1974 rcuh_kind section_kind);
36586728 1975
fd820528 1976static void init_cutu_and_read_dies
f4dc4d17 1977 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
58f0c718 1978 int use_existing_cu, int keep, bool skip_partial,
3019eac3
DE
1979 die_reader_func_ftype *die_reader_func, void *data);
1980
dee91e82
DE
1981static void init_cutu_and_read_dies_simple
1982 (struct dwarf2_per_cu_data *this_cu,
1983 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1984
673bfd45 1985static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1986
3019eac3
DE
1987static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1988
57d63ce2 1989static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
1990 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1991 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 1992 ULONGEST signature, int is_debug_types);
a2ce51a0 1993
ed2dc618
SM
1994static struct dwp_file *get_dwp_file
1995 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 1996
3019eac3 1997static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1998 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1999
2000static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 2001 (struct signatured_type *, const char *, const char *);
3019eac3 2002
89e63ee4
DE
2003static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
2004
263db9a1
TT
2005/* A unique pointer to a dwo_file. */
2006
51ac9db5 2007typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 2008
ed2dc618 2009static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 2010
1b80a9fa 2011static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
2012
2013static void free_line_header_voidp (void *arg);
4390d890
DE
2014\f
2015/* Various complaints about symbol reading that don't abort the process. */
2016
2017static void
2018dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2019{
b98664d3 2020 complaint (_("statement list doesn't fit in .debug_line section"));
4390d890
DE
2021}
2022
2023static void
2024dwarf2_debug_line_missing_file_complaint (void)
2025{
b98664d3 2026 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
2027}
2028
2029static void
2030dwarf2_debug_line_missing_end_sequence_complaint (void)
2031{
b98664d3 2032 complaint (_(".debug_line section has line "
4390d890
DE
2033 "program sequence without an end"));
2034}
2035
2036static void
2037dwarf2_complex_location_expr_complaint (void)
2038{
b98664d3 2039 complaint (_("location expression too complex"));
4390d890
DE
2040}
2041
2042static void
2043dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2044 int arg3)
2045{
b98664d3 2046 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
2047 arg1, arg2, arg3);
2048}
2049
2050static void
2051dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2052{
b98664d3 2053 complaint (_("debug info runs off end of %s section"
4390d890 2054 " [in module %s]"),
a32a8923
DE
2055 get_section_name (section),
2056 get_section_file_name (section));
4390d890 2057}
1b80a9fa 2058
4390d890
DE
2059static void
2060dwarf2_macro_malformed_definition_complaint (const char *arg1)
2061{
b98664d3 2062 complaint (_("macro debug info contains a "
4390d890
DE
2063 "malformed macro definition:\n`%s'"),
2064 arg1);
2065}
2066
2067static void
2068dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2069{
b98664d3 2070 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
2071 arg1, arg2);
2072}
527f3840
JK
2073
2074/* Hash function for line_header_hash. */
2075
2076static hashval_t
2077line_header_hash (const struct line_header *ofs)
2078{
9c541725 2079 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
2080}
2081
2082/* Hash function for htab_create_alloc_ex for line_header_hash. */
2083
2084static hashval_t
2085line_header_hash_voidp (const void *item)
2086{
9a3c8263 2087 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
2088
2089 return line_header_hash (ofs);
2090}
2091
2092/* Equality function for line_header_hash. */
2093
2094static int
2095line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2096{
9a3c8263
SM
2097 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2098 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 2099
9c541725 2100 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
2101 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2102}
2103
4390d890 2104\f
9291a0cd 2105
31aa7e4e
JB
2106/* Read the given attribute value as an address, taking the attribute's
2107 form into account. */
2108
2109static CORE_ADDR
2110attr_value_as_address (struct attribute *attr)
2111{
2112 CORE_ADDR addr;
2113
336d760d
AT
2114 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_addrx
2115 && attr->form != DW_FORM_GNU_addr_index)
31aa7e4e
JB
2116 {
2117 /* Aside from a few clearly defined exceptions, attributes that
2118 contain an address must always be in DW_FORM_addr form.
2119 Unfortunately, some compilers happen to be violating this
2120 requirement by encoding addresses using other forms, such
2121 as DW_FORM_data4 for example. For those broken compilers,
2122 we try to do our best, without any guarantee of success,
2123 to interpret the address correctly. It would also be nice
2124 to generate a complaint, but that would require us to maintain
2125 a list of legitimate cases where a non-address form is allowed,
2126 as well as update callers to pass in at least the CU's DWARF
2127 version. This is more overhead than what we're willing to
2128 expand for a pretty rare case. */
2129 addr = DW_UNSND (attr);
2130 }
2131 else
2132 addr = DW_ADDR (attr);
2133
2134 return addr;
2135}
2136
330cdd98
PA
2137/* See declaration. */
2138
2139dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2140 const dwarf2_debug_sections *names)
2141 : objfile (objfile_)
2142{
2143 if (names == NULL)
2144 names = &dwarf2_elf_names;
2145
2146 bfd *obfd = objfile->obfd;
2147
2148 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2149 locate_sections (obfd, sec, *names);
2150}
2151
2152dwarf2_per_objfile::~dwarf2_per_objfile ()
2153{
2154 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2155 free_cached_comp_units ();
2156
2157 if (quick_file_names_table)
2158 htab_delete (quick_file_names_table);
2159
2160 if (line_header_hash)
2161 htab_delete (line_header_hash);
2162
b76e467d
SM
2163 for (dwarf2_per_cu_data *per_cu : all_comp_units)
2164 VEC_free (dwarf2_per_cu_ptr, per_cu->imported_symtabs);
fc8e7e75 2165
b2bdb8cf
SM
2166 for (signatured_type *sig_type : all_type_units)
2167 VEC_free (dwarf2_per_cu_ptr, sig_type->per_cu.imported_symtabs);
fc8e7e75 2168
330cdd98
PA
2169 /* Everything else should be on the objfile obstack. */
2170}
2171
2172/* See declaration. */
2173
2174void
2175dwarf2_per_objfile::free_cached_comp_units ()
2176{
2177 dwarf2_per_cu_data *per_cu = read_in_chain;
2178 dwarf2_per_cu_data **last_chain = &read_in_chain;
2179 while (per_cu != NULL)
2180 {
2181 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2182
fcd3b13d 2183 delete per_cu->cu;
330cdd98
PA
2184 *last_chain = next_cu;
2185 per_cu = next_cu;
2186 }
2187}
2188
11ed8cad
TT
2189/* A helper class that calls free_cached_comp_units on
2190 destruction. */
2191
2192class free_cached_comp_units
2193{
2194public:
2195
2196 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2197 : m_per_objfile (per_objfile)
2198 {
2199 }
2200
2201 ~free_cached_comp_units ()
2202 {
2203 m_per_objfile->free_cached_comp_units ();
2204 }
2205
2206 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2207
2208private:
2209
2210 dwarf2_per_objfile *m_per_objfile;
2211};
2212
c906108c 2213/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
2214 information and return true if we have enough to do something.
2215 NAMES points to the dwarf2 section names, or is NULL if the standard
2216 ELF names are used. */
c906108c
SS
2217
2218int
251d32d9
TG
2219dwarf2_has_info (struct objfile *objfile,
2220 const struct dwarf2_debug_sections *names)
c906108c 2221{
97cbe998
SDJ
2222 if (objfile->flags & OBJF_READNEVER)
2223 return 0;
2224
ed2dc618
SM
2225 struct dwarf2_per_objfile *dwarf2_per_objfile
2226 = get_dwarf2_per_objfile (objfile);
2227
2228 if (dwarf2_per_objfile == NULL)
5bfd760d
TT
2229 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
2230 names);
2231
73869dc2 2232 return (!dwarf2_per_objfile->info.is_virtual
049412e3 2233 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 2234 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 2235 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
2236}
2237
2238/* Return the containing section of virtual section SECTION. */
2239
2240static struct dwarf2_section_info *
2241get_containing_section (const struct dwarf2_section_info *section)
2242{
2243 gdb_assert (section->is_virtual);
2244 return section->s.containing_section;
c906108c
SS
2245}
2246
a32a8923
DE
2247/* Return the bfd owner of SECTION. */
2248
2249static struct bfd *
2250get_section_bfd_owner (const struct dwarf2_section_info *section)
2251{
73869dc2
DE
2252 if (section->is_virtual)
2253 {
2254 section = get_containing_section (section);
2255 gdb_assert (!section->is_virtual);
2256 }
049412e3 2257 return section->s.section->owner;
a32a8923
DE
2258}
2259
2260/* Return the bfd section of SECTION.
2261 Returns NULL if the section is not present. */
2262
2263static asection *
2264get_section_bfd_section (const struct dwarf2_section_info *section)
2265{
73869dc2
DE
2266 if (section->is_virtual)
2267 {
2268 section = get_containing_section (section);
2269 gdb_assert (!section->is_virtual);
2270 }
049412e3 2271 return section->s.section;
a32a8923
DE
2272}
2273
2274/* Return the name of SECTION. */
2275
2276static const char *
2277get_section_name (const struct dwarf2_section_info *section)
2278{
2279 asection *sectp = get_section_bfd_section (section);
2280
2281 gdb_assert (sectp != NULL);
2282 return bfd_section_name (get_section_bfd_owner (section), sectp);
2283}
2284
2285/* Return the name of the file SECTION is in. */
2286
2287static const char *
2288get_section_file_name (const struct dwarf2_section_info *section)
2289{
2290 bfd *abfd = get_section_bfd_owner (section);
2291
2292 return bfd_get_filename (abfd);
2293}
2294
2295/* Return the id of SECTION.
2296 Returns 0 if SECTION doesn't exist. */
2297
2298static int
2299get_section_id (const struct dwarf2_section_info *section)
2300{
2301 asection *sectp = get_section_bfd_section (section);
2302
2303 if (sectp == NULL)
2304 return 0;
2305 return sectp->id;
2306}
2307
2308/* Return the flags of SECTION.
73869dc2 2309 SECTION (or containing section if this is a virtual section) must exist. */
a32a8923
DE
2310
2311static int
2312get_section_flags (const struct dwarf2_section_info *section)
2313{
2314 asection *sectp = get_section_bfd_section (section);
2315
2316 gdb_assert (sectp != NULL);
2317 return bfd_get_section_flags (sectp->owner, sectp);
2318}
2319
251d32d9
TG
2320/* When loading sections, we look either for uncompressed section or for
2321 compressed section names. */
233a11ab
CS
2322
2323static int
251d32d9
TG
2324section_is_p (const char *section_name,
2325 const struct dwarf2_section_names *names)
233a11ab 2326{
251d32d9
TG
2327 if (names->normal != NULL
2328 && strcmp (section_name, names->normal) == 0)
2329 return 1;
2330 if (names->compressed != NULL
2331 && strcmp (section_name, names->compressed) == 0)
2332 return 1;
2333 return 0;
233a11ab
CS
2334}
2335
330cdd98 2336/* See declaration. */
c906108c 2337
330cdd98
PA
2338void
2339dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2340 const dwarf2_debug_sections &names)
c906108c 2341{
dc7650b8 2342 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9 2343
dc7650b8
JK
2344 if ((aflag & SEC_HAS_CONTENTS) == 0)
2345 {
2346 }
330cdd98 2347 else if (section_is_p (sectp->name, &names.info))
c906108c 2348 {
330cdd98
PA
2349 this->info.s.section = sectp;
2350 this->info.size = bfd_get_section_size (sectp);
c906108c 2351 }
330cdd98 2352 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 2353 {
330cdd98
PA
2354 this->abbrev.s.section = sectp;
2355 this->abbrev.size = bfd_get_section_size (sectp);
c906108c 2356 }
330cdd98 2357 else if (section_is_p (sectp->name, &names.line))
c906108c 2358 {
330cdd98
PA
2359 this->line.s.section = sectp;
2360 this->line.size = bfd_get_section_size (sectp);
c906108c 2361 }
330cdd98 2362 else if (section_is_p (sectp->name, &names.loc))
c906108c 2363 {
330cdd98
PA
2364 this->loc.s.section = sectp;
2365 this->loc.size = bfd_get_section_size (sectp);
c906108c 2366 }
330cdd98 2367 else if (section_is_p (sectp->name, &names.loclists))
43988095 2368 {
330cdd98
PA
2369 this->loclists.s.section = sectp;
2370 this->loclists.size = bfd_get_section_size (sectp);
43988095 2371 }
330cdd98 2372 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 2373 {
330cdd98
PA
2374 this->macinfo.s.section = sectp;
2375 this->macinfo.size = bfd_get_section_size (sectp);
c906108c 2376 }
330cdd98 2377 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 2378 {
330cdd98
PA
2379 this->macro.s.section = sectp;
2380 this->macro.size = bfd_get_section_size (sectp);
cf2c3c16 2381 }
330cdd98 2382 else if (section_is_p (sectp->name, &names.str))
c906108c 2383 {
330cdd98
PA
2384 this->str.s.section = sectp;
2385 this->str.size = bfd_get_section_size (sectp);
c906108c 2386 }
330cdd98 2387 else if (section_is_p (sectp->name, &names.line_str))
43988095 2388 {
330cdd98
PA
2389 this->line_str.s.section = sectp;
2390 this->line_str.size = bfd_get_section_size (sectp);
43988095 2391 }
330cdd98 2392 else if (section_is_p (sectp->name, &names.addr))
3019eac3 2393 {
330cdd98
PA
2394 this->addr.s.section = sectp;
2395 this->addr.size = bfd_get_section_size (sectp);
3019eac3 2396 }
330cdd98 2397 else if (section_is_p (sectp->name, &names.frame))
b6af0555 2398 {
330cdd98
PA
2399 this->frame.s.section = sectp;
2400 this->frame.size = bfd_get_section_size (sectp);
b6af0555 2401 }
330cdd98 2402 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2403 {
330cdd98
PA
2404 this->eh_frame.s.section = sectp;
2405 this->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 2406 }
330cdd98 2407 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2408 {
330cdd98
PA
2409 this->ranges.s.section = sectp;
2410 this->ranges.size = bfd_get_section_size (sectp);
af34e669 2411 }
330cdd98 2412 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2413 {
330cdd98
PA
2414 this->rnglists.s.section = sectp;
2415 this->rnglists.size = bfd_get_section_size (sectp);
43988095 2416 }
330cdd98 2417 else if (section_is_p (sectp->name, &names.types))
348e048f 2418 {
8b70b953
TT
2419 struct dwarf2_section_info type_section;
2420
2421 memset (&type_section, 0, sizeof (type_section));
049412e3 2422 type_section.s.section = sectp;
8b70b953
TT
2423 type_section.size = bfd_get_section_size (sectp);
2424
fd5866f6 2425 this->types.push_back (type_section);
348e048f 2426 }
330cdd98 2427 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2428 {
330cdd98
PA
2429 this->gdb_index.s.section = sectp;
2430 this->gdb_index.size = bfd_get_section_size (sectp);
9291a0cd 2431 }
927aa2e7
JK
2432 else if (section_is_p (sectp->name, &names.debug_names))
2433 {
2434 this->debug_names.s.section = sectp;
2435 this->debug_names.size = bfd_get_section_size (sectp);
2436 }
2437 else if (section_is_p (sectp->name, &names.debug_aranges))
2438 {
2439 this->debug_aranges.s.section = sectp;
2440 this->debug_aranges.size = bfd_get_section_size (sectp);
2441 }
dce234bc 2442
b4e1fd61 2443 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
72dca2f5 2444 && bfd_section_vma (abfd, sectp) == 0)
330cdd98 2445 this->has_section_at_zero = true;
c906108c
SS
2446}
2447
fceca515
DE
2448/* A helper function that decides whether a section is empty,
2449 or not present. */
9e0ac564
TT
2450
2451static int
19ac8c2e 2452dwarf2_section_empty_p (const struct dwarf2_section_info *section)
9e0ac564 2453{
73869dc2
DE
2454 if (section->is_virtual)
2455 return section->size == 0;
049412e3 2456 return section->s.section == NULL || section->size == 0;
9e0ac564
TT
2457}
2458
cd4fb1b2 2459/* See dwarf2read.h. */
c906108c 2460
cd4fb1b2
SM
2461void
2462dwarf2_read_section (struct objfile *objfile, dwarf2_section_info *info)
c906108c 2463{
a32a8923 2464 asection *sectp;
3019eac3 2465 bfd *abfd;
dce234bc 2466 gdb_byte *buf, *retbuf;
c906108c 2467
be391dca
TT
2468 if (info->readin)
2469 return;
dce234bc 2470 info->buffer = NULL;
dc4ccb6f 2471 info->readin = true;
188dd5d6 2472
9e0ac564 2473 if (dwarf2_section_empty_p (info))
dce234bc 2474 return;
c906108c 2475
a32a8923 2476 sectp = get_section_bfd_section (info);
3019eac3 2477
73869dc2
DE
2478 /* If this is a virtual section we need to read in the real one first. */
2479 if (info->is_virtual)
2480 {
2481 struct dwarf2_section_info *containing_section =
2482 get_containing_section (info);
2483
2484 gdb_assert (sectp != NULL);
2485 if ((sectp->flags & SEC_RELOC) != 0)
2486 {
2487 error (_("Dwarf Error: DWP format V2 with relocations is not"
2488 " supported in section %s [in module %s]"),
2489 get_section_name (info), get_section_file_name (info));
2490 }
2491 dwarf2_read_section (objfile, containing_section);
2492 /* Other code should have already caught virtual sections that don't
2493 fit. */
2494 gdb_assert (info->virtual_offset + info->size
2495 <= containing_section->size);
2496 /* If the real section is empty or there was a problem reading the
2497 section we shouldn't get here. */
2498 gdb_assert (containing_section->buffer != NULL);
2499 info->buffer = containing_section->buffer + info->virtual_offset;
2500 return;
2501 }
2502
4bf44c1c
TT
2503 /* If the section has relocations, we must read it ourselves.
2504 Otherwise we attach it to the BFD. */
2505 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 2506 {
d521ce57 2507 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 2508 return;
dce234bc 2509 }
dce234bc 2510
224c3ddb 2511 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
4bf44c1c 2512 info->buffer = buf;
dce234bc
PP
2513
2514 /* When debugging .o files, we may need to apply relocations; see
2515 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2516 We never compress sections in .o files, so we only need to
2517 try this when the section is not compressed. */
ac8035ab 2518 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2519 if (retbuf != NULL)
2520 {
2521 info->buffer = retbuf;
2522 return;
2523 }
2524
a32a8923
DE
2525 abfd = get_section_bfd_owner (info);
2526 gdb_assert (abfd != NULL);
2527
dce234bc
PP
2528 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2529 || bfd_bread (buf, info->size, abfd) != info->size)
19ac8c2e
DE
2530 {
2531 error (_("Dwarf Error: Can't read DWARF data"
2532 " in section %s [in module %s]"),
2533 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2534 }
dce234bc
PP
2535}
2536
9e0ac564
TT
2537/* A helper function that returns the size of a section in a safe way.
2538 If you are positive that the section has been read before using the
2539 size, then it is safe to refer to the dwarf2_section_info object's
2540 "size" field directly. In other cases, you must call this
2541 function, because for compressed sections the size field is not set
2542 correctly until the section has been read. */
2543
2544static bfd_size_type
2545dwarf2_section_size (struct objfile *objfile,
2546 struct dwarf2_section_info *info)
2547{
2548 if (!info->readin)
2549 dwarf2_read_section (objfile, info);
2550 return info->size;
2551}
2552
dce234bc 2553/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2554 SECTION_NAME. */
af34e669 2555
dce234bc 2556void
3017a003
TG
2557dwarf2_get_section_info (struct objfile *objfile,
2558 enum dwarf2_section_enum sect,
d521ce57 2559 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2560 bfd_size_type *sizep)
2561{
5bfd760d 2562 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
dce234bc 2563 struct dwarf2_section_info *info;
a3b2a86b
TT
2564
2565 /* We may see an objfile without any DWARF, in which case we just
2566 return nothing. */
2567 if (data == NULL)
2568 {
2569 *sectp = NULL;
2570 *bufp = NULL;
2571 *sizep = 0;
2572 return;
2573 }
3017a003
TG
2574 switch (sect)
2575 {
2576 case DWARF2_DEBUG_FRAME:
2577 info = &data->frame;
2578 break;
2579 case DWARF2_EH_FRAME:
2580 info = &data->eh_frame;
2581 break;
2582 default:
2583 gdb_assert_not_reached ("unexpected section");
2584 }
dce234bc 2585
9e0ac564 2586 dwarf2_read_section (objfile, info);
dce234bc 2587
a32a8923 2588 *sectp = get_section_bfd_section (info);
dce234bc
PP
2589 *bufp = info->buffer;
2590 *sizep = info->size;
2591}
2592
36586728
TT
2593/* A helper function to find the sections for a .dwz file. */
2594
2595static void
2596locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2597{
9a3c8263 2598 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2599
2600 /* Note that we only support the standard ELF names, because .dwz
2601 is ELF-only (at the time of writing). */
2602 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2603 {
049412e3 2604 dwz_file->abbrev.s.section = sectp;
36586728
TT
2605 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2606 }
2607 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2608 {
049412e3 2609 dwz_file->info.s.section = sectp;
36586728
TT
2610 dwz_file->info.size = bfd_get_section_size (sectp);
2611 }
2612 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2613 {
049412e3 2614 dwz_file->str.s.section = sectp;
36586728
TT
2615 dwz_file->str.size = bfd_get_section_size (sectp);
2616 }
2617 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2618 {
049412e3 2619 dwz_file->line.s.section = sectp;
36586728
TT
2620 dwz_file->line.size = bfd_get_section_size (sectp);
2621 }
2622 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2623 {
049412e3 2624 dwz_file->macro.s.section = sectp;
36586728
TT
2625 dwz_file->macro.size = bfd_get_section_size (sectp);
2626 }
2ec9a5e0
TT
2627 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2628 {
049412e3 2629 dwz_file->gdb_index.s.section = sectp;
2ec9a5e0
TT
2630 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2631 }
927aa2e7
JK
2632 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2633 {
2634 dwz_file->debug_names.s.section = sectp;
2635 dwz_file->debug_names.size = bfd_get_section_size (sectp);
2636 }
36586728
TT
2637}
2638
c4973306 2639/* See dwarf2read.h. */
36586728 2640
c4973306 2641struct dwz_file *
ed2dc618 2642dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 2643{
36586728 2644 const char *filename;
acd13123 2645 bfd_size_type buildid_len_arg;
dc294be5
TT
2646 size_t buildid_len;
2647 bfd_byte *buildid;
36586728
TT
2648
2649 if (dwarf2_per_objfile->dwz_file != NULL)
7ff8cb8c 2650 return dwarf2_per_objfile->dwz_file.get ();
36586728 2651
4db1a1dc 2652 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2653 gdb::unique_xmalloc_ptr<char> data
2654 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2655 &buildid_len_arg, &buildid));
4db1a1dc
TT
2656 if (data == NULL)
2657 {
2658 if (bfd_get_error () == bfd_error_no_error)
2659 return NULL;
2660 error (_("could not read '.gnu_debugaltlink' section: %s"),
2661 bfd_errmsg (bfd_get_error ()));
2662 }
791afaa2
TT
2663
2664 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2665
acd13123
TT
2666 buildid_len = (size_t) buildid_len_arg;
2667
791afaa2 2668 filename = data.get ();
d721ba37
PA
2669
2670 std::string abs_storage;
36586728
TT
2671 if (!IS_ABSOLUTE_PATH (filename))
2672 {
14278e1f
TT
2673 gdb::unique_xmalloc_ptr<char> abs
2674 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2675
14278e1f 2676 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2677 filename = abs_storage.c_str ();
36586728
TT
2678 }
2679
dc294be5
TT
2680 /* First try the file name given in the section. If that doesn't
2681 work, try to use the build-id instead. */
192b62ce 2682 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2683 if (dwz_bfd != NULL)
36586728 2684 {
192b62ce 2685 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2686 dwz_bfd.reset (nullptr);
36586728
TT
2687 }
2688
dc294be5
TT
2689 if (dwz_bfd == NULL)
2690 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2691
2692 if (dwz_bfd == NULL)
2693 error (_("could not find '.gnu_debugaltlink' file for %s"),
2694 objfile_name (dwarf2_per_objfile->objfile));
2695
7ff8cb8c
TT
2696 std::unique_ptr<struct dwz_file> result
2697 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2698
7ff8cb8c
TT
2699 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2700 result.get ());
36586728 2701
7ff8cb8c
TT
2702 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2703 result->dwz_bfd.get ());
2704 dwarf2_per_objfile->dwz_file = std::move (result);
2705 return dwarf2_per_objfile->dwz_file.get ();
36586728 2706}
9291a0cd 2707\f
7b9f3c50
DE
2708/* DWARF quick_symbols_functions support. */
2709
2710/* TUs can share .debug_line entries, and there can be a lot more TUs than
2711 unique line tables, so we maintain a separate table of all .debug_line
2712 derived entries to support the sharing.
2713 All the quick functions need is the list of file names. We discard the
2714 line_header when we're done and don't need to record it here. */
2715struct quick_file_names
2716{
094b34ac
DE
2717 /* The data used to construct the hash key. */
2718 struct stmt_list_hash hash;
7b9f3c50
DE
2719
2720 /* The number of entries in file_names, real_names. */
2721 unsigned int num_file_names;
2722
2723 /* The file names from the line table, after being run through
2724 file_full_name. */
2725 const char **file_names;
2726
2727 /* The file names from the line table after being run through
2728 gdb_realpath. These are computed lazily. */
2729 const char **real_names;
2730};
2731
2732/* When using the index (and thus not using psymtabs), each CU has an
2733 object of this type. This is used to hold information needed by
2734 the various "quick" methods. */
2735struct dwarf2_per_cu_quick_data
2736{
2737 /* The file table. This can be NULL if there was no file table
2738 or it's currently not read in.
2739 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2740 struct quick_file_names *file_names;
2741
2742 /* The corresponding symbol table. This is NULL if symbols for this
2743 CU have not yet been read. */
43f3e411 2744 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2745
2746 /* A temporary mark bit used when iterating over all CUs in
2747 expand_symtabs_matching. */
2748 unsigned int mark : 1;
2749
2750 /* True if we've tried to read the file table and found there isn't one.
2751 There will be no point in trying to read it again next time. */
2752 unsigned int no_file_data : 1;
2753};
2754
094b34ac
DE
2755/* Utility hash function for a stmt_list_hash. */
2756
2757static hashval_t
2758hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2759{
2760 hashval_t v = 0;
2761
2762 if (stmt_list_hash->dwo_unit != NULL)
2763 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2764 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2765 return v;
2766}
2767
2768/* Utility equality function for a stmt_list_hash. */
2769
2770static int
2771eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2772 const struct stmt_list_hash *rhs)
2773{
2774 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2775 return 0;
2776 if (lhs->dwo_unit != NULL
2777 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2778 return 0;
2779
9c541725 2780 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2781}
2782
7b9f3c50
DE
2783/* Hash function for a quick_file_names. */
2784
2785static hashval_t
2786hash_file_name_entry (const void *e)
2787{
9a3c8263
SM
2788 const struct quick_file_names *file_data
2789 = (const struct quick_file_names *) e;
7b9f3c50 2790
094b34ac 2791 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2792}
2793
2794/* Equality function for a quick_file_names. */
2795
2796static int
2797eq_file_name_entry (const void *a, const void *b)
2798{
9a3c8263
SM
2799 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2800 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2801
094b34ac 2802 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2803}
2804
2805/* Delete function for a quick_file_names. */
2806
2807static void
2808delete_file_name_entry (void *e)
2809{
9a3c8263 2810 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2811 int i;
2812
2813 for (i = 0; i < file_data->num_file_names; ++i)
2814 {
2815 xfree ((void*) file_data->file_names[i]);
2816 if (file_data->real_names)
2817 xfree ((void*) file_data->real_names[i]);
2818 }
2819
2820 /* The space for the struct itself lives on objfile_obstack,
2821 so we don't free it here. */
2822}
2823
2824/* Create a quick_file_names hash table. */
2825
2826static htab_t
2827create_quick_file_names_table (unsigned int nr_initial_entries)
2828{
2829 return htab_create_alloc (nr_initial_entries,
2830 hash_file_name_entry, eq_file_name_entry,
2831 delete_file_name_entry, xcalloc, xfree);
2832}
9291a0cd 2833
918dd910
JK
2834/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2835 have to be created afterwards. You should call age_cached_comp_units after
2836 processing PER_CU->CU. dw2_setup must have been already called. */
2837
2838static void
58f0c718 2839load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
918dd910 2840{
3019eac3 2841 if (per_cu->is_debug_types)
e5fe5e75 2842 load_full_type_unit (per_cu);
918dd910 2843 else
58f0c718 2844 load_full_comp_unit (per_cu, skip_partial, language_minimal);
918dd910 2845
cc12ce38
DE
2846 if (per_cu->cu == NULL)
2847 return; /* Dummy CU. */
2dc860c0
DE
2848
2849 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2850}
2851
a0f42c21 2852/* Read in the symbols for PER_CU. */
2fdf6df6 2853
9291a0cd 2854static void
58f0c718 2855dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2856{
ed2dc618 2857 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9291a0cd 2858
f4dc4d17
DE
2859 /* Skip type_unit_groups, reading the type units they contain
2860 is handled elsewhere. */
2861 if (IS_TYPE_UNIT_GROUP (per_cu))
2862 return;
2863
b303c6f6
AB
2864 /* The destructor of dwarf2_queue_guard frees any entries left on
2865 the queue. After this point we're guaranteed to leave this function
2866 with the dwarf queue empty. */
2867 dwarf2_queue_guard q_guard;
9291a0cd 2868
95554aad 2869 if (dwarf2_per_objfile->using_index
43f3e411 2870 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2871 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2872 {
2873 queue_comp_unit (per_cu, language_minimal);
58f0c718 2874 load_cu (per_cu, skip_partial);
89e63ee4
DE
2875
2876 /* If we just loaded a CU from a DWO, and we're working with an index
2877 that may badly handle TUs, load all the TUs in that DWO as well.
2878 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2879 if (!per_cu->is_debug_types
cc12ce38 2880 && per_cu->cu != NULL
89e63ee4
DE
2881 && per_cu->cu->dwo_unit != NULL
2882 && dwarf2_per_objfile->index_table != NULL
2883 && dwarf2_per_objfile->index_table->version <= 7
2884 /* DWP files aren't supported yet. */
ed2dc618 2885 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 2886 queue_and_load_all_dwo_tus (per_cu);
95554aad 2887 }
9291a0cd 2888
ed2dc618 2889 process_queue (dwarf2_per_objfile);
9291a0cd
TT
2890
2891 /* Age the cache, releasing compilation units that have not
2892 been used recently. */
ed2dc618 2893 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
2894}
2895
2896/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2897 the objfile from which this CU came. Returns the resulting symbol
2898 table. */
2fdf6df6 2899
43f3e411 2900static struct compunit_symtab *
58f0c718 2901dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2902{
ed2dc618
SM
2903 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2904
95554aad 2905 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2906 if (!per_cu->v.quick->compunit_symtab)
9291a0cd 2907 {
11ed8cad 2908 free_cached_comp_units freer (dwarf2_per_objfile);
c83dd867 2909 scoped_restore decrementer = increment_reading_symtab ();
58f0c718 2910 dw2_do_instantiate_symtab (per_cu, skip_partial);
ed2dc618 2911 process_cu_includes (dwarf2_per_objfile);
9291a0cd 2912 }
f194fefb 2913
43f3e411 2914 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2915}
2916
ff4c9fec 2917/* See declaration. */
f4dc4d17 2918
ff4c9fec
SM
2919dwarf2_per_cu_data *
2920dwarf2_per_objfile::get_cutu (int index)
2921{
b76e467d 2922 if (index >= this->all_comp_units.size ())
ff4c9fec 2923 {
b76e467d 2924 index -= this->all_comp_units.size ();
b2bdb8cf 2925 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2926 return &this->all_type_units[index]->per_cu;
2927 }
f4dc4d17 2928
ff4c9fec
SM
2929 return this->all_comp_units[index];
2930}
f4dc4d17 2931
ff4c9fec 2932/* See declaration. */
2fdf6df6 2933
ff4c9fec
SM
2934dwarf2_per_cu_data *
2935dwarf2_per_objfile::get_cu (int index)
1fd400ff 2936{
b76e467d 2937 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2938
ff4c9fec 2939 return this->all_comp_units[index];
f4dc4d17
DE
2940}
2941
ff4c9fec 2942/* See declaration. */
f4dc4d17 2943
ff4c9fec
SM
2944signatured_type *
2945dwarf2_per_objfile::get_tu (int index)
f4dc4d17 2946{
b2bdb8cf 2947 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2948
ff4c9fec 2949 return this->all_type_units[index];
1fd400ff
TT
2950}
2951
4b514bc8
JK
2952/* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2953 objfile_obstack, and constructed with the specified field
2954 values. */
2955
2956static dwarf2_per_cu_data *
ed2dc618 2957create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
2958 struct dwarf2_section_info *section,
2959 int is_dwz,
2960 sect_offset sect_off, ULONGEST length)
2961{
ed2dc618 2962 struct objfile *objfile = dwarf2_per_objfile->objfile;
4b514bc8
JK
2963 dwarf2_per_cu_data *the_cu
2964 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2965 struct dwarf2_per_cu_data);
2966 the_cu->sect_off = sect_off;
2967 the_cu->length = length;
e3b94546 2968 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
4b514bc8
JK
2969 the_cu->section = section;
2970 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2971 struct dwarf2_per_cu_quick_data);
2972 the_cu->is_dwz = is_dwz;
2973 return the_cu;
2974}
2975
2ec9a5e0
TT
2976/* A helper for create_cus_from_index that handles a given list of
2977 CUs. */
2fdf6df6 2978
74a0d9f6 2979static void
12359b5e 2980create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2981 const gdb_byte *cu_list, offset_type n_elements,
2982 struct dwarf2_section_info *section,
b76e467d 2983 int is_dwz)
9291a0cd 2984{
12359b5e 2985 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2986 {
74a0d9f6 2987 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2988
2989 sect_offset sect_off
2990 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2991 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2992 cu_list += 2 * 8;
2993
b76e467d 2994 dwarf2_per_cu_data *per_cu
ed2dc618
SM
2995 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2996 sect_off, length);
b76e467d 2997 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
9291a0cd 2998 }
9291a0cd
TT
2999}
3000
2ec9a5e0 3001/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 3002 the CU objects for this objfile. */
2ec9a5e0 3003
74a0d9f6 3004static void
12359b5e 3005create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
3006 const gdb_byte *cu_list, offset_type cu_list_elements,
3007 const gdb_byte *dwz_list, offset_type dwz_elements)
3008{
b76e467d
SM
3009 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
3010 dwarf2_per_objfile->all_comp_units.reserve
3011 ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 3012
12359b5e 3013 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
b76e467d 3014 &dwarf2_per_objfile->info, 0);
2ec9a5e0
TT
3015
3016 if (dwz_elements == 0)
74a0d9f6 3017 return;
2ec9a5e0 3018
12359b5e
SM
3019 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3020 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
b76e467d 3021 &dwz->info, 1);
2ec9a5e0
TT
3022}
3023
1fd400ff 3024/* Create the signatured type hash table from the index. */
673bfd45 3025
74a0d9f6 3026static void
12359b5e
SM
3027create_signatured_type_table_from_index
3028 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3029 struct dwarf2_section_info *section,
3030 const gdb_byte *bytes,
3031 offset_type elements)
1fd400ff 3032{
12359b5e 3033 struct objfile *objfile = dwarf2_per_objfile->objfile;
1fd400ff 3034
b2bdb8cf
SM
3035 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3036 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
1fd400ff 3037
12359b5e 3038 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff 3039
12359b5e 3040 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 3041 {
52dc124a 3042 struct signatured_type *sig_type;
9c541725 3043 ULONGEST signature;
1fd400ff 3044 void **slot;
9c541725 3045 cu_offset type_offset_in_tu;
1fd400ff 3046
74a0d9f6 3047 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3048 sect_offset sect_off
3049 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3050 type_offset_in_tu
3051 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3052 BFD_ENDIAN_LITTLE);
1fd400ff
TT
3053 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3054 bytes += 3 * 8;
3055
52dc124a 3056 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 3057 struct signatured_type);
52dc124a 3058 sig_type->signature = signature;
9c541725 3059 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 3060 sig_type->per_cu.is_debug_types = 1;
8a0459fd 3061 sig_type->per_cu.section = section;
9c541725 3062 sig_type->per_cu.sect_off = sect_off;
e3b94546 3063 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
52dc124a 3064 sig_type->per_cu.v.quick
1fd400ff
TT
3065 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3066 struct dwarf2_per_cu_quick_data);
3067
52dc124a
DE
3068 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3069 *slot = sig_type;
1fd400ff 3070
b2bdb8cf 3071 dwarf2_per_objfile->all_type_units.push_back (sig_type);
1fd400ff
TT
3072 }
3073
673bfd45 3074 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
3075}
3076
927aa2e7
JK
3077/* Create the signatured type hash table from .debug_names. */
3078
3079static void
3080create_signatured_type_table_from_debug_names
ed2dc618 3081 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3082 const mapped_debug_names &map,
3083 struct dwarf2_section_info *section,
3084 struct dwarf2_section_info *abbrev_section)
3085{
ed2dc618
SM
3086 struct objfile *objfile = dwarf2_per_objfile->objfile;
3087
927aa2e7
JK
3088 dwarf2_read_section (objfile, section);
3089 dwarf2_read_section (objfile, abbrev_section);
3090
b2bdb8cf
SM
3091 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3092 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
927aa2e7
JK
3093
3094 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3095
3096 for (uint32_t i = 0; i < map.tu_count; ++i)
3097 {
3098 struct signatured_type *sig_type;
927aa2e7 3099 void **slot;
927aa2e7
JK
3100
3101 sect_offset sect_off
3102 = (sect_offset) (extract_unsigned_integer
3103 (map.tu_table_reordered + i * map.offset_size,
3104 map.offset_size,
3105 map.dwarf5_byte_order));
3106
3107 comp_unit_head cu_header;
ed2dc618
SM
3108 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3109 abbrev_section,
927aa2e7
JK
3110 section->buffer + to_underlying (sect_off),
3111 rcuh_kind::TYPE);
3112
3113 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3114 struct signatured_type);
3115 sig_type->signature = cu_header.signature;
3116 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3117 sig_type->per_cu.is_debug_types = 1;
3118 sig_type->per_cu.section = section;
3119 sig_type->per_cu.sect_off = sect_off;
e3b94546 3120 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
927aa2e7
JK
3121 sig_type->per_cu.v.quick
3122 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3123 struct dwarf2_per_cu_quick_data);
3124
3125 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3126 *slot = sig_type;
3127
b2bdb8cf 3128 dwarf2_per_objfile->all_type_units.push_back (sig_type);
927aa2e7
JK
3129 }
3130
3131 dwarf2_per_objfile->signatured_types = sig_types_hash;
3132}
3133
9291a0cd
TT
3134/* Read the address map data from the mapped index, and use it to
3135 populate the objfile's psymtabs_addrmap. */
2fdf6df6 3136
9291a0cd 3137static void
ed2dc618
SM
3138create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3139 struct mapped_index *index)
9291a0cd 3140{
ed2dc618 3141 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 3142 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 3143 const gdb_byte *iter, *end;
9291a0cd 3144 struct addrmap *mutable_map;
9291a0cd
TT
3145 CORE_ADDR baseaddr;
3146
8268c778
PA
3147 auto_obstack temp_obstack;
3148
9291a0cd
TT
3149 mutable_map = addrmap_create_mutable (&temp_obstack);
3150
f00a2de2
PA
3151 iter = index->address_table.data ();
3152 end = iter + index->address_table.size ();
9291a0cd
TT
3153
3154 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3155
3156 while (iter < end)
3157 {
3158 ULONGEST hi, lo, cu_index;
3159 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3160 iter += 8;
3161 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3162 iter += 8;
3163 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3164 iter += 4;
f652bce2 3165
24a55014 3166 if (lo > hi)
f652bce2 3167 {
b98664d3 3168 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 3169 hex_string (lo), hex_string (hi));
24a55014 3170 continue;
f652bce2 3171 }
24a55014 3172
b76e467d 3173 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
f652bce2 3174 {
b98664d3 3175 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 3176 (unsigned) cu_index);
24a55014 3177 continue;
f652bce2 3178 }
24a55014 3179
79748972
TT
3180 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
3181 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 3182 addrmap_set_empty (mutable_map, lo, hi - 1,
ff4c9fec 3183 dwarf2_per_objfile->get_cu (cu_index));
9291a0cd
TT
3184 }
3185
d320c2b5 3186 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 3187 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
3188}
3189
927aa2e7
JK
3190/* Read the address map data from DWARF-5 .debug_aranges, and use it to
3191 populate the objfile's psymtabs_addrmap. */
3192
3193static void
ed2dc618 3194create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3195 struct dwarf2_section_info *section)
3196{
ed2dc618 3197 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
3198 bfd *abfd = objfile->obfd;
3199 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3200 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3201 SECT_OFF_TEXT (objfile));
3202
3203 auto_obstack temp_obstack;
3204 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3205
3206 std::unordered_map<sect_offset,
3207 dwarf2_per_cu_data *,
3208 gdb::hash_enum<sect_offset>>
3209 debug_info_offset_to_per_cu;
b76e467d 3210 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 3211 {
927aa2e7
JK
3212 const auto insertpair
3213 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3214 if (!insertpair.second)
3215 {
3216 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
3217 "debug_info_offset %s, ignoring .debug_aranges."),
3218 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
3219 return;
3220 }
3221 }
3222
3223 dwarf2_read_section (objfile, section);
3224
3225 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3226
3227 const gdb_byte *addr = section->buffer;
3228
3229 while (addr < section->buffer + section->size)
3230 {
3231 const gdb_byte *const entry_addr = addr;
3232 unsigned int bytes_read;
3233
3234 const LONGEST entry_length = read_initial_length (abfd, addr,
3235 &bytes_read);
3236 addr += bytes_read;
3237
3238 const gdb_byte *const entry_end = addr + entry_length;
3239 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3240 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3241 if (addr + entry_length > section->buffer + section->size)
3242 {
47e3f474 3243 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3244 "length %s exceeds section length %s, "
3245 "ignoring .debug_aranges."),
47e3f474
TV
3246 objfile_name (objfile),
3247 plongest (entry_addr - section->buffer),
927aa2e7
JK
3248 plongest (bytes_read + entry_length),
3249 pulongest (section->size));
3250 return;
3251 }
3252
3253 /* The version number. */
3254 const uint16_t version = read_2_bytes (abfd, addr);
3255 addr += 2;
3256 if (version != 2)
3257 {
47e3f474 3258 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 3259 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
3260 objfile_name (objfile),
3261 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
3262 return;
3263 }
3264
3265 const uint64_t debug_info_offset
3266 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3267 addr += offset_size;
3268 const auto per_cu_it
3269 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3270 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3271 {
47e3f474 3272 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3273 "debug_info_offset %s does not exists, "
3274 "ignoring .debug_aranges."),
47e3f474
TV
3275 objfile_name (objfile),
3276 plongest (entry_addr - section->buffer),
927aa2e7
JK
3277 pulongest (debug_info_offset));
3278 return;
3279 }
3280 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3281
3282 const uint8_t address_size = *addr++;
3283 if (address_size < 1 || address_size > 8)
3284 {
47e3f474 3285 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 3286 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
3287 objfile_name (objfile),
3288 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
3289 return;
3290 }
3291
3292 const uint8_t segment_selector_size = *addr++;
3293 if (segment_selector_size != 0)
3294 {
47e3f474 3295 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3296 "segment_selector_size %u is not supported, "
3297 "ignoring .debug_aranges."),
47e3f474
TV
3298 objfile_name (objfile),
3299 plongest (entry_addr - section->buffer),
927aa2e7
JK
3300 segment_selector_size);
3301 return;
3302 }
3303
3304 /* Must pad to an alignment boundary that is twice the address
3305 size. It is undocumented by the DWARF standard but GCC does
3306 use it. */
3307 for (size_t padding = ((-(addr - section->buffer))
3308 & (2 * address_size - 1));
3309 padding > 0; padding--)
3310 if (*addr++ != 0)
3311 {
47e3f474 3312 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 3313 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
3314 objfile_name (objfile),
3315 plongest (entry_addr - section->buffer));
927aa2e7
JK
3316 return;
3317 }
3318
3319 for (;;)
3320 {
3321 if (addr + 2 * address_size > entry_end)
3322 {
47e3f474 3323 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
3324 "address list is not properly terminated, "
3325 "ignoring .debug_aranges."),
47e3f474
TV
3326 objfile_name (objfile),
3327 plongest (entry_addr - section->buffer));
927aa2e7
JK
3328 return;
3329 }
3330 ULONGEST start = extract_unsigned_integer (addr, address_size,
3331 dwarf5_byte_order);
3332 addr += address_size;
3333 ULONGEST length = extract_unsigned_integer (addr, address_size,
3334 dwarf5_byte_order);
3335 addr += address_size;
3336 if (start == 0 && length == 0)
3337 break;
3338 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3339 {
3340 /* Symbol was eliminated due to a COMDAT group. */
3341 continue;
3342 }
3343 ULONGEST end = start + length;
79748972
TT
3344 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3345 - baseaddr);
3346 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3347 - baseaddr);
927aa2e7
JK
3348 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3349 }
3350 }
3351
d320c2b5 3352 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 3353 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
3354}
3355
9291a0cd
TT
3356/* Find a slot in the mapped index INDEX for the object named NAME.
3357 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
3358 constant pool and return true. If NAME cannot be found, return
3359 false. */
2fdf6df6 3360
109483d9 3361static bool
9291a0cd
TT
3362find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3363 offset_type **vec_out)
3364{
0cf03b49 3365 offset_type hash;
9291a0cd 3366 offset_type slot, step;
559a7a62 3367 int (*cmp) (const char *, const char *);
9291a0cd 3368
791afaa2 3369 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 3370 if (current_language->la_language == language_cplus
45280282
IB
3371 || current_language->la_language == language_fortran
3372 || current_language->la_language == language_d)
0cf03b49
JK
3373 {
3374 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3375 not contain any. */
a8719064 3376
72998fb3 3377 if (strchr (name, '(') != NULL)
0cf03b49 3378 {
109483d9 3379 without_params = cp_remove_params (name);
0cf03b49 3380
72998fb3 3381 if (without_params != NULL)
791afaa2 3382 name = without_params.get ();
0cf03b49
JK
3383 }
3384 }
3385
559a7a62 3386 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3387 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3388 simulate our NAME being searched is also lowercased. */
3389 hash = mapped_index_string_hash ((index->version == 4
3390 && case_sensitivity == case_sensitive_off
3391 ? 5 : index->version),
3392 name);
3393
f00a2de2
PA
3394 slot = hash & (index->symbol_table.size () - 1);
3395 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 3396 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3397
3398 for (;;)
3399 {
9291a0cd 3400 const char *str;
f00a2de2
PA
3401
3402 const auto &bucket = index->symbol_table[slot];
3403 if (bucket.name == 0 && bucket.vec == 0)
109483d9 3404 return false;
9291a0cd 3405
f00a2de2 3406 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 3407 if (!cmp (name, str))
9291a0cd
TT
3408 {
3409 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 3410 + MAYBE_SWAP (bucket.vec));
109483d9 3411 return true;
9291a0cd
TT
3412 }
3413
f00a2de2 3414 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
3415 }
3416}
3417
4485a1c1
SM
3418/* A helper function that reads the .gdb_index from BUFFER and fills
3419 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 3420 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
3421 ok to use deprecated sections.
3422
3423 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3424 out parameters that are filled in with information about the CU and
3425 TU lists in the section.
3426
4485a1c1 3427 Returns true if all went well, false otherwise. */
2fdf6df6 3428
d33bc52e 3429static bool
4485a1c1
SM
3430read_gdb_index_from_buffer (struct objfile *objfile,
3431 const char *filename,
3432 bool deprecated_ok,
3433 gdb::array_view<const gdb_byte> buffer,
3434 struct mapped_index *map,
3435 const gdb_byte **cu_list,
3436 offset_type *cu_list_elements,
3437 const gdb_byte **types_list,
3438 offset_type *types_list_elements)
3439{
3440 const gdb_byte *addr = &buffer[0];
82430852 3441
9291a0cd 3442 /* Version check. */
4485a1c1 3443 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3444 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3445 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3446 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3447 indices. */
831adc1f 3448 if (version < 4)
481860b3
GB
3449 {
3450 static int warning_printed = 0;
3451 if (!warning_printed)
3452 {
3453 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3454 filename);
481860b3
GB
3455 warning_printed = 1;
3456 }
3457 return 0;
3458 }
3459 /* Index version 4 uses a different hash function than index version
3460 5 and later.
3461
3462 Versions earlier than 6 did not emit psymbols for inlined
3463 functions. Using these files will cause GDB not to be able to
3464 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3465 indices unless the user has done
3466 "set use-deprecated-index-sections on". */
2ec9a5e0 3467 if (version < 6 && !deprecated_ok)
481860b3
GB
3468 {
3469 static int warning_printed = 0;
3470 if (!warning_printed)
3471 {
e615022a
DE
3472 warning (_("\
3473Skipping deprecated .gdb_index section in %s.\n\
3474Do \"set use-deprecated-index-sections on\" before the file is read\n\
3475to use the section anyway."),
2ec9a5e0 3476 filename);
481860b3
GB
3477 warning_printed = 1;
3478 }
3479 return 0;
3480 }
796a7ff8 3481 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3482 of the TU (for symbols coming from TUs),
3483 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3484 Plus gold-generated indices can have duplicate entries for global symbols,
3485 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3486 These are just performance bugs, and we can't distinguish gdb-generated
3487 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3488
481860b3 3489 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3490 longer backward compatible. */
796a7ff8 3491 if (version > 8)
594e8718 3492 return 0;
9291a0cd 3493
559a7a62 3494 map->version = version;
9291a0cd 3495
4485a1c1 3496 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 3497
4485a1c1 3498 int i = 0;
2ec9a5e0
TT
3499 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3500 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3501 / 8);
1fd400ff
TT
3502 ++i;
3503
2ec9a5e0
TT
3504 *types_list = addr + MAYBE_SWAP (metadata[i]);
3505 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3506 - MAYBE_SWAP (metadata[i]))
3507 / 8);
987d643c 3508 ++i;
1fd400ff 3509
f00a2de2
PA
3510 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3511 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3512 map->address_table
3513 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3514 ++i;
3515
f00a2de2
PA
3516 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3517 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3518 map->symbol_table
3519 = gdb::array_view<mapped_index::symbol_table_slot>
3520 ((mapped_index::symbol_table_slot *) symbol_table,
3521 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3522
f00a2de2 3523 ++i;
f9d83a0b 3524 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3525
2ec9a5e0
TT
3526 return 1;
3527}
3528
4485a1c1
SM
3529/* Callback types for dwarf2_read_gdb_index. */
3530
3531typedef gdb::function_view
3532 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
3533 get_gdb_index_contents_ftype;
3534typedef gdb::function_view
3535 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3536 get_gdb_index_contents_dwz_ftype;
3537
927aa2e7 3538/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3539 elements of all the CUs and return 1. Otherwise, return 0. */
3540
3541static int
4485a1c1
SM
3542dwarf2_read_gdb_index
3543 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3544 get_gdb_index_contents_ftype get_gdb_index_contents,
3545 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3546{
2ec9a5e0
TT
3547 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3548 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3549 struct dwz_file *dwz;
12359b5e 3550 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ec9a5e0 3551
4485a1c1
SM
3552 gdb::array_view<const gdb_byte> main_index_contents
3553 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3554
3555 if (main_index_contents.empty ())
3556 return 0;
3557
3063847f 3558 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
4485a1c1
SM
3559 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
3560 use_deprecated_index_sections,
3561 main_index_contents, map.get (), &cu_list,
3562 &cu_list_elements, &types_list,
3563 &types_list_elements))
2ec9a5e0
TT
3564 return 0;
3565
0fefef59 3566 /* Don't use the index if it's empty. */
3063847f 3567 if (map->symbol_table.empty ())
0fefef59
DE
3568 return 0;
3569
2ec9a5e0
TT
3570 /* If there is a .dwz file, read it so we can get its CU list as
3571 well. */
ed2dc618 3572 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 3573 if (dwz != NULL)
2ec9a5e0 3574 {
2ec9a5e0
TT
3575 struct mapped_index dwz_map;
3576 const gdb_byte *dwz_types_ignore;
3577 offset_type dwz_types_elements_ignore;
3578
4485a1c1
SM
3579 gdb::array_view<const gdb_byte> dwz_index_content
3580 = get_gdb_index_contents_dwz (objfile, dwz);
3581
3582 if (dwz_index_content.empty ())
3583 return 0;
3584
3585 if (!read_gdb_index_from_buffer (objfile,
3586 bfd_get_filename (dwz->dwz_bfd), 1,
3587 dwz_index_content, &dwz_map,
3588 &dwz_list, &dwz_list_elements,
3589 &dwz_types_ignore,
3590 &dwz_types_elements_ignore))
2ec9a5e0
TT
3591 {
3592 warning (_("could not read '.gdb_index' section from %s; skipping"),
3593 bfd_get_filename (dwz->dwz_bfd));
3594 return 0;
3595 }
3596 }
3597
12359b5e
SM
3598 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3599 dwz_list, dwz_list_elements);
1fd400ff 3600
8b70b953
TT
3601 if (types_list_elements)
3602 {
8b70b953
TT
3603 /* We can only handle a single .debug_types when we have an
3604 index. */
fd5866f6 3605 if (dwarf2_per_objfile->types.size () != 1)
8b70b953
TT
3606 return 0;
3607
fd5866f6 3608 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
8b70b953 3609
12359b5e
SM
3610 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3611 types_list, types_list_elements);
8b70b953 3612 }
9291a0cd 3613
3063847f 3614 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
9291a0cd 3615
3063847f 3616 dwarf2_per_objfile->index_table = std::move (map);
9291a0cd 3617 dwarf2_per_objfile->using_index = 1;
7b9f3c50 3618 dwarf2_per_objfile->quick_file_names_table =
b76e467d 3619 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd
TT
3620
3621 return 1;
3622}
3623
dee91e82 3624/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3625
dee91e82
DE
3626static void
3627dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3628 const gdb_byte *info_ptr,
dee91e82
DE
3629 struct die_info *comp_unit_die,
3630 int has_children,
3631 void *data)
9291a0cd 3632{
dee91e82 3633 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3634 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
518817b3
SM
3635 struct dwarf2_per_objfile *dwarf2_per_objfile
3636 = cu->per_cu->dwarf2_per_objfile;
dee91e82 3637 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3638 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3639 struct attribute *attr;
dee91e82 3640 int i;
7b9f3c50
DE
3641 void **slot;
3642 struct quick_file_names *qfn;
9291a0cd 3643
0186c6a7
DE
3644 gdb_assert (! this_cu->is_debug_types);
3645
07261596
TT
3646 /* Our callers never want to match partial units -- instead they
3647 will match the enclosing full CU. */
3648 if (comp_unit_die->tag == DW_TAG_partial_unit)
3649 {
3650 this_cu->v.quick->no_file_data = 1;
3651 return;
3652 }
3653
0186c6a7 3654 lh_cu = this_cu;
7b9f3c50 3655 slot = NULL;
dee91e82 3656
fff8551c 3657 line_header_up lh;
9c541725 3658 sect_offset line_offset {};
fff8551c 3659
dee91e82 3660 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
3661 if (attr)
3662 {
7b9f3c50
DE
3663 struct quick_file_names find_entry;
3664
9c541725 3665 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3666
3667 /* We may have already read in this line header (TU line header sharing).
3668 If we have we're done. */
094b34ac 3669 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3670 find_entry.hash.line_sect_off = line_offset;
7b9f3c50
DE
3671 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3672 &find_entry, INSERT);
3673 if (*slot != NULL)
3674 {
9a3c8263 3675 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3676 return;
7b9f3c50
DE
3677 }
3678
3019eac3 3679 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3680 }
3681 if (lh == NULL)
3682 {
094b34ac 3683 lh_cu->v.quick->no_file_data = 1;
dee91e82 3684 return;
9291a0cd
TT
3685 }
3686
8d749320 3687 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3688 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3689 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3690 gdb_assert (slot != NULL);
3691 *slot = qfn;
9291a0cd 3692
d721ba37 3693 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3694
fff8551c 3695 qfn->num_file_names = lh->file_names.size ();
8d749320 3696 qfn->file_names =
fff8551c
PA
3697 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3698 for (i = 0; i < lh->file_names.size (); ++i)
3699 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
7b9f3c50 3700 qfn->real_names = NULL;
9291a0cd 3701
094b34ac 3702 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3703}
3704
3705/* A helper for the "quick" functions which attempts to read the line
3706 table for THIS_CU. */
3707
3708static struct quick_file_names *
e4a48d9d 3709dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3710{
0186c6a7
DE
3711 /* This should never be called for TUs. */
3712 gdb_assert (! this_cu->is_debug_types);
3713 /* Nor type unit groups. */
3714 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 3715
dee91e82
DE
3716 if (this_cu->v.quick->file_names != NULL)
3717 return this_cu->v.quick->file_names;
3718 /* If we know there is no line data, no point in looking again. */
3719 if (this_cu->v.quick->no_file_data)
3720 return NULL;
3721
0186c6a7 3722 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
3723
3724 if (this_cu->v.quick->no_file_data)
3725 return NULL;
3726 return this_cu->v.quick->file_names;
9291a0cd
TT
3727}
3728
3729/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3730 real path for a given file name from the line table. */
2fdf6df6 3731
9291a0cd 3732static const char *
7b9f3c50
DE
3733dw2_get_real_path (struct objfile *objfile,
3734 struct quick_file_names *qfn, int index)
9291a0cd 3735{
7b9f3c50
DE
3736 if (qfn->real_names == NULL)
3737 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3738 qfn->num_file_names, const char *);
9291a0cd 3739
7b9f3c50 3740 if (qfn->real_names[index] == NULL)
14278e1f 3741 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3742
7b9f3c50 3743 return qfn->real_names[index];
9291a0cd
TT
3744}
3745
3746static struct symtab *
3747dw2_find_last_source_symtab (struct objfile *objfile)
3748{
ed2dc618
SM
3749 struct dwarf2_per_objfile *dwarf2_per_objfile
3750 = get_dwarf2_per_objfile (objfile);
b76e467d 3751 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
58f0c718 3752 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
ae2de4f8 3753
43f3e411
DE
3754 if (cust == NULL)
3755 return NULL;
ed2dc618 3756
43f3e411 3757 return compunit_primary_filetab (cust);
9291a0cd
TT
3758}
3759
7b9f3c50
DE
3760/* Traversal function for dw2_forget_cached_source_info. */
3761
3762static int
3763dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3764{
7b9f3c50 3765 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3766
7b9f3c50 3767 if (file_data->real_names)
9291a0cd 3768 {
7b9f3c50 3769 int i;
9291a0cd 3770
7b9f3c50 3771 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3772 {
7b9f3c50
DE
3773 xfree ((void*) file_data->real_names[i]);
3774 file_data->real_names[i] = NULL;
9291a0cd
TT
3775 }
3776 }
7b9f3c50
DE
3777
3778 return 1;
3779}
3780
3781static void
3782dw2_forget_cached_source_info (struct objfile *objfile)
3783{
ed2dc618
SM
3784 struct dwarf2_per_objfile *dwarf2_per_objfile
3785 = get_dwarf2_per_objfile (objfile);
7b9f3c50
DE
3786
3787 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3788 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3789}
3790
f8eba3c6
TT
3791/* Helper function for dw2_map_symtabs_matching_filename that expands
3792 the symtabs and calls the iterator. */
3793
3794static int
3795dw2_map_expand_apply (struct objfile *objfile,
3796 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3797 const char *name, const char *real_path,
14bc53a8 3798 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3799{
43f3e411 3800 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3801
3802 /* Don't visit already-expanded CUs. */
43f3e411 3803 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3804 return 0;
3805
3806 /* This may expand more than one symtab, and we want to iterate over
3807 all of them. */
58f0c718 3808 dw2_instantiate_symtab (per_cu, false);
f8eba3c6 3809
14bc53a8
PA
3810 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3811 last_made, callback);
f8eba3c6
TT
3812}
3813
3814/* Implementation of the map_symtabs_matching_filename method. */
3815
14bc53a8
PA
3816static bool
3817dw2_map_symtabs_matching_filename
3818 (struct objfile *objfile, const char *name, const char *real_path,
3819 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3820{
c011a4f4 3821 const char *name_basename = lbasename (name);
ed2dc618
SM
3822 struct dwarf2_per_objfile *dwarf2_per_objfile
3823 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3824
848e3e78
DE
3825 /* The rule is CUs specify all the files, including those used by
3826 any TU, so there's no need to scan TUs here. */
f4dc4d17 3827
b76e467d 3828 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3829 {
3d7bb9d9 3830 /* We only need to look at symtabs not already expanded. */
43f3e411 3831 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3832 continue;
3833
b76e467d 3834 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3835 if (file_data == NULL)
9291a0cd
TT
3836 continue;
3837
b76e467d 3838 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3839 {
7b9f3c50 3840 const char *this_name = file_data->file_names[j];
da235a7c 3841 const char *this_real_name;
9291a0cd 3842
af529f8f 3843 if (compare_filenames_for_search (this_name, name))
9291a0cd 3844 {
f5b95b50 3845 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3846 callback))
3847 return true;
288e77a7 3848 continue;
4aac40c8 3849 }
9291a0cd 3850
c011a4f4
DE
3851 /* Before we invoke realpath, which can get expensive when many
3852 files are involved, do a quick comparison of the basenames. */
3853 if (! basenames_may_differ
3854 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3855 continue;
3856
da235a7c
JK
3857 this_real_name = dw2_get_real_path (objfile, file_data, j);
3858 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3859 {
da235a7c 3860 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3861 callback))
3862 return true;
288e77a7 3863 continue;
da235a7c 3864 }
9291a0cd 3865
da235a7c
JK
3866 if (real_path != NULL)
3867 {
af529f8f
JK
3868 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3869 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3870 if (this_real_name != NULL
af529f8f 3871 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3872 {
f5b95b50 3873 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3874 callback))
3875 return true;
288e77a7 3876 continue;
9291a0cd
TT
3877 }
3878 }
3879 }
3880 }
3881
14bc53a8 3882 return false;
9291a0cd
TT
3883}
3884
da51c347
DE
3885/* Struct used to manage iterating over all CUs looking for a symbol. */
3886
3887struct dw2_symtab_iterator
9291a0cd 3888{
ed2dc618
SM
3889 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3890 struct dwarf2_per_objfile *dwarf2_per_objfile;
2b79f376
SM
3891 /* If set, only look for symbols that match that block. Valid values are
3892 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3893 gdb::optional<block_enum> block_index;
da51c347
DE
3894 /* The kind of symbol we're looking for. */
3895 domain_enum domain;
3896 /* The list of CUs from the index entry of the symbol,
3897 or NULL if not found. */
3898 offset_type *vec;
3899 /* The next element in VEC to look at. */
3900 int next;
3901 /* The number of elements in VEC, or zero if there is no match. */
3902 int length;
8943b874
DE
3903 /* Have we seen a global version of the symbol?
3904 If so we can ignore all further global instances.
3905 This is to work around gold/15646, inefficient gold-generated
3906 indices. */
3907 int global_seen;
da51c347 3908};
9291a0cd 3909
2b79f376 3910/* Initialize the index symtab iterator ITER. */
2fdf6df6 3911
9291a0cd 3912static void
da51c347 3913dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3914 struct dwarf2_per_objfile *dwarf2_per_objfile,
c7f839cb 3915 gdb::optional<block_enum> block_index,
da51c347
DE
3916 domain_enum domain,
3917 const char *name)
3918{
ed2dc618 3919 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3920 iter->block_index = block_index;
3921 iter->domain = domain;
3922 iter->next = 0;
8943b874 3923 iter->global_seen = 0;
da51c347 3924
3063847f 3925 mapped_index *index = dwarf2_per_objfile->index_table.get ();
ed2dc618
SM
3926
3927 /* index is NULL if OBJF_READNOW. */
3928 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3929 iter->length = MAYBE_SWAP (*iter->vec);
3930 else
3931 {
3932 iter->vec = NULL;
3933 iter->length = 0;
3934 }
3935}
3936
3937/* Return the next matching CU or NULL if there are no more. */
3938
3939static struct dwarf2_per_cu_data *
3940dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3941{
ed2dc618
SM
3942 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3943
da51c347
DE
3944 for ( ; iter->next < iter->length; ++iter->next)
3945 {
3946 offset_type cu_index_and_attrs =
3947 MAYBE_SWAP (iter->vec[iter->next + 1]);
3948 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3949 gdb_index_symbol_kind symbol_kind =
3950 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3951 /* Only check the symbol attributes if they're present.
3952 Indices prior to version 7 don't record them,
3953 and indices >= 7 may elide them for certain symbols
3954 (gold does this). */
3955 int attrs_valid =
ed2dc618 3956 (dwarf2_per_objfile->index_table->version >= 7
da51c347
DE
3957 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3958
3190f0c6 3959 /* Don't crash on bad data. */
b76e467d 3960 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3961 + dwarf2_per_objfile->all_type_units.size ()))
3190f0c6 3962 {
b98664d3 3963 complaint (_(".gdb_index entry has bad CU index"
4262abfb
JK
3964 " [in module %s]"),
3965 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3966 continue;
3967 }
3968
ff4c9fec 3969 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3190f0c6 3970
da51c347 3971 /* Skip if already read in. */
43f3e411 3972 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
3973 continue;
3974
8943b874
DE
3975 /* Check static vs global. */
3976 if (attrs_valid)
3977 {
2b79f376
SM
3978 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3979
3980 if (iter->block_index.has_value ())
3981 {
3982 bool want_static = *iter->block_index == STATIC_BLOCK;
3983
3984 if (is_static != want_static)
3985 continue;
3986 }
3987
8943b874
DE
3988 /* Work around gold/15646. */
3989 if (!is_static && iter->global_seen)
3990 continue;
3991 if (!is_static)
3992 iter->global_seen = 1;
3993 }
da51c347
DE
3994
3995 /* Only check the symbol's kind if it has one. */
3996 if (attrs_valid)
3997 {
3998 switch (iter->domain)
3999 {
4000 case VAR_DOMAIN:
4001 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
4002 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
4003 /* Some types are also in VAR_DOMAIN. */
4004 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4005 continue;
4006 break;
4007 case STRUCT_DOMAIN:
4008 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4009 continue;
4010 break;
4011 case LABEL_DOMAIN:
4012 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4013 continue;
4014 break;
4015 default:
4016 break;
4017 }
4018 }
4019
4020 ++iter->next;
4021 return per_cu;
4022 }
4023
4024 return NULL;
4025}
4026
43f3e411 4027static struct compunit_symtab *
c7f839cb 4028dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 4029 const char *name, domain_enum domain)
9291a0cd 4030{
43f3e411 4031 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
4032 struct dwarf2_per_objfile *dwarf2_per_objfile
4033 = get_dwarf2_per_objfile (objfile);
9291a0cd 4034
b5ec771e
PA
4035 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4036
ed2dc618
SM
4037 struct dw2_symtab_iterator iter;
4038 struct dwarf2_per_cu_data *per_cu;
da51c347 4039
2b79f376 4040 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
9291a0cd 4041
ed2dc618
SM
4042 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4043 {
4044 struct symbol *sym, *with_opaque = NULL;
58f0c718 4045 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
ed2dc618 4046 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 4047 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 4048
ed2dc618
SM
4049 sym = block_find_symbol (block, name, domain,
4050 block_find_non_opaque_type_preferred,
4051 &with_opaque);
b2e2f908 4052
ed2dc618
SM
4053 /* Some caution must be observed with overloaded functions
4054 and methods, since the index will not contain any overload
4055 information (but NAME might contain it). */
da51c347 4056
ed2dc618
SM
4057 if (sym != NULL
4058 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4059 return stab;
4060 if (with_opaque != NULL
4061 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4062 stab_best = stab;
da51c347 4063
ed2dc618 4064 /* Keep looking through other CUs. */
9291a0cd 4065 }
9291a0cd 4066
da51c347 4067 return stab_best;
9291a0cd
TT
4068}
4069
4070static void
4071dw2_print_stats (struct objfile *objfile)
4072{
ed2dc618
SM
4073 struct dwarf2_per_objfile *dwarf2_per_objfile
4074 = get_dwarf2_per_objfile (objfile);
b76e467d 4075 int total = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4076 + dwarf2_per_objfile->all_type_units.size ());
ed2dc618 4077 int count = 0;
9291a0cd 4078
ed2dc618 4079 for (int i = 0; i < total; ++i)
9291a0cd 4080 {
ff4c9fec 4081 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 4082
43f3e411 4083 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4084 ++count;
4085 }
e4a48d9d 4086 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
4087 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4088}
4089
779bd270
DE
4090/* This dumps minimal information about the index.
4091 It is called via "mt print objfiles".
4092 One use is to verify .gdb_index has been loaded by the
4093 gdb.dwarf2/gdb-index.exp testcase. */
4094
9291a0cd
TT
4095static void
4096dw2_dump (struct objfile *objfile)
4097{
ed2dc618
SM
4098 struct dwarf2_per_objfile *dwarf2_per_objfile
4099 = get_dwarf2_per_objfile (objfile);
4100
779bd270
DE
4101 gdb_assert (dwarf2_per_objfile->using_index);
4102 printf_filtered (".gdb_index:");
4103 if (dwarf2_per_objfile->index_table != NULL)
4104 {
4105 printf_filtered (" version %d\n",
4106 dwarf2_per_objfile->index_table->version);
4107 }
4108 else
4109 printf_filtered (" faked for \"readnow\"\n");
4110 printf_filtered ("\n");
9291a0cd
TT
4111}
4112
9291a0cd
TT
4113static void
4114dw2_expand_symtabs_for_function (struct objfile *objfile,
4115 const char *func_name)
4116{
ed2dc618
SM
4117 struct dwarf2_per_objfile *dwarf2_per_objfile
4118 = get_dwarf2_per_objfile (objfile);
da51c347 4119
ed2dc618
SM
4120 struct dw2_symtab_iterator iter;
4121 struct dwarf2_per_cu_data *per_cu;
da51c347 4122
2b79f376 4123 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 4124
ed2dc618 4125 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
58f0c718 4126 dw2_instantiate_symtab (per_cu, false);
da51c347 4127
9291a0cd
TT
4128}
4129
4130static void
4131dw2_expand_all_symtabs (struct objfile *objfile)
4132{
ed2dc618
SM
4133 struct dwarf2_per_objfile *dwarf2_per_objfile
4134 = get_dwarf2_per_objfile (objfile);
b76e467d 4135 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4136 + dwarf2_per_objfile->all_type_units.size ());
9291a0cd 4137
ed2dc618 4138 for (int i = 0; i < total_units; ++i)
9291a0cd 4139 {
ff4c9fec 4140 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 4141
58f0c718
TT
4142 /* We don't want to directly expand a partial CU, because if we
4143 read it with the wrong language, then assertion failures can
4144 be triggered later on. See PR symtab/23010. So, tell
4145 dw2_instantiate_symtab to skip partial CUs -- any important
4146 partial CU will be read via DW_TAG_imported_unit anyway. */
4147 dw2_instantiate_symtab (per_cu, true);
9291a0cd
TT
4148 }
4149}
4150
4151static void
652a8996
JK
4152dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4153 const char *fullname)
9291a0cd 4154{
ed2dc618
SM
4155 struct dwarf2_per_objfile *dwarf2_per_objfile
4156 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
4157
4158 /* We don't need to consider type units here.
4159 This is only called for examining code, e.g. expand_line_sal.
4160 There can be an order of magnitude (or more) more type units
4161 than comp units, and we avoid them if we can. */
4162
b76e467d 4163 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 4164 {
3d7bb9d9 4165 /* We only need to look at symtabs not already expanded. */
43f3e411 4166 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4167 continue;
4168
b76e467d 4169 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 4170 if (file_data == NULL)
9291a0cd
TT
4171 continue;
4172
b76e467d 4173 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4174 {
652a8996
JK
4175 const char *this_fullname = file_data->file_names[j];
4176
4177 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 4178 {
58f0c718 4179 dw2_instantiate_symtab (per_cu, false);
9291a0cd
TT
4180 break;
4181 }
4182 }
4183 }
4184}
4185
9291a0cd 4186static void
199b4314
TT
4187dw2_map_matching_symbols
4188 (struct objfile *objfile,
b054970d 4189 const lookup_name_info &name, domain_enum domain,
199b4314
TT
4190 int global,
4191 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 4192 symbol_compare_ftype *ordered_compare)
9291a0cd 4193{
40658b94 4194 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
4195 current language is Ada for a non-Ada objfile using GNU index. As Ada
4196 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
4197}
4198
b5ec771e
PA
4199/* Symbol name matcher for .gdb_index names.
4200
4201 Symbol names in .gdb_index have a few particularities:
4202
4203 - There's no indication of which is the language of each symbol.
4204
4205 Since each language has its own symbol name matching algorithm,
4206 and we don't know which language is the right one, we must match
3f563c84
PA
4207 each symbol against all languages. This would be a potential
4208 performance problem if it were not mitigated by the
4209 mapped_index::name_components lookup table, which significantly
4210 reduces the number of times we need to call into this matcher,
4211 making it a non-issue.
b5ec771e
PA
4212
4213 - Symbol names in the index have no overload (parameter)
4214 information. I.e., in C++, "foo(int)" and "foo(long)" both
4215 appear as "foo" in the index, for example.
4216
4217 This means that the lookup names passed to the symbol name
4218 matcher functions must have no parameter information either
4219 because (e.g.) symbol search name "foo" does not match
4220 lookup-name "foo(int)" [while swapping search name for lookup
4221 name would match].
4222*/
4223class gdb_index_symbol_name_matcher
4224{
4225public:
4226 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4227 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4228
4229 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4230 Returns true if any matcher matches. */
4231 bool matches (const char *symbol_name);
4232
4233private:
4234 /* A reference to the lookup name we're matching against. */
4235 const lookup_name_info &m_lookup_name;
4236
4237 /* A vector holding all the different symbol name matchers, for all
4238 languages. */
4239 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4240};
4241
4242gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4243 (const lookup_name_info &lookup_name)
4244 : m_lookup_name (lookup_name)
4245{
4246 /* Prepare the vector of comparison functions upfront, to avoid
4247 doing the same work for each symbol. Care is taken to avoid
4248 matching with the same matcher more than once if/when multiple
4249 languages use the same matcher function. */
4250 auto &matchers = m_symbol_name_matcher_funcs;
4251 matchers.reserve (nr_languages);
4252
4253 matchers.push_back (default_symbol_name_matcher);
4254
4255 for (int i = 0; i < nr_languages; i++)
4256 {
4257 const language_defn *lang = language_def ((enum language) i);
c63d3e8d 4258 symbol_name_matcher_ftype *name_matcher
618daa93 4259 = get_symbol_name_matcher (lang, m_lookup_name);
c63d3e8d
PA
4260
4261 /* Don't insert the same comparison routine more than once.
4262 Note that we do this linear walk instead of a seemingly
4263 cheaper sorted insert, or use a std::set or something like
4264 that, because relative order of function addresses is not
4265 stable. This is not a problem in practice because the number
4266 of supported languages is low, and the cost here is tiny
4267 compared to the number of searches we'll do afterwards using
4268 this object. */
4269 if (name_matcher != default_symbol_name_matcher
4270 && (std::find (matchers.begin (), matchers.end (), name_matcher)
4271 == matchers.end ()))
4272 matchers.push_back (name_matcher);
b5ec771e
PA
4273 }
4274}
4275
4276bool
4277gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4278{
4279 for (auto matches_name : m_symbol_name_matcher_funcs)
4280 if (matches_name (symbol_name, m_lookup_name, NULL))
4281 return true;
4282
4283 return false;
4284}
4285
e1ef7d7a
PA
4286/* Starting from a search name, return the string that finds the upper
4287 bound of all strings that start with SEARCH_NAME in a sorted name
4288 list. Returns the empty string to indicate that the upper bound is
4289 the end of the list. */
4290
4291static std::string
4292make_sort_after_prefix_name (const char *search_name)
4293{
4294 /* When looking to complete "func", we find the upper bound of all
4295 symbols that start with "func" by looking for where we'd insert
4296 the closest string that would follow "func" in lexicographical
4297 order. Usually, that's "func"-with-last-character-incremented,
4298 i.e. "fund". Mind non-ASCII characters, though. Usually those
4299 will be UTF-8 multi-byte sequences, but we can't be certain.
4300 Especially mind the 0xff character, which is a valid character in
4301 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4302 rule out compilers allowing it in identifiers. Note that
4303 conveniently, strcmp/strcasecmp are specified to compare
4304 characters interpreted as unsigned char. So what we do is treat
4305 the whole string as a base 256 number composed of a sequence of
4306 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4307 to 0, and carries 1 to the following more-significant position.
4308 If the very first character in SEARCH_NAME ends up incremented
4309 and carries/overflows, then the upper bound is the end of the
4310 list. The string after the empty string is also the empty
4311 string.
4312
4313 Some examples of this operation:
4314
4315 SEARCH_NAME => "+1" RESULT
4316
4317 "abc" => "abd"
4318 "ab\xff" => "ac"
4319 "\xff" "a" "\xff" => "\xff" "b"
4320 "\xff" => ""
4321 "\xff\xff" => ""
4322 "" => ""
4323
4324 Then, with these symbols for example:
4325
4326 func
4327 func1
4328 fund
4329
4330 completing "func" looks for symbols between "func" and
4331 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4332 which finds "func" and "func1", but not "fund".
4333
4334 And with:
4335
4336 funcÿ (Latin1 'ÿ' [0xff])
4337 funcÿ1
4338 fund
4339
4340 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4341 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4342
4343 And with:
4344
4345 ÿÿ (Latin1 'ÿ' [0xff])
4346 ÿÿ1
4347
4348 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4349 the end of the list.
4350 */
4351 std::string after = search_name;
4352 while (!after.empty () && (unsigned char) after.back () == 0xff)
4353 after.pop_back ();
4354 if (!after.empty ())
4355 after.back () = (unsigned char) after.back () + 1;
4356 return after;
4357}
4358
5c58de74 4359/* See declaration. */
61d96d7e 4360
5c58de74
PA
4361std::pair<std::vector<name_component>::const_iterator,
4362 std::vector<name_component>::const_iterator>
44ed8f3e 4363mapped_index_base::find_name_components_bounds
5c58de74 4364 (const lookup_name_info &lookup_name_without_params) const
3f563c84 4365{
5c58de74
PA
4366 auto *name_cmp
4367 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84
PA
4368
4369 const char *cplus
c62446b1 4370 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
9291a0cd 4371
3f563c84
PA
4372 /* Comparison function object for lower_bound that matches against a
4373 given symbol name. */
4374 auto lookup_compare_lower = [&] (const name_component &elem,
4375 const char *name)
4376 {
5c58de74 4377 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4378 const char *elem_name = elem_qualified + elem.name_offset;
4379 return name_cmp (elem_name, name) < 0;
4380 };
4381
4382 /* Comparison function object for upper_bound that matches against a
4383 given symbol name. */
4384 auto lookup_compare_upper = [&] (const char *name,
4385 const name_component &elem)
4386 {
5c58de74 4387 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4388 const char *elem_name = elem_qualified + elem.name_offset;
4389 return name_cmp (name, elem_name) < 0;
4390 };
4391
5c58de74
PA
4392 auto begin = this->name_components.begin ();
4393 auto end = this->name_components.end ();
3f563c84
PA
4394
4395 /* Find the lower bound. */
4396 auto lower = [&] ()
4397 {
5c58de74 4398 if (lookup_name_without_params.completion_mode () && cplus[0] == '\0')
3f563c84
PA
4399 return begin;
4400 else
4401 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4402 } ();
4403
4404 /* Find the upper bound. */
4405 auto upper = [&] ()
4406 {
5c58de74 4407 if (lookup_name_without_params.completion_mode ())
3f563c84 4408 {
e1ef7d7a
PA
4409 /* In completion mode, we want UPPER to point past all
4410 symbols names that have the same prefix. I.e., with
4411 these symbols, and completing "func":
4412
4413 function << lower bound
4414 function1
4415 other_function << upper bound
4416
4417 We find the upper bound by looking for the insertion
4418 point of "func"-with-last-character-incremented,
4419 i.e. "fund". */
4420 std::string after = make_sort_after_prefix_name (cplus);
4421 if (after.empty ())
3f563c84 4422 return end;
e6b2f5ef
PA
4423 return std::lower_bound (lower, end, after.c_str (),
4424 lookup_compare_lower);
3f563c84
PA
4425 }
4426 else
4427 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4428 } ();
4429
5c58de74
PA
4430 return {lower, upper};
4431}
4432
4433/* See declaration. */
4434
4435void
44ed8f3e 4436mapped_index_base::build_name_components ()
5c58de74
PA
4437{
4438 if (!this->name_components.empty ())
4439 return;
4440
4441 this->name_components_casing = case_sensitivity;
4442 auto *name_cmp
4443 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4444
4445 /* The code below only knows how to break apart components of C++
4446 symbol names (and other languages that use '::' as
4447 namespace/module separator). If we add support for wild matching
4448 to some language that uses some other operator (E.g., Ada, Go and
4449 D use '.'), then we'll need to try splitting the symbol name
4450 according to that language too. Note that Ada does support wild
4451 matching, but doesn't currently support .gdb_index. */
44ed8f3e
PA
4452 auto count = this->symbol_name_count ();
4453 for (offset_type idx = 0; idx < count; idx++)
5c58de74 4454 {
44ed8f3e 4455 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
4456 continue;
4457
4458 const char *name = this->symbol_name_at (idx);
4459
4460 /* Add each name component to the name component table. */
4461 unsigned int previous_len = 0;
4462 for (unsigned int current_len = cp_find_first_component (name);
4463 name[current_len] != '\0';
4464 current_len += cp_find_first_component (name + current_len))
4465 {
4466 gdb_assert (name[current_len] == ':');
4467 this->name_components.push_back ({previous_len, idx});
4468 /* Skip the '::'. */
4469 current_len += 2;
4470 previous_len = current_len;
4471 }
4472 this->name_components.push_back ({previous_len, idx});
4473 }
4474
4475 /* Sort name_components elements by name. */
4476 auto name_comp_compare = [&] (const name_component &left,
4477 const name_component &right)
4478 {
4479 const char *left_qualified = this->symbol_name_at (left.idx);
4480 const char *right_qualified = this->symbol_name_at (right.idx);
4481
4482 const char *left_name = left_qualified + left.name_offset;
4483 const char *right_name = right_qualified + right.name_offset;
4484
4485 return name_cmp (left_name, right_name) < 0;
4486 };
4487
4488 std::sort (this->name_components.begin (),
4489 this->name_components.end (),
4490 name_comp_compare);
4491}
4492
4493/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4494 mapped_index_base instead of the containing objfile. This is split
4495 to a separate function in order to be able to unit test the
4496 name_components matching using a mock mapped_index_base. For each
5c58de74 4497 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4498 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4499
4500static void
4501dw2_expand_symtabs_matching_symbol
44ed8f3e 4502 (mapped_index_base &index,
5c58de74
PA
4503 const lookup_name_info &lookup_name_in,
4504 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4505 enum search_domain kind,
4506 gdb::function_view<void (offset_type)> match_callback)
4507{
4508 lookup_name_info lookup_name_without_params
4509 = lookup_name_in.make_ignore_params ();
4510 gdb_index_symbol_name_matcher lookup_name_matcher
4511 (lookup_name_without_params);
4512
4513 /* Build the symbol name component sorted vector, if we haven't
4514 yet. */
4515 index.build_name_components ();
4516
4517 auto bounds = index.find_name_components_bounds (lookup_name_without_params);
4518
3f563c84
PA
4519 /* Now for each symbol name in range, check to see if we have a name
4520 match, and if so, call the MATCH_CALLBACK callback. */
4521
4522 /* The same symbol may appear more than once in the range though.
4523 E.g., if we're looking for symbols that complete "w", and we have
4524 a symbol named "w1::w2", we'll find the two name components for
4525 that same symbol in the range. To be sure we only call the
4526 callback once per symbol, we first collect the symbol name
4527 indexes that matched in a temporary vector and ignore
4528 duplicates. */
4529 std::vector<offset_type> matches;
5c58de74 4530 matches.reserve (std::distance (bounds.first, bounds.second));
3f563c84 4531
5c58de74 4532 for (; bounds.first != bounds.second; ++bounds.first)
3f563c84 4533 {
5c58de74 4534 const char *qualified = index.symbol_name_at (bounds.first->idx);
3f563c84
PA
4535
4536 if (!lookup_name_matcher.matches (qualified)
4537 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
9291a0cd
TT
4538 continue;
4539
5c58de74 4540 matches.push_back (bounds.first->idx);
3f563c84
PA
4541 }
4542
4543 std::sort (matches.begin (), matches.end ());
4544
4545 /* Finally call the callback, once per match. */
4546 ULONGEST prev = -1;
4547 for (offset_type idx : matches)
4548 {
4549 if (prev != idx)
4550 {
4551 match_callback (idx);
4552 prev = idx;
4553 }
4554 }
4555
4556 /* Above we use a type wider than idx's for 'prev', since 0 and
4557 (offset_type)-1 are both possible values. */
4558 static_assert (sizeof (prev) > sizeof (offset_type), "");
4559}
4560
c62446b1
PA
4561#if GDB_SELF_TEST
4562
4563namespace selftests { namespace dw2_expand_symtabs_matching {
4564
a3c5fafd
PA
4565/* A mock .gdb_index/.debug_names-like name index table, enough to
4566 exercise dw2_expand_symtabs_matching_symbol, which works with the
4567 mapped_index_base interface. Builds an index from the symbol list
4568 passed as parameter to the constructor. */
4569class mock_mapped_index : public mapped_index_base
c62446b1
PA
4570{
4571public:
a3c5fafd
PA
4572 mock_mapped_index (gdb::array_view<const char *> symbols)
4573 : m_symbol_table (symbols)
c62446b1
PA
4574 {}
4575
a3c5fafd 4576 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4577
a3c5fafd 4578 /* Return the number of names in the symbol table. */
632e107b 4579 size_t symbol_name_count () const override
c62446b1 4580 {
a3c5fafd 4581 return m_symbol_table.size ();
c62446b1
PA
4582 }
4583
a3c5fafd 4584 /* Get the name of the symbol at IDX in the symbol table. */
632e107b 4585 const char *symbol_name_at (offset_type idx) const override
a3c5fafd
PA
4586 {
4587 return m_symbol_table[idx];
4588 }
c62446b1 4589
a3c5fafd
PA
4590private:
4591 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4592};
4593
4594/* Convenience function that converts a NULL pointer to a "<null>"
4595 string, to pass to print routines. */
4596
4597static const char *
4598string_or_null (const char *str)
4599{
4600 return str != NULL ? str : "<null>";
4601}
4602
4603/* Check if a lookup_name_info built from
4604 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4605 index. EXPECTED_LIST is the list of expected matches, in expected
4606 matching order. If no match expected, then an empty list is
4607 specified. Returns true on success. On failure prints a warning
4608 indicating the file:line that failed, and returns false. */
4609
4610static bool
4611check_match (const char *file, int line,
4612 mock_mapped_index &mock_index,
4613 const char *name, symbol_name_match_type match_type,
4614 bool completion_mode,
4615 std::initializer_list<const char *> expected_list)
4616{
4617 lookup_name_info lookup_name (name, match_type, completion_mode);
4618
4619 bool matched = true;
4620
4621 auto mismatch = [&] (const char *expected_str,
4622 const char *got)
4623 {
4624 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4625 "expected=\"%s\", got=\"%s\"\n"),
4626 file, line,
4627 (match_type == symbol_name_match_type::FULL
4628 ? "FULL" : "WILD"),
4629 name, string_or_null (expected_str), string_or_null (got));
4630 matched = false;
4631 };
4632
4633 auto expected_it = expected_list.begin ();
4634 auto expected_end = expected_list.end ();
4635
a3c5fafd 4636 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4637 NULL, ALL_DOMAIN,
4638 [&] (offset_type idx)
4639 {
a3c5fafd 4640 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4641 const char *expected_str
4642 = expected_it == expected_end ? NULL : *expected_it++;
4643
4644 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4645 mismatch (expected_str, matched_name);
4646 });
4647
4648 const char *expected_str
4649 = expected_it == expected_end ? NULL : *expected_it++;
4650 if (expected_str != NULL)
4651 mismatch (expected_str, NULL);
4652
4653 return matched;
4654}
4655
4656/* The symbols added to the mock mapped_index for testing (in
4657 canonical form). */
4658static const char *test_symbols[] = {
4659 "function",
4660 "std::bar",
4661 "std::zfunction",
4662 "std::zfunction2",
4663 "w1::w2",
4664 "ns::foo<char*>",
4665 "ns::foo<int>",
4666 "ns::foo<long>",
a20714ff
PA
4667 "ns2::tmpl<int>::foo2",
4668 "(anonymous namespace)::A::B::C",
c62446b1 4669
e1ef7d7a
PA
4670 /* These are used to check that the increment-last-char in the
4671 matching algorithm for completion doesn't match "t1_fund" when
4672 completing "t1_func". */
4673 "t1_func",
4674 "t1_func1",
4675 "t1_fund",
4676 "t1_fund1",
4677
4678 /* A UTF-8 name with multi-byte sequences to make sure that
4679 cp-name-parser understands this as a single identifier ("função"
4680 is "function" in PT). */
4681 u8"u8função",
4682
4683 /* \377 (0xff) is Latin1 'ÿ'. */
4684 "yfunc\377",
4685
4686 /* \377 (0xff) is Latin1 'ÿ'. */
4687 "\377",
4688 "\377\377123",
4689
c62446b1
PA
4690 /* A name with all sorts of complications. Starts with "z" to make
4691 it easier for the completion tests below. */
4692#define Z_SYM_NAME \
4693 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4694 "::tuple<(anonymous namespace)::ui*, " \
4695 "std::default_delete<(anonymous namespace)::ui>, void>"
4696
4697 Z_SYM_NAME
4698};
4699
a3c5fafd
PA
4700/* Returns true if the mapped_index_base::find_name_component_bounds
4701 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4702 in completion mode. */
5c58de74
PA
4703
4704static bool
a3c5fafd 4705check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4706 const char *search_name,
4707 gdb::array_view<const char *> expected_syms)
4708{
4709 lookup_name_info lookup_name (search_name,
4710 symbol_name_match_type::FULL, true);
4711
4712 auto bounds = index.find_name_components_bounds (lookup_name);
4713
4714 size_t distance = std::distance (bounds.first, bounds.second);
4715 if (distance != expected_syms.size ())
4716 return false;
4717
4718 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4719 {
4720 auto nc_elem = bounds.first + exp_elem;
4721 const char *qualified = index.symbol_name_at (nc_elem->idx);
4722 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4723 return false;
4724 }
4725
4726 return true;
4727}
4728
4729/* Test the lower-level mapped_index::find_name_component_bounds
4730 method. */
4731
c62446b1 4732static void
5c58de74
PA
4733test_mapped_index_find_name_component_bounds ()
4734{
4735 mock_mapped_index mock_index (test_symbols);
4736
a3c5fafd 4737 mock_index.build_name_components ();
5c58de74
PA
4738
4739 /* Test the lower-level mapped_index::find_name_component_bounds
4740 method in completion mode. */
4741 {
4742 static const char *expected_syms[] = {
4743 "t1_func",
4744 "t1_func1",
5c58de74
PA
4745 };
4746
a3c5fafd 4747 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4748 "t1_func", expected_syms));
4749 }
4750
4751 /* Check that the increment-last-char in the name matching algorithm
4752 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4753 {
4754 static const char *expected_syms1[] = {
4755 "\377",
4756 "\377\377123",
4757 };
a3c5fafd 4758 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4759 "\377", expected_syms1));
4760
4761 static const char *expected_syms2[] = {
4762 "\377\377123",
4763 };
a3c5fafd 4764 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4765 "\377\377", expected_syms2));
4766 }
4767}
4768
4769/* Test dw2_expand_symtabs_matching_symbol. */
4770
4771static void
4772test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4773{
4774 mock_mapped_index mock_index (test_symbols);
4775
4776 /* We let all tests run until the end even if some fails, for debug
4777 convenience. */
4778 bool any_mismatch = false;
4779
4780 /* Create the expected symbols list (an initializer_list). Needed
4781 because lists have commas, and we need to pass them to CHECK,
4782 which is a macro. */
4783#define EXPECT(...) { __VA_ARGS__ }
4784
4785 /* Wrapper for check_match that passes down the current
4786 __FILE__/__LINE__. */
4787#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4788 any_mismatch |= !check_match (__FILE__, __LINE__, \
4789 mock_index, \
4790 NAME, MATCH_TYPE, COMPLETION_MODE, \
4791 EXPECTED_LIST)
4792
4793 /* Identity checks. */
4794 for (const char *sym : test_symbols)
4795 {
4796 /* Should be able to match all existing symbols. */
4797 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4798 EXPECT (sym));
4799
4800 /* Should be able to match all existing symbols with
4801 parameters. */
4802 std::string with_params = std::string (sym) + "(int)";
4803 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4804 EXPECT (sym));
4805
4806 /* Should be able to match all existing symbols with
4807 parameters and qualifiers. */
4808 with_params = std::string (sym) + " ( int ) const";
4809 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4810 EXPECT (sym));
4811
4812 /* This should really find sym, but cp-name-parser.y doesn't
4813 know about lvalue/rvalue qualifiers yet. */
4814 with_params = std::string (sym) + " ( int ) &&";
4815 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4816 {});
4817 }
4818
e1ef7d7a
PA
4819 /* Check that the name matching algorithm for completion doesn't get
4820 confused with Latin1 'ÿ' / 0xff. */
4821 {
4822 static const char str[] = "\377";
4823 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4824 EXPECT ("\377", "\377\377123"));
4825 }
4826
4827 /* Check that the increment-last-char in the matching algorithm for
4828 completion doesn't match "t1_fund" when completing "t1_func". */
4829 {
4830 static const char str[] = "t1_func";
4831 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4832 EXPECT ("t1_func", "t1_func1"));
4833 }
4834
c62446b1
PA
4835 /* Check that completion mode works at each prefix of the expected
4836 symbol name. */
4837 {
4838 static const char str[] = "function(int)";
4839 size_t len = strlen (str);
4840 std::string lookup;
4841
4842 for (size_t i = 1; i < len; i++)
4843 {
4844 lookup.assign (str, i);
4845 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4846 EXPECT ("function"));
4847 }
4848 }
4849
4850 /* While "w" is a prefix of both components, the match function
4851 should still only be called once. */
4852 {
4853 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4854 EXPECT ("w1::w2"));
a20714ff
PA
4855 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4856 EXPECT ("w1::w2"));
c62446b1
PA
4857 }
4858
4859 /* Same, with a "complicated" symbol. */
4860 {
4861 static const char str[] = Z_SYM_NAME;
4862 size_t len = strlen (str);
4863 std::string lookup;
4864
4865 for (size_t i = 1; i < len; i++)
4866 {
4867 lookup.assign (str, i);
4868 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4869 EXPECT (Z_SYM_NAME));
4870 }
4871 }
4872
4873 /* In FULL mode, an incomplete symbol doesn't match. */
4874 {
4875 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4876 {});
4877 }
4878
4879 /* A complete symbol with parameters matches any overload, since the
4880 index has no overload info. */
4881 {
4882 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4883 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4884 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4885 EXPECT ("std::zfunction", "std::zfunction2"));
4886 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4887 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4888 }
4889
4890 /* Check that whitespace is ignored appropriately. A symbol with a
4891 template argument list. */
4892 {
4893 static const char expected[] = "ns::foo<int>";
4894 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4895 EXPECT (expected));
a20714ff
PA
4896 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4897 EXPECT (expected));
c62446b1
PA
4898 }
4899
4900 /* Check that whitespace is ignored appropriately. A symbol with a
4901 template argument list that includes a pointer. */
4902 {
4903 static const char expected[] = "ns::foo<char*>";
4904 /* Try both completion and non-completion modes. */
4905 static const bool completion_mode[2] = {false, true};
4906 for (size_t i = 0; i < 2; i++)
4907 {
4908 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4909 completion_mode[i], EXPECT (expected));
a20714ff
PA
4910 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4911 completion_mode[i], EXPECT (expected));
c62446b1
PA
4912
4913 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4914 completion_mode[i], EXPECT (expected));
a20714ff
PA
4915 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4916 completion_mode[i], EXPECT (expected));
c62446b1
PA
4917 }
4918 }
4919
4920 {
4921 /* Check method qualifiers are ignored. */
4922 static const char expected[] = "ns::foo<char*>";
4923 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4924 symbol_name_match_type::FULL, true, EXPECT (expected));
4925 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4926 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4927 CHECK_MATCH ("foo < char * > ( int ) const",
4928 symbol_name_match_type::WILD, true, EXPECT (expected));
4929 CHECK_MATCH ("foo < char * > ( int ) &&",
4930 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4931 }
4932
4933 /* Test lookup names that don't match anything. */
4934 {
a20714ff
PA
4935 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4936 {});
4937
c62446b1
PA
4938 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4939 {});
4940 }
4941
a20714ff
PA
4942 /* Some wild matching tests, exercising "(anonymous namespace)",
4943 which should not be confused with a parameter list. */
4944 {
4945 static const char *syms[] = {
4946 "A::B::C",
4947 "B::C",
4948 "C",
4949 "A :: B :: C ( int )",
4950 "B :: C ( int )",
4951 "C ( int )",
4952 };
4953
4954 for (const char *s : syms)
4955 {
4956 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4957 EXPECT ("(anonymous namespace)::A::B::C"));
4958 }
4959 }
4960
4961 {
4962 static const char expected[] = "ns2::tmpl<int>::foo2";
4963 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4964 EXPECT (expected));
4965 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4966 EXPECT (expected));
4967 }
4968
c62446b1
PA
4969 SELF_CHECK (!any_mismatch);
4970
4971#undef EXPECT
4972#undef CHECK_MATCH
4973}
4974
5c58de74
PA
4975static void
4976run_test ()
4977{
4978 test_mapped_index_find_name_component_bounds ();
4979 test_dw2_expand_symtabs_matching_symbol ();
4980}
4981
c62446b1
PA
4982}} // namespace selftests::dw2_expand_symtabs_matching
4983
4984#endif /* GDB_SELF_TEST */
4985
4b514bc8
JK
4986/* If FILE_MATCHER is NULL or if PER_CU has
4987 dwarf2_per_cu_quick_data::MARK set (see
4988 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4989 EXPANSION_NOTIFY on it. */
4990
4991static void
4992dw2_expand_symtabs_matching_one
4993 (struct dwarf2_per_cu_data *per_cu,
4994 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4995 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4996{
4997 if (file_matcher == NULL || per_cu->v.quick->mark)
4998 {
4999 bool symtab_was_null
5000 = (per_cu->v.quick->compunit_symtab == NULL);
5001
58f0c718 5002 dw2_instantiate_symtab (per_cu, false);
4b514bc8
JK
5003
5004 if (expansion_notify != NULL
5005 && symtab_was_null
5006 && per_cu->v.quick->compunit_symtab != NULL)
5007 expansion_notify (per_cu->v.quick->compunit_symtab);
5008 }
5009}
5010
3f563c84
PA
5011/* Helper for dw2_expand_matching symtabs. Called on each symbol
5012 matched, to expand corresponding CUs that were marked. IDX is the
5013 index of the symbol name that matched. */
5014
5015static void
5016dw2_expand_marked_cus
ed2dc618 5017 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
5018 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5019 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5020 search_domain kind)
5021{
3f563c84
PA
5022 offset_type *vec, vec_len, vec_idx;
5023 bool global_seen = false;
ed2dc618 5024 mapped_index &index = *dwarf2_per_objfile->index_table;
3f563c84 5025
61920122 5026 vec = (offset_type *) (index.constant_pool
f00a2de2 5027 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
5028 vec_len = MAYBE_SWAP (vec[0]);
5029 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5030 {
61920122
PA
5031 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5032 /* This value is only valid for index versions >= 7. */
5033 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5034 gdb_index_symbol_kind symbol_kind =
5035 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5036 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5037 /* Only check the symbol attributes if they're present.
5038 Indices prior to version 7 don't record them,
5039 and indices >= 7 may elide them for certain symbols
5040 (gold does this). */
5041 int attrs_valid =
5042 (index.version >= 7
5043 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5044
5045 /* Work around gold/15646. */
5046 if (attrs_valid)
9291a0cd 5047 {
61920122
PA
5048 if (!is_static && global_seen)
5049 continue;
5050 if (!is_static)
5051 global_seen = true;
5052 }
3190f0c6 5053
61920122
PA
5054 /* Only check the symbol's kind if it has one. */
5055 if (attrs_valid)
5056 {
5057 switch (kind)
8943b874 5058 {
61920122
PA
5059 case VARIABLES_DOMAIN:
5060 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5061 continue;
5062 break;
5063 case FUNCTIONS_DOMAIN:
5064 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 5065 continue;
61920122
PA
5066 break;
5067 case TYPES_DOMAIN:
5068 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5069 continue;
5070 break;
5071 default:
5072 break;
8943b874 5073 }
61920122 5074 }
8943b874 5075
61920122 5076 /* Don't crash on bad data. */
b76e467d 5077 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 5078 + dwarf2_per_objfile->all_type_units.size ()))
61920122 5079 {
b98664d3 5080 complaint (_(".gdb_index entry has bad CU index"
ed2dc618
SM
5081 " [in module %s]"),
5082 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
5083 continue;
5084 }
5085
ff4c9fec 5086 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4b514bc8
JK
5087 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5088 expansion_notify);
61920122
PA
5089 }
5090}
5091
4b514bc8
JK
5092/* If FILE_MATCHER is non-NULL, set all the
5093 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5094 that match FILE_MATCHER. */
5095
61920122 5096static void
4b514bc8 5097dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
5098 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5099 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 5100{
4b514bc8 5101 if (file_matcher == NULL)
61920122
PA
5102 return;
5103
4b514bc8
JK
5104 objfile *const objfile = dwarf2_per_objfile->objfile;
5105
5106 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5107 htab_eq_pointer,
5108 NULL, xcalloc, xfree));
5109 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
5110 htab_eq_pointer,
5111 NULL, xcalloc, xfree));
61920122 5112
4b514bc8
JK
5113 /* The rule is CUs specify all the files, including those used by
5114 any TU, so there's no need to scan TUs here. */
61920122 5115
b76e467d 5116 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5117 {
927aa2e7
JK
5118 QUIT;
5119
5120 per_cu->v.quick->mark = 0;
5121
5122 /* We only need to look at symtabs not already expanded. */
5123 if (per_cu->v.quick->compunit_symtab)
5124 continue;
5125
b76e467d 5126 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
5127 if (file_data == NULL)
5128 continue;
5129
5130 if (htab_find (visited_not_found.get (), file_data) != NULL)
5131 continue;
5132 else if (htab_find (visited_found.get (), file_data) != NULL)
5133 {
5134 per_cu->v.quick->mark = 1;
5135 continue;
5136 }
5137
b76e467d 5138 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
5139 {
5140 const char *this_real_name;
5141
5142 if (file_matcher (file_data->file_names[j], false))
5143 {
5144 per_cu->v.quick->mark = 1;
5145 break;
5146 }
5147
5148 /* Before we invoke realpath, which can get expensive when many
5149 files are involved, do a quick comparison of the basenames. */
5150 if (!basenames_may_differ
5151 && !file_matcher (lbasename (file_data->file_names[j]),
5152 true))
5153 continue;
5154
5155 this_real_name = dw2_get_real_path (objfile, file_data, j);
5156 if (file_matcher (this_real_name, false))
5157 {
5158 per_cu->v.quick->mark = 1;
5159 break;
5160 }
5161 }
5162
b76e467d
SM
5163 void **slot = htab_find_slot (per_cu->v.quick->mark
5164 ? visited_found.get ()
5165 : visited_not_found.get (),
5166 file_data, INSERT);
927aa2e7
JK
5167 *slot = file_data;
5168 }
5169}
5170
5171static void
5172dw2_expand_symtabs_matching
5173 (struct objfile *objfile,
5174 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5175 const lookup_name_info &lookup_name,
5176 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5177 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5178 enum search_domain kind)
5179{
ed2dc618
SM
5180 struct dwarf2_per_objfile *dwarf2_per_objfile
5181 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5182
5183 /* index_table is NULL if OBJF_READNOW. */
5184 if (!dwarf2_per_objfile->index_table)
5185 return;
5186
ed2dc618 5187 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7
JK
5188
5189 mapped_index &index = *dwarf2_per_objfile->index_table;
5190
5191 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5192 symbol_matcher,
5193 kind, [&] (offset_type idx)
5194 {
ed2dc618 5195 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7
JK
5196 expansion_notify, kind);
5197 });
5198}
5199
5200/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5201 symtab. */
5202
5203static struct compunit_symtab *
5204recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5205 CORE_ADDR pc)
5206{
5207 int i;
5208
5209 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5210 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5211 return cust;
5212
5213 if (cust->includes == NULL)
5214 return NULL;
5215
5216 for (i = 0; cust->includes[i]; ++i)
5217 {
5218 struct compunit_symtab *s = cust->includes[i];
5219
5220 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5221 if (s != NULL)
5222 return s;
5223 }
5224
5225 return NULL;
5226}
5227
5228static struct compunit_symtab *
5229dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5230 struct bound_minimal_symbol msymbol,
5231 CORE_ADDR pc,
5232 struct obj_section *section,
5233 int warn_if_readin)
5234{
5235 struct dwarf2_per_cu_data *data;
5236 struct compunit_symtab *result;
5237
d320c2b5 5238 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
5239 return NULL;
5240
79748972
TT
5241 CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
5242 SECT_OFF_TEXT (objfile));
d320c2b5
TT
5243 data = (struct dwarf2_per_cu_data *) addrmap_find
5244 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
5245 if (!data)
5246 return NULL;
5247
5248 if (warn_if_readin && data->v.quick->compunit_symtab)
5249 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5250 paddress (get_objfile_arch (objfile), pc));
5251
5252 result
58f0c718
TT
5253 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
5254 false),
927aa2e7
JK
5255 pc);
5256 gdb_assert (result != NULL);
5257 return result;
5258}
5259
5260static void
5261dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5262 void *data, int need_fullname)
5263{
ed2dc618
SM
5264 struct dwarf2_per_objfile *dwarf2_per_objfile
5265 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5266
5267 if (!dwarf2_per_objfile->filenames_cache)
5268 {
5269 dwarf2_per_objfile->filenames_cache.emplace ();
5270
5271 htab_up visited (htab_create_alloc (10,
5272 htab_hash_pointer, htab_eq_pointer,
5273 NULL, xcalloc, xfree));
5274
5275 /* The rule is CUs specify all the files, including those used
5276 by any TU, so there's no need to scan TUs here. We can
5277 ignore file names coming from already-expanded CUs. */
5278
b76e467d 5279 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5280 {
927aa2e7
JK
5281 if (per_cu->v.quick->compunit_symtab)
5282 {
5283 void **slot = htab_find_slot (visited.get (),
5284 per_cu->v.quick->file_names,
5285 INSERT);
5286
5287 *slot = per_cu->v.quick->file_names;
5288 }
5289 }
5290
b76e467d 5291 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5292 {
927aa2e7
JK
5293 /* We only need to look at symtabs not already expanded. */
5294 if (per_cu->v.quick->compunit_symtab)
5295 continue;
5296
b76e467d 5297 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
5298 if (file_data == NULL)
5299 continue;
5300
b76e467d 5301 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
5302 if (*slot)
5303 {
5304 /* Already visited. */
5305 continue;
5306 }
5307 *slot = file_data;
5308
5309 for (int j = 0; j < file_data->num_file_names; ++j)
5310 {
5311 const char *filename = file_data->file_names[j];
5312 dwarf2_per_objfile->filenames_cache->seen (filename);
5313 }
5314 }
5315 }
5316
5317 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5318 {
5319 gdb::unique_xmalloc_ptr<char> this_real_name;
5320
5321 if (need_fullname)
5322 this_real_name = gdb_realpath (filename);
5323 (*fun) (filename, this_real_name.get (), data);
5324 });
5325}
5326
5327static int
5328dw2_has_symbols (struct objfile *objfile)
5329{
5330 return 1;
5331}
5332
5333const struct quick_symbol_functions dwarf2_gdb_index_functions =
5334{
5335 dw2_has_symbols,
5336 dw2_find_last_source_symtab,
5337 dw2_forget_cached_source_info,
5338 dw2_map_symtabs_matching_filename,
5339 dw2_lookup_symbol,
5340 dw2_print_stats,
5341 dw2_dump,
927aa2e7
JK
5342 dw2_expand_symtabs_for_function,
5343 dw2_expand_all_symtabs,
5344 dw2_expand_symtabs_with_fullname,
5345 dw2_map_matching_symbols,
5346 dw2_expand_symtabs_matching,
5347 dw2_find_pc_sect_compunit_symtab,
5348 NULL,
5349 dw2_map_symbol_filenames
5350};
5351
5352/* DWARF-5 debug_names reader. */
5353
5354/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5355static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5356
5357/* A helper function that reads the .debug_names section in SECTION
5358 and fills in MAP. FILENAME is the name of the file containing the
5359 section; it is used for error reporting.
5360
5361 Returns true if all went well, false otherwise. */
5362
5363static bool
5364read_debug_names_from_section (struct objfile *objfile,
5365 const char *filename,
5366 struct dwarf2_section_info *section,
5367 mapped_debug_names &map)
5368{
5369 if (dwarf2_section_empty_p (section))
5370 return false;
5371
5372 /* Older elfutils strip versions could keep the section in the main
5373 executable while splitting it for the separate debug info file. */
5374 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5375 return false;
5376
5377 dwarf2_read_section (objfile, section);
5378
5379 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5380
5381 const gdb_byte *addr = section->buffer;
5382
5383 bfd *const abfd = get_section_bfd_owner (section);
5384
5385 unsigned int bytes_read;
5386 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5387 addr += bytes_read;
5388
5389 map.dwarf5_is_dwarf64 = bytes_read != 4;
5390 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5391 if (bytes_read + length != section->size)
5392 {
5393 /* There may be multiple per-CU indices. */
5394 warning (_("Section .debug_names in %s length %s does not match "
5395 "section length %s, ignoring .debug_names."),
5396 filename, plongest (bytes_read + length),
5397 pulongest (section->size));
5398 return false;
5399 }
5400
5401 /* The version number. */
5402 uint16_t version = read_2_bytes (abfd, addr);
5403 addr += 2;
5404 if (version != 5)
5405 {
5406 warning (_("Section .debug_names in %s has unsupported version %d, "
5407 "ignoring .debug_names."),
5408 filename, version);
5409 return false;
5410 }
5411
5412 /* Padding. */
5413 uint16_t padding = read_2_bytes (abfd, addr);
5414 addr += 2;
5415 if (padding != 0)
5416 {
5417 warning (_("Section .debug_names in %s has unsupported padding %d, "
5418 "ignoring .debug_names."),
5419 filename, padding);
5420 return false;
5421 }
5422
5423 /* comp_unit_count - The number of CUs in the CU list. */
5424 map.cu_count = read_4_bytes (abfd, addr);
5425 addr += 4;
5426
5427 /* local_type_unit_count - The number of TUs in the local TU
5428 list. */
5429 map.tu_count = read_4_bytes (abfd, addr);
5430 addr += 4;
5431
5432 /* foreign_type_unit_count - The number of TUs in the foreign TU
5433 list. */
5434 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5435 addr += 4;
5436 if (foreign_tu_count != 0)
5437 {
5438 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5439 "ignoring .debug_names."),
5440 filename, static_cast<unsigned long> (foreign_tu_count));
5441 return false;
5442 }
5443
5444 /* bucket_count - The number of hash buckets in the hash lookup
5445 table. */
5446 map.bucket_count = read_4_bytes (abfd, addr);
5447 addr += 4;
5448
5449 /* name_count - The number of unique names in the index. */
5450 map.name_count = read_4_bytes (abfd, addr);
5451 addr += 4;
5452
5453 /* abbrev_table_size - The size in bytes of the abbreviations
5454 table. */
5455 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5456 addr += 4;
5457
5458 /* augmentation_string_size - The size in bytes of the augmentation
5459 string. This value is rounded up to a multiple of 4. */
5460 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5461 addr += 4;
5462 map.augmentation_is_gdb = ((augmentation_string_size
5463 == sizeof (dwarf5_augmentation))
5464 && memcmp (addr, dwarf5_augmentation,
5465 sizeof (dwarf5_augmentation)) == 0);
5466 augmentation_string_size += (-augmentation_string_size) & 3;
5467 addr += augmentation_string_size;
5468
5469 /* List of CUs */
5470 map.cu_table_reordered = addr;
5471 addr += map.cu_count * map.offset_size;
5472
5473 /* List of Local TUs */
5474 map.tu_table_reordered = addr;
5475 addr += map.tu_count * map.offset_size;
5476
5477 /* Hash Lookup Table */
5478 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5479 addr += map.bucket_count * 4;
5480 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5481 addr += map.name_count * 4;
5482
5483 /* Name Table */
5484 map.name_table_string_offs_reordered = addr;
5485 addr += map.name_count * map.offset_size;
5486 map.name_table_entry_offs_reordered = addr;
5487 addr += map.name_count * map.offset_size;
5488
5489 const gdb_byte *abbrev_table_start = addr;
5490 for (;;)
5491 {
927aa2e7
JK
5492 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5493 addr += bytes_read;
5494 if (index_num == 0)
5495 break;
5496
5497 const auto insertpair
5498 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5499 if (!insertpair.second)
5500 {
5501 warning (_("Section .debug_names in %s has duplicate index %s, "
5502 "ignoring .debug_names."),
5503 filename, pulongest (index_num));
5504 return false;
5505 }
5506 mapped_debug_names::index_val &indexval = insertpair.first->second;
5507 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5508 addr += bytes_read;
5509
5510 for (;;)
5511 {
5512 mapped_debug_names::index_val::attr attr;
5513 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5514 addr += bytes_read;
5515 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5516 addr += bytes_read;
5517 if (attr.form == DW_FORM_implicit_const)
5518 {
5519 attr.implicit_const = read_signed_leb128 (abfd, addr,
5520 &bytes_read);
5521 addr += bytes_read;
5522 }
5523 if (attr.dw_idx == 0 && attr.form == 0)
5524 break;
5525 indexval.attr_vec.push_back (std::move (attr));
5526 }
5527 }
5528 if (addr != abbrev_table_start + abbrev_table_size)
5529 {
5530 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
5531 "of size %s vs. written as %u, ignoring .debug_names."),
5532 filename, plongest (addr - abbrev_table_start),
5533 abbrev_table_size);
927aa2e7
JK
5534 return false;
5535 }
5536 map.entry_pool = addr;
5537
5538 return true;
5539}
5540
5541/* A helper for create_cus_from_debug_names that handles the MAP's CU
5542 list. */
5543
5544static void
ed2dc618 5545create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5546 const mapped_debug_names &map,
5547 dwarf2_section_info &section,
b76e467d 5548 bool is_dwz)
927aa2e7
JK
5549{
5550 sect_offset sect_off_prev;
5551 for (uint32_t i = 0; i <= map.cu_count; ++i)
5552 {
5553 sect_offset sect_off_next;
5554 if (i < map.cu_count)
5555 {
5556 sect_off_next
5557 = (sect_offset) (extract_unsigned_integer
5558 (map.cu_table_reordered + i * map.offset_size,
5559 map.offset_size,
5560 map.dwarf5_byte_order));
5561 }
5562 else
5563 sect_off_next = (sect_offset) section.size;
5564 if (i >= 1)
5565 {
5566 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5567 dwarf2_per_cu_data *per_cu
ed2dc618 5568 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7 5569 sect_off_prev, length);
b76e467d 5570 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
927aa2e7
JK
5571 }
5572 sect_off_prev = sect_off_next;
5573 }
5574}
5575
5576/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5577 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5578
5579static void
ed2dc618 5580create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5581 const mapped_debug_names &map,
5582 const mapped_debug_names &dwz_map)
5583{
b76e467d
SM
5584 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5585 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5586
ed2dc618
SM
5587 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5588 dwarf2_per_objfile->info,
b76e467d 5589 false /* is_dwz */);
927aa2e7
JK
5590
5591 if (dwz_map.cu_count == 0)
5592 return;
5593
ed2dc618
SM
5594 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5595 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
b76e467d 5596 true /* is_dwz */);
927aa2e7
JK
5597}
5598
5599/* Read .debug_names. If everything went ok, initialize the "quick"
5600 elements of all the CUs and return true. Otherwise, return false. */
5601
5602static bool
ed2dc618 5603dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5604{
22ca247e
TT
5605 std::unique_ptr<mapped_debug_names> map
5606 (new mapped_debug_names (dwarf2_per_objfile));
ed2dc618
SM
5607 mapped_debug_names dwz_map (dwarf2_per_objfile);
5608 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5609
5610 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5611 &dwarf2_per_objfile->debug_names,
22ca247e 5612 *map))
927aa2e7
JK
5613 return false;
5614
5615 /* Don't use the index if it's empty. */
22ca247e 5616 if (map->name_count == 0)
927aa2e7
JK
5617 return false;
5618
5619 /* If there is a .dwz file, read it so we can get its CU list as
5620 well. */
ed2dc618 5621 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
927aa2e7
JK
5622 if (dwz != NULL)
5623 {
5624 if (!read_debug_names_from_section (objfile,
5625 bfd_get_filename (dwz->dwz_bfd),
5626 &dwz->debug_names, dwz_map))
5627 {
5628 warning (_("could not read '.debug_names' section from %s; skipping"),
5629 bfd_get_filename (dwz->dwz_bfd));
5630 return false;
5631 }
5632 }
5633
22ca247e 5634 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
927aa2e7 5635
22ca247e 5636 if (map->tu_count != 0)
927aa2e7
JK
5637 {
5638 /* We can only handle a single .debug_types when we have an
5639 index. */
fd5866f6 5640 if (dwarf2_per_objfile->types.size () != 1)
927aa2e7
JK
5641 return false;
5642
fd5866f6 5643 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
927aa2e7
JK
5644
5645 create_signatured_type_table_from_debug_names
22ca247e 5646 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
927aa2e7
JK
5647 }
5648
ed2dc618
SM
5649 create_addrmap_from_aranges (dwarf2_per_objfile,
5650 &dwarf2_per_objfile->debug_aranges);
927aa2e7 5651
22ca247e 5652 dwarf2_per_objfile->debug_names_table = std::move (map);
927aa2e7
JK
5653 dwarf2_per_objfile->using_index = 1;
5654 dwarf2_per_objfile->quick_file_names_table =
b76e467d 5655 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
927aa2e7
JK
5656
5657 return true;
5658}
5659
927aa2e7
JK
5660/* Type used to manage iterating over all CUs looking for a symbol for
5661 .debug_names. */
5662
5663class dw2_debug_names_iterator
5664{
5665public:
927aa2e7 5666 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5667 gdb::optional<block_enum> block_index,
5668 domain_enum domain,
927aa2e7 5669 const char *name)
2b79f376 5670 : m_map (map), m_block_index (block_index), m_domain (domain),
927aa2e7
JK
5671 m_addr (find_vec_in_debug_names (map, name))
5672 {}
5673
5674 dw2_debug_names_iterator (const mapped_debug_names &map,
5675 search_domain search, uint32_t namei)
5676 : m_map (map),
5677 m_search (search),
5678 m_addr (find_vec_in_debug_names (map, namei))
5679 {}
5680
5681 /* Return the next matching CU or NULL if there are no more. */
5682 dwarf2_per_cu_data *next ();
5683
5684private:
5685 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5686 const char *name);
5687 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5688 uint32_t namei);
5689
5690 /* The internalized form of .debug_names. */
5691 const mapped_debug_names &m_map;
5692
2b79f376
SM
5693 /* If set, only look for symbols that match that block. Valid values are
5694 GLOBAL_BLOCK and STATIC_BLOCK. */
5695 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5696
5697 /* The kind of symbol we're looking for. */
5698 const domain_enum m_domain = UNDEF_DOMAIN;
5699 const search_domain m_search = ALL_DOMAIN;
5700
5701 /* The list of CUs from the index entry of the symbol, or NULL if
5702 not found. */
5703 const gdb_byte *m_addr;
5704};
5705
5706const char *
5707mapped_debug_names::namei_to_name (uint32_t namei) const
5708{
5709 const ULONGEST namei_string_offs
5710 = extract_unsigned_integer ((name_table_string_offs_reordered
5711 + namei * offset_size),
5712 offset_size,
5713 dwarf5_byte_order);
5714 return read_indirect_string_at_offset
ed2dc618 5715 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
927aa2e7
JK
5716}
5717
5718/* Find a slot in .debug_names for the object named NAME. If NAME is
5719 found, return pointer to its pool data. If NAME cannot be found,
5720 return NULL. */
5721
5722const gdb_byte *
5723dw2_debug_names_iterator::find_vec_in_debug_names
5724 (const mapped_debug_names &map, const char *name)
5725{
5726 int (*cmp) (const char *, const char *);
5727
54ee4252 5728 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5729 if (current_language->la_language == language_cplus
5730 || current_language->la_language == language_fortran
5731 || current_language->la_language == language_d)
5732 {
5733 /* NAME is already canonical. Drop any qualifiers as
5734 .debug_names does not contain any. */
5735
5736 if (strchr (name, '(') != NULL)
5737 {
54ee4252 5738 without_params = cp_remove_params (name);
927aa2e7 5739 if (without_params != NULL)
54ee4252 5740 name = without_params.get ();
927aa2e7
JK
5741 }
5742 }
5743
5744 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5745
5746 const uint32_t full_hash = dwarf5_djb_hash (name);
5747 uint32_t namei
5748 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5749 (map.bucket_table_reordered
5750 + (full_hash % map.bucket_count)), 4,
5751 map.dwarf5_byte_order);
5752 if (namei == 0)
5753 return NULL;
5754 --namei;
5755 if (namei >= map.name_count)
5756 {
b98664d3 5757 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5758 "[in module %s]"),
5759 namei, map.name_count,
ed2dc618 5760 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5761 return NULL;
5762 }
5763
5764 for (;;)
5765 {
5766 const uint32_t namei_full_hash
5767 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5768 (map.hash_table_reordered + namei), 4,
5769 map.dwarf5_byte_order);
5770 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5771 return NULL;
5772
5773 if (full_hash == namei_full_hash)
5774 {
5775 const char *const namei_string = map.namei_to_name (namei);
5776
5777#if 0 /* An expensive sanity check. */
5778 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5779 {
b98664d3 5780 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5781 "[in module %s]"),
5782 namei, objfile_name (dwarf2_per_objfile->objfile));
5783 return NULL;
5784 }
5785#endif
5786
5787 if (cmp (namei_string, name) == 0)
5788 {
5789 const ULONGEST namei_entry_offs
5790 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5791 + namei * map.offset_size),
5792 map.offset_size, map.dwarf5_byte_order);
5793 return map.entry_pool + namei_entry_offs;
5794 }
5795 }
5796
5797 ++namei;
5798 if (namei >= map.name_count)
5799 return NULL;
5800 }
5801}
5802
5803const gdb_byte *
5804dw2_debug_names_iterator::find_vec_in_debug_names
5805 (const mapped_debug_names &map, uint32_t namei)
5806{
5807 if (namei >= map.name_count)
5808 {
b98664d3 5809 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5810 "[in module %s]"),
5811 namei, map.name_count,
ed2dc618 5812 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5813 return NULL;
5814 }
5815
5816 const ULONGEST namei_entry_offs
5817 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5818 + namei * map.offset_size),
5819 map.offset_size, map.dwarf5_byte_order);
5820 return map.entry_pool + namei_entry_offs;
5821}
5822
5823/* See dw2_debug_names_iterator. */
5824
5825dwarf2_per_cu_data *
5826dw2_debug_names_iterator::next ()
5827{
5828 if (m_addr == NULL)
5829 return NULL;
5830
ed2dc618
SM
5831 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5832 struct objfile *objfile = dwarf2_per_objfile->objfile;
5833 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5834
5835 again:
5836
5837 unsigned int bytes_read;
5838 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5839 m_addr += bytes_read;
5840 if (abbrev == 0)
5841 return NULL;
5842
5843 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5844 if (indexval_it == m_map.abbrev_map.cend ())
5845 {
b98664d3 5846 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5847 "[in module %s]"),
ed2dc618 5848 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5849 return NULL;
5850 }
5851 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5852 enum class symbol_linkage {
5853 unknown,
5854 static_,
5855 extern_,
23c13d42 5856 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5857 dwarf2_per_cu_data *per_cu = NULL;
5858 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5859 {
5860 ULONGEST ull;
5861 switch (attr.form)
5862 {
5863 case DW_FORM_implicit_const:
5864 ull = attr.implicit_const;
5865 break;
5866 case DW_FORM_flag_present:
5867 ull = 1;
5868 break;
5869 case DW_FORM_udata:
5870 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5871 m_addr += bytes_read;
5872 break;
5873 default:
b98664d3 5874 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5875 dwarf_form_name (attr.form),
ed2dc618 5876 objfile_name (objfile));
927aa2e7
JK
5877 return NULL;
5878 }
5879 switch (attr.dw_idx)
5880 {
5881 case DW_IDX_compile_unit:
5882 /* Don't crash on bad data. */
b76e467d 5883 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
927aa2e7 5884 {
b98664d3 5885 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5886 " [in module %s]"),
5887 pulongest (ull),
5888 objfile_name (dwarf2_per_objfile->objfile));
5889 continue;
5890 }
ff4c9fec 5891 per_cu = dwarf2_per_objfile->get_cutu (ull);
927aa2e7 5892 break;
8af5c486
JK
5893 case DW_IDX_type_unit:
5894 /* Don't crash on bad data. */
b2bdb8cf 5895 if (ull >= dwarf2_per_objfile->all_type_units.size ())
8af5c486 5896 {
b98664d3 5897 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5898 " [in module %s]"),
5899 pulongest (ull),
5900 objfile_name (dwarf2_per_objfile->objfile));
5901 continue;
5902 }
ff4c9fec 5903 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
8af5c486 5904 break;
927aa2e7
JK
5905 case DW_IDX_GNU_internal:
5906 if (!m_map.augmentation_is_gdb)
5907 break;
23c13d42 5908 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5909 break;
5910 case DW_IDX_GNU_external:
5911 if (!m_map.augmentation_is_gdb)
5912 break;
23c13d42 5913 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5914 break;
5915 }
5916 }
5917
5918 /* Skip if already read in. */
5919 if (per_cu->v.quick->compunit_symtab)
5920 goto again;
5921
5922 /* Check static vs global. */
23c13d42 5923 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5924 {
2b79f376 5925 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5926 const bool symbol_is_static =
5927 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5928 if (want_static != symbol_is_static)
2b79f376 5929 goto again;
927aa2e7
JK
5930 }
5931
5932 /* Match dw2_symtab_iter_next, symbol_kind
5933 and debug_names::psymbol_tag. */
5934 switch (m_domain)
5935 {
5936 case VAR_DOMAIN:
5937 switch (indexval.dwarf_tag)
5938 {
5939 case DW_TAG_variable:
5940 case DW_TAG_subprogram:
5941 /* Some types are also in VAR_DOMAIN. */
5942 case DW_TAG_typedef:
5943 case DW_TAG_structure_type:
5944 break;
5945 default:
5946 goto again;
5947 }
5948 break;
5949 case STRUCT_DOMAIN:
5950 switch (indexval.dwarf_tag)
5951 {
5952 case DW_TAG_typedef:
5953 case DW_TAG_structure_type:
5954 break;
5955 default:
5956 goto again;
5957 }
5958 break;
5959 case LABEL_DOMAIN:
5960 switch (indexval.dwarf_tag)
5961 {
5962 case 0:
5963 case DW_TAG_variable:
5964 break;
5965 default:
5966 goto again;
5967 }
5968 break;
5969 default:
5970 break;
5971 }
5972
5973 /* Match dw2_expand_symtabs_matching, symbol_kind and
5974 debug_names::psymbol_tag. */
5975 switch (m_search)
4b514bc8 5976 {
927aa2e7
JK
5977 case VARIABLES_DOMAIN:
5978 switch (indexval.dwarf_tag)
4b514bc8 5979 {
927aa2e7
JK
5980 case DW_TAG_variable:
5981 break;
5982 default:
5983 goto again;
4b514bc8 5984 }
927aa2e7
JK
5985 break;
5986 case FUNCTIONS_DOMAIN:
5987 switch (indexval.dwarf_tag)
4b514bc8 5988 {
927aa2e7
JK
5989 case DW_TAG_subprogram:
5990 break;
5991 default:
5992 goto again;
4b514bc8 5993 }
927aa2e7
JK
5994 break;
5995 case TYPES_DOMAIN:
5996 switch (indexval.dwarf_tag)
5997 {
5998 case DW_TAG_typedef:
5999 case DW_TAG_structure_type:
6000 break;
6001 default:
6002 goto again;
6003 }
6004 break;
6005 default:
6006 break;
4b514bc8 6007 }
927aa2e7
JK
6008
6009 return per_cu;
4b514bc8 6010}
61920122 6011
927aa2e7 6012static struct compunit_symtab *
c7f839cb 6013dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 6014 const char *name, domain_enum domain)
4b514bc8 6015{
ed2dc618
SM
6016 struct dwarf2_per_objfile *dwarf2_per_objfile
6017 = get_dwarf2_per_objfile (objfile);
61920122 6018
927aa2e7
JK
6019 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6020 if (!mapp)
61920122 6021 {
927aa2e7
JK
6022 /* index is NULL if OBJF_READNOW. */
6023 return NULL;
6024 }
6025 const auto &map = *mapp;
9291a0cd 6026
2b79f376 6027 dw2_debug_names_iterator iter (map, block_index, domain, name);
9703b513 6028
927aa2e7
JK
6029 struct compunit_symtab *stab_best = NULL;
6030 struct dwarf2_per_cu_data *per_cu;
6031 while ((per_cu = iter.next ()) != NULL)
6032 {
6033 struct symbol *sym, *with_opaque = NULL;
58f0c718 6034 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
927aa2e7 6035 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 6036 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 6037
927aa2e7
JK
6038 sym = block_find_symbol (block, name, domain,
6039 block_find_non_opaque_type_preferred,
6040 &with_opaque);
9703b513 6041
927aa2e7
JK
6042 /* Some caution must be observed with overloaded functions and
6043 methods, since the index will not contain any overload
6044 information (but NAME might contain it). */
a3ec0bb1 6045
927aa2e7
JK
6046 if (sym != NULL
6047 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6048 return stab;
6049 if (with_opaque != NULL
6050 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6051 stab_best = stab;
9703b513 6052
927aa2e7 6053 /* Keep looking through other CUs. */
9703b513
TT
6054 }
6055
927aa2e7 6056 return stab_best;
9703b513
TT
6057}
6058
927aa2e7
JK
6059/* This dumps minimal information about .debug_names. It is called
6060 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6061 uses this to verify that .debug_names has been loaded. */
9291a0cd 6062
927aa2e7
JK
6063static void
6064dw2_debug_names_dump (struct objfile *objfile)
6065{
ed2dc618
SM
6066 struct dwarf2_per_objfile *dwarf2_per_objfile
6067 = get_dwarf2_per_objfile (objfile);
6068
927aa2e7
JK
6069 gdb_assert (dwarf2_per_objfile->using_index);
6070 printf_filtered (".debug_names:");
6071 if (dwarf2_per_objfile->debug_names_table)
6072 printf_filtered (" exists\n");
6073 else
6074 printf_filtered (" faked for \"readnow\"\n");
6075 printf_filtered ("\n");
9291a0cd
TT
6076}
6077
9291a0cd 6078static void
927aa2e7
JK
6079dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6080 const char *func_name)
9291a0cd 6081{
ed2dc618
SM
6082 struct dwarf2_per_objfile *dwarf2_per_objfile
6083 = get_dwarf2_per_objfile (objfile);
ae2de4f8 6084
927aa2e7
JK
6085 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6086 if (dwarf2_per_objfile->debug_names_table)
24c79950 6087 {
927aa2e7 6088 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
24c79950 6089
2b79f376 6090 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
24c79950 6091
927aa2e7
JK
6092 struct dwarf2_per_cu_data *per_cu;
6093 while ((per_cu = iter.next ()) != NULL)
58f0c718 6094 dw2_instantiate_symtab (per_cu, false);
927aa2e7
JK
6095 }
6096}
24c79950 6097
927aa2e7
JK
6098static void
6099dw2_debug_names_expand_symtabs_matching
6100 (struct objfile *objfile,
6101 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6102 const lookup_name_info &lookup_name,
6103 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6104 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6105 enum search_domain kind)
6106{
ed2dc618
SM
6107 struct dwarf2_per_objfile *dwarf2_per_objfile
6108 = get_dwarf2_per_objfile (objfile);
9291a0cd 6109
927aa2e7
JK
6110 /* debug_names_table is NULL if OBJF_READNOW. */
6111 if (!dwarf2_per_objfile->debug_names_table)
6112 return;
9291a0cd 6113
ed2dc618 6114 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 6115
44ed8f3e 6116 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
bbf2f4df 6117
44ed8f3e
PA
6118 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6119 symbol_matcher,
6120 kind, [&] (offset_type namei)
927aa2e7 6121 {
927aa2e7
JK
6122 /* The name was matched, now expand corresponding CUs that were
6123 marked. */
6124 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 6125
927aa2e7
JK
6126 struct dwarf2_per_cu_data *per_cu;
6127 while ((per_cu = iter.next ()) != NULL)
6128 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6129 expansion_notify);
44ed8f3e 6130 });
9291a0cd
TT
6131}
6132
927aa2e7 6133const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
6134{
6135 dw2_has_symbols,
6136 dw2_find_last_source_symtab,
6137 dw2_forget_cached_source_info,
f8eba3c6 6138 dw2_map_symtabs_matching_filename,
927aa2e7 6139 dw2_debug_names_lookup_symbol,
9291a0cd 6140 dw2_print_stats,
927aa2e7 6141 dw2_debug_names_dump,
927aa2e7 6142 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 6143 dw2_expand_all_symtabs,
652a8996 6144 dw2_expand_symtabs_with_fullname,
40658b94 6145 dw2_map_matching_symbols,
927aa2e7 6146 dw2_debug_names_expand_symtabs_matching,
43f3e411 6147 dw2_find_pc_sect_compunit_symtab,
71a3c369 6148 NULL,
9291a0cd
TT
6149 dw2_map_symbol_filenames
6150};
6151
4485a1c1
SM
6152/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
6153 to either a dwarf2_per_objfile or dwz_file object. */
6154
6155template <typename T>
6156static gdb::array_view<const gdb_byte>
6157get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
6158{
6159 dwarf2_section_info *section = &section_owner->gdb_index;
6160
6161 if (dwarf2_section_empty_p (section))
6162 return {};
6163
6164 /* Older elfutils strip versions could keep the section in the main
6165 executable while splitting it for the separate debug info file. */
6166 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
6167 return {};
6168
6169 dwarf2_read_section (obj, section);
6170
8bebfcda
PA
6171 /* dwarf2_section_info::size is a bfd_size_type, while
6172 gdb::array_view works with size_t. On 32-bit hosts, with
6173 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
6174 is 32-bit. So we need an explicit narrowing conversion here.
6175 This is fine, because it's impossible to allocate or mmap an
6176 array/buffer larger than what size_t can represent. */
6177 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
6178}
6179
87d6a7aa
SM
6180/* Lookup the index cache for the contents of the index associated to
6181 DWARF2_OBJ. */
6182
6183static gdb::array_view<const gdb_byte>
6184get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
6185{
6186 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
6187 if (build_id == nullptr)
6188 return {};
6189
6190 return global_index_cache.lookup_gdb_index (build_id,
6191 &dwarf2_obj->index_cache_res);
6192}
6193
6194/* Same as the above, but for DWZ. */
6195
6196static gdb::array_view<const gdb_byte>
6197get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
6198{
6199 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
6200 if (build_id == nullptr)
6201 return {};
6202
6203 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6204}
6205
3c0aa29a 6206/* See symfile.h. */
9291a0cd 6207
3c0aa29a
PA
6208bool
6209dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 6210{
ed2dc618
SM
6211 struct dwarf2_per_objfile *dwarf2_per_objfile
6212 = get_dwarf2_per_objfile (objfile);
6213
9291a0cd
TT
6214 /* If we're about to read full symbols, don't bother with the
6215 indices. In this case we also don't care if some other debug
6216 format is making psymtabs, because they are all about to be
6217 expanded anyway. */
6218 if ((objfile->flags & OBJF_READNOW))
6219 {
9291a0cd 6220 dwarf2_per_objfile->using_index = 1;
ed2dc618
SM
6221 create_all_comp_units (dwarf2_per_objfile);
6222 create_all_type_units (dwarf2_per_objfile);
b76e467d
SM
6223 dwarf2_per_objfile->quick_file_names_table
6224 = create_quick_file_names_table
6225 (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd 6226
b76e467d 6227 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 6228 + dwarf2_per_objfile->all_type_units.size ()); ++i)
9291a0cd 6229 {
ff4c9fec 6230 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 6231
e254ef6a
DE
6232 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6233 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
6234 }
6235
6236 /* Return 1 so that gdb sees the "quick" functions. However,
6237 these functions will be no-ops because we will have expanded
6238 all symtabs. */
3c0aa29a
PA
6239 *index_kind = dw_index_kind::GDB_INDEX;
6240 return true;
9291a0cd
TT
6241 }
6242
ed2dc618 6243 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
6244 {
6245 *index_kind = dw_index_kind::DEBUG_NAMES;
6246 return true;
6247 }
927aa2e7 6248
4485a1c1
SM
6249 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6250 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
6251 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
6252 {
6253 *index_kind = dw_index_kind::GDB_INDEX;
6254 return true;
6255 }
9291a0cd 6256
87d6a7aa
SM
6257 /* ... otherwise, try to find the index in the index cache. */
6258 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
6259 get_gdb_index_contents_from_cache,
6260 get_gdb_index_contents_from_cache_dwz))
6261 {
6262 global_index_cache.hit ();
6263 *index_kind = dw_index_kind::GDB_INDEX;
6264 return true;
6265 }
6266
6267 global_index_cache.miss ();
3c0aa29a 6268 return false;
9291a0cd
TT
6269}
6270
6271\f
6272
dce234bc
PP
6273/* Build a partial symbol table. */
6274
6275void
f29dff0a 6276dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 6277{
ed2dc618
SM
6278 struct dwarf2_per_objfile *dwarf2_per_objfile
6279 = get_dwarf2_per_objfile (objfile);
c9bf0622 6280
6eee24ce 6281 init_psymbol_list (objfile, 1024);
c906108c 6282
a70b8144 6283 try
c9bf0622
TT
6284 {
6285 /* This isn't really ideal: all the data we allocate on the
6286 objfile's obstack is still uselessly kept around. However,
6287 freeing it seems unsafe. */
906768f9 6288 psymtab_discarder psymtabs (objfile);
ed2dc618 6289 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 6290 psymtabs.keep ();
87d6a7aa
SM
6291
6292 /* (maybe) store an index in the cache. */
6293 global_index_cache.store (dwarf2_per_objfile);
c9bf0622 6294 }
230d2906 6295 catch (const gdb_exception_error &except)
492d29ea
PA
6296 {
6297 exception_print (gdb_stderr, except);
6298 }
c906108c 6299}
c906108c 6300
1ce1cefd
DE
6301/* Return the total length of the CU described by HEADER. */
6302
6303static unsigned int
6304get_cu_length (const struct comp_unit_head *header)
6305{
6306 return header->initial_length_size + header->length;
6307}
6308
9c541725 6309/* Return TRUE if SECT_OFF is within CU_HEADER. */
45452591 6310
9c541725
PA
6311static inline bool
6312offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
45452591 6313{
9c541725
PA
6314 sect_offset bottom = cu_header->sect_off;
6315 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
9a619af0 6316
9c541725 6317 return sect_off >= bottom && sect_off < top;
45452591
DE
6318}
6319
3b80fe9b
DE
6320/* Find the base address of the compilation unit for range lists and
6321 location lists. It will normally be specified by DW_AT_low_pc.
6322 In DWARF-3 draft 4, the base address could be overridden by
6323 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6324 compilation units with discontinuous ranges. */
6325
6326static void
6327dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6328{
6329 struct attribute *attr;
6330
6331 cu->base_known = 0;
6332 cu->base_address = 0;
6333
6334 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6335 if (attr)
6336 {
31aa7e4e 6337 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6338 cu->base_known = 1;
6339 }
6340 else
6341 {
6342 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6343 if (attr)
6344 {
31aa7e4e 6345 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6346 cu->base_known = 1;
6347 }
6348 }
6349}
6350
93311388 6351/* Read in the comp unit header information from the debug_info at info_ptr.
43988095 6352 Use rcuh_kind::COMPILE as the default type if not known by the caller.
93311388
DE
6353 NOTE: This leaves members offset, first_die_offset to be filled in
6354 by the caller. */
107d2387 6355
d521ce57 6356static const gdb_byte *
107d2387 6357read_comp_unit_head (struct comp_unit_head *cu_header,
43988095
JK
6358 const gdb_byte *info_ptr,
6359 struct dwarf2_section_info *section,
6360 rcuh_kind section_kind)
107d2387
AC
6361{
6362 int signed_addr;
891d2f0b 6363 unsigned int bytes_read;
43988095
JK
6364 const char *filename = get_section_file_name (section);
6365 bfd *abfd = get_section_bfd_owner (section);
c764a876
DE
6366
6367 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6368 cu_header->initial_length_size = bytes_read;
6369 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 6370 info_ptr += bytes_read;
107d2387 6371 cu_header->version = read_2_bytes (abfd, info_ptr);
1ea5da02
TV
6372 if (cu_header->version < 2 || cu_header->version > 5)
6373 error (_("Dwarf Error: wrong version in compilation unit header "
6374 "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
6375 cu_header->version, filename);
107d2387 6376 info_ptr += 2;
43988095
JK
6377 if (cu_header->version < 5)
6378 switch (section_kind)
6379 {
6380 case rcuh_kind::COMPILE:
6381 cu_header->unit_type = DW_UT_compile;
6382 break;
6383 case rcuh_kind::TYPE:
6384 cu_header->unit_type = DW_UT_type;
6385 break;
6386 default:
6387 internal_error (__FILE__, __LINE__,
6388 _("read_comp_unit_head: invalid section_kind"));
6389 }
6390 else
6391 {
6392 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6393 (read_1_byte (abfd, info_ptr));
6394 info_ptr += 1;
6395 switch (cu_header->unit_type)
6396 {
6397 case DW_UT_compile:
a084a2a6
AT
6398 case DW_UT_partial:
6399 case DW_UT_skeleton:
6400 case DW_UT_split_compile:
43988095
JK
6401 if (section_kind != rcuh_kind::COMPILE)
6402 error (_("Dwarf Error: wrong unit_type in compilation unit header "
a084a2a6
AT
6403 "(is %s, should be %s) [in module %s]"),
6404 dwarf_unit_type_name (cu_header->unit_type),
6405 dwarf_unit_type_name (DW_UT_type), filename);
43988095
JK
6406 break;
6407 case DW_UT_type:
a084a2a6 6408 case DW_UT_split_type:
43988095
JK
6409 section_kind = rcuh_kind::TYPE;
6410 break;
6411 default:
6412 error (_("Dwarf Error: wrong unit_type in compilation unit header "
a084a2a6
AT
6413 "(is %#04x, should be one of: %s, %s, %s, %s or %s) "
6414 "[in module %s]"), cu_header->unit_type,
6415 dwarf_unit_type_name (DW_UT_compile),
6416 dwarf_unit_type_name (DW_UT_skeleton),
6417 dwarf_unit_type_name (DW_UT_split_compile),
6418 dwarf_unit_type_name (DW_UT_type),
6419 dwarf_unit_type_name (DW_UT_split_type), filename);
43988095
JK
6420 }
6421
6422 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6423 info_ptr += 1;
6424 }
9c541725
PA
6425 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6426 cu_header,
6427 &bytes_read);
613e1657 6428 info_ptr += bytes_read;
43988095
JK
6429 if (cu_header->version < 5)
6430 {
6431 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6432 info_ptr += 1;
6433 }
107d2387
AC
6434 signed_addr = bfd_get_sign_extend_vma (abfd);
6435 if (signed_addr < 0)
8e65ff28 6436 internal_error (__FILE__, __LINE__,
e2e0b3e5 6437 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 6438 cu_header->signed_addr_p = signed_addr;
c764a876 6439
a084a2a6
AT
6440 bool header_has_signature = section_kind == rcuh_kind::TYPE
6441 || cu_header->unit_type == DW_UT_skeleton
6442 || cu_header->unit_type == DW_UT_split_compile;
43988095 6443
a084a2a6
AT
6444 if (header_has_signature)
6445 {
43988095
JK
6446 cu_header->signature = read_8_bytes (abfd, info_ptr);
6447 info_ptr += 8;
a084a2a6 6448 }
43988095 6449
a084a2a6
AT
6450 if (section_kind == rcuh_kind::TYPE)
6451 {
6452 LONGEST type_offset;
43988095
JK
6453 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6454 info_ptr += bytes_read;
9c541725
PA
6455 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6456 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
43988095
JK
6457 error (_("Dwarf Error: Too big type_offset in compilation unit "
6458 "header (is %s) [in module %s]"), plongest (type_offset),
6459 filename);
6460 }
6461
107d2387
AC
6462 return info_ptr;
6463}
6464
36586728
TT
6465/* Helper function that returns the proper abbrev section for
6466 THIS_CU. */
6467
6468static struct dwarf2_section_info *
6469get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6470{
6471 struct dwarf2_section_info *abbrev;
ed2dc618 6472 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
36586728
TT
6473
6474 if (this_cu->is_dwz)
ed2dc618 6475 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
36586728
TT
6476 else
6477 abbrev = &dwarf2_per_objfile->abbrev;
6478
6479 return abbrev;
6480}
6481
9ff913ba
DE
6482/* Subroutine of read_and_check_comp_unit_head and
6483 read_and_check_type_unit_head to simplify them.
6484 Perform various error checking on the header. */
6485
6486static void
ed2dc618
SM
6487error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6488 struct comp_unit_head *header,
4bdcc0c1
DE
6489 struct dwarf2_section_info *section,
6490 struct dwarf2_section_info *abbrev_section)
9ff913ba 6491{
a32a8923 6492 const char *filename = get_section_file_name (section);
9ff913ba 6493
9c541725 6494 if (to_underlying (header->abbrev_sect_off)
36586728 6495 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9d8780f0
SM
6496 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6497 "(offset %s + 6) [in module %s]"),
6498 sect_offset_str (header->abbrev_sect_off),
6499 sect_offset_str (header->sect_off),
9ff913ba
DE
6500 filename);
6501
9c541725 6502 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
9ff913ba 6503 avoid potential 32-bit overflow. */
9c541725 6504 if (((ULONGEST) header->sect_off + get_cu_length (header))
9ff913ba 6505 > section->size)
9c541725 6506 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
9d8780f0
SM
6507 "(offset %s + 0) [in module %s]"),
6508 header->length, sect_offset_str (header->sect_off),
9ff913ba
DE
6509 filename);
6510}
6511
6512/* Read in a CU/TU header and perform some basic error checking.
6513 The contents of the header are stored in HEADER.
6514 The result is a pointer to the start of the first DIE. */
adabb602 6515
d521ce57 6516static const gdb_byte *
ed2dc618
SM
6517read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6518 struct comp_unit_head *header,
9ff913ba 6519 struct dwarf2_section_info *section,
4bdcc0c1 6520 struct dwarf2_section_info *abbrev_section,
d521ce57 6521 const gdb_byte *info_ptr,
43988095 6522 rcuh_kind section_kind)
72bf9492 6523{
d521ce57 6524 const gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492 6525
9c541725 6526 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
adabb602 6527
43988095 6528 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
9ff913ba 6529
9c541725 6530 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
348e048f 6531
ed2dc618
SM
6532 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6533 abbrev_section);
9ff913ba
DE
6534
6535 return info_ptr;
348e048f
DE
6536}
6537
f4dc4d17
DE
6538/* Fetch the abbreviation table offset from a comp or type unit header. */
6539
6540static sect_offset
ed2dc618
SM
6541read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6542 struct dwarf2_section_info *section,
9c541725 6543 sect_offset sect_off)
f4dc4d17 6544{
a32a8923 6545 bfd *abfd = get_section_bfd_owner (section);
d521ce57 6546 const gdb_byte *info_ptr;
ac298888 6547 unsigned int initial_length_size, offset_size;
43988095 6548 uint16_t version;
f4dc4d17
DE
6549
6550 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
9c541725 6551 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6552 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6553 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6554 info_ptr += initial_length_size;
6555
6556 version = read_2_bytes (abfd, info_ptr);
6557 info_ptr += 2;
6558 if (version >= 5)
6559 {
6560 /* Skip unit type and address size. */
6561 info_ptr += 2;
6562 }
6563
9c541725 6564 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
f4dc4d17
DE
6565}
6566
aaa75496
JB
6567/* Allocate a new partial symtab for file named NAME and mark this new
6568 partial symtab as being an include of PST. */
6569
6570static void
d521ce57 6571dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
6572 struct objfile *objfile)
6573{
6574 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6575
fbd9ab74
JK
6576 if (!IS_ABSOLUTE_PATH (subpst->filename))
6577 {
6578 /* It shares objfile->objfile_obstack. */
6579 subpst->dirname = pst->dirname;
6580 }
6581
a9342b62 6582 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6583 subpst->dependencies[0] = pst;
6584 subpst->number_of_dependencies = 1;
6585
aaa75496 6586 subpst->read_symtab = pst->read_symtab;
aaa75496
JB
6587
6588 /* No private part is necessary for include psymtabs. This property
6589 can be used to differentiate between such include psymtabs and
10b3939b 6590 the regular ones. */
58a9656e 6591 subpst->read_symtab_private = NULL;
aaa75496
JB
6592}
6593
6594/* Read the Line Number Program data and extract the list of files
6595 included by the source file represented by PST. Build an include
d85a05f0 6596 partial symtab for each of these included files. */
aaa75496
JB
6597
6598static void
6599dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
6600 struct die_info *die,
6601 struct partial_symtab *pst)
aaa75496 6602{
fff8551c 6603 line_header_up lh;
d85a05f0 6604 struct attribute *attr;
aaa75496 6605
d85a05f0
DJ
6606 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6607 if (attr)
9c541725 6608 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6609 if (lh == NULL)
6610 return; /* No linetable, so no includes. */
6611
79748972
TT
6612 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6613 that we pass in the raw text_low here; that is ok because we're
6614 only decoding the line table to make include partial symtabs, and
6615 so the addresses aren't really used. */
4ae976d1 6616 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6617 pst->raw_text_low (), 1);
aaa75496
JB
6618}
6619
348e048f 6620static hashval_t
52dc124a 6621hash_signatured_type (const void *item)
348e048f 6622{
9a3c8263
SM
6623 const struct signatured_type *sig_type
6624 = (const struct signatured_type *) item;
9a619af0 6625
348e048f 6626 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6627 return sig_type->signature;
348e048f
DE
6628}
6629
6630static int
52dc124a 6631eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6632{
9a3c8263
SM
6633 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6634 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6635
348e048f
DE
6636 return lhs->signature == rhs->signature;
6637}
6638
1fd400ff
TT
6639/* Allocate a hash table for signatured types. */
6640
6641static htab_t
673bfd45 6642allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
6643{
6644 return htab_create_alloc_ex (41,
52dc124a
DE
6645 hash_signatured_type,
6646 eq_signatured_type,
1fd400ff
TT
6647 NULL,
6648 &objfile->objfile_obstack,
6649 hashtab_obstack_allocate,
6650 dummy_obstack_deallocate);
6651}
6652
d467dd73 6653/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6654
6655static int
d467dd73 6656add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6657{
9a3c8263 6658 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6659 std::vector<signatured_type *> *all_type_units
6660 = (std::vector<signatured_type *> *) datum;
1fd400ff 6661
b2bdb8cf 6662 all_type_units->push_back (sigt);
1fd400ff
TT
6663
6664 return 1;
6665}
6666
78d4d2c5 6667/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6668 and fill them into TYPES_HTAB. It will process only type units,
6669 therefore DW_UT_type. */
c88ee1f0 6670
78d4d2c5 6671static void
ed2dc618
SM
6672create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6673 struct dwo_file *dwo_file,
43988095
JK
6674 dwarf2_section_info *section, htab_t &types_htab,
6675 rcuh_kind section_kind)
348e048f 6676{
3019eac3 6677 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6678 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6679 bfd *abfd;
6680 const gdb_byte *info_ptr, *end_ptr;
348e048f 6681
4bdcc0c1
DE
6682 abbrev_section = (dwo_file != NULL
6683 ? &dwo_file->sections.abbrev
6684 : &dwarf2_per_objfile->abbrev);
6685
b4f54984 6686 if (dwarf_read_debug)
43988095
JK
6687 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6688 get_section_name (section),
a32a8923 6689 get_section_file_name (abbrev_section));
09406207 6690
78d4d2c5
JK
6691 dwarf2_read_section (objfile, section);
6692 info_ptr = section->buffer;
348e048f 6693
78d4d2c5
JK
6694 if (info_ptr == NULL)
6695 return;
348e048f 6696
78d4d2c5
JK
6697 /* We can't set abfd until now because the section may be empty or
6698 not present, in which case the bfd is unknown. */
6699 abfd = get_section_bfd_owner (section);
348e048f 6700
78d4d2c5
JK
6701 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6702 because we don't need to read any dies: the signature is in the
6703 header. */
3019eac3 6704
78d4d2c5
JK
6705 end_ptr = info_ptr + section->size;
6706 while (info_ptr < end_ptr)
6707 {
78d4d2c5
JK
6708 struct signatured_type *sig_type;
6709 struct dwo_unit *dwo_tu;
6710 void **slot;
6711 const gdb_byte *ptr = info_ptr;
6712 struct comp_unit_head header;
6713 unsigned int length;
8b70b953 6714
9c541725 6715 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6716
a49dd8dd
JK
6717 /* Initialize it due to a false compiler warning. */
6718 header.signature = -1;
9c541725 6719 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6720
78d4d2c5
JK
6721 /* We need to read the type's signature in order to build the hash
6722 table, but we don't need anything else just yet. */
348e048f 6723
ed2dc618 6724 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6725 abbrev_section, ptr, section_kind);
348e048f 6726
78d4d2c5 6727 length = get_cu_length (&header);
6caca83c 6728
78d4d2c5
JK
6729 /* Skip dummy type units. */
6730 if (ptr >= info_ptr + length
43988095
JK
6731 || peek_abbrev_code (abfd, ptr) == 0
6732 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6733 {
6734 info_ptr += length;
6735 continue;
6736 }
dee91e82 6737
78d4d2c5
JK
6738 if (types_htab == NULL)
6739 {
6740 if (dwo_file)
6741 types_htab = allocate_dwo_unit_table (objfile);
6742 else
6743 types_htab = allocate_signatured_type_table (objfile);
6744 }
8b70b953 6745
78d4d2c5
JK
6746 if (dwo_file)
6747 {
6748 sig_type = NULL;
6749 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6750 struct dwo_unit);
6751 dwo_tu->dwo_file = dwo_file;
43988095 6752 dwo_tu->signature = header.signature;
9c541725 6753 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6754 dwo_tu->section = section;
9c541725 6755 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6756 dwo_tu->length = length;
6757 }
6758 else
6759 {
6760 /* N.B.: type_offset is not usable if this type uses a DWO file.
6761 The real type_offset is in the DWO file. */
6762 dwo_tu = NULL;
6763 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6764 struct signatured_type);
43988095 6765 sig_type->signature = header.signature;
9c541725 6766 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 6767 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
6768 sig_type->per_cu.is_debug_types = 1;
6769 sig_type->per_cu.section = section;
9c541725 6770 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6771 sig_type->per_cu.length = length;
6772 }
6773
6774 slot = htab_find_slot (types_htab,
6775 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6776 INSERT);
6777 gdb_assert (slot != NULL);
6778 if (*slot != NULL)
6779 {
9c541725 6780 sect_offset dup_sect_off;
0349ea22 6781
3019eac3
DE
6782 if (dwo_file)
6783 {
78d4d2c5
JK
6784 const struct dwo_unit *dup_tu
6785 = (const struct dwo_unit *) *slot;
6786
9c541725 6787 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6788 }
6789 else
6790 {
78d4d2c5
JK
6791 const struct signatured_type *dup_tu
6792 = (const struct signatured_type *) *slot;
6793
9c541725 6794 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6795 }
8b70b953 6796
b98664d3 6797 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6798 " the entry at offset %s, signature %s"),
6799 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6800 hex_string (header.signature));
78d4d2c5
JK
6801 }
6802 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6803
78d4d2c5 6804 if (dwarf_read_debug > 1)
9d8780f0
SM
6805 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6806 sect_offset_str (sect_off),
43988095 6807 hex_string (header.signature));
3019eac3 6808
78d4d2c5
JK
6809 info_ptr += length;
6810 }
6811}
3019eac3 6812
78d4d2c5
JK
6813/* Create the hash table of all entries in the .debug_types
6814 (or .debug_types.dwo) section(s).
6815 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6816 otherwise it is NULL.
b3c8eb43 6817
78d4d2c5 6818 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6819
78d4d2c5 6820 Note: This function processes DWO files only, not DWP files. */
348e048f 6821
78d4d2c5 6822static void
ed2dc618
SM
6823create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6824 struct dwo_file *dwo_file,
fd5866f6 6825 gdb::array_view<dwarf2_section_info> type_sections,
78d4d2c5
JK
6826 htab_t &types_htab)
6827{
fd5866f6
SM
6828 for (dwarf2_section_info &section : type_sections)
6829 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
ed2dc618 6830 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6831}
6832
6833/* Create the hash table of all entries in the .debug_types section,
6834 and initialize all_type_units.
6835 The result is zero if there is an error (e.g. missing .debug_types section),
6836 otherwise non-zero. */
6837
6838static int
ed2dc618 6839create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6840{
78d4d2c5 6841 htab_t types_htab = NULL;
3019eac3 6842
ed2dc618
SM
6843 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6844 &dwarf2_per_objfile->info, types_htab,
43988095 6845 rcuh_kind::COMPILE);
ed2dc618
SM
6846 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6847 dwarf2_per_objfile->types, types_htab);
3019eac3
DE
6848 if (types_htab == NULL)
6849 {
6850 dwarf2_per_objfile->signatured_types = NULL;
6851 return 0;
6852 }
6853
348e048f
DE
6854 dwarf2_per_objfile->signatured_types = types_htab;
6855
b2bdb8cf
SM
6856 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6857 dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
6858
6859 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
6860 &dwarf2_per_objfile->all_type_units);
1fd400ff 6861
348e048f
DE
6862 return 1;
6863}
6864
6aa5f3a6
DE
6865/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6866 If SLOT is non-NULL, it is the entry to use in the hash table.
6867 Otherwise we find one. */
6868
6869static struct signatured_type *
ed2dc618
SM
6870add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6871 void **slot)
6aa5f3a6
DE
6872{
6873 struct objfile *objfile = dwarf2_per_objfile->objfile;
6aa5f3a6 6874
b2bdb8cf
SM
6875 if (dwarf2_per_objfile->all_type_units.size ()
6876 == dwarf2_per_objfile->all_type_units.capacity ())
6877 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6878
b2bdb8cf
SM
6879 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6880 struct signatured_type);
6881
6882 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6883 sig_type->signature = sig;
6884 sig_type->per_cu.is_debug_types = 1;
6885 if (dwarf2_per_objfile->using_index)
6886 {
6887 sig_type->per_cu.v.quick =
6888 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6889 struct dwarf2_per_cu_quick_data);
6890 }
6891
6892 if (slot == NULL)
6893 {
6894 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6895 sig_type, INSERT);
6896 }
6897 gdb_assert (*slot == NULL);
6898 *slot = sig_type;
6899 /* The rest of sig_type must be filled in by the caller. */
6900 return sig_type;
6901}
6902
a2ce51a0
DE
6903/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6904 Fill in SIG_ENTRY with DWO_ENTRY. */
6905
6906static void
ed2dc618 6907fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
6908 struct signatured_type *sig_entry,
6909 struct dwo_unit *dwo_entry)
6910{
7ee85ab1 6911 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
6912 gdb_assert (! sig_entry->per_cu.queued);
6913 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
6914 if (dwarf2_per_objfile->using_index)
6915 {
6916 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 6917 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
6918 }
6919 else
6920 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6921 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6922 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6923 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6924 gdb_assert (sig_entry->dwo_unit == NULL);
6925
6926 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6927 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6928 sig_entry->per_cu.length = dwo_entry->length;
6929 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 6930 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
a2ce51a0
DE
6931 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6932 sig_entry->dwo_unit = dwo_entry;
6933}
6934
6935/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6936 If we haven't read the TU yet, create the signatured_type data structure
6937 for a TU to be read in directly from a DWO file, bypassing the stub.
6938 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6939 using .gdb_index, then when reading a CU we want to stay in the DWO file
6940 containing that CU. Otherwise we could end up reading several other DWO
6941 files (due to comdat folding) to process the transitive closure of all the
6942 mentioned TUs, and that can be slow. The current DWO file will have every
6943 type signature that it needs.
a2ce51a0
DE
6944 We only do this for .gdb_index because in the psymtab case we already have
6945 to read all the DWOs to build the type unit groups. */
6946
6947static struct signatured_type *
6948lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6949{
518817b3
SM
6950 struct dwarf2_per_objfile *dwarf2_per_objfile
6951 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0
DE
6952 struct objfile *objfile = dwarf2_per_objfile->objfile;
6953 struct dwo_file *dwo_file;
6954 struct dwo_unit find_dwo_entry, *dwo_entry;
6955 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6956 void **slot;
a2ce51a0
DE
6957
6958 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6959
6aa5f3a6
DE
6960 /* If TU skeletons have been removed then we may not have read in any
6961 TUs yet. */
6962 if (dwarf2_per_objfile->signatured_types == NULL)
6963 {
6964 dwarf2_per_objfile->signatured_types
6965 = allocate_signatured_type_table (objfile);
6966 }
a2ce51a0
DE
6967
6968 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6969 Use the global signatured_types array to do our own comdat-folding
6970 of types. If this is the first time we're reading this TU, and
6971 the TU has an entry in .gdb_index, replace the recorded data from
6972 .gdb_index with this TU. */
a2ce51a0 6973
a2ce51a0 6974 find_sig_entry.signature = sig;
6aa5f3a6
DE
6975 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6976 &find_sig_entry, INSERT);
9a3c8263 6977 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6978
6979 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6980 read. Don't reassign the global entry to point to this DWO if that's
6981 the case. Also note that if the TU is already being read, it may not
6982 have come from a DWO, the program may be a mix of Fission-compiled
6983 code and non-Fission-compiled code. */
6984
6985 /* Have we already tried to read this TU?
6986 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6987 needn't exist in the global table yet). */
6988 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6989 return sig_entry;
6990
6aa5f3a6
DE
6991 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6992 dwo_unit of the TU itself. */
6993 dwo_file = cu->dwo_unit->dwo_file;
6994
a2ce51a0
DE
6995 /* Ok, this is the first time we're reading this TU. */
6996 if (dwo_file->tus == NULL)
6997 return NULL;
6998 find_dwo_entry.signature = sig;
9a3c8263 6999 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
a2ce51a0
DE
7000 if (dwo_entry == NULL)
7001 return NULL;
7002
6aa5f3a6
DE
7003 /* If the global table doesn't have an entry for this TU, add one. */
7004 if (sig_entry == NULL)
ed2dc618 7005 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 7006
ed2dc618 7007 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 7008 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
7009 return sig_entry;
7010}
7011
a2ce51a0
DE
7012/* Subroutine of lookup_signatured_type.
7013 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
7014 then try the DWP file. If the TU stub (skeleton) has been removed then
7015 it won't be in .gdb_index. */
a2ce51a0
DE
7016
7017static struct signatured_type *
7018lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
7019{
518817b3
SM
7020 struct dwarf2_per_objfile *dwarf2_per_objfile
7021 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0 7022 struct objfile *objfile = dwarf2_per_objfile->objfile;
ed2dc618 7023 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
7024 struct dwo_unit *dwo_entry;
7025 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 7026 void **slot;
a2ce51a0
DE
7027
7028 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
7029 gdb_assert (dwp_file != NULL);
7030
6aa5f3a6
DE
7031 /* If TU skeletons have been removed then we may not have read in any
7032 TUs yet. */
7033 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 7034 {
6aa5f3a6
DE
7035 dwarf2_per_objfile->signatured_types
7036 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
7037 }
7038
6aa5f3a6
DE
7039 find_sig_entry.signature = sig;
7040 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
7041 &find_sig_entry, INSERT);
9a3c8263 7042 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
7043
7044 /* Have we already tried to read this TU?
7045 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
7046 needn't exist in the global table yet). */
7047 if (sig_entry != NULL)
7048 return sig_entry;
7049
a2ce51a0
DE
7050 if (dwp_file->tus == NULL)
7051 return NULL;
ed2dc618 7052 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 7053 sig, 1 /* is_debug_types */);
a2ce51a0
DE
7054 if (dwo_entry == NULL)
7055 return NULL;
7056
ed2dc618
SM
7057 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
7058 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 7059
a2ce51a0
DE
7060 return sig_entry;
7061}
7062
380bca97 7063/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
7064 Returns NULL if signature SIG is not present in the table.
7065 It is up to the caller to complain about this. */
348e048f
DE
7066
7067static struct signatured_type *
a2ce51a0 7068lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 7069{
518817b3
SM
7070 struct dwarf2_per_objfile *dwarf2_per_objfile
7071 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 7072
a2ce51a0
DE
7073 if (cu->dwo_unit
7074 && dwarf2_per_objfile->using_index)
7075 {
7076 /* We're in a DWO/DWP file, and we're using .gdb_index.
7077 These cases require special processing. */
ed2dc618 7078 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
7079 return lookup_dwo_signatured_type (cu, sig);
7080 else
7081 return lookup_dwp_signatured_type (cu, sig);
7082 }
7083 else
7084 {
7085 struct signatured_type find_entry, *entry;
348e048f 7086
a2ce51a0
DE
7087 if (dwarf2_per_objfile->signatured_types == NULL)
7088 return NULL;
7089 find_entry.signature = sig;
9a3c8263
SM
7090 entry = ((struct signatured_type *)
7091 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
a2ce51a0
DE
7092 return entry;
7093 }
348e048f 7094}
42e7ad6c
DE
7095\f
7096/* Low level DIE reading support. */
348e048f 7097
d85a05f0
DJ
7098/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7099
7100static void
7101init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 7102 struct dwarf2_cu *cu,
3019eac3 7103 struct dwarf2_section_info *section,
685af9cd
TT
7104 struct dwo_file *dwo_file,
7105 struct abbrev_table *abbrev_table)
d85a05f0 7106{
fceca515 7107 gdb_assert (section->readin && section->buffer != NULL);
a32a8923 7108 reader->abfd = get_section_bfd_owner (section);
d85a05f0 7109 reader->cu = cu;
3019eac3 7110 reader->dwo_file = dwo_file;
dee91e82
DE
7111 reader->die_section = section;
7112 reader->buffer = section->buffer;
f664829e 7113 reader->buffer_end = section->buffer + section->size;
a2ce51a0 7114 reader->comp_dir = NULL;
685af9cd 7115 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
7116}
7117
b0c7bfa9
DE
7118/* Subroutine of init_cutu_and_read_dies to simplify it.
7119 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7120 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7121 already.
7122
7123 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7124 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
7125 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7126 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
7127 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7128 STUB_COMP_DIR may be non-NULL.
b0c7bfa9
DE
7129 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7130 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
7131 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7132 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7133 kept around for at least as long as *RESULT_READER.
7134
b0c7bfa9
DE
7135 The result is non-zero if a valid (non-dummy) DIE was found. */
7136
7137static int
7138read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7139 struct dwo_unit *dwo_unit,
b0c7bfa9 7140 struct die_info *stub_comp_unit_die,
a2ce51a0 7141 const char *stub_comp_dir,
b0c7bfa9 7142 struct die_reader_specs *result_reader,
d521ce57 7143 const gdb_byte **result_info_ptr,
b0c7bfa9 7144 struct die_info **result_comp_unit_die,
685af9cd
TT
7145 int *result_has_children,
7146 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 7147{
ed2dc618 7148 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
b0c7bfa9
DE
7149 struct objfile *objfile = dwarf2_per_objfile->objfile;
7150 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 7151 bfd *abfd;
d521ce57 7152 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
7153 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7154 int i,num_extra_attrs;
7155 struct dwarf2_section_info *dwo_abbrev_section;
7156 struct attribute *attr;
7157 struct die_info *comp_unit_die;
7158
b0aeadb3
DE
7159 /* At most one of these may be provided. */
7160 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 7161
b0c7bfa9
DE
7162 /* These attributes aren't processed until later:
7163 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
7164 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7165 referenced later. However, these attributes are found in the stub
7166 which we won't have later. In order to not impose this complication
7167 on the rest of the code, we read them here and copy them to the
7168 DWO CU/TU die. */
b0c7bfa9
DE
7169
7170 stmt_list = NULL;
7171 low_pc = NULL;
7172 high_pc = NULL;
7173 ranges = NULL;
7174 comp_dir = NULL;
7175
7176 if (stub_comp_unit_die != NULL)
7177 {
7178 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7179 DWO file. */
7180 if (! this_cu->is_debug_types)
7181 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7182 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7183 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7184 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7185 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7186
7187 /* There should be a DW_AT_addr_base attribute here (if needed).
336d760d
AT
7188 We need the value before we can process DW_FORM_GNU_addr_index
7189 or DW_FORM_addrx. */
b0c7bfa9
DE
7190 cu->addr_base = 0;
7191 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7192 if (attr)
7193 cu->addr_base = DW_UNSND (attr);
7194
7195 /* There should be a DW_AT_ranges_base attribute here (if needed).
7196 We need the value before we can process DW_AT_ranges. */
7197 cu->ranges_base = 0;
7198 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7199 if (attr)
7200 cu->ranges_base = DW_UNSND (attr);
7201 }
a2ce51a0
DE
7202 else if (stub_comp_dir != NULL)
7203 {
7204 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 7205 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
7206 comp_dir->name = DW_AT_comp_dir;
7207 comp_dir->form = DW_FORM_string;
7208 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7209 DW_STRING (comp_dir) = stub_comp_dir;
7210 }
b0c7bfa9
DE
7211
7212 /* Set up for reading the DWO CU/TU. */
7213 cu->dwo_unit = dwo_unit;
685af9cd 7214 dwarf2_section_info *section = dwo_unit->section;
b0c7bfa9 7215 dwarf2_read_section (objfile, section);
a32a8923 7216 abfd = get_section_bfd_owner (section);
9c541725
PA
7217 begin_info_ptr = info_ptr = (section->buffer
7218 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 7219 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9
DE
7220
7221 if (this_cu->is_debug_types)
7222 {
b0c7bfa9
DE
7223 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7224
ed2dc618
SM
7225 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7226 &cu->header, section,
b0c7bfa9 7227 dwo_abbrev_section,
43988095 7228 info_ptr, rcuh_kind::TYPE);
a2ce51a0 7229 /* This is not an assert because it can be caused by bad debug info. */
43988095 7230 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
7231 {
7232 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 7233 " TU at offset %s [in module %s]"),
a2ce51a0 7234 hex_string (sig_type->signature),
43988095 7235 hex_string (cu->header.signature),
9d8780f0 7236 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
7237 bfd_get_filename (abfd));
7238 }
9c541725 7239 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7240 /* For DWOs coming from DWP files, we don't know the CU length
7241 nor the type's offset in the TU until now. */
7242 dwo_unit->length = get_cu_length (&cu->header);
9c541725 7243 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
7244
7245 /* Establish the type offset that can be used to lookup the type.
7246 For DWO files, we don't know it until now. */
9c541725
PA
7247 sig_type->type_offset_in_section
7248 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
7249 }
7250 else
7251 {
ed2dc618
SM
7252 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7253 &cu->header, section,
b0c7bfa9 7254 dwo_abbrev_section,
43988095 7255 info_ptr, rcuh_kind::COMPILE);
9c541725 7256 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7257 /* For DWOs coming from DWP files, we don't know the CU length
7258 until now. */
7259 dwo_unit->length = get_cu_length (&cu->header);
7260 }
7261
685af9cd
TT
7262 *result_dwo_abbrev_table
7263 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7264 cu->header.abbrev_sect_off);
7265 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7266 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
7267
7268 /* Read in the die, but leave space to copy over the attributes
7269 from the stub. This has the benefit of simplifying the rest of
7270 the code - all the work to maintain the illusion of a single
7271 DW_TAG_{compile,type}_unit DIE is done here. */
7272 num_extra_attrs = ((stmt_list != NULL)
7273 + (low_pc != NULL)
7274 + (high_pc != NULL)
7275 + (ranges != NULL)
7276 + (comp_dir != NULL));
7277 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7278 result_has_children, num_extra_attrs);
7279
7280 /* Copy over the attributes from the stub to the DIE we just read in. */
7281 comp_unit_die = *result_comp_unit_die;
7282 i = comp_unit_die->num_attrs;
7283 if (stmt_list != NULL)
7284 comp_unit_die->attrs[i++] = *stmt_list;
7285 if (low_pc != NULL)
7286 comp_unit_die->attrs[i++] = *low_pc;
7287 if (high_pc != NULL)
7288 comp_unit_die->attrs[i++] = *high_pc;
7289 if (ranges != NULL)
7290 comp_unit_die->attrs[i++] = *ranges;
7291 if (comp_dir != NULL)
7292 comp_unit_die->attrs[i++] = *comp_dir;
7293 comp_unit_die->num_attrs += num_extra_attrs;
7294
b4f54984 7295 if (dwarf_die_debug)
bf6af496
DE
7296 {
7297 fprintf_unfiltered (gdb_stdlog,
7298 "Read die from %s@0x%x of %s:\n",
a32a8923 7299 get_section_name (section),
bf6af496
DE
7300 (unsigned) (begin_info_ptr - section->buffer),
7301 bfd_get_filename (abfd));
b4f54984 7302 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
7303 }
7304
a2ce51a0
DE
7305 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7306 TUs by skipping the stub and going directly to the entry in the DWO file.
7307 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7308 to get it via circuitous means. Blech. */
7309 if (comp_dir != NULL)
7310 result_reader->comp_dir = DW_STRING (comp_dir);
7311
b0c7bfa9
DE
7312 /* Skip dummy compilation units. */
7313 if (info_ptr >= begin_info_ptr + dwo_unit->length
7314 || peek_abbrev_code (abfd, info_ptr) == 0)
7315 return 0;
7316
7317 *result_info_ptr = info_ptr;
7318 return 1;
7319}
7320
a084a2a6
AT
7321/* Return the signature of the compile unit, if found. In DWARF 4 and before,
7322 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7323 signature is part of the header. */
7324static gdb::optional<ULONGEST>
7325lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7326{
7327 if (cu->header.version >= 5)
7328 return cu->header.signature;
7329 struct attribute *attr;
7330 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7331 if (attr == nullptr)
7332 return gdb::optional<ULONGEST> ();
7333 return DW_UNSND (attr);
7334}
7335
b0c7bfa9
DE
7336/* Subroutine of init_cutu_and_read_dies to simplify it.
7337 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 7338 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
7339
7340static struct dwo_unit *
7341lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7342 struct die_info *comp_unit_die)
7343{
7344 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9
DE
7345 struct dwo_unit *dwo_unit;
7346 const char *comp_dir, *dwo_name;
7347
a2ce51a0
DE
7348 gdb_assert (cu != NULL);
7349
b0c7bfa9 7350 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 7351 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 7352 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
7353
7354 if (this_cu->is_debug_types)
7355 {
7356 struct signatured_type *sig_type;
7357
7358 /* Since this_cu is the first member of struct signatured_type,
7359 we can go from a pointer to one to a pointer to the other. */
7360 sig_type = (struct signatured_type *) this_cu;
b0c7bfa9
DE
7361 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7362 }
7363 else
7364 {
a084a2a6
AT
7365 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
7366 if (!signature.has_value ())
b0c7bfa9
DE
7367 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7368 " [in module %s]"),
e3b94546 7369 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
b0c7bfa9 7370 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
a084a2a6 7371 *signature);
b0c7bfa9
DE
7372 }
7373
b0c7bfa9
DE
7374 return dwo_unit;
7375}
7376
a2ce51a0 7377/* Subroutine of init_cutu_and_read_dies to simplify it.
6aa5f3a6 7378 See it for a description of the parameters.
fcd3b13d 7379 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0
DE
7380
7381static void
6aa5f3a6
DE
7382init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7383 int use_existing_cu, int keep,
a2ce51a0
DE
7384 die_reader_func_ftype *die_reader_func,
7385 void *data)
7386{
fcd3b13d 7387 std::unique_ptr<dwarf2_cu> new_cu;
a2ce51a0 7388 struct signatured_type *sig_type;
a2ce51a0
DE
7389 struct die_reader_specs reader;
7390 const gdb_byte *info_ptr;
7391 struct die_info *comp_unit_die;
7392 int has_children;
ed2dc618 7393 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
a2ce51a0
DE
7394
7395 /* Verify we can do the following downcast, and that we have the
7396 data we need. */
7397 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7398 sig_type = (struct signatured_type *) this_cu;
7399 gdb_assert (sig_type->dwo_unit != NULL);
7400
6aa5f3a6
DE
7401 if (use_existing_cu && this_cu->cu != NULL)
7402 {
7403 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6
DE
7404 /* There's no need to do the rereading_dwo_cu handling that
7405 init_cutu_and_read_dies does since we don't read the stub. */
7406 }
7407 else
7408 {
7409 /* If !use_existing_cu, this_cu->cu must be NULL. */
7410 gdb_assert (this_cu->cu == NULL);
fcd3b13d 7411 new_cu.reset (new dwarf2_cu (this_cu));
6aa5f3a6
DE
7412 }
7413
7414 /* A future optimization, if needed, would be to use an existing
7415 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7416 could share abbrev tables. */
a2ce51a0 7417
685af9cd
TT
7418 /* The abbreviation table used by READER, this must live at least as long as
7419 READER. */
7420 abbrev_table_up dwo_abbrev_table;
7421
a2ce51a0 7422 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
a2ce51a0
DE
7423 NULL /* stub_comp_unit_die */,
7424 sig_type->dwo_unit->dwo_file->comp_dir,
7425 &reader, &info_ptr,
685af9cd
TT
7426 &comp_unit_die, &has_children,
7427 &dwo_abbrev_table) == 0)
a2ce51a0
DE
7428 {
7429 /* Dummy die. */
a2ce51a0
DE
7430 return;
7431 }
7432
7433 /* All the "real" work is done here. */
7434 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7435
6aa5f3a6 7436 /* This duplicates the code in init_cutu_and_read_dies,
a2ce51a0
DE
7437 but the alternative is making the latter more complex.
7438 This function is only for the special case of using DWO files directly:
7439 no point in overly complicating the general case just to handle this. */
fcd3b13d 7440 if (new_cu != NULL && keep)
a2ce51a0 7441 {
fcd3b13d
SM
7442 /* Link this CU into read_in_chain. */
7443 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7444 dwarf2_per_objfile->read_in_chain = this_cu;
7445 /* The chain owns it now. */
7446 new_cu.release ();
a2ce51a0 7447 }
a2ce51a0
DE
7448}
7449
fd820528 7450/* Initialize a CU (or TU) and read its DIEs.
3019eac3 7451 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 7452
f4dc4d17
DE
7453 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7454 Otherwise the table specified in the comp unit header is read in and used.
7455 This is an optimization for when we already have the abbrev table.
7456
dee91e82
DE
7457 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7458 Otherwise, a new CU is allocated with xmalloc.
7459
7460 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7461 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7462
7463 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 7464 linker) then DIE_READER_FUNC will not get called. */
aaa75496 7465
70221824 7466static void
fd820528 7467init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 7468 struct abbrev_table *abbrev_table,
fd820528 7469 int use_existing_cu, int keep,
58f0c718 7470 bool skip_partial,
fd820528
DE
7471 die_reader_func_ftype *die_reader_func,
7472 void *data)
c906108c 7473{
ed2dc618 7474 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7475 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7476 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7477 bfd *abfd = get_section_bfd_owner (section);
dee91e82 7478 struct dwarf2_cu *cu;
d521ce57 7479 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7480 struct die_reader_specs reader;
d85a05f0 7481 struct die_info *comp_unit_die;
dee91e82 7482 int has_children;
dee91e82 7483 struct signatured_type *sig_type = NULL;
4bdcc0c1 7484 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7485 /* Non-zero if CU currently points to a DWO file and we need to
7486 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7487 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7488 int rereading_dwo_cu = 0;
c906108c 7489
b4f54984 7490 if (dwarf_die_debug)
9d8780f0 7491 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7492 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7493 sect_offset_str (this_cu->sect_off));
09406207 7494
dee91e82
DE
7495 if (use_existing_cu)
7496 gdb_assert (keep);
23745b47 7497
a2ce51a0
DE
7498 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7499 file (instead of going through the stub), short-circuit all of this. */
7500 if (this_cu->reading_dwo_directly)
7501 {
7502 /* Narrow down the scope of possibilities to have to understand. */
7503 gdb_assert (this_cu->is_debug_types);
7504 gdb_assert (abbrev_table == NULL);
6aa5f3a6
DE
7505 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7506 die_reader_func, data);
a2ce51a0
DE
7507 return;
7508 }
7509
dee91e82
DE
7510 /* This is cheap if the section is already read in. */
7511 dwarf2_read_section (objfile, section);
7512
9c541725 7513 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7514
7515 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 7516
fcd3b13d 7517 std::unique_ptr<dwarf2_cu> new_cu;
dee91e82
DE
7518 if (use_existing_cu && this_cu->cu != NULL)
7519 {
7520 cu = this_cu->cu;
42e7ad6c
DE
7521 /* If this CU is from a DWO file we need to start over, we need to
7522 refetch the attributes from the skeleton CU.
7523 This could be optimized by retrieving those attributes from when we
7524 were here the first time: the previous comp_unit_die was stored in
7525 comp_unit_obstack. But there's no data yet that we need this
7526 optimization. */
7527 if (cu->dwo_unit != NULL)
7528 rereading_dwo_cu = 1;
dee91e82
DE
7529 }
7530 else
7531 {
7532 /* If !use_existing_cu, this_cu->cu must be NULL. */
7533 gdb_assert (this_cu->cu == NULL);
fcd3b13d
SM
7534 new_cu.reset (new dwarf2_cu (this_cu));
7535 cu = new_cu.get ();
42e7ad6c 7536 }
dee91e82 7537
b0c7bfa9 7538 /* Get the header. */
9c541725 7539 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7540 {
7541 /* We already have the header, there's no need to read it in again. */
9c541725 7542 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7543 }
7544 else
7545 {
3019eac3 7546 if (this_cu->is_debug_types)
dee91e82 7547 {
ed2dc618
SM
7548 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7549 &cu->header, section,
4bdcc0c1 7550 abbrev_section, info_ptr,
43988095 7551 rcuh_kind::TYPE);
dee91e82 7552
42e7ad6c
DE
7553 /* Since per_cu is the first member of struct signatured_type,
7554 we can go from a pointer to one to a pointer to the other. */
7555 sig_type = (struct signatured_type *) this_cu;
43988095 7556 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7557 gdb_assert (sig_type->type_offset_in_tu
7558 == cu->header.type_cu_offset_in_tu);
7559 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7560
42e7ad6c
DE
7561 /* LENGTH has not been set yet for type units if we're
7562 using .gdb_index. */
1ce1cefd 7563 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
7564
7565 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7566 sig_type->type_offset_in_section =
7567 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7568
7569 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7570 }
7571 else
7572 {
ed2dc618
SM
7573 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7574 &cu->header, section,
4bdcc0c1 7575 abbrev_section,
43988095
JK
7576 info_ptr,
7577 rcuh_kind::COMPILE);
dee91e82 7578
9c541725 7579 gdb_assert (this_cu->sect_off == cu->header.sect_off);
1ce1cefd 7580 gdb_assert (this_cu->length == get_cu_length (&cu->header));
43988095 7581 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7582 }
7583 }
10b3939b 7584
6caca83c 7585 /* Skip dummy compilation units. */
dee91e82 7586 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7587 || peek_abbrev_code (abfd, info_ptr) == 0)
fcd3b13d 7588 return;
6caca83c 7589
433df2d4
DE
7590 /* If we don't have them yet, read the abbrevs for this compilation unit.
7591 And if we need to read them now, make sure they're freed when we're
685af9cd
TT
7592 done (own the table through ABBREV_TABLE_HOLDER). */
7593 abbrev_table_up abbrev_table_holder;
f4dc4d17 7594 if (abbrev_table != NULL)
685af9cd
TT
7595 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7596 else
f4dc4d17 7597 {
685af9cd
TT
7598 abbrev_table_holder
7599 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7600 cu->header.abbrev_sect_off);
7601 abbrev_table = abbrev_table_holder.get ();
42e7ad6c 7602 }
af703f96 7603
dee91e82 7604 /* Read the top level CU/TU die. */
685af9cd 7605 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
dee91e82 7606 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 7607
58f0c718
TT
7608 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7609 return;
7610
b0c7bfa9 7611 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7612 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7613 table from the DWO file and pass the ownership over to us. It will be
7614 referenced from READER, so we must make sure to free it after we're done
7615 with READER.
7616
b0c7bfa9
DE
7617 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7618 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 7619 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
685af9cd 7620 abbrev_table_up dwo_abbrev_table;
a084a2a6 7621 if (dwo_name != nullptr)
3019eac3 7622 {
3019eac3 7623 struct dwo_unit *dwo_unit;
b0c7bfa9 7624 struct die_info *dwo_comp_unit_die;
3019eac3
DE
7625
7626 if (has_children)
6a506a2d 7627 {
b98664d3 7628 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7629 " has children (offset %s) [in module %s]"),
7630 sect_offset_str (this_cu->sect_off),
7631 bfd_get_filename (abfd));
6a506a2d 7632 }
b0c7bfa9 7633 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 7634 if (dwo_unit != NULL)
3019eac3 7635 {
6a506a2d 7636 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
a2ce51a0 7637 comp_unit_die, NULL,
6a506a2d 7638 &reader, &info_ptr,
685af9cd
TT
7639 &dwo_comp_unit_die, &has_children,
7640 &dwo_abbrev_table) == 0)
6a506a2d
DE
7641 {
7642 /* Dummy die. */
6a506a2d
DE
7643 return;
7644 }
7645 comp_unit_die = dwo_comp_unit_die;
7646 }
7647 else
7648 {
7649 /* Yikes, we couldn't find the rest of the DIE, we only have
7650 the stub. A complaint has already been logged. There's
7651 not much more we can do except pass on the stub DIE to
7652 die_reader_func. We don't want to throw an error on bad
7653 debug info. */
3019eac3
DE
7654 }
7655 }
7656
b0c7bfa9 7657 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
7658 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7659
b0c7bfa9 7660 /* Done, clean up. */
fcd3b13d 7661 if (new_cu != NULL && keep)
348e048f 7662 {
fcd3b13d
SM
7663 /* Link this CU into read_in_chain. */
7664 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7665 dwarf2_per_objfile->read_in_chain = this_cu;
7666 /* The chain owns it now. */
7667 new_cu.release ();
348e048f 7668 }
dee91e82
DE
7669}
7670
33e80786
DE
7671/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7672 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7673 to have already done the lookup to find the DWO file).
dee91e82
DE
7674
7675 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7676 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7677
7678 We fill in THIS_CU->length.
7679
7680 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7681 linker) then DIE_READER_FUNC will not get called.
7682
7683 THIS_CU->cu is always freed when done.
3019eac3
DE
7684 This is done in order to not leave THIS_CU->cu in a state where we have
7685 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
7686
7687static void
7688init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3019eac3 7689 struct dwo_file *dwo_file,
dee91e82
DE
7690 die_reader_func_ftype *die_reader_func,
7691 void *data)
7692{
ed2dc618 7693 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7694 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7695 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7696 bfd *abfd = get_section_bfd_owner (section);
33e80786 7697 struct dwarf2_section_info *abbrev_section;
d521ce57 7698 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7699 struct die_reader_specs reader;
dee91e82
DE
7700 struct die_info *comp_unit_die;
7701 int has_children;
7702
b4f54984 7703 if (dwarf_die_debug)
9d8780f0 7704 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7705 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7706 sect_offset_str (this_cu->sect_off));
09406207 7707
dee91e82
DE
7708 gdb_assert (this_cu->cu == NULL);
7709
33e80786
DE
7710 abbrev_section = (dwo_file != NULL
7711 ? &dwo_file->sections.abbrev
7712 : get_abbrev_section_for_cu (this_cu));
7713
dee91e82
DE
7714 /* This is cheap if the section is already read in. */
7715 dwarf2_read_section (objfile, section);
7716
fcd3b13d 7717 struct dwarf2_cu cu (this_cu);
dee91e82 7718
9c541725 7719 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618
SM
7720 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7721 &cu.header, section,
4bdcc0c1 7722 abbrev_section, info_ptr,
43988095
JK
7723 (this_cu->is_debug_types
7724 ? rcuh_kind::TYPE
7725 : rcuh_kind::COMPILE));
dee91e82 7726
1ce1cefd 7727 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
7728
7729 /* Skip dummy compilation units. */
7730 if (info_ptr >= begin_info_ptr + this_cu->length
7731 || peek_abbrev_code (abfd, info_ptr) == 0)
fcd3b13d 7732 return;
72bf9492 7733
685af9cd
TT
7734 abbrev_table_up abbrev_table
7735 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7736 cu.header.abbrev_sect_off);
dee91e82 7737
685af9cd 7738 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
dee91e82
DE
7739 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7740
7741 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
dee91e82
DE
7742}
7743
3019eac3
DE
7744/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
7745 does not lookup the specified DWO file.
7746 This cannot be used to read DWO files.
dee91e82
DE
7747
7748 THIS_CU->cu is always freed when done.
3019eac3
DE
7749 This is done in order to not leave THIS_CU->cu in a state where we have
7750 to care whether it refers to the "main" CU or the DWO CU.
7751 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
7752
7753static void
7754init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
7755 die_reader_func_ftype *die_reader_func,
7756 void *data)
7757{
33e80786 7758 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
dee91e82 7759}
0018ea6f
DE
7760\f
7761/* Type Unit Groups.
dee91e82 7762
0018ea6f
DE
7763 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7764 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7765 so that all types coming from the same compilation (.o file) are grouped
7766 together. A future step could be to put the types in the same symtab as
7767 the CU the types ultimately came from. */
ff013f42 7768
f4dc4d17
DE
7769static hashval_t
7770hash_type_unit_group (const void *item)
7771{
9a3c8263
SM
7772 const struct type_unit_group *tu_group
7773 = (const struct type_unit_group *) item;
f4dc4d17 7774
094b34ac 7775 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7776}
348e048f
DE
7777
7778static int
f4dc4d17 7779eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7780{
9a3c8263
SM
7781 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7782 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7783
094b34ac 7784 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7785}
348e048f 7786
f4dc4d17
DE
7787/* Allocate a hash table for type unit groups. */
7788
7789static htab_t
ed2dc618 7790allocate_type_unit_groups_table (struct objfile *objfile)
f4dc4d17
DE
7791{
7792 return htab_create_alloc_ex (3,
7793 hash_type_unit_group,
7794 eq_type_unit_group,
7795 NULL,
ed2dc618 7796 &objfile->objfile_obstack,
f4dc4d17
DE
7797 hashtab_obstack_allocate,
7798 dummy_obstack_deallocate);
7799}
dee91e82 7800
f4dc4d17
DE
7801/* Type units that don't have DW_AT_stmt_list are grouped into their own
7802 partial symtabs. We combine several TUs per psymtab to not let the size
7803 of any one psymtab grow too big. */
7804#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7805#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7806
094b34ac 7807/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7808 Create the type_unit_group object used to hold one or more TUs. */
7809
7810static struct type_unit_group *
094b34ac 7811create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7812{
518817b3
SM
7813 struct dwarf2_per_objfile *dwarf2_per_objfile
7814 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17 7815 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 7816 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7817 struct type_unit_group *tu_group;
f4dc4d17
DE
7818
7819 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7820 struct type_unit_group);
094b34ac 7821 per_cu = &tu_group->per_cu;
518817b3 7822 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
f4dc4d17 7823
094b34ac
DE
7824 if (dwarf2_per_objfile->using_index)
7825 {
7826 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7827 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7828 }
7829 else
7830 {
9c541725 7831 unsigned int line_offset = to_underlying (line_offset_struct);
094b34ac 7832 struct partial_symtab *pst;
528e1572 7833 std::string name;
094b34ac
DE
7834
7835 /* Give the symtab a useful name for debug purposes. */
7836 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7837 name = string_printf ("<type_units_%d>",
7838 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7839 else
528e1572 7840 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7841
528e1572 7842 pst = create_partial_symtab (per_cu, name.c_str ());
094b34ac 7843 pst->anonymous = 1;
094b34ac 7844 }
f4dc4d17 7845
094b34ac 7846 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7847 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7848
7849 return tu_group;
7850}
7851
094b34ac
DE
7852/* Look up the type_unit_group for type unit CU, and create it if necessary.
7853 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7854
7855static struct type_unit_group *
ff39bb5e 7856get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7857{
518817b3
SM
7858 struct dwarf2_per_objfile *dwarf2_per_objfile
7859 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7860 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7861 struct type_unit_group *tu_group;
7862 void **slot;
7863 unsigned int line_offset;
7864 struct type_unit_group type_unit_group_for_lookup;
7865
7866 if (dwarf2_per_objfile->type_unit_groups == NULL)
7867 {
7868 dwarf2_per_objfile->type_unit_groups =
ed2dc618 7869 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
f4dc4d17
DE
7870 }
7871
7872 /* Do we need to create a new group, or can we use an existing one? */
7873
7874 if (stmt_list)
7875 {
7876 line_offset = DW_UNSND (stmt_list);
7877 ++tu_stats->nr_symtab_sharers;
7878 }
7879 else
7880 {
7881 /* Ugh, no stmt_list. Rare, but we have to handle it.
7882 We can do various things here like create one group per TU or
7883 spread them over multiple groups to split up the expansion work.
7884 To avoid worst case scenarios (too many groups or too large groups)
7885 we, umm, group them in bunches. */
7886 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7887 | (tu_stats->nr_stmt_less_type_units
7888 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7889 ++tu_stats->nr_stmt_less_type_units;
7890 }
7891
094b34ac 7892 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7893 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
f4dc4d17
DE
7894 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7895 &type_unit_group_for_lookup, INSERT);
7896 if (*slot != NULL)
7897 {
9a3c8263 7898 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7899 gdb_assert (tu_group != NULL);
7900 }
7901 else
7902 {
9c541725 7903 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7904 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7905 *slot = tu_group;
7906 ++tu_stats->nr_symtabs;
7907 }
7908
7909 return tu_group;
7910}
0018ea6f
DE
7911\f
7912/* Partial symbol tables. */
7913
7914/* Create a psymtab named NAME and assign it to PER_CU.
7915
7916 The caller must fill in the following details:
7917 dirname, textlow, texthigh. */
7918
7919static struct partial_symtab *
7920create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7921{
e3b94546 7922 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
0018ea6f
DE
7923 struct partial_symtab *pst;
7924
939652a5 7925 pst = start_psymtab_common (objfile, name, 0);
0018ea6f
DE
7926
7927 pst->psymtabs_addrmap_supported = 1;
7928
7929 /* This is the glue that links PST into GDB's symbol API. */
7930 pst->read_symtab_private = per_cu;
7931 pst->read_symtab = dwarf2_read_symtab;
7932 per_cu->v.psymtab = pst;
7933
7934 return pst;
7935}
7936
b93601f3
TT
7937/* The DATA object passed to process_psymtab_comp_unit_reader has this
7938 type. */
7939
7940struct process_psymtab_comp_unit_data
7941{
7942 /* True if we are reading a DW_TAG_partial_unit. */
7943
7944 int want_partial_unit;
7945
7946 /* The "pretend" language that is used if the CU doesn't declare a
7947 language. */
7948
7949 enum language pretend_language;
7950};
7951
0018ea6f
DE
7952/* die_reader_func for process_psymtab_comp_unit. */
7953
7954static void
7955process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7956 const gdb_byte *info_ptr,
0018ea6f
DE
7957 struct die_info *comp_unit_die,
7958 int has_children,
7959 void *data)
7960{
7961 struct dwarf2_cu *cu = reader->cu;
518817b3 7962 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 7963 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 7964 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7965 CORE_ADDR baseaddr;
7966 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7967 struct partial_symtab *pst;
3a2b436a 7968 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7969 const char *filename;
9a3c8263
SM
7970 struct process_psymtab_comp_unit_data *info
7971 = (struct process_psymtab_comp_unit_data *) data;
0018ea6f 7972
b93601f3 7973 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
0018ea6f
DE
7974 return;
7975
7976 gdb_assert (! per_cu->is_debug_types);
7977
b93601f3 7978 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
0018ea6f 7979
0018ea6f 7980 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
7981 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7982 if (filename == NULL)
0018ea6f 7983 filename = "";
0018ea6f
DE
7984
7985 pst = create_partial_symtab (per_cu, filename);
7986
7987 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7988 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f
DE
7989
7990 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7991
7992 dwarf2_find_base_address (comp_unit_die, cu);
7993
7994 /* Possibly set the default values of LOWPC and HIGHPC from
7995 `DW_AT_ranges'. */
3a2b436a
JK
7996 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7997 &best_highpc, cu, pst);
7998 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
7999 {
8000 CORE_ADDR low
8001 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
8002 - baseaddr);
8003 CORE_ADDR high
8004 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
8005 - baseaddr - 1);
8006 /* Store the contiguous range if it is not empty; it can be
8007 empty for CUs with no code. */
d320c2b5
TT
8008 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
8009 low, high, pst);
79748972 8010 }
0018ea6f
DE
8011
8012 /* Check if comp unit has_children.
8013 If so, read the rest of the partial symbols from this comp unit.
8014 If not, there's no more debug_info for this comp unit. */
8015 if (has_children)
8016 {
8017 struct partial_die_info *first_die;
8018 CORE_ADDR lowpc, highpc;
8019
8020 lowpc = ((CORE_ADDR) -1);
8021 highpc = ((CORE_ADDR) 0);
8022
8023 first_die = load_partial_dies (reader, info_ptr, 1);
8024
8025 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 8026 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
8027
8028 /* If we didn't find a lowpc, set it to highpc to avoid
8029 complaints from `maint check'. */
8030 if (lowpc == ((CORE_ADDR) -1))
8031 lowpc = highpc;
8032
8033 /* If the compilation unit didn't have an explicit address range,
8034 then use the information extracted from its child dies. */
e385593e 8035 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
8036 {
8037 best_lowpc = lowpc;
8038 best_highpc = highpc;
8039 }
8040 }
4ae976d1 8041 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
8042 best_lowpc + baseaddr)
8043 - baseaddr);
4ae976d1 8044 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
8045 best_highpc + baseaddr)
8046 - baseaddr);
0018ea6f 8047
8763cede 8048 end_psymtab_common (objfile, pst);
0018ea6f
DE
8049
8050 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
8051 {
8052 int i;
8053 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8054 struct dwarf2_per_cu_data *iter;
8055
8056 /* Fill in 'dependencies' here; we fill in 'users' in a
8057 post-pass. */
8058 pst->number_of_dependencies = len;
a9342b62
TT
8059 pst->dependencies
8060 = objfile->partial_symtabs->allocate_dependencies (len);
0018ea6f
DE
8061 for (i = 0;
8062 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
8063 i, iter);
8064 ++i)
8065 pst->dependencies[i] = iter->v.psymtab;
8066
8067 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
8068 }
8069
8070 /* Get the list of files included in the current compilation unit,
8071 and build a psymtab for each of them. */
8072 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8073
b4f54984 8074 if (dwarf_read_debug)
b926417a
TT
8075 fprintf_unfiltered (gdb_stdlog,
8076 "Psymtab for %s unit @%s: %s - %s"
8077 ", %d global, %d static syms\n",
8078 per_cu->is_debug_types ? "type" : "comp",
8079 sect_offset_str (per_cu->sect_off),
8080 paddress (gdbarch, pst->text_low (objfile)),
8081 paddress (gdbarch, pst->text_high (objfile)),
8082 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
8083}
8084
8085/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8086 Process compilation unit THIS_CU for a psymtab. */
8087
8088static void
8089process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
8090 int want_partial_unit,
8091 enum language pretend_language)
0018ea6f
DE
8092{
8093 /* If this compilation unit was already read in, free the
8094 cached copy in order to read it in again. This is
8095 necessary because we skipped some symbols when we first
8096 read in the compilation unit (see load_partial_dies).
8097 This problem could be avoided, but the benefit is unclear. */
8098 if (this_cu->cu != NULL)
8099 free_one_cached_comp_unit (this_cu);
8100
f1902523 8101 if (this_cu->is_debug_types)
58f0c718
TT
8102 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8103 build_type_psymtabs_reader, NULL);
f1902523
JK
8104 else
8105 {
8106 process_psymtab_comp_unit_data info;
8107 info.want_partial_unit = want_partial_unit;
8108 info.pretend_language = pretend_language;
58f0c718 8109 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
f1902523
JK
8110 process_psymtab_comp_unit_reader, &info);
8111 }
0018ea6f
DE
8112
8113 /* Age out any secondary CUs. */
ed2dc618 8114 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 8115}
f4dc4d17
DE
8116
8117/* Reader function for build_type_psymtabs. */
8118
8119static void
8120build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 8121 const gdb_byte *info_ptr,
f4dc4d17
DE
8122 struct die_info *type_unit_die,
8123 int has_children,
8124 void *data)
8125{
ed2dc618 8126 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 8127 = reader->cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
8128 struct objfile *objfile = dwarf2_per_objfile->objfile;
8129 struct dwarf2_cu *cu = reader->cu;
8130 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 8131 struct signatured_type *sig_type;
f4dc4d17
DE
8132 struct type_unit_group *tu_group;
8133 struct attribute *attr;
8134 struct partial_die_info *first_die;
8135 CORE_ADDR lowpc, highpc;
8136 struct partial_symtab *pst;
8137
8138 gdb_assert (data == NULL);
0186c6a7
DE
8139 gdb_assert (per_cu->is_debug_types);
8140 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
8141
8142 if (! has_children)
8143 return;
8144
8145 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 8146 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 8147
0186c6a7 8148 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
f4dc4d17
DE
8149
8150 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
f4dc4d17
DE
8151 pst = create_partial_symtab (per_cu, "");
8152 pst->anonymous = 1;
8153
8154 first_die = load_partial_dies (reader, info_ptr, 1);
8155
8156 lowpc = (CORE_ADDR) -1;
8157 highpc = (CORE_ADDR) 0;
8158 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8159
8763cede 8160 end_psymtab_common (objfile, pst);
f4dc4d17
DE
8161}
8162
73051182
DE
8163/* Struct used to sort TUs by their abbreviation table offset. */
8164
8165struct tu_abbrev_offset
8166{
b2bdb8cf
SM
8167 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
8168 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
8169 {}
8170
8171 signatured_type *sig_type;
73051182
DE
8172 sect_offset abbrev_offset;
8173};
8174
484cf504 8175/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 8176
484cf504
TT
8177static bool
8178sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8179 const struct tu_abbrev_offset &b)
73051182 8180{
484cf504 8181 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
8182}
8183
8184/* Efficiently read all the type units.
8185 This does the bulk of the work for build_type_psymtabs.
8186
8187 The efficiency is because we sort TUs by the abbrev table they use and
8188 only read each abbrev table once. In one program there are 200K TUs
8189 sharing 8K abbrev tables.
8190
8191 The main purpose of this function is to support building the
8192 dwarf2_per_objfile->type_unit_groups table.
8193 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8194 can collapse the search space by grouping them by stmt_list.
8195 The savings can be significant, in the same program from above the 200K TUs
8196 share 8K stmt_list tables.
8197
8198 FUNC is expected to call get_type_unit_group, which will create the
8199 struct type_unit_group if necessary and add it to
8200 dwarf2_per_objfile->type_unit_groups. */
8201
8202static void
ed2dc618 8203build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 8204{
73051182 8205 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
685af9cd 8206 abbrev_table_up abbrev_table;
73051182 8207 sect_offset abbrev_offset;
73051182
DE
8208
8209 /* It's up to the caller to not call us multiple times. */
8210 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8211
b2bdb8cf 8212 if (dwarf2_per_objfile->all_type_units.empty ())
73051182
DE
8213 return;
8214
8215 /* TUs typically share abbrev tables, and there can be way more TUs than
8216 abbrev tables. Sort by abbrev table to reduce the number of times we
8217 read each abbrev table in.
8218 Alternatives are to punt or to maintain a cache of abbrev tables.
8219 This is simpler and efficient enough for now.
8220
8221 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8222 symtab to use). Typically TUs with the same abbrev offset have the same
8223 stmt_list value too so in practice this should work well.
8224
8225 The basic algorithm here is:
8226
8227 sort TUs by abbrev table
8228 for each TU with same abbrev table:
8229 read abbrev table if first user
8230 read TU top level DIE
8231 [IWBN if DWO skeletons had DW_AT_stmt_list]
8232 call FUNC */
8233
b4f54984 8234 if (dwarf_read_debug)
73051182
DE
8235 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8236
8237 /* Sort in a separate table to maintain the order of all_type_units
8238 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf
SM
8239 std::vector<tu_abbrev_offset> sorted_by_abbrev;
8240 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
8241
8242 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
8243 sorted_by_abbrev.emplace_back
8244 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
8245 sig_type->per_cu.section,
8246 sig_type->per_cu.sect_off));
73051182 8247
484cf504
TT
8248 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8249 sort_tu_by_abbrev_offset);
73051182 8250
9c541725 8251 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 8252
b2bdb8cf 8253 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 8254 {
73051182
DE
8255 /* Switch to the next abbrev table if necessary. */
8256 if (abbrev_table == NULL
b2bdb8cf 8257 || tu.abbrev_offset != abbrev_offset)
73051182 8258 {
b2bdb8cf 8259 abbrev_offset = tu.abbrev_offset;
73051182 8260 abbrev_table =
ed2dc618
SM
8261 abbrev_table_read_table (dwarf2_per_objfile,
8262 &dwarf2_per_objfile->abbrev,
73051182
DE
8263 abbrev_offset);
8264 ++tu_stats->nr_uniq_abbrev_tables;
8265 }
8266
b2bdb8cf 8267 init_cutu_and_read_dies (&tu.sig_type->per_cu, abbrev_table.get (),
58f0c718 8268 0, 0, false, build_type_psymtabs_reader, NULL);
73051182 8269 }
6aa5f3a6 8270}
73051182 8271
6aa5f3a6
DE
8272/* Print collected type unit statistics. */
8273
8274static void
ed2dc618 8275print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8276{
8277 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8278
8279 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf
SM
8280 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8281 dwarf2_per_objfile->all_type_units.size ());
6aa5f3a6
DE
8282 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8283 tu_stats->nr_uniq_abbrev_tables);
8284 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8285 tu_stats->nr_symtabs);
8286 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8287 tu_stats->nr_symtab_sharers);
8288 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8289 tu_stats->nr_stmt_less_type_units);
8290 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8291 tu_stats->nr_all_type_units_reallocs);
73051182
DE
8292}
8293
f4dc4d17
DE
8294/* Traversal function for build_type_psymtabs. */
8295
8296static int
8297build_type_psymtab_dependencies (void **slot, void *info)
8298{
ed2dc618
SM
8299 struct dwarf2_per_objfile *dwarf2_per_objfile
8300 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
8301 struct objfile *objfile = dwarf2_per_objfile->objfile;
8302 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 8303 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 8304 struct partial_symtab *pst = per_cu->v.psymtab;
0186c6a7
DE
8305 int len = VEC_length (sig_type_ptr, tu_group->tus);
8306 struct signatured_type *iter;
f4dc4d17
DE
8307 int i;
8308
8309 gdb_assert (len > 0);
0186c6a7 8310 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
8311
8312 pst->number_of_dependencies = len;
a9342b62 8313 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
f4dc4d17 8314 for (i = 0;
0186c6a7 8315 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
f4dc4d17
DE
8316 ++i)
8317 {
0186c6a7
DE
8318 gdb_assert (iter->per_cu.is_debug_types);
8319 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 8320 iter->type_unit_group = tu_group;
f4dc4d17
DE
8321 }
8322
0186c6a7 8323 VEC_free (sig_type_ptr, tu_group->tus);
348e048f
DE
8324
8325 return 1;
8326}
8327
8328/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8329 Build partial symbol tables for the .debug_types comp-units. */
8330
8331static void
ed2dc618 8332build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 8333{
ed2dc618 8334 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
8335 return;
8336
ed2dc618 8337 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 8338}
f4dc4d17 8339
6aa5f3a6
DE
8340/* Traversal function for process_skeletonless_type_unit.
8341 Read a TU in a DWO file and build partial symbols for it. */
8342
8343static int
8344process_skeletonless_type_unit (void **slot, void *info)
8345{
8346 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
8347 struct dwarf2_per_objfile *dwarf2_per_objfile
8348 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
8349 struct signatured_type find_entry, *entry;
8350
8351 /* If this TU doesn't exist in the global table, add it and read it in. */
8352
8353 if (dwarf2_per_objfile->signatured_types == NULL)
8354 {
8355 dwarf2_per_objfile->signatured_types
ed2dc618 8356 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
6aa5f3a6
DE
8357 }
8358
8359 find_entry.signature = dwo_unit->signature;
8360 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8361 INSERT);
8362 /* If we've already seen this type there's nothing to do. What's happening
8363 is we're doing our own version of comdat-folding here. */
8364 if (*slot != NULL)
8365 return 1;
8366
8367 /* This does the job that create_all_type_units would have done for
8368 this TU. */
ed2dc618
SM
8369 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8370 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
8371 *slot = entry;
8372
8373 /* This does the job that build_type_psymtabs_1 would have done. */
58f0c718 8374 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0, false,
6aa5f3a6
DE
8375 build_type_psymtabs_reader, NULL);
8376
8377 return 1;
8378}
8379
8380/* Traversal function for process_skeletonless_type_units. */
8381
8382static int
8383process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8384{
8385 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8386
8387 if (dwo_file->tus != NULL)
8388 {
8389 htab_traverse_noresize (dwo_file->tus,
8390 process_skeletonless_type_unit, info);
8391 }
8392
8393 return 1;
8394}
8395
8396/* Scan all TUs of DWO files, verifying we've processed them.
8397 This is needed in case a TU was emitted without its skeleton.
8398 Note: This can't be done until we know what all the DWO files are. */
8399
8400static void
ed2dc618 8401process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8402{
8403 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 8404 if (get_dwp_file (dwarf2_per_objfile) == NULL
6aa5f3a6
DE
8405 && dwarf2_per_objfile->dwo_files != NULL)
8406 {
51ac9db5 8407 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
6aa5f3a6 8408 process_dwo_file_for_skeletonless_type_units,
ed2dc618 8409 dwarf2_per_objfile);
6aa5f3a6 8410 }
348e048f
DE
8411}
8412
ed2dc618 8413/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
8414
8415static void
ed2dc618 8416set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 8417{
b76e467d 8418 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
95554aad 8419 {
95554aad 8420 struct partial_symtab *pst = per_cu->v.psymtab;
95554aad 8421
36586728
TT
8422 if (pst == NULL)
8423 continue;
8424
b76e467d 8425 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
8426 {
8427 /* Set the 'user' field only if it is not already set. */
8428 if (pst->dependencies[j]->user == NULL)
8429 pst->dependencies[j]->user = pst;
8430 }
8431 }
8432}
8433
93311388
DE
8434/* Build the partial symbol table by doing a quick pass through the
8435 .debug_info and .debug_abbrev sections. */
72bf9492 8436
93311388 8437static void
ed2dc618 8438dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 8439{
ed2dc618 8440 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 8441
b4f54984 8442 if (dwarf_read_debug)
45cfd468
DE
8443 {
8444 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 8445 objfile_name (objfile));
45cfd468
DE
8446 }
8447
98bfdba5
PA
8448 dwarf2_per_objfile->reading_partial_symbols = 1;
8449
be391dca 8450 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 8451
93311388
DE
8452 /* Any cached compilation units will be linked by the per-objfile
8453 read_in_chain. Make sure to free them when we're done. */
11ed8cad 8454 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 8455
ed2dc618 8456 build_type_psymtabs (dwarf2_per_objfile);
348e048f 8457
ed2dc618 8458 create_all_comp_units (dwarf2_per_objfile);
c906108c 8459
60606b2c
TT
8460 /* Create a temporary address map on a temporary obstack. We later
8461 copy this to the final obstack. */
8268c778 8462 auto_obstack temp_obstack;
791afaa2
TT
8463
8464 scoped_restore save_psymtabs_addrmap
d320c2b5 8465 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 8466 addrmap_create_mutable (&temp_obstack));
72bf9492 8467
b76e467d
SM
8468 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8469 process_psymtab_comp_unit (per_cu, 0, language_minimal);
ff013f42 8470
6aa5f3a6 8471 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 8472 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
8473
8474 /* Now that all TUs have been processed we can fill in the dependencies. */
8475 if (dwarf2_per_objfile->type_unit_groups != NULL)
8476 {
8477 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
ed2dc618 8478 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
8479 }
8480
b4f54984 8481 if (dwarf_read_debug)
ed2dc618 8482 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 8483
ed2dc618 8484 set_partial_user (dwarf2_per_objfile);
95554aad 8485
d320c2b5
TT
8486 objfile->partial_symtabs->psymtabs_addrmap
8487 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 8488 objfile->partial_symtabs->obstack ());
791afaa2
TT
8489 /* At this point we want to keep the address map. */
8490 save_psymtabs_addrmap.release ();
ff013f42 8491
b4f54984 8492 if (dwarf_read_debug)
45cfd468 8493 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 8494 objfile_name (objfile));
ae038cb0
DJ
8495}
8496
3019eac3 8497/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
8498
8499static void
dee91e82 8500load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8501 const gdb_byte *info_ptr,
dee91e82
DE
8502 struct die_info *comp_unit_die,
8503 int has_children,
8504 void *data)
ae038cb0 8505{
dee91e82 8506 struct dwarf2_cu *cu = reader->cu;
ae038cb0 8507
95554aad 8508 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 8509
ae038cb0
DJ
8510 /* Check if comp unit has_children.
8511 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 8512 If not, there's no more debug_info for this comp unit. */
d85a05f0 8513 if (has_children)
dee91e82
DE
8514 load_partial_dies (reader, info_ptr, 0);
8515}
98bfdba5 8516
dee91e82
DE
8517/* Load the partial DIEs for a secondary CU into memory.
8518 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8519
dee91e82
DE
8520static void
8521load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8522{
58f0c718 8523 init_cutu_and_read_dies (this_cu, NULL, 1, 1, false,
f4dc4d17 8524 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
8525}
8526
ae038cb0 8527static void
ed2dc618 8528read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 8529 struct dwarf2_section_info *section,
f1902523 8530 struct dwarf2_section_info *abbrev_section,
b76e467d 8531 unsigned int is_dwz)
ae038cb0 8532{
d521ce57 8533 const gdb_byte *info_ptr;
ed2dc618 8534 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 8535
b4f54984 8536 if (dwarf_read_debug)
bf6af496 8537 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
a32a8923
DE
8538 get_section_name (section),
8539 get_section_file_name (section));
bf6af496 8540
36586728 8541 dwarf2_read_section (objfile, section);
ae038cb0 8542
36586728 8543 info_ptr = section->buffer;
6e70227d 8544
36586728 8545 while (info_ptr < section->buffer + section->size)
ae038cb0 8546 {
ae038cb0 8547 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8548
9c541725 8549 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8550
f1902523 8551 comp_unit_head cu_header;
ed2dc618
SM
8552 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8553 abbrev_section, info_ptr,
8554 rcuh_kind::COMPILE);
ae038cb0
DJ
8555
8556 /* Save the compilation unit for later lookup. */
f1902523
JK
8557 if (cu_header.unit_type != DW_UT_type)
8558 {
8559 this_cu = XOBNEW (&objfile->objfile_obstack,
8560 struct dwarf2_per_cu_data);
8561 memset (this_cu, 0, sizeof (*this_cu));
8562 }
8563 else
8564 {
8565 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8566 struct signatured_type);
8567 memset (sig_type, 0, sizeof (*sig_type));
8568 sig_type->signature = cu_header.signature;
8569 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8570 this_cu = &sig_type->per_cu;
8571 }
8572 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8573 this_cu->sect_off = sect_off;
f1902523 8574 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8575 this_cu->is_dwz = is_dwz;
e3b94546 8576 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 8577 this_cu->section = section;
ae038cb0 8578
b76e467d 8579 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8580
8581 info_ptr = info_ptr + this_cu->length;
8582 }
36586728
TT
8583}
8584
8585/* Create a list of all compilation units in OBJFILE.
8586 This is only done for -readnow and building partial symtabs. */
8587
8588static void
ed2dc618 8589create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 8590{
b76e467d 8591 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
ed2dc618 8592 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
b76e467d 8593 &dwarf2_per_objfile->abbrev, 0);
36586728 8594
b76e467d 8595 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 8596 if (dwz != NULL)
ed2dc618 8597 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
b76e467d 8598 1);
c906108c
SS
8599}
8600
5734ee8b 8601/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8602 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8603 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8604 DW_AT_ranges). See the comments of add_partial_subprogram on how
8605 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8606
72bf9492
DJ
8607static void
8608scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8609 CORE_ADDR *highpc, int set_addrmap,
8610 struct dwarf2_cu *cu)
c906108c 8611{
72bf9492 8612 struct partial_die_info *pdi;
c906108c 8613
91c24f0a
DC
8614 /* Now, march along the PDI's, descending into ones which have
8615 interesting children but skipping the children of the other ones,
8616 until we reach the end of the compilation unit. */
c906108c 8617
72bf9492 8618 pdi = first_die;
91c24f0a 8619
72bf9492
DJ
8620 while (pdi != NULL)
8621 {
52356b79 8622 pdi->fixup (cu);
c906108c 8623
f55ee35c 8624 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8625 children, so we need to look at them. Ditto for anonymous
8626 enums. */
933c6fe4 8627
72bf9492 8628 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8629 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8630 || pdi->tag == DW_TAG_imported_unit
8631 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8632 {
72bf9492 8633 switch (pdi->tag)
c906108c
SS
8634 {
8635 case DW_TAG_subprogram:
b1dc1806 8636 case DW_TAG_inlined_subroutine:
cdc07690 8637 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 8638 break;
72929c62 8639 case DW_TAG_constant:
c906108c
SS
8640 case DW_TAG_variable:
8641 case DW_TAG_typedef:
91c24f0a 8642 case DW_TAG_union_type:
72bf9492 8643 if (!pdi->is_declaration)
63d06c5c 8644 {
72bf9492 8645 add_partial_symbol (pdi, cu);
63d06c5c
DC
8646 }
8647 break;
c906108c 8648 case DW_TAG_class_type:
680b30c7 8649 case DW_TAG_interface_type:
c906108c 8650 case DW_TAG_structure_type:
72bf9492 8651 if (!pdi->is_declaration)
c906108c 8652 {
72bf9492 8653 add_partial_symbol (pdi, cu);
c906108c 8654 }
b7fee5a3
KS
8655 if ((cu->language == language_rust
8656 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8657 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8658 set_addrmap, cu);
c906108c 8659 break;
91c24f0a 8660 case DW_TAG_enumeration_type:
72bf9492
DJ
8661 if (!pdi->is_declaration)
8662 add_partial_enumeration (pdi, cu);
c906108c
SS
8663 break;
8664 case DW_TAG_base_type:
a02abb62 8665 case DW_TAG_subrange_type:
c906108c 8666 /* File scope base type definitions are added to the partial
c5aa993b 8667 symbol table. */
72bf9492 8668 add_partial_symbol (pdi, cu);
c906108c 8669 break;
d9fa45fe 8670 case DW_TAG_namespace:
cdc07690 8671 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8672 break;
5d7cb8df 8673 case DW_TAG_module:
cdc07690 8674 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8675 break;
95554aad
TT
8676 case DW_TAG_imported_unit:
8677 {
8678 struct dwarf2_per_cu_data *per_cu;
8679
f4dc4d17
DE
8680 /* For now we don't handle imported units in type units. */
8681 if (cu->per_cu->is_debug_types)
8682 {
8683 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8684 " supported in type units [in module %s]"),
518817b3 8685 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
8686 }
8687
e3b94546
SM
8688 per_cu = dwarf2_find_containing_comp_unit
8689 (pdi->d.sect_off, pdi->is_dwz,
518817b3 8690 cu->per_cu->dwarf2_per_objfile);
95554aad
TT
8691
8692 /* Go read the partial unit, if needed. */
8693 if (per_cu->v.psymtab == NULL)
b93601f3 8694 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 8695
f4dc4d17 8696 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 8697 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
8698 }
8699 break;
74921315
KS
8700 case DW_TAG_imported_declaration:
8701 add_partial_symbol (pdi, cu);
8702 break;
c906108c
SS
8703 default:
8704 break;
8705 }
8706 }
8707
72bf9492
DJ
8708 /* If the die has a sibling, skip to the sibling. */
8709
8710 pdi = pdi->die_sibling;
8711 }
8712}
8713
8714/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8715
72bf9492 8716 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8717 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8718 Enumerators are an exception; they use the scope of their parent
8719 enumeration type, i.e. the name of the enumeration type is not
8720 prepended to the enumerator.
91c24f0a 8721
72bf9492
DJ
8722 There are two complexities. One is DW_AT_specification; in this
8723 case "parent" means the parent of the target of the specification,
8724 instead of the direct parent of the DIE. The other is compilers
8725 which do not emit DW_TAG_namespace; in this case we try to guess
8726 the fully qualified name of structure types from their members'
8727 linkage names. This must be done using the DIE's children rather
8728 than the children of any DW_AT_specification target. We only need
8729 to do this for structures at the top level, i.e. if the target of
8730 any DW_AT_specification (if any; otherwise the DIE itself) does not
8731 have a parent. */
8732
8733/* Compute the scope prefix associated with PDI's parent, in
8734 compilation unit CU. The result will be allocated on CU's
8735 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8736 field. NULL is returned if no prefix is necessary. */
15d034d0 8737static const char *
72bf9492
DJ
8738partial_die_parent_scope (struct partial_die_info *pdi,
8739 struct dwarf2_cu *cu)
8740{
15d034d0 8741 const char *grandparent_scope;
72bf9492 8742 struct partial_die_info *parent, *real_pdi;
91c24f0a 8743
72bf9492
DJ
8744 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8745 then this means the parent of the specification DIE. */
8746
8747 real_pdi = pdi;
72bf9492 8748 while (real_pdi->has_specification)
fb816e8b 8749 {
122cf0f2
AB
8750 auto res = find_partial_die (real_pdi->spec_offset,
8751 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8752 real_pdi = res.pdi;
8753 cu = res.cu;
8754 }
72bf9492
DJ
8755
8756 parent = real_pdi->die_parent;
8757 if (parent == NULL)
8758 return NULL;
8759
8760 if (parent->scope_set)
8761 return parent->scope;
8762
52356b79 8763 parent->fixup (cu);
72bf9492 8764
10b3939b 8765 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8766
acebe513
UW
8767 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8768 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8769 Work around this problem here. */
8770 if (cu->language == language_cplus
6e70227d 8771 && parent->tag == DW_TAG_namespace
acebe513
UW
8772 && strcmp (parent->name, "::") == 0
8773 && grandparent_scope == NULL)
8774 {
8775 parent->scope = NULL;
8776 parent->scope_set = 1;
8777 return NULL;
8778 }
8779
9c6c53f7
SA
8780 if (pdi->tag == DW_TAG_enumerator)
8781 /* Enumerators should not get the name of the enumeration as a prefix. */
8782 parent->scope = grandparent_scope;
8783 else if (parent->tag == DW_TAG_namespace
f55ee35c 8784 || parent->tag == DW_TAG_module
72bf9492
DJ
8785 || parent->tag == DW_TAG_structure_type
8786 || parent->tag == DW_TAG_class_type
680b30c7 8787 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
8788 || parent->tag == DW_TAG_union_type
8789 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
8790 {
8791 if (grandparent_scope == NULL)
8792 parent->scope = parent->name;
8793 else
3e43a32a
MS
8794 parent->scope = typename_concat (&cu->comp_unit_obstack,
8795 grandparent_scope,
f55ee35c 8796 parent->name, 0, cu);
72bf9492 8797 }
72bf9492
DJ
8798 else
8799 {
8800 /* FIXME drow/2004-04-01: What should we be doing with
8801 function-local names? For partial symbols, we should probably be
8802 ignoring them. */
fa9c3fa0
TT
8803 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8804 dwarf_tag_name (parent->tag),
8805 sect_offset_str (pdi->sect_off));
72bf9492 8806 parent->scope = grandparent_scope;
c906108c
SS
8807 }
8808
72bf9492
DJ
8809 parent->scope_set = 1;
8810 return parent->scope;
8811}
8812
8813/* Return the fully scoped name associated with PDI, from compilation unit
8814 CU. The result will be allocated with malloc. */
4568ecf9 8815
72bf9492
DJ
8816static char *
8817partial_die_full_name (struct partial_die_info *pdi,
8818 struct dwarf2_cu *cu)
8819{
15d034d0 8820 const char *parent_scope;
72bf9492 8821
98bfdba5
PA
8822 /* If this is a template instantiation, we can not work out the
8823 template arguments from partial DIEs. So, unfortunately, we have
8824 to go through the full DIEs. At least any work we do building
8825 types here will be reused if full symbols are loaded later. */
8826 if (pdi->has_template_arguments)
8827 {
52356b79 8828 pdi->fixup (cu);
98bfdba5
PA
8829
8830 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8831 {
8832 struct die_info *die;
8833 struct attribute attr;
8834 struct dwarf2_cu *ref_cu = cu;
8835
b64f50a1 8836 /* DW_FORM_ref_addr is using section offset. */
b4069958 8837 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8838 attr.form = DW_FORM_ref_addr;
9c541725 8839 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8840 die = follow_die_ref (NULL, &attr, &ref_cu);
8841
8842 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8843 }
8844 }
8845
72bf9492
DJ
8846 parent_scope = partial_die_parent_scope (pdi, cu);
8847 if (parent_scope == NULL)
8848 return NULL;
8849 else
f55ee35c 8850 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
8851}
8852
8853static void
72bf9492 8854add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8855{
518817b3
SM
8856 struct dwarf2_per_objfile *dwarf2_per_objfile
8857 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 8858 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 8859 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 8860 CORE_ADDR addr = 0;
15d034d0 8861 const char *actual_name = NULL;
e142c38c 8862 CORE_ADDR baseaddr;
15d034d0 8863 char *built_actual_name;
e142c38c
DJ
8864
8865 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 8866
15d034d0
TT
8867 built_actual_name = partial_die_full_name (pdi, cu);
8868 if (built_actual_name != NULL)
8869 actual_name = built_actual_name;
63d06c5c 8870
72bf9492
DJ
8871 if (actual_name == NULL)
8872 actual_name = pdi->name;
8873
c906108c
SS
8874 switch (pdi->tag)
8875 {
b1dc1806 8876 case DW_TAG_inlined_subroutine:
c906108c 8877 case DW_TAG_subprogram:
79748972
TT
8878 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8879 - baseaddr);
2cfa0c8d 8880 if (pdi->is_external || cu->language == language_ada)
c906108c 8881 {
2cfa0c8d
JB
8882 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
8883 of the global scope. But in Ada, we want to be able to access
8884 nested procedures globally. So all Ada subprograms are stored
8885 in the global scope. */
f47fb265 8886 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8887 built_actual_name != NULL,
f47fb265 8888 VAR_DOMAIN, LOC_BLOCK,
79748972 8889 SECT_OFF_TEXT (objfile),
75aedd27 8890 psymbol_placement::GLOBAL,
79748972
TT
8891 addr,
8892 cu->language, objfile);
c906108c
SS
8893 }
8894 else
8895 {
f47fb265 8896 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8897 built_actual_name != NULL,
f47fb265 8898 VAR_DOMAIN, LOC_BLOCK,
79748972 8899 SECT_OFF_TEXT (objfile),
75aedd27 8900 psymbol_placement::STATIC,
1762568f 8901 addr, cu->language, objfile);
c906108c 8902 }
0c1b455e
TT
8903
8904 if (pdi->main_subprogram && actual_name != NULL)
8905 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8906 break;
72929c62 8907 case DW_TAG_constant:
75aedd27
TT
8908 add_psymbol_to_list (actual_name, strlen (actual_name),
8909 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8910 -1, (pdi->is_external
8911 ? psymbol_placement::GLOBAL
8912 : psymbol_placement::STATIC),
8913 0, cu->language, objfile);
72929c62 8914 break;
c906108c 8915 case DW_TAG_variable:
95554aad
TT
8916 if (pdi->d.locdesc)
8917 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8918
95554aad 8919 if (pdi->d.locdesc
caac4577
JG
8920 && addr == 0
8921 && !dwarf2_per_objfile->has_section_at_zero)
8922 {
8923 /* A global or static variable may also have been stripped
8924 out by the linker if unused, in which case its address
8925 will be nullified; do not add such variables into partial
8926 symbol table then. */
8927 }
8928 else if (pdi->is_external)
c906108c
SS
8929 {
8930 /* Global Variable.
8931 Don't enter into the minimal symbol tables as there is
8932 a minimal symbol table entry from the ELF symbols already.
8933 Enter into partial symbol table if it has a location
8934 descriptor or a type.
8935 If the location descriptor is missing, new_symbol will create
8936 a LOC_UNRESOLVED symbol, the address of the variable will then
8937 be determined from the minimal symbol table whenever the variable
8938 is referenced.
8939 The address for the partial symbol table entry is not
8940 used by GDB, but it comes in handy for debugging partial symbol
8941 table building. */
8942
95554aad 8943 if (pdi->d.locdesc || pdi->has_type)
f47fb265 8944 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8945 built_actual_name != NULL,
f47fb265 8946 VAR_DOMAIN, LOC_STATIC,
79748972 8947 SECT_OFF_TEXT (objfile),
75aedd27 8948 psymbol_placement::GLOBAL,
79748972 8949 addr, cu->language, objfile);
c906108c
SS
8950 }
8951 else
8952 {
ff908ebf
AW
8953 int has_loc = pdi->d.locdesc != NULL;
8954
8955 /* Static Variable. Skip symbols whose value we cannot know (those
8956 without location descriptors or constant values). */
8957 if (!has_loc && !pdi->has_const_value)
decbce07 8958 {
15d034d0 8959 xfree (built_actual_name);
decbce07
MS
8960 return;
8961 }
ff908ebf 8962
f47fb265 8963 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8964 built_actual_name != NULL,
f47fb265 8965 VAR_DOMAIN, LOC_STATIC,
79748972 8966 SECT_OFF_TEXT (objfile),
75aedd27 8967 psymbol_placement::STATIC,
79748972 8968 has_loc ? addr : 0,
f47fb265 8969 cu->language, objfile);
c906108c
SS
8970 }
8971 break;
8972 case DW_TAG_typedef:
8973 case DW_TAG_base_type:
a02abb62 8974 case DW_TAG_subrange_type:
38d518c9 8975 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8976 built_actual_name != NULL,
79748972 8977 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 8978 psymbol_placement::STATIC,
1762568f 8979 0, cu->language, objfile);
c906108c 8980 break;
74921315 8981 case DW_TAG_imported_declaration:
72bf9492
DJ
8982 case DW_TAG_namespace:
8983 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8984 built_actual_name != NULL,
79748972 8985 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 8986 psymbol_placement::GLOBAL,
1762568f 8987 0, cu->language, objfile);
72bf9492 8988 break;
530e8392 8989 case DW_TAG_module:
a5fd13a9
BH
8990 /* With Fortran 77 there might be a "BLOCK DATA" module
8991 available without any name. If so, we skip the module as it
8992 doesn't bring any value. */
8993 if (actual_name != nullptr)
8994 add_psymbol_to_list (actual_name, strlen (actual_name),
8995 built_actual_name != NULL,
8996 MODULE_DOMAIN, LOC_TYPEDEF, -1,
8997 psymbol_placement::GLOBAL,
8998 0, cu->language, objfile);
530e8392 8999 break;
c906108c 9000 case DW_TAG_class_type:
680b30c7 9001 case DW_TAG_interface_type:
c906108c
SS
9002 case DW_TAG_structure_type:
9003 case DW_TAG_union_type:
9004 case DW_TAG_enumeration_type:
fa4028e9
JB
9005 /* Skip external references. The DWARF standard says in the section
9006 about "Structure, Union, and Class Type Entries": "An incomplete
9007 structure, union or class type is represented by a structure,
9008 union or class entry that does not have a byte size attribute
9009 and that has a DW_AT_declaration attribute." */
9010 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 9011 {
15d034d0 9012 xfree (built_actual_name);
decbce07
MS
9013 return;
9014 }
fa4028e9 9015
63d06c5c
DC
9016 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
9017 static vs. global. */
38d518c9 9018 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9019 built_actual_name != NULL,
79748972 9020 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
9c37b5ae 9021 cu->language == language_cplus
75aedd27
TT
9022 ? psymbol_placement::GLOBAL
9023 : psymbol_placement::STATIC,
1762568f 9024 0, cu->language, objfile);
c906108c 9025
c906108c
SS
9026 break;
9027 case DW_TAG_enumerator:
38d518c9 9028 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 9029 built_actual_name != NULL,
79748972 9030 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 9031 cu->language == language_cplus
75aedd27
TT
9032 ? psymbol_placement::GLOBAL
9033 : psymbol_placement::STATIC,
1762568f 9034 0, cu->language, objfile);
c906108c
SS
9035 break;
9036 default:
9037 break;
9038 }
5c4e30ca 9039
15d034d0 9040 xfree (built_actual_name);
c906108c
SS
9041}
9042
5c4e30ca
DC
9043/* Read a partial die corresponding to a namespace; also, add a symbol
9044 corresponding to that namespace to the symbol table. NAMESPACE is
9045 the name of the enclosing namespace. */
91c24f0a 9046
72bf9492
DJ
9047static void
9048add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 9049 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9050 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 9051{
72bf9492 9052 /* Add a symbol for the namespace. */
e7c27a73 9053
72bf9492 9054 add_partial_symbol (pdi, cu);
5c4e30ca
DC
9055
9056 /* Now scan partial symbols in that namespace. */
9057
91c24f0a 9058 if (pdi->has_children)
cdc07690 9059 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
9060}
9061
5d7cb8df
JK
9062/* Read a partial die corresponding to a Fortran module. */
9063
9064static void
9065add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 9066 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 9067{
530e8392
KB
9068 /* Add a symbol for the namespace. */
9069
9070 add_partial_symbol (pdi, cu);
9071
f55ee35c 9072 /* Now scan partial symbols in that module. */
5d7cb8df
JK
9073
9074 if (pdi->has_children)
cdc07690 9075 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
9076}
9077
b1dc1806
XR
9078/* Read a partial die corresponding to a subprogram or an inlined
9079 subprogram and create a partial symbol for that subprogram.
9080 When the CU language allows it, this routine also defines a partial
9081 symbol for each nested subprogram that this subprogram contains.
9082 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9083 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 9084
cdc07690
YQ
9085 PDI may also be a lexical block, in which case we simply search
9086 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
9087 Again, this is only performed when the CU language allows this
9088 type of definitions. */
9089
9090static void
9091add_partial_subprogram (struct partial_die_info *pdi,
9092 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9093 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 9094{
b1dc1806 9095 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
9096 {
9097 if (pdi->has_pc_info)
9098 {
9099 if (pdi->lowpc < *lowpc)
9100 *lowpc = pdi->lowpc;
9101 if (pdi->highpc > *highpc)
9102 *highpc = pdi->highpc;
cdc07690 9103 if (set_addrmap)
5734ee8b 9104 {
518817b3 9105 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a
MR
9106 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9107 CORE_ADDR baseaddr;
b926417a
TT
9108 CORE_ADDR this_highpc;
9109 CORE_ADDR this_lowpc;
5734ee8b
DJ
9110
9111 baseaddr = ANOFFSET (objfile->section_offsets,
9112 SECT_OFF_TEXT (objfile));
b926417a
TT
9113 this_lowpc
9114 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9115 pdi->lowpc + baseaddr)
9116 - baseaddr);
9117 this_highpc
9118 = (gdbarch_adjust_dwarf2_addr (gdbarch,
9119 pdi->highpc + baseaddr)
9120 - baseaddr);
d320c2b5 9121 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 9122 this_lowpc, this_highpc - 1,
9291a0cd 9123 cu->per_cu->v.psymtab);
5734ee8b 9124 }
481860b3
GB
9125 }
9126
9127 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9128 {
bc30ff58 9129 if (!pdi->is_declaration)
e8d05480
JB
9130 /* Ignore subprogram DIEs that do not have a name, they are
9131 illegal. Do not emit a complaint at this point, we will
9132 do so when we convert this psymtab into a symtab. */
9133 if (pdi->name)
9134 add_partial_symbol (pdi, cu);
bc30ff58
JB
9135 }
9136 }
6e70227d 9137
bc30ff58
JB
9138 if (! pdi->has_children)
9139 return;
9140
9141 if (cu->language == language_ada)
9142 {
9143 pdi = pdi->die_child;
9144 while (pdi != NULL)
9145 {
52356b79 9146 pdi->fixup (cu);
bc30ff58 9147 if (pdi->tag == DW_TAG_subprogram
b1dc1806 9148 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 9149 || pdi->tag == DW_TAG_lexical_block)
cdc07690 9150 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
9151 pdi = pdi->die_sibling;
9152 }
9153 }
9154}
9155
91c24f0a
DC
9156/* Read a partial die corresponding to an enumeration type. */
9157
72bf9492
DJ
9158static void
9159add_partial_enumeration (struct partial_die_info *enum_pdi,
9160 struct dwarf2_cu *cu)
91c24f0a 9161{
72bf9492 9162 struct partial_die_info *pdi;
91c24f0a
DC
9163
9164 if (enum_pdi->name != NULL)
72bf9492
DJ
9165 add_partial_symbol (enum_pdi, cu);
9166
9167 pdi = enum_pdi->die_child;
9168 while (pdi)
91c24f0a 9169 {
72bf9492 9170 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
b98664d3 9171 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 9172 else
72bf9492
DJ
9173 add_partial_symbol (pdi, cu);
9174 pdi = pdi->die_sibling;
91c24f0a 9175 }
91c24f0a
DC
9176}
9177
6caca83c
CC
9178/* Return the initial uleb128 in the die at INFO_PTR. */
9179
9180static unsigned int
d521ce57 9181peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
9182{
9183 unsigned int bytes_read;
9184
9185 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9186}
9187
685af9cd
TT
9188/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9189 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9190
4bb7a0a7
DJ
9191 Return the corresponding abbrev, or NULL if the number is zero (indicating
9192 an empty DIE). In either case *BYTES_READ will be set to the length of
9193 the initial number. */
9194
9195static struct abbrev_info *
685af9cd
TT
9196peek_die_abbrev (const die_reader_specs &reader,
9197 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 9198{
685af9cd 9199 dwarf2_cu *cu = reader.cu;
518817b3 9200 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
685af9cd
TT
9201 unsigned int abbrev_number
9202 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
9203
9204 if (abbrev_number == 0)
9205 return NULL;
9206
685af9cd 9207 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
9208 if (!abbrev)
9209 {
422b9917 9210 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 9211 " at offset %s [in module %s]"),
422b9917 9212 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 9213 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
9214 }
9215
9216 return abbrev;
9217}
9218
93311388
DE
9219/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9220 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
9221 DIE. Any children of the skipped DIEs will also be skipped. */
9222
d521ce57
TT
9223static const gdb_byte *
9224skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 9225{
4bb7a0a7
DJ
9226 while (1)
9227 {
685af9cd
TT
9228 unsigned int bytes_read;
9229 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9230
4bb7a0a7
DJ
9231 if (abbrev == NULL)
9232 return info_ptr + bytes_read;
9233 else
dee91e82 9234 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
9235 }
9236}
9237
93311388
DE
9238/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9239 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
9240 abbrev corresponding to that skipped uleb128 should be passed in
9241 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9242 children. */
9243
d521ce57
TT
9244static const gdb_byte *
9245skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 9246 struct abbrev_info *abbrev)
4bb7a0a7
DJ
9247{
9248 unsigned int bytes_read;
9249 struct attribute attr;
dee91e82
DE
9250 bfd *abfd = reader->abfd;
9251 struct dwarf2_cu *cu = reader->cu;
d521ce57 9252 const gdb_byte *buffer = reader->buffer;
f664829e 9253 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
9254 unsigned int form, i;
9255
9256 for (i = 0; i < abbrev->num_attrs; i++)
9257 {
9258 /* The only abbrev we care about is DW_AT_sibling. */
9259 if (abbrev->attrs[i].name == DW_AT_sibling)
9260 {
dee91e82 9261 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 9262 if (attr.form == DW_FORM_ref_addr)
b98664d3 9263 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 9264 else
b9502d3f 9265 {
9c541725
PA
9266 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9267 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
9268
9269 if (sibling_ptr < info_ptr)
b98664d3 9270 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
9271 else if (sibling_ptr > reader->buffer_end)
9272 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
9273 else
9274 return sibling_ptr;
9275 }
4bb7a0a7
DJ
9276 }
9277
9278 /* If it isn't DW_AT_sibling, skip this attribute. */
9279 form = abbrev->attrs[i].form;
9280 skip_attribute:
9281 switch (form)
9282 {
4bb7a0a7 9283 case DW_FORM_ref_addr:
ae411497
TT
9284 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9285 and later it is offset sized. */
9286 if (cu->header.version == 2)
9287 info_ptr += cu->header.addr_size;
9288 else
9289 info_ptr += cu->header.offset_size;
9290 break;
36586728
TT
9291 case DW_FORM_GNU_ref_alt:
9292 info_ptr += cu->header.offset_size;
9293 break;
ae411497 9294 case DW_FORM_addr:
4bb7a0a7
DJ
9295 info_ptr += cu->header.addr_size;
9296 break;
9297 case DW_FORM_data1:
9298 case DW_FORM_ref1:
9299 case DW_FORM_flag:
9300 info_ptr += 1;
9301 break;
2dc7f7b3 9302 case DW_FORM_flag_present:
43988095 9303 case DW_FORM_implicit_const:
2dc7f7b3 9304 break;
4bb7a0a7
DJ
9305 case DW_FORM_data2:
9306 case DW_FORM_ref2:
9307 info_ptr += 2;
9308 break;
9309 case DW_FORM_data4:
9310 case DW_FORM_ref4:
9311 info_ptr += 4;
9312 break;
9313 case DW_FORM_data8:
9314 case DW_FORM_ref8:
55f1336d 9315 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
9316 info_ptr += 8;
9317 break;
0224619f
JK
9318 case DW_FORM_data16:
9319 info_ptr += 16;
9320 break;
4bb7a0a7 9321 case DW_FORM_string:
9b1c24c8 9322 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
9323 info_ptr += bytes_read;
9324 break;
2dc7f7b3 9325 case DW_FORM_sec_offset:
4bb7a0a7 9326 case DW_FORM_strp:
36586728 9327 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
9328 info_ptr += cu->header.offset_size;
9329 break;
2dc7f7b3 9330 case DW_FORM_exprloc:
4bb7a0a7
DJ
9331 case DW_FORM_block:
9332 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9333 info_ptr += bytes_read;
9334 break;
9335 case DW_FORM_block1:
9336 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9337 break;
9338 case DW_FORM_block2:
9339 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9340 break;
9341 case DW_FORM_block4:
9342 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9343 break;
336d760d 9344 case DW_FORM_addrx:
cf532bd1 9345 case DW_FORM_strx:
4bb7a0a7
DJ
9346 case DW_FORM_sdata:
9347 case DW_FORM_udata:
9348 case DW_FORM_ref_udata:
3019eac3
DE
9349 case DW_FORM_GNU_addr_index:
9350 case DW_FORM_GNU_str_index:
d521ce57 9351 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
9352 break;
9353 case DW_FORM_indirect:
9354 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9355 info_ptr += bytes_read;
9356 /* We need to continue parsing from here, so just go back to
9357 the top. */
9358 goto skip_attribute;
9359
9360 default:
3e43a32a
MS
9361 error (_("Dwarf Error: Cannot handle %s "
9362 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
9363 dwarf_form_name (form),
9364 bfd_get_filename (abfd));
9365 }
9366 }
9367
9368 if (abbrev->has_children)
dee91e82 9369 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
9370 else
9371 return info_ptr;
9372}
9373
93311388 9374/* Locate ORIG_PDI's sibling.
dee91e82 9375 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 9376
d521ce57 9377static const gdb_byte *
dee91e82
DE
9378locate_pdi_sibling (const struct die_reader_specs *reader,
9379 struct partial_die_info *orig_pdi,
d521ce57 9380 const gdb_byte *info_ptr)
91c24f0a
DC
9381{
9382 /* Do we know the sibling already? */
72bf9492 9383
91c24f0a
DC
9384 if (orig_pdi->sibling)
9385 return orig_pdi->sibling;
9386
9387 /* Are there any children to deal with? */
9388
9389 if (!orig_pdi->has_children)
9390 return info_ptr;
9391
4bb7a0a7 9392 /* Skip the children the long way. */
91c24f0a 9393
dee91e82 9394 return skip_children (reader, info_ptr);
91c24f0a
DC
9395}
9396
257e7a09 9397/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 9398 not NULL. */
c906108c
SS
9399
9400static void
257e7a09
YQ
9401dwarf2_read_symtab (struct partial_symtab *self,
9402 struct objfile *objfile)
c906108c 9403{
ed2dc618
SM
9404 struct dwarf2_per_objfile *dwarf2_per_objfile
9405 = get_dwarf2_per_objfile (objfile);
9406
257e7a09 9407 if (self->readin)
c906108c 9408 {
442e4d9c 9409 warning (_("bug: psymtab for %s is already read in."),
257e7a09 9410 self->filename);
442e4d9c
YQ
9411 }
9412 else
9413 {
9414 if (info_verbose)
c906108c 9415 {
442e4d9c 9416 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 9417 self->filename);
442e4d9c 9418 gdb_flush (gdb_stdout);
c906108c 9419 }
c906108c 9420
442e4d9c
YQ
9421 /* If this psymtab is constructed from a debug-only objfile, the
9422 has_section_at_zero flag will not necessarily be correct. We
9423 can get the correct value for this flag by looking at the data
9424 associated with the (presumably stripped) associated objfile. */
9425 if (objfile->separate_debug_objfile_backlink)
9426 {
9427 struct dwarf2_per_objfile *dpo_backlink
ed2dc618 9428 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9a619af0 9429
442e4d9c
YQ
9430 dwarf2_per_objfile->has_section_at_zero
9431 = dpo_backlink->has_section_at_zero;
9432 }
b2ab525c 9433
442e4d9c 9434 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 9435
257e7a09 9436 psymtab_to_symtab_1 (self);
c906108c 9437
442e4d9c
YQ
9438 /* Finish up the debug error message. */
9439 if (info_verbose)
9440 printf_filtered (_("done.\n"));
c906108c 9441 }
95554aad 9442
ed2dc618 9443 process_cu_includes (dwarf2_per_objfile);
c906108c 9444}
9cdd5dbd
DE
9445\f
9446/* Reading in full CUs. */
c906108c 9447
10b3939b
DJ
9448/* Add PER_CU to the queue. */
9449
9450static void
95554aad
TT
9451queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9452 enum language pretend_language)
10b3939b
DJ
9453{
9454 struct dwarf2_queue_item *item;
9455
9456 per_cu->queued = 1;
8d749320 9457 item = XNEW (struct dwarf2_queue_item);
10b3939b 9458 item->per_cu = per_cu;
95554aad 9459 item->pretend_language = pretend_language;
10b3939b
DJ
9460 item->next = NULL;
9461
9462 if (dwarf2_queue == NULL)
9463 dwarf2_queue = item;
9464 else
9465 dwarf2_queue_tail->next = item;
9466
9467 dwarf2_queue_tail = item;
9468}
9469
89e63ee4
DE
9470/* If PER_CU is not yet queued, add it to the queue.
9471 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9472 dependency.
0907af0c 9473 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
9474 meaning either PER_CU is already queued or it is already loaded.
9475
9476 N.B. There is an invariant here that if a CU is queued then it is loaded.
9477 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
9478
9479static int
89e63ee4 9480maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
9481 struct dwarf2_per_cu_data *per_cu,
9482 enum language pretend_language)
9483{
9484 /* We may arrive here during partial symbol reading, if we need full
9485 DIEs to process an unusual case (e.g. template arguments). Do
9486 not queue PER_CU, just tell our caller to load its DIEs. */
ed2dc618 9487 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
0907af0c
DE
9488 {
9489 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9490 return 1;
9491 return 0;
9492 }
9493
9494 /* Mark the dependence relation so that we don't flush PER_CU
9495 too early. */
89e63ee4
DE
9496 if (dependent_cu != NULL)
9497 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
9498
9499 /* If it's already on the queue, we have nothing to do. */
9500 if (per_cu->queued)
9501 return 0;
9502
9503 /* If the compilation unit is already loaded, just mark it as
9504 used. */
9505 if (per_cu->cu != NULL)
9506 {
9507 per_cu->cu->last_used = 0;
9508 return 0;
9509 }
9510
9511 /* Add it to the queue. */
9512 queue_comp_unit (per_cu, pretend_language);
9513
9514 return 1;
9515}
9516
10b3939b
DJ
9517/* Process the queue. */
9518
9519static void
ed2dc618 9520process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b
DJ
9521{
9522 struct dwarf2_queue_item *item, *next_item;
9523
b4f54984 9524 if (dwarf_read_debug)
45cfd468
DE
9525 {
9526 fprintf_unfiltered (gdb_stdlog,
9527 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 9528 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
9529 }
9530
03dd20cc
DJ
9531 /* The queue starts out with one item, but following a DIE reference
9532 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
9533 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9534 {
cc12ce38
DE
9535 if ((dwarf2_per_objfile->using_index
9536 ? !item->per_cu->v.quick->compunit_symtab
9537 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9538 /* Skip dummy CUs. */
9539 && item->per_cu->cu != NULL)
f4dc4d17
DE
9540 {
9541 struct dwarf2_per_cu_data *per_cu = item->per_cu;
73be47f5 9542 unsigned int debug_print_threshold;
247f5c4f 9543 char buf[100];
f4dc4d17 9544
247f5c4f 9545 if (per_cu->is_debug_types)
f4dc4d17 9546 {
247f5c4f
DE
9547 struct signatured_type *sig_type =
9548 (struct signatured_type *) per_cu;
9549
9d8780f0 9550 sprintf (buf, "TU %s at offset %s",
73be47f5 9551 hex_string (sig_type->signature),
9d8780f0 9552 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9553 /* There can be 100s of TUs.
9554 Only print them in verbose mode. */
9555 debug_print_threshold = 2;
f4dc4d17 9556 }
247f5c4f 9557 else
73be47f5 9558 {
9d8780f0
SM
9559 sprintf (buf, "CU at offset %s",
9560 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9561 debug_print_threshold = 1;
9562 }
247f5c4f 9563
b4f54984 9564 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9565 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
9566
9567 if (per_cu->is_debug_types)
9568 process_full_type_unit (per_cu, item->pretend_language);
9569 else
9570 process_full_comp_unit (per_cu, item->pretend_language);
9571
b4f54984 9572 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9573 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 9574 }
10b3939b
DJ
9575
9576 item->per_cu->queued = 0;
9577 next_item = item->next;
9578 xfree (item);
9579 }
9580
9581 dwarf2_queue_tail = NULL;
45cfd468 9582
b4f54984 9583 if (dwarf_read_debug)
45cfd468
DE
9584 {
9585 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 9586 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 9587 }
10b3939b
DJ
9588}
9589
10b3939b
DJ
9590/* Read in full symbols for PST, and anything it depends on. */
9591
c906108c 9592static void
fba45db2 9593psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 9594{
10b3939b 9595 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
9596 int i;
9597
95554aad
TT
9598 if (pst->readin)
9599 return;
9600
aaa75496 9601 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
9602 if (!pst->dependencies[i]->readin
9603 && pst->dependencies[i]->user == NULL)
aaa75496
JB
9604 {
9605 /* Inform about additional files that need to be read in. */
9606 if (info_verbose)
9607 {
a3f17187 9608 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
9609 fputs_filtered (" ", gdb_stdout);
9610 wrap_here ("");
9611 fputs_filtered ("and ", gdb_stdout);
9612 wrap_here ("");
9613 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 9614 wrap_here (""); /* Flush output. */
aaa75496
JB
9615 gdb_flush (gdb_stdout);
9616 }
9617 psymtab_to_symtab_1 (pst->dependencies[i]);
9618 }
9619
9a3c8263 9620 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10b3939b
DJ
9621
9622 if (per_cu == NULL)
aaa75496
JB
9623 {
9624 /* It's an include file, no symbols to read for it.
9625 Everything is in the parent symtab. */
9626 pst->readin = 1;
9627 return;
9628 }
c906108c 9629
58f0c718 9630 dw2_do_instantiate_symtab (per_cu, false);
10b3939b
DJ
9631}
9632
dee91e82
DE
9633/* Trivial hash function for die_info: the hash value of a DIE
9634 is its offset in .debug_info for this objfile. */
10b3939b 9635
dee91e82
DE
9636static hashval_t
9637die_hash (const void *item)
10b3939b 9638{
9a3c8263 9639 const struct die_info *die = (const struct die_info *) item;
6502dd73 9640
9c541725 9641 return to_underlying (die->sect_off);
dee91e82 9642}
63d06c5c 9643
dee91e82
DE
9644/* Trivial comparison function for die_info structures: two DIEs
9645 are equal if they have the same offset. */
98bfdba5 9646
dee91e82
DE
9647static int
9648die_eq (const void *item_lhs, const void *item_rhs)
9649{
9a3c8263
SM
9650 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9651 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9652
9c541725 9653 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9654}
c906108c 9655
dee91e82
DE
9656/* die_reader_func for load_full_comp_unit.
9657 This is identical to read_signatured_type_reader,
9658 but is kept separate for now. */
c906108c 9659
dee91e82
DE
9660static void
9661load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 9662 const gdb_byte *info_ptr,
dee91e82
DE
9663 struct die_info *comp_unit_die,
9664 int has_children,
9665 void *data)
9666{
9667 struct dwarf2_cu *cu = reader->cu;
9a3c8263 9668 enum language *language_ptr = (enum language *) data;
6caca83c 9669
dee91e82
DE
9670 gdb_assert (cu->die_hash == NULL);
9671 cu->die_hash =
9672 htab_create_alloc_ex (cu->header.length / 12,
9673 die_hash,
9674 die_eq,
9675 NULL,
9676 &cu->comp_unit_obstack,
9677 hashtab_obstack_allocate,
9678 dummy_obstack_deallocate);
e142c38c 9679
dee91e82
DE
9680 if (has_children)
9681 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
9682 &info_ptr, comp_unit_die);
9683 cu->dies = comp_unit_die;
9684 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9685
9686 /* We try not to read any attributes in this function, because not
9cdd5dbd 9687 all CUs needed for references have been loaded yet, and symbol
10b3939b 9688 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9689 or we won't be able to build types correctly.
9690 Similarly, if we do not read the producer, we can not apply
9691 producer-specific interpretation. */
95554aad 9692 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 9693}
10b3939b 9694
dee91e82 9695/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 9696
dee91e82 9697static void
95554aad 9698load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
58f0c718 9699 bool skip_partial,
95554aad 9700 enum language pretend_language)
dee91e82 9701{
3019eac3 9702 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 9703
58f0c718 9704 init_cutu_and_read_dies (this_cu, NULL, 1, 1, skip_partial,
f4dc4d17 9705 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
9706}
9707
3da10d80
KS
9708/* Add a DIE to the delayed physname list. */
9709
9710static void
9711add_to_method_list (struct type *type, int fnfield_index, int index,
9712 const char *name, struct die_info *die,
9713 struct dwarf2_cu *cu)
9714{
9715 struct delayed_method_info mi;
9716 mi.type = type;
9717 mi.fnfield_index = fnfield_index;
9718 mi.index = index;
9719 mi.name = name;
9720 mi.die = die;
c89b44cd 9721 cu->method_list.push_back (mi);
3da10d80
KS
9722}
9723
3693fdb3
PA
9724/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9725 "const" / "volatile". If so, decrements LEN by the length of the
9726 modifier and return true. Otherwise return false. */
9727
9728template<size_t N>
9729static bool
9730check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9731{
9732 size_t mod_len = sizeof (mod) - 1;
9733 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9734 {
9735 len -= mod_len;
9736 return true;
9737 }
9738 return false;
9739}
9740
3da10d80
KS
9741/* Compute the physnames of any methods on the CU's method list.
9742
9743 The computation of method physnames is delayed in order to avoid the
9744 (bad) condition that one of the method's formal parameters is of an as yet
9745 incomplete type. */
9746
9747static void
9748compute_delayed_physnames (struct dwarf2_cu *cu)
9749{
3693fdb3 9750 /* Only C++ delays computing physnames. */
c89b44cd 9751 if (cu->method_list.empty ())
3693fdb3
PA
9752 return;
9753 gdb_assert (cu->language == language_cplus);
9754
52941706 9755 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9756 {
1d06ead6 9757 const char *physname;
3da10d80 9758 struct fn_fieldlist *fn_flp
c89b44cd
TT
9759 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9760 physname = dwarf2_physname (mi.name, mi.die, cu);
9761 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9762 = physname ? physname : "";
3693fdb3
PA
9763
9764 /* Since there's no tag to indicate whether a method is a
9765 const/volatile overload, extract that information out of the
9766 demangled name. */
9767 if (physname != NULL)
9768 {
9769 size_t len = strlen (physname);
9770
9771 while (1)
9772 {
9773 if (physname[len] == ')') /* shortcut */
9774 break;
9775 else if (check_modifier (physname, len, " const"))
c89b44cd 9776 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9777 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9778 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9779 else
9780 break;
9781 }
9782 }
3da10d80 9783 }
c89b44cd
TT
9784
9785 /* The list is no longer needed. */
9786 cu->method_list.clear ();
3da10d80
KS
9787}
9788
a766d390
DE
9789/* Go objects should be embedded in a DW_TAG_module DIE,
9790 and it's not clear if/how imported objects will appear.
9791 To keep Go support simple until that's worked out,
9792 go back through what we've read and create something usable.
9793 We could do this while processing each DIE, and feels kinda cleaner,
9794 but that way is more invasive.
9795 This is to, for example, allow the user to type "p var" or "b main"
9796 without having to specify the package name, and allow lookups
9797 of module.object to work in contexts that use the expression
9798 parser. */
9799
9800static void
9801fixup_go_packaging (struct dwarf2_cu *cu)
9802{
9803 char *package_name = NULL;
9804 struct pending *list;
9805 int i;
9806
c24bdb02 9807 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9808 list != NULL;
9809 list = list->next)
a766d390
DE
9810 {
9811 for (i = 0; i < list->nsyms; ++i)
9812 {
9813 struct symbol *sym = list->symbol[i];
9814
9815 if (SYMBOL_LANGUAGE (sym) == language_go
9816 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9817 {
9818 char *this_package_name = go_symbol_package_name (sym);
9819
9820 if (this_package_name == NULL)
9821 continue;
9822 if (package_name == NULL)
9823 package_name = this_package_name;
9824 else
9825 {
518817b3
SM
9826 struct objfile *objfile
9827 = cu->per_cu->dwarf2_per_objfile->objfile;
a766d390 9828 if (strcmp (package_name, this_package_name) != 0)
b98664d3 9829 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9830 (symbol_symtab (sym) != NULL
9831 ? symtab_to_filename_for_display
9832 (symbol_symtab (sym))
e3b94546 9833 : objfile_name (objfile)),
a766d390
DE
9834 this_package_name, package_name);
9835 xfree (this_package_name);
9836 }
9837 }
9838 }
9839 }
9840
9841 if (package_name != NULL)
9842 {
518817b3 9843 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
34a68019 9844 const char *saved_package_name
021887d8 9845 = obstack_strdup (&objfile->per_bfd->storage_obstack, package_name);
19f392bc
UW
9846 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9847 saved_package_name);
a766d390
DE
9848 struct symbol *sym;
9849
e623cf5d 9850 sym = allocate_symbol (objfile);
f85f34ed 9851 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
9852 SYMBOL_SET_NAMES (sym, saved_package_name,
9853 strlen (saved_package_name), 0, objfile);
a766d390
DE
9854 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9855 e.g., "main" finds the "main" module and not C's main(). */
9856 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9857 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9858 SYMBOL_TYPE (sym) = type;
9859
c24bdb02 9860 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9861
9862 xfree (package_name);
9863 }
9864}
9865
c9317f21
TT
9866/* Allocate a fully-qualified name consisting of the two parts on the
9867 obstack. */
9868
9869static const char *
9870rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9871{
9872 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9873}
9874
9875/* A helper that allocates a struct discriminant_info to attach to a
9876 union type. */
9877
9878static struct discriminant_info *
9879alloc_discriminant_info (struct type *type, int discriminant_index,
9880 int default_index)
9881{
9882 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
c7b15a66
TT
9883 gdb_assert (discriminant_index == -1
9884 || (discriminant_index >= 0
9885 && discriminant_index < TYPE_NFIELDS (type)));
c9317f21 9886 gdb_assert (default_index == -1
c7b15a66 9887 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
c9317f21
TT
9888
9889 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9890
9891 struct discriminant_info *disc
9892 = ((struct discriminant_info *)
9893 TYPE_ZALLOC (type,
9894 offsetof (struct discriminant_info, discriminants)
9895 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9896 disc->default_index = default_index;
9897 disc->discriminant_index = discriminant_index;
9898
9899 struct dynamic_prop prop;
9900 prop.kind = PROP_UNDEFINED;
9901 prop.data.baton = disc;
9902
9903 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9904
9905 return disc;
9906}
9907
9908/* Some versions of rustc emitted enums in an unusual way.
9909
9910 Ordinary enums were emitted as unions. The first element of each
9911 structure in the union was named "RUST$ENUM$DISR". This element
9912 held the discriminant.
9913
9914 These versions of Rust also implemented the "non-zero"
9915 optimization. When the enum had two values, and one is empty and
9916 the other holds a pointer that cannot be zero, the pointer is used
9917 as the discriminant, with a zero value meaning the empty variant.
9918 Here, the union's first member is of the form
9919 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9920 where the fieldnos are the indices of the fields that should be
9921 traversed in order to find the field (which may be several fields deep)
9922 and the variantname is the name of the variant of the case when the
9923 field is zero.
9924
9925 This function recognizes whether TYPE is of one of these forms,
9926 and, if so, smashes it to be a variant type. */
9927
9928static void
9929quirk_rust_enum (struct type *type, struct objfile *objfile)
9930{
9931 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9932
9933 /* We don't need to deal with empty enums. */
9934 if (TYPE_NFIELDS (type) == 0)
9935 return;
9936
9937#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9938 if (TYPE_NFIELDS (type) == 1
9939 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9940 {
9941 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9942
9943 /* Decode the field name to find the offset of the
9944 discriminant. */
9945 ULONGEST bit_offset = 0;
9946 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9947 while (name[0] >= '0' && name[0] <= '9')
9948 {
9949 char *tail;
9950 unsigned long index = strtoul (name, &tail, 10);
9951 name = tail;
9952 if (*name != '$'
9953 || index >= TYPE_NFIELDS (field_type)
9954 || (TYPE_FIELD_LOC_KIND (field_type, index)
9955 != FIELD_LOC_KIND_BITPOS))
9956 {
b98664d3 9957 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9958 "[in module %s]"),
9959 TYPE_FIELD_NAME (type, 0),
9960 objfile_name (objfile));
9961 return;
9962 }
9963 ++name;
9964
9965 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9966 field_type = TYPE_FIELD_TYPE (field_type, index);
9967 }
9968
9969 /* Make a union to hold the variants. */
9970 struct type *union_type = alloc_type (objfile);
9971 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9972 TYPE_NFIELDS (union_type) = 3;
9973 TYPE_FIELDS (union_type)
9974 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9975 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9976 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9977
9978 /* Put the discriminant must at index 0. */
9979 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9980 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9981 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9982 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9983
9984 /* The order of fields doesn't really matter, so put the real
9985 field at index 1 and the data-less field at index 2. */
9986 struct discriminant_info *disc
9987 = alloc_discriminant_info (union_type, 0, 1);
9988 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9989 TYPE_FIELD_NAME (union_type, 1)
9990 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9991 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9992 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9993 TYPE_FIELD_NAME (union_type, 1));
9994
9995 const char *dataless_name
9996 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9997 name);
9998 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9999 dataless_name);
10000 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
10001 /* NAME points into the original discriminant name, which
10002 already has the correct lifetime. */
10003 TYPE_FIELD_NAME (union_type, 2) = name;
10004 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
10005 disc->discriminants[2] = 0;
10006
10007 /* Smash this type to be a structure type. We have to do this
10008 because the type has already been recorded. */
10009 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10010 TYPE_NFIELDS (type) = 1;
10011 TYPE_FIELDS (type)
10012 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
10013
10014 /* Install the variant part. */
10015 TYPE_FIELD_TYPE (type, 0) = union_type;
10016 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10017 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10018 }
10019 else if (TYPE_NFIELDS (type) == 1)
10020 {
10021 /* We assume that a union with a single field is a univariant
10022 enum. */
10023 /* Smash this type to be a structure type. We have to do this
10024 because the type has already been recorded. */
10025 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10026
10027 /* Make a union to hold the variants. */
10028 struct type *union_type = alloc_type (objfile);
10029 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10030 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
10031 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10032 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10033 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
10034
10035 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
10036 const char *variant_name
10037 = rust_last_path_segment (TYPE_NAME (field_type));
10038 TYPE_FIELD_NAME (union_type, 0) = variant_name;
10039 TYPE_NAME (field_type)
10040 = rust_fully_qualify (&objfile->objfile_obstack,
c7b15a66 10041 TYPE_NAME (type), variant_name);
c9317f21
TT
10042
10043 /* Install the union in the outer struct type. */
10044 TYPE_NFIELDS (type) = 1;
10045 TYPE_FIELDS (type)
10046 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
10047 TYPE_FIELD_TYPE (type, 0) = union_type;
10048 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10049 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10050
10051 alloc_discriminant_info (union_type, -1, 0);
10052 }
10053 else
10054 {
10055 struct type *disr_type = nullptr;
10056 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10057 {
10058 disr_type = TYPE_FIELD_TYPE (type, i);
10059
a037790e
TT
10060 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
10061 {
10062 /* All fields of a true enum will be structs. */
10063 return;
10064 }
10065 else if (TYPE_NFIELDS (disr_type) == 0)
c9317f21
TT
10066 {
10067 /* Could be data-less variant, so keep going. */
a037790e 10068 disr_type = nullptr;
c9317f21
TT
10069 }
10070 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10071 "RUST$ENUM$DISR") != 0)
10072 {
10073 /* Not a Rust enum. */
10074 return;
10075 }
10076 else
10077 {
10078 /* Found one. */
10079 break;
10080 }
10081 }
10082
10083 /* If we got here without a discriminant, then it's probably
10084 just a union. */
10085 if (disr_type == nullptr)
10086 return;
10087
10088 /* Smash this type to be a structure type. We have to do this
10089 because the type has already been recorded. */
10090 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10091
10092 /* Make a union to hold the variants. */
10093 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10094 struct type *union_type = alloc_type (objfile);
10095 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10096 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10097 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10098 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10099 TYPE_FIELDS (union_type)
10100 = (struct field *) TYPE_ZALLOC (union_type,
10101 (TYPE_NFIELDS (union_type)
10102 * sizeof (struct field)));
10103
10104 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10105 TYPE_NFIELDS (type) * sizeof (struct field));
10106
10107 /* Install the discriminant at index 0 in the union. */
10108 TYPE_FIELD (union_type, 0) = *disr_field;
10109 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10110 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10111
10112 /* Install the union in the outer struct type. */
10113 TYPE_FIELD_TYPE (type, 0) = union_type;
10114 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10115 TYPE_NFIELDS (type) = 1;
10116
10117 /* Set the size and offset of the union type. */
10118 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10119
10120 /* We need a way to find the correct discriminant given a
10121 variant name. For convenience we build a map here. */
10122 struct type *enum_type = FIELD_TYPE (*disr_field);
10123 std::unordered_map<std::string, ULONGEST> discriminant_map;
10124 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10125 {
10126 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10127 {
10128 const char *name
10129 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10130 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10131 }
10132 }
10133
10134 int n_fields = TYPE_NFIELDS (union_type);
10135 struct discriminant_info *disc
10136 = alloc_discriminant_info (union_type, 0, -1);
10137 /* Skip the discriminant here. */
10138 for (int i = 1; i < n_fields; ++i)
10139 {
10140 /* Find the final word in the name of this variant's type.
10141 That name can be used to look up the correct
10142 discriminant. */
10143 const char *variant_name
10144 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10145 i)));
10146
10147 auto iter = discriminant_map.find (variant_name);
10148 if (iter != discriminant_map.end ())
10149 disc->discriminants[i] = iter->second;
10150
bedda9ac 10151 /* Remove the discriminant field, if it exists. */
c9317f21 10152 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
bedda9ac
TT
10153 if (TYPE_NFIELDS (sub_type) > 0)
10154 {
10155 --TYPE_NFIELDS (sub_type);
10156 ++TYPE_FIELDS (sub_type);
10157 }
c9317f21
TT
10158 TYPE_FIELD_NAME (union_type, i) = variant_name;
10159 TYPE_NAME (sub_type)
10160 = rust_fully_qualify (&objfile->objfile_obstack,
10161 TYPE_NAME (type), variant_name);
10162 }
10163 }
10164}
10165
10166/* Rewrite some Rust unions to be structures with variants parts. */
10167
10168static void
10169rust_union_quirks (struct dwarf2_cu *cu)
10170{
10171 gdb_assert (cu->language == language_rust);
52941706
SM
10172 for (type *type_ : cu->rust_unions)
10173 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
2d79090e
TT
10174 /* We don't need this any more. */
10175 cu->rust_unions.clear ();
c9317f21
TT
10176}
10177
95554aad
TT
10178/* Return the symtab for PER_CU. This works properly regardless of
10179 whether we're using the index or psymtabs. */
10180
43f3e411
DE
10181static struct compunit_symtab *
10182get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad 10183{
ed2dc618 10184 return (per_cu->dwarf2_per_objfile->using_index
43f3e411
DE
10185 ? per_cu->v.quick->compunit_symtab
10186 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
10187}
10188
10189/* A helper function for computing the list of all symbol tables
10190 included by PER_CU. */
10191
10192static void
4c39bc03 10193recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 10194 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 10195 struct dwarf2_per_cu_data *per_cu,
43f3e411 10196 struct compunit_symtab *immediate_parent)
95554aad
TT
10197{
10198 void **slot;
10199 int ix;
43f3e411 10200 struct compunit_symtab *cust;
95554aad
TT
10201 struct dwarf2_per_cu_data *iter;
10202
10203 slot = htab_find_slot (all_children, per_cu, INSERT);
10204 if (*slot != NULL)
10205 {
10206 /* This inclusion and its children have been processed. */
10207 return;
10208 }
10209
10210 *slot = per_cu;
10211 /* Only add a CU if it has a symbol table. */
43f3e411
DE
10212 cust = get_compunit_symtab (per_cu);
10213 if (cust != NULL)
ec94af83
DE
10214 {
10215 /* If this is a type unit only add its symbol table if we haven't
10216 seen it yet (type unit per_cu's can share symtabs). */
10217 if (per_cu->is_debug_types)
10218 {
43f3e411 10219 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
10220 if (*slot == NULL)
10221 {
43f3e411 10222 *slot = cust;
4c39bc03 10223 result->push_back (cust);
43f3e411
DE
10224 if (cust->user == NULL)
10225 cust->user = immediate_parent;
ec94af83
DE
10226 }
10227 }
10228 else
f9125b6c 10229 {
4c39bc03 10230 result->push_back (cust);
43f3e411
DE
10231 if (cust->user == NULL)
10232 cust->user = immediate_parent;
f9125b6c 10233 }
ec94af83 10234 }
95554aad
TT
10235
10236 for (ix = 0;
796a7ff8 10237 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad 10238 ++ix)
ec94af83
DE
10239 {
10240 recursively_compute_inclusions (result, all_children,
43f3e411 10241 all_type_symtabs, iter, cust);
ec94af83 10242 }
95554aad
TT
10243}
10244
43f3e411 10245/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
10246 PER_CU. */
10247
10248static void
43f3e411 10249compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 10250{
f4dc4d17
DE
10251 gdb_assert (! per_cu->is_debug_types);
10252
796a7ff8 10253 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
10254 {
10255 int ix, len;
ec94af83 10256 struct dwarf2_per_cu_data *per_cu_iter;
4c39bc03 10257 std::vector<compunit_symtab *> result_symtabs;
ec94af83 10258 htab_t all_children, all_type_symtabs;
43f3e411 10259 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
10260
10261 /* If we don't have a symtab, we can just skip this case. */
43f3e411 10262 if (cust == NULL)
95554aad
TT
10263 return;
10264
10265 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10266 NULL, xcalloc, xfree);
ec94af83
DE
10267 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10268 NULL, xcalloc, xfree);
95554aad
TT
10269
10270 for (ix = 0;
796a7ff8 10271 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
ec94af83 10272 ix, per_cu_iter);
95554aad 10273 ++ix)
ec94af83
DE
10274 {
10275 recursively_compute_inclusions (&result_symtabs, all_children,
f9125b6c 10276 all_type_symtabs, per_cu_iter,
43f3e411 10277 cust);
ec94af83 10278 }
95554aad 10279
ec94af83 10280 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 10281 len = result_symtabs.size ();
43f3e411 10282 cust->includes
ed2dc618 10283 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 10284 struct compunit_symtab *, len + 1);
4c39bc03
TT
10285 memcpy (cust->includes, result_symtabs.data (),
10286 len * sizeof (compunit_symtab *));
43f3e411 10287 cust->includes[len] = NULL;
95554aad 10288
95554aad 10289 htab_delete (all_children);
ec94af83 10290 htab_delete (all_type_symtabs);
95554aad
TT
10291 }
10292}
10293
10294/* Compute the 'includes' field for the symtabs of all the CUs we just
10295 read. */
10296
10297static void
ed2dc618 10298process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 10299{
71b73764 10300 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
f4dc4d17
DE
10301 {
10302 if (! iter->is_debug_types)
43f3e411 10303 compute_compunit_symtab_includes (iter);
f4dc4d17 10304 }
95554aad 10305
c5d0225d 10306 dwarf2_per_objfile->just_read_cus.clear ();
95554aad
TT
10307}
10308
9cdd5dbd 10309/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
10310 already been loaded into memory. */
10311
10312static void
95554aad
TT
10313process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10314 enum language pretend_language)
10b3939b 10315{
10b3939b 10316 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10317 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10318 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 10319 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 10320 CORE_ADDR lowpc, highpc;
43f3e411 10321 struct compunit_symtab *cust;
10b3939b 10322 CORE_ADDR baseaddr;
4359dff1 10323 struct block *static_block;
3e29f34a 10324 CORE_ADDR addr;
10b3939b
DJ
10325
10326 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10327
c89b44cd
TT
10328 /* Clear the list here in case something was left over. */
10329 cu->method_list.clear ();
10b3939b 10330
95554aad
TT
10331 cu->language = pretend_language;
10332 cu->language_defn = language_def (cu->language);
10333
c906108c 10334 /* Do line number decoding in read_file_scope () */
10b3939b 10335 process_die (cu->dies, cu);
c906108c 10336
a766d390
DE
10337 /* For now fudge the Go package. */
10338 if (cu->language == language_go)
10339 fixup_go_packaging (cu);
10340
3da10d80
KS
10341 /* Now that we have processed all the DIEs in the CU, all the types
10342 should be complete, and it should now be safe to compute all of the
10343 physnames. */
10344 compute_delayed_physnames (cu);
3da10d80 10345
c9317f21
TT
10346 if (cu->language == language_rust)
10347 rust_union_quirks (cu);
10348
fae299cd
DC
10349 /* Some compilers don't define a DW_AT_high_pc attribute for the
10350 compilation unit. If the DW_AT_high_pc is missing, synthesize
10351 it, by scanning the DIE's below the compilation unit. */
10b3939b 10352 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 10353
3e29f34a 10354 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 10355 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
10356
10357 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10358 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10359 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10360 addrmap to help ensure it has an accurate map of pc values belonging to
10361 this comp unit. */
10362 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10363
c24bdb02 10364 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
10365 SECT_OFF_TEXT (objfile),
10366 0);
c906108c 10367
43f3e411 10368 if (cust != NULL)
c906108c 10369 {
df15bd07 10370 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 10371
8be455d7
JK
10372 /* Set symtab language to language from DW_AT_language. If the
10373 compilation is from a C file generated by language preprocessors, do
10374 not set the language if it was already deduced by start_subfile. */
43f3e411 10375 if (!(cu->language == language_c
40e3ad0e 10376 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 10377 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
10378
10379 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10380 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
10381 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10382 there were bugs in prologue debug info, fixed later in GCC-4.5
10383 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
10384
10385 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10386 needed, it would be wrong due to missing DW_AT_producer there.
10387
10388 Still one can confuse GDB by using non-standard GCC compilation
10389 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10390 */
ab260dad 10391 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 10392 cust->locations_valid = 1;
e0d00bc7
JK
10393
10394 if (gcc_4_minor >= 5)
43f3e411 10395 cust->epilogue_unwind_valid = 1;
96408a79 10396
43f3e411 10397 cust->call_site_htab = cu->call_site_htab;
c906108c 10398 }
9291a0cd
TT
10399
10400 if (dwarf2_per_objfile->using_index)
43f3e411 10401 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
10402 else
10403 {
10404 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10405 pst->compunit_symtab = cust;
9291a0cd
TT
10406 pst->readin = 1;
10407 }
c906108c 10408
95554aad 10409 /* Push it for inclusion processing later. */
c5d0225d 10410 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
804d2729
TT
10411
10412 /* Not needed any more. */
c24bdb02 10413 cu->reset_builder ();
f4dc4d17 10414}
45cfd468 10415
f4dc4d17
DE
10416/* Generate full symbol information for type unit PER_CU, whose DIEs have
10417 already been loaded into memory. */
10418
10419static void
10420process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10421 enum language pretend_language)
10422{
10423 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10424 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10425 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 10426 struct compunit_symtab *cust;
0186c6a7
DE
10427 struct signatured_type *sig_type;
10428
10429 gdb_assert (per_cu->is_debug_types);
10430 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 10431
c89b44cd
TT
10432 /* Clear the list here in case something was left over. */
10433 cu->method_list.clear ();
f4dc4d17 10434
f4dc4d17
DE
10435 cu->language = pretend_language;
10436 cu->language_defn = language_def (cu->language);
10437
10438 /* The symbol tables are set up in read_type_unit_scope. */
10439 process_die (cu->dies, cu);
10440
10441 /* For now fudge the Go package. */
10442 if (cu->language == language_go)
10443 fixup_go_packaging (cu);
10444
10445 /* Now that we have processed all the DIEs in the CU, all the types
10446 should be complete, and it should now be safe to compute all of the
10447 physnames. */
10448 compute_delayed_physnames (cu);
f4dc4d17 10449
c9317f21
TT
10450 if (cu->language == language_rust)
10451 rust_union_quirks (cu);
10452
f4dc4d17
DE
10453 /* TUs share symbol tables.
10454 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
10455 of it with end_expandable_symtab. Otherwise, complete the addition of
10456 this TU's symbols to the existing symtab. */
43f3e411 10457 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 10458 {
c24bdb02
KS
10459 buildsym_compunit *builder = cu->get_builder ();
10460 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
43f3e411 10461 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 10462
43f3e411 10463 if (cust != NULL)
f4dc4d17
DE
10464 {
10465 /* Set symtab language to language from DW_AT_language. If the
10466 compilation is from a C file generated by language preprocessors,
10467 do not set the language if it was already deduced by
10468 start_subfile. */
43f3e411
DE
10469 if (!(cu->language == language_c
10470 && COMPUNIT_FILETABS (cust)->language != language_c))
10471 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
10472 }
10473 }
10474 else
10475 {
c24bdb02 10476 cu->get_builder ()->augment_type_symtab ();
43f3e411 10477 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
10478 }
10479
10480 if (dwarf2_per_objfile->using_index)
43f3e411 10481 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
10482 else
10483 {
10484 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10485 pst->compunit_symtab = cust;
f4dc4d17 10486 pst->readin = 1;
45cfd468 10487 }
804d2729
TT
10488
10489 /* Not needed any more. */
c24bdb02 10490 cu->reset_builder ();
c906108c
SS
10491}
10492
95554aad
TT
10493/* Process an imported unit DIE. */
10494
10495static void
10496process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10497{
10498 struct attribute *attr;
10499
f4dc4d17
DE
10500 /* For now we don't handle imported units in type units. */
10501 if (cu->per_cu->is_debug_types)
10502 {
10503 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10504 " supported in type units [in module %s]"),
518817b3 10505 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
10506 }
10507
95554aad
TT
10508 attr = dwarf2_attr (die, DW_AT_import, cu);
10509 if (attr != NULL)
10510 {
9c541725
PA
10511 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10512 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10513 dwarf2_per_cu_data *per_cu
e3b94546 10514 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
518817b3 10515 cu->per_cu->dwarf2_per_objfile);
95554aad 10516
69d751e3 10517 /* If necessary, add it to the queue and load its DIEs. */
95554aad 10518 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 10519 load_full_comp_unit (per_cu, false, cu->language);
95554aad 10520
796a7ff8 10521 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
10522 per_cu);
10523 }
10524}
10525
4c8aa72d
PA
10526/* RAII object that represents a process_die scope: i.e.,
10527 starts/finishes processing a DIE. */
10528class process_die_scope
adde2bff 10529{
4c8aa72d
PA
10530public:
10531 process_die_scope (die_info *die, dwarf2_cu *cu)
10532 : m_die (die), m_cu (cu)
10533 {
10534 /* We should only be processing DIEs not already in process. */
10535 gdb_assert (!m_die->in_process);
10536 m_die->in_process = true;
10537 }
8c3cb9fa 10538
4c8aa72d
PA
10539 ~process_die_scope ()
10540 {
10541 m_die->in_process = false;
10542
10543 /* If we're done processing the DIE for the CU that owns the line
10544 header, we don't need the line header anymore. */
10545 if (m_cu->line_header_die_owner == m_die)
10546 {
10547 delete m_cu->line_header;
10548 m_cu->line_header = NULL;
10549 m_cu->line_header_die_owner = NULL;
10550 }
10551 }
10552
10553private:
10554 die_info *m_die;
10555 dwarf2_cu *m_cu;
10556};
adde2bff 10557
c906108c
SS
10558/* Process a die and its children. */
10559
10560static void
e7c27a73 10561process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10562{
4c8aa72d 10563 process_die_scope scope (die, cu);
adde2bff 10564
c906108c
SS
10565 switch (die->tag)
10566 {
10567 case DW_TAG_padding:
10568 break;
10569 case DW_TAG_compile_unit:
95554aad 10570 case DW_TAG_partial_unit:
e7c27a73 10571 read_file_scope (die, cu);
c906108c 10572 break;
348e048f
DE
10573 case DW_TAG_type_unit:
10574 read_type_unit_scope (die, cu);
10575 break;
c906108c 10576 case DW_TAG_subprogram:
c906108c 10577 case DW_TAG_inlined_subroutine:
edb3359d 10578 read_func_scope (die, cu);
c906108c
SS
10579 break;
10580 case DW_TAG_lexical_block:
14898363
L
10581 case DW_TAG_try_block:
10582 case DW_TAG_catch_block:
e7c27a73 10583 read_lexical_block_scope (die, cu);
c906108c 10584 break;
216f72a1 10585 case DW_TAG_call_site:
96408a79
SA
10586 case DW_TAG_GNU_call_site:
10587 read_call_site_scope (die, cu);
10588 break;
c906108c 10589 case DW_TAG_class_type:
680b30c7 10590 case DW_TAG_interface_type:
c906108c
SS
10591 case DW_TAG_structure_type:
10592 case DW_TAG_union_type:
134d01f1 10593 process_structure_scope (die, cu);
c906108c
SS
10594 break;
10595 case DW_TAG_enumeration_type:
134d01f1 10596 process_enumeration_scope (die, cu);
c906108c 10597 break;
134d01f1 10598
f792889a
DJ
10599 /* These dies have a type, but processing them does not create
10600 a symbol or recurse to process the children. Therefore we can
10601 read them on-demand through read_type_die. */
c906108c 10602 case DW_TAG_subroutine_type:
72019c9c 10603 case DW_TAG_set_type:
c906108c 10604 case DW_TAG_array_type:
c906108c 10605 case DW_TAG_pointer_type:
c906108c 10606 case DW_TAG_ptr_to_member_type:
c906108c 10607 case DW_TAG_reference_type:
4297a3f0 10608 case DW_TAG_rvalue_reference_type:
c906108c 10609 case DW_TAG_string_type:
c906108c 10610 break;
134d01f1 10611
c906108c 10612 case DW_TAG_base_type:
a02abb62 10613 case DW_TAG_subrange_type:
cb249c71 10614 case DW_TAG_typedef:
134d01f1
DJ
10615 /* Add a typedef symbol for the type definition, if it has a
10616 DW_AT_name. */
f792889a 10617 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10618 break;
c906108c 10619 case DW_TAG_common_block:
e7c27a73 10620 read_common_block (die, cu);
c906108c
SS
10621 break;
10622 case DW_TAG_common_inclusion:
10623 break;
d9fa45fe 10624 case DW_TAG_namespace:
9068261f 10625 cu->processing_has_namespace_info = true;
e7c27a73 10626 read_namespace (die, cu);
d9fa45fe 10627 break;
5d7cb8df 10628 case DW_TAG_module:
9068261f 10629 cu->processing_has_namespace_info = true;
5d7cb8df
JK
10630 read_module (die, cu);
10631 break;
d9fa45fe 10632 case DW_TAG_imported_declaration:
9068261f 10633 cu->processing_has_namespace_info = true;
74921315
KS
10634 if (read_namespace_alias (die, cu))
10635 break;
86a73007
TT
10636 /* The declaration is not a global namespace alias. */
10637 /* Fall through. */
d9fa45fe 10638 case DW_TAG_imported_module:
9068261f 10639 cu->processing_has_namespace_info = true;
27aa8d6a
SW
10640 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10641 || cu->language != language_fortran))
b98664d3 10642 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10643 dwarf_tag_name (die->tag));
10644 read_import_statement (die, cu);
d9fa45fe 10645 break;
95554aad
TT
10646
10647 case DW_TAG_imported_unit:
10648 process_imported_unit_die (die, cu);
10649 break;
10650
71a3c369
TT
10651 case DW_TAG_variable:
10652 read_variable (die, cu);
10653 break;
10654
c906108c 10655 default:
e7c27a73 10656 new_symbol (die, NULL, cu);
c906108c
SS
10657 break;
10658 }
10659}
ca69b9e6
DE
10660\f
10661/* DWARF name computation. */
c906108c 10662
94af9270
KS
10663/* A helper function for dwarf2_compute_name which determines whether DIE
10664 needs to have the name of the scope prepended to the name listed in the
10665 die. */
10666
10667static int
10668die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10669{
1c809c68
TT
10670 struct attribute *attr;
10671
94af9270
KS
10672 switch (die->tag)
10673 {
10674 case DW_TAG_namespace:
10675 case DW_TAG_typedef:
10676 case DW_TAG_class_type:
10677 case DW_TAG_interface_type:
10678 case DW_TAG_structure_type:
10679 case DW_TAG_union_type:
10680 case DW_TAG_enumeration_type:
10681 case DW_TAG_enumerator:
10682 case DW_TAG_subprogram:
08a76f8a 10683 case DW_TAG_inlined_subroutine:
94af9270 10684 case DW_TAG_member:
74921315 10685 case DW_TAG_imported_declaration:
94af9270
KS
10686 return 1;
10687
10688 case DW_TAG_variable:
c2b0a229 10689 case DW_TAG_constant:
94af9270
KS
10690 /* We only need to prefix "globally" visible variables. These include
10691 any variable marked with DW_AT_external or any variable that
10692 lives in a namespace. [Variables in anonymous namespaces
10693 require prefixing, but they are not DW_AT_external.] */
10694
10695 if (dwarf2_attr (die, DW_AT_specification, cu))
10696 {
10697 struct dwarf2_cu *spec_cu = cu;
9a619af0 10698
94af9270
KS
10699 return die_needs_namespace (die_specification (die, &spec_cu),
10700 spec_cu);
10701 }
10702
1c809c68 10703 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10704 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10705 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10706 return 0;
10707 /* A variable in a lexical block of some kind does not need a
10708 namespace, even though in C++ such variables may be external
10709 and have a mangled name. */
10710 if (die->parent->tag == DW_TAG_lexical_block
10711 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10712 || die->parent->tag == DW_TAG_catch_block
10713 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10714 return 0;
10715 return 1;
94af9270
KS
10716
10717 default:
10718 return 0;
10719 }
10720}
10721
73b9be8b
KS
10722/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10723 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10724 defined for the given DIE. */
10725
10726static struct attribute *
10727dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10728{
10729 struct attribute *attr;
10730
10731 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10732 if (attr == NULL)
10733 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10734
10735 return attr;
10736}
10737
10738/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10739 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10740 defined for the given DIE. */
10741
10742static const char *
10743dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10744{
10745 const char *linkage_name;
10746
10747 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10748 if (linkage_name == NULL)
10749 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10750
10751 return linkage_name;
10752}
10753
94af9270 10754/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10755 compute the physname for the object, which include a method's:
9c37b5ae 10756 - formal parameters (C++),
a766d390 10757 - receiver type (Go),
a766d390
DE
10758
10759 The term "physname" is a bit confusing.
10760 For C++, for example, it is the demangled name.
10761 For Go, for example, it's the mangled name.
94af9270 10762
af6b7be1
JB
10763 For Ada, return the DIE's linkage name rather than the fully qualified
10764 name. PHYSNAME is ignored..
10765
94af9270
KS
10766 The result is allocated on the objfile_obstack and canonicalized. */
10767
10768static const char *
15d034d0
TT
10769dwarf2_compute_name (const char *name,
10770 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10771 int physname)
10772{
518817b3 10773 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
bb5ed363 10774
94af9270
KS
10775 if (name == NULL)
10776 name = dwarf2_name (die, cu);
10777
2ee7123e
DE
10778 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10779 but otherwise compute it by typename_concat inside GDB.
10780 FIXME: Actually this is not really true, or at least not always true.
10781 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
5e2db402 10782 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10783 will set the demangled name to the result of dwarf2_full_name, and it is
10784 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10785 if (cu->language == language_ada
10786 || (cu->language == language_fortran && physname))
10787 {
10788 /* For Ada unit, we prefer the linkage name over the name, as
10789 the former contains the exported name, which the user expects
10790 to be able to reference. Ideally, we want the user to be able
10791 to reference this entity using either natural or linkage name,
10792 but we haven't started looking at this enhancement yet. */
73b9be8b 10793 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10794
2ee7123e
DE
10795 if (linkage_name != NULL)
10796 return linkage_name;
f55ee35c
JK
10797 }
10798
94af9270
KS
10799 /* These are the only languages we know how to qualify names in. */
10800 if (name != NULL
9c37b5ae 10801 && (cu->language == language_cplus
c44af4eb
TT
10802 || cu->language == language_fortran || cu->language == language_d
10803 || cu->language == language_rust))
94af9270
KS
10804 {
10805 if (die_needs_namespace (die, cu))
10806 {
0d5cff50 10807 const char *prefix;
34a68019 10808 const char *canonical_name = NULL;
94af9270 10809
d7e74731
PA
10810 string_file buf;
10811
94af9270 10812 prefix = determine_prefix (die, cu);
94af9270
KS
10813 if (*prefix != '\0')
10814 {
f55ee35c
JK
10815 char *prefixed_name = typename_concat (NULL, prefix, name,
10816 physname, cu);
9a619af0 10817
d7e74731 10818 buf.puts (prefixed_name);
94af9270
KS
10819 xfree (prefixed_name);
10820 }
10821 else
d7e74731 10822 buf.puts (name);
94af9270 10823
98bfdba5
PA
10824 /* Template parameters may be specified in the DIE's DW_AT_name, or
10825 as children with DW_TAG_template_type_param or
10826 DW_TAG_value_type_param. If the latter, add them to the name
10827 here. If the name already has template parameters, then
10828 skip this step; some versions of GCC emit both, and
10829 it is more efficient to use the pre-computed name.
10830
10831 Something to keep in mind about this process: it is very
10832 unlikely, or in some cases downright impossible, to produce
10833 something that will match the mangled name of a function.
10834 If the definition of the function has the same debug info,
10835 we should be able to match up with it anyway. But fallbacks
10836 using the minimal symbol, for instance to find a method
10837 implemented in a stripped copy of libstdc++, will not work.
10838 If we do not have debug info for the definition, we will have to
10839 match them up some other way.
10840
10841 When we do name matching there is a related problem with function
10842 templates; two instantiated function templates are allowed to
10843 differ only by their return types, which we do not add here. */
10844
10845 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10846 {
10847 struct attribute *attr;
10848 struct die_info *child;
10849 int first = 1;
10850
10851 die->building_fullname = 1;
10852
10853 for (child = die->child; child != NULL; child = child->sibling)
10854 {
10855 struct type *type;
12df843f 10856 LONGEST value;
d521ce57 10857 const gdb_byte *bytes;
98bfdba5
PA
10858 struct dwarf2_locexpr_baton *baton;
10859 struct value *v;
10860
10861 if (child->tag != DW_TAG_template_type_param
10862 && child->tag != DW_TAG_template_value_param)
10863 continue;
10864
10865 if (first)
10866 {
d7e74731 10867 buf.puts ("<");
98bfdba5
PA
10868 first = 0;
10869 }
10870 else
d7e74731 10871 buf.puts (", ");
98bfdba5
PA
10872
10873 attr = dwarf2_attr (child, DW_AT_type, cu);
10874 if (attr == NULL)
10875 {
b98664d3 10876 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10877 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10878 continue;
10879 }
10880 type = die_type (child, cu);
10881
10882 if (child->tag == DW_TAG_template_type_param)
10883 {
c1ec8cea
TT
10884 c_print_type (type, "", &buf, -1, 0, cu->language,
10885 &type_print_raw_options);
98bfdba5
PA
10886 continue;
10887 }
10888
10889 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10890 if (attr == NULL)
10891 {
b98664d3 10892 complaint (_("template parameter missing "
3e43a32a 10893 "DW_AT_const_value"));
d7e74731 10894 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10895 continue;
10896 }
10897
10898 dwarf2_const_value_attr (attr, type, name,
10899 &cu->comp_unit_obstack, cu,
10900 &value, &bytes, &baton);
10901
10902 if (TYPE_NOSIGN (type))
10903 /* GDB prints characters as NUMBER 'CHAR'. If that's
10904 changed, this can use value_print instead. */
d7e74731 10905 c_printchar (value, type, &buf);
98bfdba5
PA
10906 else
10907 {
10908 struct value_print_options opts;
10909
10910 if (baton != NULL)
10911 v = dwarf2_evaluate_loc_desc (type, NULL,
10912 baton->data,
10913 baton->size,
10914 baton->per_cu);
10915 else if (bytes != NULL)
10916 {
10917 v = allocate_value (type);
10918 memcpy (value_contents_writeable (v), bytes,
10919 TYPE_LENGTH (type));
10920 }
10921 else
10922 v = value_from_longest (type, value);
10923
3e43a32a
MS
10924 /* Specify decimal so that we do not depend on
10925 the radix. */
98bfdba5
PA
10926 get_formatted_print_options (&opts, 'd');
10927 opts.raw = 1;
d7e74731 10928 value_print (v, &buf, &opts);
98bfdba5 10929 release_value (v);
98bfdba5
PA
10930 }
10931 }
10932
10933 die->building_fullname = 0;
10934
10935 if (!first)
10936 {
10937 /* Close the argument list, with a space if necessary
10938 (nested templates). */
d7e74731
PA
10939 if (!buf.empty () && buf.string ().back () == '>')
10940 buf.puts (" >");
98bfdba5 10941 else
d7e74731 10942 buf.puts (">");
98bfdba5
PA
10943 }
10944 }
10945
9c37b5ae 10946 /* For C++ methods, append formal parameter type
94af9270 10947 information, if PHYSNAME. */
6e70227d 10948
94af9270 10949 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10950 && cu->language == language_cplus)
94af9270
KS
10951 {
10952 struct type *type = read_type_die (die, cu);
10953
d7e74731 10954 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10955 &type_print_raw_options);
94af9270 10956
9c37b5ae 10957 if (cu->language == language_cplus)
94af9270 10958 {
60430eff
DJ
10959 /* Assume that an artificial first parameter is
10960 "this", but do not crash if it is not. RealView
10961 marks unnamed (and thus unused) parameters as
10962 artificial; there is no way to differentiate
10963 the two cases. */
94af9270
KS
10964 if (TYPE_NFIELDS (type) > 0
10965 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 10966 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
10967 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10968 0))))
d7e74731 10969 buf.puts (" const");
94af9270
KS
10970 }
10971 }
10972
d7e74731 10973 const std::string &intermediate_name = buf.string ();
94af9270
KS
10974
10975 if (cu->language == language_cplus)
34a68019 10976 canonical_name
322a8516 10977 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
10978 &objfile->per_bfd->storage_obstack);
10979
10980 /* If we only computed INTERMEDIATE_NAME, or if
10981 INTERMEDIATE_NAME is already canonical, then we need to
10982 copy it to the appropriate obstack. */
322a8516 10983 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
efba19b0
TT
10984 name = obstack_strdup (&objfile->per_bfd->storage_obstack,
10985 intermediate_name);
34a68019
TT
10986 else
10987 name = canonical_name;
94af9270
KS
10988 }
10989 }
10990
10991 return name;
10992}
10993
0114d602
DJ
10994/* Return the fully qualified name of DIE, based on its DW_AT_name.
10995 If scope qualifiers are appropriate they will be added. The result
34a68019 10996 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10997 not have a name. NAME may either be from a previous call to
10998 dwarf2_name or NULL.
10999
9c37b5ae 11000 The output string will be canonicalized (if C++). */
0114d602
DJ
11001
11002static const char *
15d034d0 11003dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 11004{
94af9270
KS
11005 return dwarf2_compute_name (name, die, cu, 0);
11006}
0114d602 11007
94af9270
KS
11008/* Construct a physname for the given DIE in CU. NAME may either be
11009 from a previous call to dwarf2_name or NULL. The result will be
11010 allocated on the objfile_objstack or NULL if the DIE does not have a
11011 name.
0114d602 11012
9c37b5ae 11013 The output string will be canonicalized (if C++). */
0114d602 11014
94af9270 11015static const char *
15d034d0 11016dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 11017{
518817b3 11018 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
900e11f9 11019 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
11020 int need_copy = 1;
11021
11022 /* In this case dwarf2_compute_name is just a shortcut not building anything
11023 on its own. */
11024 if (!die_needs_namespace (die, cu))
11025 return dwarf2_compute_name (name, die, cu, 1);
11026
73b9be8b 11027 mangled = dw2_linkage_name (die, cu);
900e11f9 11028
e98c9e7c
TT
11029 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
11030 See https://github.com/rust-lang/rust/issues/32925. */
11031 if (cu->language == language_rust && mangled != NULL
11032 && strchr (mangled, '{') != NULL)
11033 mangled = NULL;
11034
900e11f9
JK
11035 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
11036 has computed. */
791afaa2 11037 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 11038 if (mangled != NULL)
900e11f9 11039 {
900e11f9 11040
59cc4834
JB
11041 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
11042 {
11043 /* Do nothing (do not demangle the symbol name). */
11044 }
11045 else if (cu->language == language_go)
a766d390 11046 {
5e2db402
TT
11047 /* This is a lie, but we already lie to the caller new_symbol.
11048 new_symbol assumes we return the mangled name.
a766d390 11049 This just undoes that lie until things are cleaned up. */
a766d390
DE
11050 }
11051 else
11052 {
0eb876f5
JB
11053 /* Use DMGL_RET_DROP for C++ template functions to suppress
11054 their return type. It is easier for GDB users to search
11055 for such functions as `name(params)' than `long name(params)'.
11056 In such case the minimal symbol names do not match the full
11057 symbol names but for template functions there is never a need
11058 to look up their definition from their declaration so
11059 the only disadvantage remains the minimal symbol variant
11060 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
11061 demangled.reset (gdb_demangle (mangled,
11062 (DMGL_PARAMS | DMGL_ANSI
11063 | DMGL_RET_DROP)));
a766d390 11064 }
900e11f9 11065 if (demangled)
791afaa2 11066 canon = demangled.get ();
900e11f9
JK
11067 else
11068 {
11069 canon = mangled;
11070 need_copy = 0;
11071 }
11072 }
11073
11074 if (canon == NULL || check_physname)
11075 {
11076 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11077
11078 if (canon != NULL && strcmp (physname, canon) != 0)
11079 {
11080 /* It may not mean a bug in GDB. The compiler could also
11081 compute DW_AT_linkage_name incorrectly. But in such case
11082 GDB would need to be bug-to-bug compatible. */
11083
b98664d3 11084 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
11085 "(from linkage <%s>) - DIE at %s [in module %s]"),
11086 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 11087 objfile_name (objfile));
900e11f9
JK
11088
11089 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11090 is available here - over computed PHYSNAME. It is safer
11091 against both buggy GDB and buggy compilers. */
11092
11093 retval = canon;
11094 }
11095 else
11096 {
11097 retval = physname;
11098 need_copy = 0;
11099 }
11100 }
11101 else
11102 retval = canon;
11103
11104 if (need_copy)
021887d8 11105 retval = obstack_strdup (&objfile->per_bfd->storage_obstack, retval);
900e11f9 11106
900e11f9 11107 return retval;
0114d602
DJ
11108}
11109
74921315
KS
11110/* Inspect DIE in CU for a namespace alias. If one exists, record
11111 a new symbol for it.
11112
11113 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11114
11115static int
11116read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11117{
11118 struct attribute *attr;
11119
11120 /* If the die does not have a name, this is not a namespace
11121 alias. */
11122 attr = dwarf2_attr (die, DW_AT_name, cu);
11123 if (attr != NULL)
11124 {
11125 int num;
11126 struct die_info *d = die;
11127 struct dwarf2_cu *imported_cu = cu;
11128
11129 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11130 keep inspecting DIEs until we hit the underlying import. */
11131#define MAX_NESTED_IMPORTED_DECLARATIONS 100
11132 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11133 {
11134 attr = dwarf2_attr (d, DW_AT_import, cu);
11135 if (attr == NULL)
11136 break;
11137
11138 d = follow_die_ref (d, attr, &imported_cu);
11139 if (d->tag != DW_TAG_imported_declaration)
11140 break;
11141 }
11142
11143 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11144 {
b98664d3 11145 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 11146 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
11147 return 0;
11148 }
11149
11150 if (attr != NULL)
11151 {
11152 struct type *type;
9c541725 11153 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 11154
9c541725 11155 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
11156 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11157 {
11158 /* This declaration is a global namespace alias. Add
11159 a symbol for it whose type is the aliased namespace. */
11160 new_symbol (die, type, cu);
11161 return 1;
11162 }
11163 }
11164 }
11165
11166 return 0;
11167}
11168
22cee43f 11169/* Return the using directives repository (global or local?) to use in the
804d2729 11170 current context for CU.
22cee43f
PMR
11171
11172 For Ada, imported declarations can materialize renamings, which *may* be
11173 global. However it is impossible (for now?) in DWARF to distinguish
11174 "external" imported declarations and "static" ones. As all imported
11175 declarations seem to be static in all other languages, make them all CU-wide
11176 global only in Ada. */
11177
11178static struct using_direct **
804d2729 11179using_directives (struct dwarf2_cu *cu)
22cee43f 11180{
c24bdb02
KS
11181 if (cu->language == language_ada
11182 && cu->get_builder ()->outermost_context_p ())
11183 return cu->get_builder ()->get_global_using_directives ();
22cee43f 11184 else
c24bdb02 11185 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
11186}
11187
27aa8d6a
SW
11188/* Read the import statement specified by the given die and record it. */
11189
11190static void
11191read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11192{
518817b3 11193 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
27aa8d6a 11194 struct attribute *import_attr;
32019081 11195 struct die_info *imported_die, *child_die;
de4affc9 11196 struct dwarf2_cu *imported_cu;
27aa8d6a 11197 const char *imported_name;
794684b6 11198 const char *imported_name_prefix;
13387711
SW
11199 const char *canonical_name;
11200 const char *import_alias;
11201 const char *imported_declaration = NULL;
794684b6 11202 const char *import_prefix;
eb1e02fd 11203 std::vector<const char *> excludes;
13387711 11204
27aa8d6a
SW
11205 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11206 if (import_attr == NULL)
11207 {
b98664d3 11208 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
11209 dwarf_tag_name (die->tag));
11210 return;
11211 }
11212
de4affc9
CC
11213 imported_cu = cu;
11214 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11215 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
11216 if (imported_name == NULL)
11217 {
11218 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11219
11220 The import in the following code:
11221 namespace A
11222 {
11223 typedef int B;
11224 }
11225
11226 int main ()
11227 {
11228 using A::B;
11229 B b;
11230 return b;
11231 }
11232
11233 ...
11234 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11235 <52> DW_AT_decl_file : 1
11236 <53> DW_AT_decl_line : 6
11237 <54> DW_AT_import : <0x75>
11238 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11239 <59> DW_AT_name : B
11240 <5b> DW_AT_decl_file : 1
11241 <5c> DW_AT_decl_line : 2
11242 <5d> DW_AT_type : <0x6e>
11243 ...
11244 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11245 <76> DW_AT_byte_size : 4
11246 <77> DW_AT_encoding : 5 (signed)
11247
11248 imports the wrong die ( 0x75 instead of 0x58 ).
11249 This case will be ignored until the gcc bug is fixed. */
11250 return;
11251 }
11252
82856980
SW
11253 /* Figure out the local name after import. */
11254 import_alias = dwarf2_name (die, cu);
27aa8d6a 11255
794684b6
SW
11256 /* Figure out where the statement is being imported to. */
11257 import_prefix = determine_prefix (die, cu);
11258
11259 /* Figure out what the scope of the imported die is and prepend it
11260 to the name of the imported die. */
de4affc9 11261 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 11262
f55ee35c
JK
11263 if (imported_die->tag != DW_TAG_namespace
11264 && imported_die->tag != DW_TAG_module)
794684b6 11265 {
13387711
SW
11266 imported_declaration = imported_name;
11267 canonical_name = imported_name_prefix;
794684b6 11268 }
13387711 11269 else if (strlen (imported_name_prefix) > 0)
12aaed36 11270 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
11271 imported_name_prefix,
11272 (cu->language == language_d ? "." : "::"),
11273 imported_name, (char *) NULL);
13387711
SW
11274 else
11275 canonical_name = imported_name;
794684b6 11276
32019081
JK
11277 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11278 for (child_die = die->child; child_die && child_die->tag;
11279 child_die = sibling_die (child_die))
11280 {
11281 /* DWARF-4: A Fortran use statement with a “rename list” may be
11282 represented by an imported module entry with an import attribute
11283 referring to the module and owned entries corresponding to those
11284 entities that are renamed as part of being imported. */
11285
11286 if (child_die->tag != DW_TAG_imported_declaration)
11287 {
b98664d3 11288 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
11289 "- DIE at %s [in module %s]"),
11290 sect_offset_str (child_die->sect_off),
11291 objfile_name (objfile));
32019081
JK
11292 continue;
11293 }
11294
11295 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11296 if (import_attr == NULL)
11297 {
b98664d3 11298 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
11299 dwarf_tag_name (child_die->tag));
11300 continue;
11301 }
11302
11303 imported_cu = cu;
11304 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11305 &imported_cu);
11306 imported_name = dwarf2_name (imported_die, imported_cu);
11307 if (imported_name == NULL)
11308 {
b98664d3 11309 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
11310 "imported name - DIE at %s [in module %s]"),
11311 sect_offset_str (child_die->sect_off),
11312 objfile_name (objfile));
32019081
JK
11313 continue;
11314 }
11315
eb1e02fd 11316 excludes.push_back (imported_name);
32019081
JK
11317
11318 process_die (child_die, cu);
11319 }
11320
804d2729 11321 add_using_directive (using_directives (cu),
22cee43f
PMR
11322 import_prefix,
11323 canonical_name,
11324 import_alias,
11325 imported_declaration,
11326 excludes,
11327 0,
11328 &objfile->objfile_obstack);
27aa8d6a
SW
11329}
11330
5230b05a
WT
11331/* ICC<14 does not output the required DW_AT_declaration on incomplete
11332 types, but gives them a size of zero. Starting with version 14,
11333 ICC is compatible with GCC. */
11334
9068261f 11335static bool
5230b05a
WT
11336producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11337{
11338 if (!cu->checked_producer)
11339 check_producer (cu);
11340
11341 return cu->producer_is_icc_lt_14;
11342}
11343
eb77c9df
AB
11344/* ICC generates a DW_AT_type for C void functions. This was observed on
11345 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11346 which says that void functions should not have a DW_AT_type. */
11347
11348static bool
11349producer_is_icc (struct dwarf2_cu *cu)
11350{
11351 if (!cu->checked_producer)
11352 check_producer (cu);
11353
11354 return cu->producer_is_icc;
11355}
11356
1b80a9fa
JK
11357/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11358 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11359 this, it was first present in GCC release 4.3.0. */
11360
9068261f 11361static bool
1b80a9fa
JK
11362producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11363{
11364 if (!cu->checked_producer)
11365 check_producer (cu);
11366
11367 return cu->producer_is_gcc_lt_4_3;
11368}
11369
d721ba37
PA
11370static file_and_directory
11371find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 11372{
d721ba37
PA
11373 file_and_directory res;
11374
9291a0cd
TT
11375 /* Find the filename. Do not use dwarf2_name here, since the filename
11376 is not a source language identifier. */
d721ba37
PA
11377 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11378 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 11379
d721ba37
PA
11380 if (res.comp_dir == NULL
11381 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11382 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 11383 {
d721ba37
PA
11384 res.comp_dir_storage = ldirname (res.name);
11385 if (!res.comp_dir_storage.empty ())
11386 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 11387 }
d721ba37 11388 if (res.comp_dir != NULL)
9291a0cd
TT
11389 {
11390 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11391 directory, get rid of it. */
d721ba37 11392 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 11393
d721ba37
PA
11394 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11395 res.comp_dir = cp + 1;
9291a0cd
TT
11396 }
11397
d721ba37
PA
11398 if (res.name == NULL)
11399 res.name = "<unknown>";
11400
11401 return res;
9291a0cd
TT
11402}
11403
f4dc4d17
DE
11404/* Handle DW_AT_stmt_list for a compilation unit.
11405 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
11406 COMP_DIR is the compilation directory. LOWPC is passed to
11407 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
11408
11409static void
11410handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 11411 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 11412{
518817b3
SM
11413 struct dwarf2_per_objfile *dwarf2_per_objfile
11414 = cu->per_cu->dwarf2_per_objfile;
527f3840 11415 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 11416 struct attribute *attr;
527f3840
JK
11417 struct line_header line_header_local;
11418 hashval_t line_header_local_hash;
527f3840
JK
11419 void **slot;
11420 int decode_mapping;
2ab95328 11421
f4dc4d17
DE
11422 gdb_assert (! cu->per_cu->is_debug_types);
11423
2ab95328 11424 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
11425 if (attr == NULL)
11426 return;
11427
9c541725 11428 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
11429
11430 /* The line header hash table is only created if needed (it exists to
11431 prevent redundant reading of the line table for partial_units).
11432 If we're given a partial_unit, we'll need it. If we're given a
11433 compile_unit, then use the line header hash table if it's already
11434 created, but don't create one just yet. */
11435
11436 if (dwarf2_per_objfile->line_header_hash == NULL
11437 && die->tag == DW_TAG_partial_unit)
2ab95328 11438 {
527f3840
JK
11439 dwarf2_per_objfile->line_header_hash
11440 = htab_create_alloc_ex (127, line_header_hash_voidp,
11441 line_header_eq_voidp,
11442 free_line_header_voidp,
11443 &objfile->objfile_obstack,
11444 hashtab_obstack_allocate,
11445 dummy_obstack_deallocate);
11446 }
2ab95328 11447
9c541725 11448 line_header_local.sect_off = line_offset;
527f3840
JK
11449 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11450 line_header_local_hash = line_header_hash (&line_header_local);
11451 if (dwarf2_per_objfile->line_header_hash != NULL)
11452 {
11453 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11454 &line_header_local,
11455 line_header_local_hash, NO_INSERT);
11456
11457 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11458 is not present in *SLOT (since if there is something in *SLOT then
11459 it will be for a partial_unit). */
11460 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 11461 {
527f3840 11462 gdb_assert (*slot != NULL);
9a3c8263 11463 cu->line_header = (struct line_header *) *slot;
527f3840 11464 return;
dee91e82 11465 }
2ab95328 11466 }
527f3840
JK
11467
11468 /* dwarf_decode_line_header does not yet provide sufficient information.
11469 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
11470 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11471 if (lh == NULL)
527f3840 11472 return;
4c8aa72d
PA
11473
11474 cu->line_header = lh.release ();
11475 cu->line_header_die_owner = die;
527f3840
JK
11476
11477 if (dwarf2_per_objfile->line_header_hash == NULL)
11478 slot = NULL;
11479 else
11480 {
11481 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11482 &line_header_local,
11483 line_header_local_hash, INSERT);
11484 gdb_assert (slot != NULL);
11485 }
11486 if (slot != NULL && *slot == NULL)
11487 {
11488 /* This newly decoded line number information unit will be owned
11489 by line_header_hash hash table. */
11490 *slot = cu->line_header;
4c8aa72d 11491 cu->line_header_die_owner = NULL;
527f3840
JK
11492 }
11493 else
11494 {
11495 /* We cannot free any current entry in (*slot) as that struct line_header
11496 may be already used by multiple CUs. Create only temporary decoded
11497 line_header for this CU - it may happen at most once for each line
11498 number information unit. And if we're not using line_header_hash
11499 then this is what we want as well. */
11500 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
11501 }
11502 decode_mapping = (die->tag != DW_TAG_partial_unit);
11503 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11504 decode_mapping);
fff8551c 11505
2ab95328
TT
11506}
11507
95554aad 11508/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 11509
c906108c 11510static void
e7c27a73 11511read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11512{
518817b3
SM
11513 struct dwarf2_per_objfile *dwarf2_per_objfile
11514 = cu->per_cu->dwarf2_per_objfile;
dee91e82 11515 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 11516 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 11517 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11518 CORE_ADDR highpc = ((CORE_ADDR) 0);
11519 struct attribute *attr;
c906108c 11520 struct die_info *child_die;
e142c38c 11521 CORE_ADDR baseaddr;
6e70227d 11522
380618d6 11523 prepare_one_comp_unit (cu, die, cu->language);
e142c38c 11524 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11525
fae299cd 11526 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11527
11528 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11529 from finish_block. */
2acceee2 11530 if (lowpc == ((CORE_ADDR) -1))
c906108c 11531 lowpc = highpc;
3e29f34a 11532 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 11533
d721ba37 11534 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 11535
f4b8a18d
KW
11536 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11537 standardised yet. As a workaround for the language detection we fall
11538 back to the DW_AT_producer string. */
11539 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11540 cu->language = language_opencl;
11541
3019eac3
DE
11542 /* Similar hack for Go. */
11543 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11544 set_cu_language (DW_LANG_Go, cu);
11545
c24bdb02 11546 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11547
11548 /* Decode line number information if present. We do this before
11549 processing child DIEs, so that the line header table is available
11550 for DW_AT_decl_file. */
d721ba37 11551 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11552
11553 /* Process all dies in compilation unit. */
11554 if (die->child != NULL)
11555 {
11556 child_die = die->child;
11557 while (child_die && child_die->tag)
11558 {
11559 process_die (child_die, cu);
11560 child_die = sibling_die (child_die);
11561 }
11562 }
11563
11564 /* Decode macro information, if present. Dwarf 2 macro information
11565 refers to information in the line number info statement program
11566 header, so we can only read it if we've read the header
11567 successfully. */
0af92d60
JK
11568 attr = dwarf2_attr (die, DW_AT_macros, cu);
11569 if (attr == NULL)
11570 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
11571 if (attr && cu->line_header)
11572 {
11573 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 11574 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11575
43f3e411 11576 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
11577 }
11578 else
11579 {
11580 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11581 if (attr && cu->line_header)
11582 {
11583 unsigned int macro_offset = DW_UNSND (attr);
11584
43f3e411 11585 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11586 }
11587 }
3019eac3
DE
11588}
11589
c24bdb02
KS
11590void
11591dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 11592{
f4dc4d17
DE
11593 struct type_unit_group *tu_group;
11594 int first_time;
3019eac3 11595 struct attribute *attr;
9c541725 11596 unsigned int i;
0186c6a7 11597 struct signatured_type *sig_type;
3019eac3 11598
f4dc4d17 11599 gdb_assert (per_cu->is_debug_types);
0186c6a7 11600 sig_type = (struct signatured_type *) per_cu;
3019eac3 11601
c24bdb02 11602 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 11603
f4dc4d17 11604 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11605 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 11606 if (sig_type->type_unit_group == NULL)
c24bdb02 11607 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 11608 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11609
11610 /* If we've already processed this stmt_list there's no real need to
11611 do it again, we could fake it and just recreate the part we need
11612 (file name,index -> symtab mapping). If data shows this optimization
11613 is useful we can do it then. */
43f3e411 11614 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
11615
11616 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11617 debug info. */
fff8551c 11618 line_header_up lh;
f4dc4d17 11619 if (attr != NULL)
3019eac3 11620 {
9c541725 11621 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
c24bdb02 11622 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
11623 }
11624 if (lh == NULL)
11625 {
11626 if (first_time)
c24bdb02 11627 start_symtab ("", NULL, 0);
f4dc4d17
DE
11628 else
11629 {
11630 gdb_assert (tu_group->symtabs == NULL);
c24bdb02 11631 gdb_assert (m_builder == nullptr);
804d2729 11632 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11633 m_builder.reset (new struct buildsym_compunit
11634 (COMPUNIT_OBJFILE (cust), "",
11635 COMPUNIT_DIRNAME (cust),
11636 compunit_language (cust),
11637 0, cust));
f4dc4d17 11638 }
f4dc4d17 11639 return;
3019eac3
DE
11640 }
11641
c24bdb02
KS
11642 line_header = lh.release ();
11643 line_header_die_owner = die;
3019eac3 11644
f4dc4d17
DE
11645 if (first_time)
11646 {
c24bdb02 11647 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 11648
1fd60fc0
DE
11649 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11650 still initializing it, and our caller (a few levels up)
11651 process_full_type_unit still needs to know if this is the first
11652 time. */
11653
c24bdb02 11654 tu_group->num_symtabs = line_header->file_names.size ();
4c8aa72d 11655 tu_group->symtabs = XNEWVEC (struct symtab *,
c24bdb02 11656 line_header->file_names.size ());
3019eac3 11657
c24bdb02 11658 for (i = 0; i < line_header->file_names.size (); ++i)
f4dc4d17 11659 {
c24bdb02 11660 file_entry &fe = line_header->file_names[i];
3019eac3 11661
c24bdb02
KS
11662 dwarf2_start_subfile (this, fe.name,
11663 fe.include_dir (line_header));
11664 buildsym_compunit *b = get_builder ();
11665 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 11666 {
4c8aa72d
PA
11667 /* NOTE: start_subfile will recognize when it's been
11668 passed a file it has already seen. So we can't
11669 assume there's a simple mapping from
11670 cu->line_header->file_names to subfiles, plus
11671 cu->line_header->file_names may contain dups. */
c24bdb02
KS
11672 b->get_current_subfile ()->symtab
11673 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11674 }
11675
c24bdb02 11676 fe.symtab = b->get_current_subfile ()->symtab;
8c43009f 11677 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
11678 }
11679 }
11680 else
3019eac3 11681 {
c24bdb02 11682 gdb_assert (m_builder == nullptr);
804d2729 11683 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
11684 m_builder.reset (new struct buildsym_compunit
11685 (COMPUNIT_OBJFILE (cust), "",
11686 COMPUNIT_DIRNAME (cust),
11687 compunit_language (cust),
11688 0, cust));
f4dc4d17 11689
c24bdb02 11690 for (i = 0; i < line_header->file_names.size (); ++i)
f4dc4d17 11691 {
c24bdb02 11692 file_entry &fe = line_header->file_names[i];
f4dc4d17 11693
4c8aa72d 11694 fe.symtab = tu_group->symtabs[i];
f4dc4d17 11695 }
3019eac3
DE
11696 }
11697
f4dc4d17
DE
11698 /* The main symtab is allocated last. Type units don't have DW_AT_name
11699 so they don't have a "real" (so to speak) symtab anyway.
11700 There is later code that will assign the main symtab to all symbols
11701 that don't have one. We need to handle the case of a symbol with a
11702 missing symtab (DW_AT_decl_file) anyway. */
11703}
3019eac3 11704
f4dc4d17
DE
11705/* Process DW_TAG_type_unit.
11706 For TUs we want to skip the first top level sibling if it's not the
11707 actual type being defined by this TU. In this case the first top
11708 level sibling is there to provide context only. */
3019eac3 11709
f4dc4d17
DE
11710static void
11711read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11712{
11713 struct die_info *child_die;
3019eac3 11714
f4dc4d17
DE
11715 prepare_one_comp_unit (cu, die, language_minimal);
11716
11717 /* Initialize (or reinitialize) the machinery for building symtabs.
11718 We do this before processing child DIEs, so that the line header table
11719 is available for DW_AT_decl_file. */
c24bdb02 11720 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11721
11722 if (die->child != NULL)
11723 {
11724 child_die = die->child;
11725 while (child_die && child_die->tag)
11726 {
11727 process_die (child_die, cu);
11728 child_die = sibling_die (child_die);
11729 }
11730 }
3019eac3
DE
11731}
11732\f
80626a55
DE
11733/* DWO/DWP files.
11734
11735 http://gcc.gnu.org/wiki/DebugFission
11736 http://gcc.gnu.org/wiki/DebugFissionDWP
11737
11738 To simplify handling of both DWO files ("object" files with the DWARF info)
11739 and DWP files (a file with the DWOs packaged up into one file), we treat
11740 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11741
11742static hashval_t
11743hash_dwo_file (const void *item)
11744{
9a3c8263 11745 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11746 hashval_t hash;
3019eac3 11747
a2ce51a0
DE
11748 hash = htab_hash_string (dwo_file->dwo_name);
11749 if (dwo_file->comp_dir != NULL)
11750 hash += htab_hash_string (dwo_file->comp_dir);
11751 return hash;
3019eac3
DE
11752}
11753
11754static int
11755eq_dwo_file (const void *item_lhs, const void *item_rhs)
11756{
9a3c8263
SM
11757 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11758 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11759
a2ce51a0
DE
11760 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11761 return 0;
11762 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11763 return lhs->comp_dir == rhs->comp_dir;
11764 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11765}
11766
11767/* Allocate a hash table for DWO files. */
11768
51ac9db5 11769static htab_up
ed2dc618 11770allocate_dwo_file_hash_table (struct objfile *objfile)
3019eac3 11771{
51ac9db5
SM
11772 auto delete_dwo_file = [] (void *item)
11773 {
11774 struct dwo_file *dwo_file = (struct dwo_file *) item;
11775
11776 delete dwo_file;
11777 };
11778
11779 return htab_up (htab_create_alloc_ex (41,
11780 hash_dwo_file,
11781 eq_dwo_file,
11782 delete_dwo_file,
11783 &objfile->objfile_obstack,
11784 hashtab_obstack_allocate,
11785 dummy_obstack_deallocate));
3019eac3
DE
11786}
11787
80626a55
DE
11788/* Lookup DWO file DWO_NAME. */
11789
11790static void **
ed2dc618
SM
11791lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11792 const char *dwo_name,
11793 const char *comp_dir)
80626a55
DE
11794{
11795 struct dwo_file find_entry;
11796 void **slot;
11797
11798 if (dwarf2_per_objfile->dwo_files == NULL)
ed2dc618
SM
11799 dwarf2_per_objfile->dwo_files
11800 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
80626a55 11801
0ac5b59e
DE
11802 find_entry.dwo_name = dwo_name;
11803 find_entry.comp_dir = comp_dir;
51ac9db5
SM
11804 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11805 INSERT);
80626a55
DE
11806
11807 return slot;
11808}
11809
3019eac3
DE
11810static hashval_t
11811hash_dwo_unit (const void *item)
11812{
9a3c8263 11813 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11814
11815 /* This drops the top 32 bits of the id, but is ok for a hash. */
11816 return dwo_unit->signature;
11817}
11818
11819static int
11820eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11821{
9a3c8263
SM
11822 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11823 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11824
11825 /* The signature is assumed to be unique within the DWO file.
11826 So while object file CU dwo_id's always have the value zero,
11827 that's OK, assuming each object file DWO file has only one CU,
11828 and that's the rule for now. */
11829 return lhs->signature == rhs->signature;
11830}
11831
11832/* Allocate a hash table for DWO CUs,TUs.
11833 There is one of these tables for each of CUs,TUs for each DWO file. */
11834
11835static htab_t
11836allocate_dwo_unit_table (struct objfile *objfile)
11837{
11838 /* Start out with a pretty small number.
11839 Generally DWO files contain only one CU and maybe some TUs. */
11840 return htab_create_alloc_ex (3,
11841 hash_dwo_unit,
11842 eq_dwo_unit,
11843 NULL,
11844 &objfile->objfile_obstack,
11845 hashtab_obstack_allocate,
11846 dummy_obstack_deallocate);
11847}
11848
80626a55 11849/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 11850
19c3d4c9 11851struct create_dwo_cu_data
3019eac3
DE
11852{
11853 struct dwo_file *dwo_file;
19c3d4c9 11854 struct dwo_unit dwo_unit;
3019eac3
DE
11855};
11856
19c3d4c9 11857/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11858
11859static void
19c3d4c9
DE
11860create_dwo_cu_reader (const struct die_reader_specs *reader,
11861 const gdb_byte *info_ptr,
11862 struct die_info *comp_unit_die,
11863 int has_children,
11864 void *datap)
3019eac3
DE
11865{
11866 struct dwarf2_cu *cu = reader->cu;
9c541725 11867 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11868 struct dwarf2_section_info *section = cu->per_cu->section;
9a3c8263 11869 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
3019eac3 11870 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 11871 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 11872
a084a2a6
AT
11873 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11874 if (!signature.has_value ())
3019eac3 11875 {
b98664d3 11876 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11877 " its dwo_id [in module %s]"),
9d8780f0 11878 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11879 return;
11880 }
11881
3019eac3 11882 dwo_unit->dwo_file = dwo_file;
a084a2a6 11883 dwo_unit->signature = *signature;
8a0459fd 11884 dwo_unit->section = section;
9c541725 11885 dwo_unit->sect_off = sect_off;
3019eac3
DE
11886 dwo_unit->length = cu->per_cu->length;
11887
b4f54984 11888 if (dwarf_read_debug)
9d8780f0
SM
11889 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11890 sect_offset_str (sect_off),
9c541725 11891 hex_string (dwo_unit->signature));
3019eac3
DE
11892}
11893
33c5cd75 11894/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11895 Note: This function processes DWO files only, not DWP files. */
3019eac3 11896
33c5cd75 11897static void
ed2dc618
SM
11898create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11899 struct dwo_file &dwo_file, dwarf2_section_info &section,
33c5cd75 11900 htab_t &cus_htab)
3019eac3
DE
11901{
11902 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 11903 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11904
33c5cd75
DB
11905 dwarf2_read_section (objfile, &section);
11906 info_ptr = section.buffer;
3019eac3
DE
11907
11908 if (info_ptr == NULL)
33c5cd75 11909 return;
3019eac3 11910
b4f54984 11911 if (dwarf_read_debug)
19c3d4c9
DE
11912 {
11913 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
33c5cd75
DB
11914 get_section_name (&section),
11915 get_section_file_name (&section));
19c3d4c9 11916 }
3019eac3 11917
33c5cd75 11918 end_ptr = info_ptr + section.size;
3019eac3
DE
11919 while (info_ptr < end_ptr)
11920 {
11921 struct dwarf2_per_cu_data per_cu;
33c5cd75
DB
11922 struct create_dwo_cu_data create_dwo_cu_data;
11923 struct dwo_unit *dwo_unit;
11924 void **slot;
11925 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3 11926
19c3d4c9
DE
11927 memset (&create_dwo_cu_data.dwo_unit, 0,
11928 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3 11929 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 11930 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3019eac3 11931 per_cu.is_debug_types = 0;
33c5cd75
DB
11932 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11933 per_cu.section = &section;
c5ed0576 11934 create_dwo_cu_data.dwo_file = &dwo_file;
33c5cd75
DB
11935
11936 init_cutu_and_read_dies_no_follow (
11937 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
11938 info_ptr += per_cu.length;
11939
11940 // If the unit could not be parsed, skip it.
11941 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
11942 continue;
3019eac3 11943
33c5cd75
DB
11944 if (cus_htab == NULL)
11945 cus_htab = allocate_dwo_unit_table (objfile);
19c3d4c9 11946
33c5cd75
DB
11947 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11948 *dwo_unit = create_dwo_cu_data.dwo_unit;
11949 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
11950 gdb_assert (slot != NULL);
11951 if (*slot != NULL)
19c3d4c9 11952 {
33c5cd75
DB
11953 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11954 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11955
b98664d3 11956 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11957 " the entry at offset %s, signature %s"),
11958 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11959 hex_string (dwo_unit->signature));
19c3d4c9 11960 }
33c5cd75 11961 *slot = (void *)dwo_unit;
3019eac3 11962 }
3019eac3
DE
11963}
11964
80626a55
DE
11965/* DWP file .debug_{cu,tu}_index section format:
11966 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11967
d2415c6c
DE
11968 DWP Version 1:
11969
80626a55
DE
11970 Both index sections have the same format, and serve to map a 64-bit
11971 signature to a set of section numbers. Each section begins with a header,
11972 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11973 indexes, and a pool of 32-bit section numbers. The index sections will be
11974 aligned at 8-byte boundaries in the file.
11975
d2415c6c
DE
11976 The index section header consists of:
11977
11978 V, 32 bit version number
11979 -, 32 bits unused
11980 N, 32 bit number of compilation units or type units in the index
11981 M, 32 bit number of slots in the hash table
80626a55 11982
d2415c6c 11983 Numbers are recorded using the byte order of the application binary.
80626a55 11984
d2415c6c
DE
11985 The hash table begins at offset 16 in the section, and consists of an array
11986 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11987 order of the application binary). Unused slots in the hash table are 0.
11988 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11989
d2415c6c
DE
11990 The parallel table begins immediately after the hash table
11991 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11992 array of 32-bit indexes (using the byte order of the application binary),
11993 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11994 table contains a 32-bit index into the pool of section numbers. For unused
11995 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11996
73869dc2
DE
11997 The pool of section numbers begins immediately following the hash table
11998 (at offset 16 + 12 * M from the beginning of the section). The pool of
11999 section numbers consists of an array of 32-bit words (using the byte order
12000 of the application binary). Each item in the array is indexed starting
12001 from 0. The hash table entry provides the index of the first section
12002 number in the set. Additional section numbers in the set follow, and the
12003 set is terminated by a 0 entry (section number 0 is not used in ELF).
12004
12005 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
12006 section must be the first entry in the set, and the .debug_abbrev.dwo must
12007 be the second entry. Other members of the set may follow in any order.
12008
12009 ---
12010
12011 DWP Version 2:
12012
12013 DWP Version 2 combines all the .debug_info, etc. sections into one,
12014 and the entries in the index tables are now offsets into these sections.
12015 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
12016 section.
12017
12018 Index Section Contents:
12019 Header
12020 Hash Table of Signatures dwp_hash_table.hash_table
12021 Parallel Table of Indices dwp_hash_table.unit_table
12022 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
12023 Table of Section Sizes dwp_hash_table.v2.sizes
12024
12025 The index section header consists of:
12026
12027 V, 32 bit version number
12028 L, 32 bit number of columns in the table of section offsets
12029 N, 32 bit number of compilation units or type units in the index
12030 M, 32 bit number of slots in the hash table
12031
12032 Numbers are recorded using the byte order of the application binary.
12033
12034 The hash table has the same format as version 1.
12035 The parallel table of indices has the same format as version 1,
12036 except that the entries are origin-1 indices into the table of sections
12037 offsets and the table of section sizes.
12038
12039 The table of offsets begins immediately following the parallel table
12040 (at offset 16 + 12 * M from the beginning of the section). The table is
12041 a two-dimensional array of 32-bit words (using the byte order of the
12042 application binary), with L columns and N+1 rows, in row-major order.
12043 Each row in the array is indexed starting from 0. The first row provides
12044 a key to the remaining rows: each column in this row provides an identifier
12045 for a debug section, and the offsets in the same column of subsequent rows
12046 refer to that section. The section identifiers are:
12047
12048 DW_SECT_INFO 1 .debug_info.dwo
12049 DW_SECT_TYPES 2 .debug_types.dwo
12050 DW_SECT_ABBREV 3 .debug_abbrev.dwo
12051 DW_SECT_LINE 4 .debug_line.dwo
12052 DW_SECT_LOC 5 .debug_loc.dwo
12053 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
12054 DW_SECT_MACINFO 7 .debug_macinfo.dwo
12055 DW_SECT_MACRO 8 .debug_macro.dwo
12056
12057 The offsets provided by the CU and TU index sections are the base offsets
12058 for the contributions made by each CU or TU to the corresponding section
12059 in the package file. Each CU and TU header contains an abbrev_offset
12060 field, used to find the abbreviations table for that CU or TU within the
12061 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12062 be interpreted as relative to the base offset given in the index section.
12063 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12064 should be interpreted as relative to the base offset for .debug_line.dwo,
12065 and offsets into other debug sections obtained from DWARF attributes should
12066 also be interpreted as relative to the corresponding base offset.
12067
12068 The table of sizes begins immediately following the table of offsets.
12069 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12070 with L columns and N rows, in row-major order. Each row in the array is
12071 indexed starting from 1 (row 0 is shared by the two tables).
12072
12073 ---
12074
12075 Hash table lookup is handled the same in version 1 and 2:
12076
12077 We assume that N and M will not exceed 2^32 - 1.
12078 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12079
d2415c6c
DE
12080 Given a 64-bit compilation unit signature or a type signature S, an entry
12081 in the hash table is located as follows:
80626a55 12082
d2415c6c
DE
12083 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12084 the low-order k bits all set to 1.
80626a55 12085
d2415c6c 12086 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 12087
d2415c6c
DE
12088 3) If the hash table entry at index H matches the signature, use that
12089 entry. If the hash table entry at index H is unused (all zeroes),
12090 terminate the search: the signature is not present in the table.
80626a55 12091
d2415c6c 12092 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 12093
d2415c6c 12094 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 12095 to stop at an unused slot or find the match. */
80626a55
DE
12096
12097/* Create a hash table to map DWO IDs to their CU/TU entry in
12098 .debug_{info,types}.dwo in DWP_FILE.
12099 Returns NULL if there isn't one.
12100 Note: This function processes DWP files only, not DWO files. */
12101
12102static struct dwp_hash_table *
ed2dc618
SM
12103create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12104 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
12105{
12106 struct objfile *objfile = dwarf2_per_objfile->objfile;
400174b1 12107 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 12108 const gdb_byte *index_ptr, *index_end;
80626a55 12109 struct dwarf2_section_info *index;
73869dc2 12110 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
12111 struct dwp_hash_table *htab;
12112
12113 if (is_debug_types)
12114 index = &dwp_file->sections.tu_index;
12115 else
12116 index = &dwp_file->sections.cu_index;
12117
12118 if (dwarf2_section_empty_p (index))
12119 return NULL;
12120 dwarf2_read_section (objfile, index);
12121
12122 index_ptr = index->buffer;
12123 index_end = index_ptr + index->size;
12124
12125 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
12126 index_ptr += 4;
12127 if (version == 2)
12128 nr_columns = read_4_bytes (dbfd, index_ptr);
12129 else
12130 nr_columns = 0;
12131 index_ptr += 4;
80626a55
DE
12132 nr_units = read_4_bytes (dbfd, index_ptr);
12133 index_ptr += 4;
12134 nr_slots = read_4_bytes (dbfd, index_ptr);
12135 index_ptr += 4;
12136
73869dc2 12137 if (version != 1 && version != 2)
80626a55 12138 {
21aa081e 12139 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 12140 " [in module %s]"),
21aa081e 12141 pulongest (version), dwp_file->name);
80626a55
DE
12142 }
12143 if (nr_slots != (nr_slots & -nr_slots))
12144 {
21aa081e 12145 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 12146 " is not power of 2 [in module %s]"),
21aa081e 12147 pulongest (nr_slots), dwp_file->name);
80626a55
DE
12148 }
12149
12150 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
12151 htab->version = version;
12152 htab->nr_columns = nr_columns;
80626a55
DE
12153 htab->nr_units = nr_units;
12154 htab->nr_slots = nr_slots;
12155 htab->hash_table = index_ptr;
12156 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
12157
12158 /* Exit early if the table is empty. */
12159 if (nr_slots == 0 || nr_units == 0
12160 || (version == 2 && nr_columns == 0))
12161 {
12162 /* All must be zero. */
12163 if (nr_slots != 0 || nr_units != 0
12164 || (version == 2 && nr_columns != 0))
12165 {
b98664d3 12166 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
12167 " all zero [in modules %s]"),
12168 dwp_file->name);
12169 }
12170 return htab;
12171 }
12172
12173 if (version == 1)
12174 {
12175 htab->section_pool.v1.indices =
12176 htab->unit_table + sizeof (uint32_t) * nr_slots;
12177 /* It's harder to decide whether the section is too small in v1.
12178 V1 is deprecated anyway so we punt. */
12179 }
12180 else
12181 {
12182 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12183 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 12184 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
12185 /* Reverse map for error checking. */
12186 int ids_seen[DW_SECT_MAX + 1];
12187 int i;
12188
12189 if (nr_columns < 2)
12190 {
12191 error (_("Dwarf Error: bad DWP hash table, too few columns"
12192 " in section table [in module %s]"),
12193 dwp_file->name);
12194 }
12195 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12196 {
12197 error (_("Dwarf Error: bad DWP hash table, too many columns"
12198 " in section table [in module %s]"),
12199 dwp_file->name);
12200 }
04fd5eed
GB
12201 memset (ids, 255, sizeof_ids);
12202 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
12203 for (i = 0; i < nr_columns; ++i)
12204 {
12205 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12206
12207 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12208 {
12209 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12210 " in section table [in module %s]"),
12211 id, dwp_file->name);
12212 }
12213 if (ids_seen[id] != -1)
12214 {
12215 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12216 " id %d in section table [in module %s]"),
12217 id, dwp_file->name);
12218 }
12219 ids_seen[id] = i;
12220 ids[i] = id;
12221 }
12222 /* Must have exactly one info or types section. */
12223 if (((ids_seen[DW_SECT_INFO] != -1)
12224 + (ids_seen[DW_SECT_TYPES] != -1))
12225 != 1)
12226 {
12227 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12228 " DWO info/types section [in module %s]"),
12229 dwp_file->name);
12230 }
12231 /* Must have an abbrev section. */
12232 if (ids_seen[DW_SECT_ABBREV] == -1)
12233 {
12234 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12235 " section [in module %s]"),
12236 dwp_file->name);
12237 }
12238 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12239 htab->section_pool.v2.sizes =
12240 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12241 * nr_units * nr_columns);
12242 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12243 * nr_units * nr_columns))
12244 > index_end)
12245 {
12246 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12247 " [in module %s]"),
12248 dwp_file->name);
12249 }
12250 }
80626a55
DE
12251
12252 return htab;
12253}
12254
12255/* Update SECTIONS with the data from SECTP.
12256
12257 This function is like the other "locate" section routines that are
12258 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 12259 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
12260
12261 The result is non-zero for success, or zero if an error was found. */
12262
12263static int
73869dc2
DE
12264locate_v1_virtual_dwo_sections (asection *sectp,
12265 struct virtual_v1_dwo_sections *sections)
80626a55
DE
12266{
12267 const struct dwop_section_names *names = &dwop_section_names;
12268
12269 if (section_is_p (sectp->name, &names->abbrev_dwo))
12270 {
12271 /* There can be only one. */
049412e3 12272 if (sections->abbrev.s.section != NULL)
80626a55 12273 return 0;
049412e3 12274 sections->abbrev.s.section = sectp;
80626a55
DE
12275 sections->abbrev.size = bfd_get_section_size (sectp);
12276 }
12277 else if (section_is_p (sectp->name, &names->info_dwo)
12278 || section_is_p (sectp->name, &names->types_dwo))
12279 {
12280 /* There can be only one. */
049412e3 12281 if (sections->info_or_types.s.section != NULL)
80626a55 12282 return 0;
049412e3 12283 sections->info_or_types.s.section = sectp;
80626a55
DE
12284 sections->info_or_types.size = bfd_get_section_size (sectp);
12285 }
12286 else if (section_is_p (sectp->name, &names->line_dwo))
12287 {
12288 /* There can be only one. */
049412e3 12289 if (sections->line.s.section != NULL)
80626a55 12290 return 0;
049412e3 12291 sections->line.s.section = sectp;
80626a55
DE
12292 sections->line.size = bfd_get_section_size (sectp);
12293 }
12294 else if (section_is_p (sectp->name, &names->loc_dwo))
12295 {
12296 /* There can be only one. */
049412e3 12297 if (sections->loc.s.section != NULL)
80626a55 12298 return 0;
049412e3 12299 sections->loc.s.section = sectp;
80626a55
DE
12300 sections->loc.size = bfd_get_section_size (sectp);
12301 }
12302 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12303 {
12304 /* There can be only one. */
049412e3 12305 if (sections->macinfo.s.section != NULL)
80626a55 12306 return 0;
049412e3 12307 sections->macinfo.s.section = sectp;
80626a55
DE
12308 sections->macinfo.size = bfd_get_section_size (sectp);
12309 }
12310 else if (section_is_p (sectp->name, &names->macro_dwo))
12311 {
12312 /* There can be only one. */
049412e3 12313 if (sections->macro.s.section != NULL)
80626a55 12314 return 0;
049412e3 12315 sections->macro.s.section = sectp;
80626a55
DE
12316 sections->macro.size = bfd_get_section_size (sectp);
12317 }
12318 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12319 {
12320 /* There can be only one. */
049412e3 12321 if (sections->str_offsets.s.section != NULL)
80626a55 12322 return 0;
049412e3 12323 sections->str_offsets.s.section = sectp;
80626a55
DE
12324 sections->str_offsets.size = bfd_get_section_size (sectp);
12325 }
12326 else
12327 {
12328 /* No other kind of section is valid. */
12329 return 0;
12330 }
12331
12332 return 1;
12333}
12334
73869dc2
DE
12335/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12336 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12337 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12338 This is for DWP version 1 files. */
80626a55
DE
12339
12340static struct dwo_unit *
ed2dc618
SM
12341create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12342 struct dwp_file *dwp_file,
73869dc2
DE
12343 uint32_t unit_index,
12344 const char *comp_dir,
12345 ULONGEST signature, int is_debug_types)
80626a55
DE
12346{
12347 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
12348 const struct dwp_hash_table *dwp_htab =
12349 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12350 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
12351 const char *kind = is_debug_types ? "TU" : "CU";
12352 struct dwo_file *dwo_file;
12353 struct dwo_unit *dwo_unit;
73869dc2 12354 struct virtual_v1_dwo_sections sections;
80626a55 12355 void **dwo_file_slot;
80626a55
DE
12356 int i;
12357
73869dc2
DE
12358 gdb_assert (dwp_file->version == 1);
12359
b4f54984 12360 if (dwarf_read_debug)
80626a55 12361 {
73869dc2 12362 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 12363 kind,
73869dc2 12364 pulongest (unit_index), hex_string (signature),
80626a55
DE
12365 dwp_file->name);
12366 }
12367
19ac8c2e 12368 /* Fetch the sections of this DWO unit.
80626a55
DE
12369 Put a limit on the number of sections we look for so that bad data
12370 doesn't cause us to loop forever. */
12371
73869dc2 12372#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
12373 (1 /* .debug_info or .debug_types */ \
12374 + 1 /* .debug_abbrev */ \
12375 + 1 /* .debug_line */ \
12376 + 1 /* .debug_loc */ \
12377 + 1 /* .debug_str_offsets */ \
19ac8c2e 12378 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
12379 + 1 /* trailing zero */)
12380
12381 memset (&sections, 0, sizeof (sections));
80626a55 12382
73869dc2 12383 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
12384 {
12385 asection *sectp;
12386 uint32_t section_nr =
12387 read_4_bytes (dbfd,
73869dc2
DE
12388 dwp_htab->section_pool.v1.indices
12389 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
12390
12391 if (section_nr == 0)
12392 break;
12393 if (section_nr >= dwp_file->num_sections)
12394 {
12395 error (_("Dwarf Error: bad DWP hash table, section number too large"
12396 " [in module %s]"),
12397 dwp_file->name);
12398 }
12399
12400 sectp = dwp_file->elf_sections[section_nr];
73869dc2 12401 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
12402 {
12403 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12404 " [in module %s]"),
12405 dwp_file->name);
12406 }
12407 }
12408
12409 if (i < 2
a32a8923
DE
12410 || dwarf2_section_empty_p (&sections.info_or_types)
12411 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
12412 {
12413 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12414 " [in module %s]"),
12415 dwp_file->name);
12416 }
73869dc2 12417 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
12418 {
12419 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12420 " [in module %s]"),
12421 dwp_file->name);
12422 }
12423
12424 /* It's easier for the rest of the code if we fake a struct dwo_file and
12425 have dwo_unit "live" in that. At least for now.
12426
12427 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 12428 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
12429 file, we can combine them back into a virtual DWO file to save space
12430 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
12431 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12432
791afaa2
TT
12433 std::string virtual_dwo_name =
12434 string_printf ("virtual-dwo/%d-%d-%d-%d",
12435 get_section_id (&sections.abbrev),
12436 get_section_id (&sections.line),
12437 get_section_id (&sections.loc),
12438 get_section_id (&sections.str_offsets));
80626a55 12439 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12440 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12441 virtual_dwo_name.c_str (),
12442 comp_dir);
80626a55
DE
12443 /* Create one if necessary. */
12444 if (*dwo_file_slot == NULL)
12445 {
b4f54984 12446 if (dwarf_read_debug)
80626a55
DE
12447 {
12448 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12449 virtual_dwo_name.c_str ());
80626a55 12450 }
51ac9db5 12451 dwo_file = new struct dwo_file;
efba19b0
TT
12452 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12453 virtual_dwo_name);
0ac5b59e 12454 dwo_file->comp_dir = comp_dir;
80626a55
DE
12455 dwo_file->sections.abbrev = sections.abbrev;
12456 dwo_file->sections.line = sections.line;
12457 dwo_file->sections.loc = sections.loc;
12458 dwo_file->sections.macinfo = sections.macinfo;
12459 dwo_file->sections.macro = sections.macro;
12460 dwo_file->sections.str_offsets = sections.str_offsets;
12461 /* The "str" section is global to the entire DWP file. */
12462 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 12463 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
12464 there's no need to record it in dwo_file.
12465 Also, we can't simply record type sections in dwo_file because
12466 we record a pointer into the vector in dwo_unit. As we collect more
12467 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
12468 for it, invalidating all copies of pointers into the previous
12469 contents. */
80626a55
DE
12470 *dwo_file_slot = dwo_file;
12471 }
12472 else
12473 {
b4f54984 12474 if (dwarf_read_debug)
80626a55
DE
12475 {
12476 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12477 virtual_dwo_name.c_str ());
80626a55 12478 }
9a3c8263 12479 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 12480 }
80626a55
DE
12481
12482 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12483 dwo_unit->dwo_file = dwo_file;
12484 dwo_unit->signature = signature;
8d749320
SM
12485 dwo_unit->section =
12486 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 12487 *dwo_unit->section = sections.info_or_types;
57d63ce2 12488 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
12489
12490 return dwo_unit;
12491}
12492
73869dc2
DE
12493/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12494 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12495 piece within that section used by a TU/CU, return a virtual section
12496 of just that piece. */
12497
12498static struct dwarf2_section_info
ed2dc618
SM
12499create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12500 struct dwarf2_section_info *section,
73869dc2
DE
12501 bfd_size_type offset, bfd_size_type size)
12502{
12503 struct dwarf2_section_info result;
12504 asection *sectp;
12505
12506 gdb_assert (section != NULL);
12507 gdb_assert (!section->is_virtual);
12508
12509 memset (&result, 0, sizeof (result));
12510 result.s.containing_section = section;
dc4ccb6f 12511 result.is_virtual = true;
73869dc2
DE
12512
12513 if (size == 0)
12514 return result;
12515
12516 sectp = get_section_bfd_section (section);
12517
12518 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12519 bounds of the real section. This is a pretty-rare event, so just
12520 flag an error (easier) instead of a warning and trying to cope. */
12521 if (sectp == NULL
12522 || offset + size > bfd_get_section_size (sectp))
12523 {
73869dc2
DE
12524 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12525 " in section %s [in module %s]"),
12526 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
12527 objfile_name (dwarf2_per_objfile->objfile));
12528 }
12529
12530 result.virtual_offset = offset;
12531 result.size = size;
12532 return result;
12533}
12534
12535/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12536 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12537 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12538 This is for DWP version 2 files. */
12539
12540static struct dwo_unit *
ed2dc618
SM
12541create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12542 struct dwp_file *dwp_file,
73869dc2
DE
12543 uint32_t unit_index,
12544 const char *comp_dir,
12545 ULONGEST signature, int is_debug_types)
12546{
12547 struct objfile *objfile = dwarf2_per_objfile->objfile;
12548 const struct dwp_hash_table *dwp_htab =
12549 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12550 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
12551 const char *kind = is_debug_types ? "TU" : "CU";
12552 struct dwo_file *dwo_file;
12553 struct dwo_unit *dwo_unit;
12554 struct virtual_v2_dwo_sections sections;
12555 void **dwo_file_slot;
73869dc2
DE
12556 int i;
12557
12558 gdb_assert (dwp_file->version == 2);
12559
b4f54984 12560 if (dwarf_read_debug)
73869dc2
DE
12561 {
12562 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12563 kind,
12564 pulongest (unit_index), hex_string (signature),
12565 dwp_file->name);
12566 }
12567
12568 /* Fetch the section offsets of this DWO unit. */
12569
12570 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12571
12572 for (i = 0; i < dwp_htab->nr_columns; ++i)
12573 {
12574 uint32_t offset = read_4_bytes (dbfd,
12575 dwp_htab->section_pool.v2.offsets
12576 + (((unit_index - 1) * dwp_htab->nr_columns
12577 + i)
12578 * sizeof (uint32_t)));
12579 uint32_t size = read_4_bytes (dbfd,
12580 dwp_htab->section_pool.v2.sizes
12581 + (((unit_index - 1) * dwp_htab->nr_columns
12582 + i)
12583 * sizeof (uint32_t)));
12584
12585 switch (dwp_htab->section_pool.v2.section_ids[i])
12586 {
12587 case DW_SECT_INFO:
12588 case DW_SECT_TYPES:
12589 sections.info_or_types_offset = offset;
12590 sections.info_or_types_size = size;
12591 break;
12592 case DW_SECT_ABBREV:
12593 sections.abbrev_offset = offset;
12594 sections.abbrev_size = size;
12595 break;
12596 case DW_SECT_LINE:
12597 sections.line_offset = offset;
12598 sections.line_size = size;
12599 break;
12600 case DW_SECT_LOC:
12601 sections.loc_offset = offset;
12602 sections.loc_size = size;
12603 break;
12604 case DW_SECT_STR_OFFSETS:
12605 sections.str_offsets_offset = offset;
12606 sections.str_offsets_size = size;
12607 break;
12608 case DW_SECT_MACINFO:
12609 sections.macinfo_offset = offset;
12610 sections.macinfo_size = size;
12611 break;
12612 case DW_SECT_MACRO:
12613 sections.macro_offset = offset;
12614 sections.macro_size = size;
12615 break;
12616 }
12617 }
12618
12619 /* It's easier for the rest of the code if we fake a struct dwo_file and
12620 have dwo_unit "live" in that. At least for now.
12621
12622 The DWP file can be made up of a random collection of CUs and TUs.
12623 However, for each CU + set of TUs that came from the same original DWO
12624 file, we can combine them back into a virtual DWO file to save space
12625 (fewer struct dwo_file objects to allocate). Remember that for really
12626 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12627
791afaa2
TT
12628 std::string virtual_dwo_name =
12629 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12630 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12631 (long) (sections.line_size ? sections.line_offset : 0),
12632 (long) (sections.loc_size ? sections.loc_offset : 0),
12633 (long) (sections.str_offsets_size
12634 ? sections.str_offsets_offset : 0));
73869dc2 12635 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12636 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12637 virtual_dwo_name.c_str (),
12638 comp_dir);
73869dc2
DE
12639 /* Create one if necessary. */
12640 if (*dwo_file_slot == NULL)
12641 {
b4f54984 12642 if (dwarf_read_debug)
73869dc2
DE
12643 {
12644 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12645 virtual_dwo_name.c_str ());
73869dc2 12646 }
51ac9db5 12647 dwo_file = new struct dwo_file;
efba19b0
TT
12648 dwo_file->dwo_name = obstack_strdup (&objfile->objfile_obstack,
12649 virtual_dwo_name);
73869dc2
DE
12650 dwo_file->comp_dir = comp_dir;
12651 dwo_file->sections.abbrev =
ed2dc618 12652 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
12653 sections.abbrev_offset, sections.abbrev_size);
12654 dwo_file->sections.line =
ed2dc618 12655 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
12656 sections.line_offset, sections.line_size);
12657 dwo_file->sections.loc =
ed2dc618 12658 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
12659 sections.loc_offset, sections.loc_size);
12660 dwo_file->sections.macinfo =
ed2dc618 12661 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
12662 sections.macinfo_offset, sections.macinfo_size);
12663 dwo_file->sections.macro =
ed2dc618 12664 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
12665 sections.macro_offset, sections.macro_size);
12666 dwo_file->sections.str_offsets =
ed2dc618
SM
12667 create_dwp_v2_section (dwarf2_per_objfile,
12668 &dwp_file->sections.str_offsets,
73869dc2
DE
12669 sections.str_offsets_offset,
12670 sections.str_offsets_size);
12671 /* The "str" section is global to the entire DWP file. */
12672 dwo_file->sections.str = dwp_file->sections.str;
12673 /* The info or types section is assigned below to dwo_unit,
12674 there's no need to record it in dwo_file.
12675 Also, we can't simply record type sections in dwo_file because
12676 we record a pointer into the vector in dwo_unit. As we collect more
12677 types we'll grow the vector and eventually have to reallocate space
12678 for it, invalidating all copies of pointers into the previous
12679 contents. */
12680 *dwo_file_slot = dwo_file;
12681 }
12682 else
12683 {
b4f54984 12684 if (dwarf_read_debug)
73869dc2
DE
12685 {
12686 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12687 virtual_dwo_name.c_str ());
73869dc2 12688 }
9a3c8263 12689 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12690 }
73869dc2
DE
12691
12692 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12693 dwo_unit->dwo_file = dwo_file;
12694 dwo_unit->signature = signature;
8d749320
SM
12695 dwo_unit->section =
12696 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
ed2dc618
SM
12697 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12698 is_debug_types
73869dc2
DE
12699 ? &dwp_file->sections.types
12700 : &dwp_file->sections.info,
12701 sections.info_or_types_offset,
12702 sections.info_or_types_size);
12703 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12704
12705 return dwo_unit;
12706}
12707
57d63ce2
DE
12708/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12709 Returns NULL if the signature isn't found. */
80626a55
DE
12710
12711static struct dwo_unit *
ed2dc618
SM
12712lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12713 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12714 ULONGEST signature, int is_debug_types)
80626a55 12715{
57d63ce2
DE
12716 const struct dwp_hash_table *dwp_htab =
12717 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12718 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12719 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12720 uint32_t hash = signature & mask;
12721 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12722 unsigned int i;
12723 void **slot;
870f88f7 12724 struct dwo_unit find_dwo_cu;
80626a55
DE
12725
12726 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12727 find_dwo_cu.signature = signature;
19ac8c2e
DE
12728 slot = htab_find_slot (is_debug_types
12729 ? dwp_file->loaded_tus
12730 : dwp_file->loaded_cus,
12731 &find_dwo_cu, INSERT);
80626a55
DE
12732
12733 if (*slot != NULL)
9a3c8263 12734 return (struct dwo_unit *) *slot;
80626a55
DE
12735
12736 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12737 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12738 {
12739 ULONGEST signature_in_table;
12740
12741 signature_in_table =
57d63ce2 12742 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12743 if (signature_in_table == signature)
12744 {
57d63ce2
DE
12745 uint32_t unit_index =
12746 read_4_bytes (dbfd,
12747 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12748
73869dc2
DE
12749 if (dwp_file->version == 1)
12750 {
ed2dc618
SM
12751 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12752 dwp_file, unit_index,
73869dc2
DE
12753 comp_dir, signature,
12754 is_debug_types);
12755 }
12756 else
12757 {
ed2dc618
SM
12758 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12759 dwp_file, unit_index,
73869dc2
DE
12760 comp_dir, signature,
12761 is_debug_types);
12762 }
9a3c8263 12763 return (struct dwo_unit *) *slot;
80626a55
DE
12764 }
12765 if (signature_in_table == 0)
12766 return NULL;
12767 hash = (hash + hash2) & mask;
12768 }
12769
12770 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12771 " [in module %s]"),
12772 dwp_file->name);
12773}
12774
ab5088bf 12775/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12776 Open the file specified by FILE_NAME and hand it off to BFD for
12777 preliminary analysis. Return a newly initialized bfd *, which
12778 includes a canonicalized copy of FILE_NAME.
80626a55 12779 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12780 SEARCH_CWD is true if the current directory is to be searched.
12781 It will be searched before debug-file-directory.
13aaf454
DE
12782 If successful, the file is added to the bfd include table of the
12783 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12784 If unable to find/open the file, return NULL.
3019eac3
DE
12785 NOTE: This function is derived from symfile_bfd_open. */
12786
192b62ce 12787static gdb_bfd_ref_ptr
ed2dc618
SM
12788try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12789 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12790{
24b9144d 12791 int desc;
9c02c129
DE
12792 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12793 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12794 to debug_file_directory. */
e0cc99a6 12795 const char *search_path;
9c02c129
DE
12796 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12797
e0cc99a6 12798 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12799 if (search_cwd)
12800 {
12801 if (*debug_file_directory != '\0')
e0cc99a6
TT
12802 {
12803 search_path_holder.reset (concat (".", dirname_separator_string,
12804 debug_file_directory,
12805 (char *) NULL));
12806 search_path = search_path_holder.get ();
12807 }
6ac97d4c 12808 else
e0cc99a6 12809 search_path = ".";
6ac97d4c 12810 }
9c02c129 12811 else
e0cc99a6 12812 search_path = debug_file_directory;
3019eac3 12813
24b9144d 12814 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12815 if (is_dwp)
12816 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12817
12818 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12819 desc = openp (search_path, flags, file_name,
3019eac3
DE
12820 O_RDONLY | O_BINARY, &absolute_name);
12821 if (desc < 0)
12822 return NULL;
12823
e0cc99a6
TT
12824 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12825 gnutarget, desc));
9c02c129
DE
12826 if (sym_bfd == NULL)
12827 return NULL;
192b62ce 12828 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12829
192b62ce
TT
12830 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12831 return NULL;
3019eac3 12832
13aaf454
DE
12833 /* Success. Record the bfd as having been included by the objfile's bfd.
12834 This is important because things like demangled_names_hash lives in the
12835 objfile's per_bfd space and may have references to things like symbol
12836 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12837 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12838
3019eac3
DE
12839 return sym_bfd;
12840}
12841
ab5088bf 12842/* Try to open DWO file FILE_NAME.
3019eac3
DE
12843 COMP_DIR is the DW_AT_comp_dir attribute.
12844 The result is the bfd handle of the file.
12845 If there is a problem finding or opening the file, return NULL.
12846 Upon success, the canonicalized path of the file is stored in the bfd,
12847 same as symfile_bfd_open. */
12848
192b62ce 12849static gdb_bfd_ref_ptr
ed2dc618
SM
12850open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12851 const char *file_name, const char *comp_dir)
3019eac3 12852{
80626a55 12853 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12854 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12855 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12856
12857 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12858
12859 if (comp_dir != NULL)
12860 {
b36cec19
PA
12861 char *path_to_try = concat (comp_dir, SLASH_STRING,
12862 file_name, (char *) NULL);
3019eac3
DE
12863
12864 /* NOTE: If comp_dir is a relative path, this will also try the
12865 search path, which seems useful. */
ed2dc618
SM
12866 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12867 path_to_try,
12868 0 /*is_dwp*/,
192b62ce 12869 1 /*search_cwd*/));
3019eac3
DE
12870 xfree (path_to_try);
12871 if (abfd != NULL)
12872 return abfd;
12873 }
12874
12875 /* That didn't work, try debug-file-directory, which, despite its name,
12876 is a list of paths. */
12877
12878 if (*debug_file_directory == '\0')
12879 return NULL;
12880
ed2dc618
SM
12881 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12882 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12883}
12884
80626a55
DE
12885/* This function is mapped across the sections and remembers the offset and
12886 size of each of the DWO debugging sections we are interested in. */
12887
12888static void
12889dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12890{
9a3c8263 12891 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12892 const struct dwop_section_names *names = &dwop_section_names;
12893
12894 if (section_is_p (sectp->name, &names->abbrev_dwo))
12895 {
049412e3 12896 dwo_sections->abbrev.s.section = sectp;
80626a55
DE
12897 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
12898 }
12899 else if (section_is_p (sectp->name, &names->info_dwo))
12900 {
049412e3 12901 dwo_sections->info.s.section = sectp;
80626a55
DE
12902 dwo_sections->info.size = bfd_get_section_size (sectp);
12903 }
12904 else if (section_is_p (sectp->name, &names->line_dwo))
12905 {
049412e3 12906 dwo_sections->line.s.section = sectp;
80626a55
DE
12907 dwo_sections->line.size = bfd_get_section_size (sectp);
12908 }
12909 else if (section_is_p (sectp->name, &names->loc_dwo))
12910 {
049412e3 12911 dwo_sections->loc.s.section = sectp;
80626a55
DE
12912 dwo_sections->loc.size = bfd_get_section_size (sectp);
12913 }
12914 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12915 {
049412e3 12916 dwo_sections->macinfo.s.section = sectp;
80626a55
DE
12917 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
12918 }
12919 else if (section_is_p (sectp->name, &names->macro_dwo))
12920 {
049412e3 12921 dwo_sections->macro.s.section = sectp;
80626a55
DE
12922 dwo_sections->macro.size = bfd_get_section_size (sectp);
12923 }
12924 else if (section_is_p (sectp->name, &names->str_dwo))
12925 {
049412e3 12926 dwo_sections->str.s.section = sectp;
80626a55
DE
12927 dwo_sections->str.size = bfd_get_section_size (sectp);
12928 }
12929 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12930 {
049412e3 12931 dwo_sections->str_offsets.s.section = sectp;
80626a55
DE
12932 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
12933 }
12934 else if (section_is_p (sectp->name, &names->types_dwo))
12935 {
12936 struct dwarf2_section_info type_section;
12937
12938 memset (&type_section, 0, sizeof (type_section));
049412e3 12939 type_section.s.section = sectp;
80626a55 12940 type_section.size = bfd_get_section_size (sectp);
fd5866f6 12941 dwo_sections->types.push_back (type_section);
80626a55
DE
12942 }
12943}
12944
ab5088bf 12945/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12946 by PER_CU. This is for the non-DWP case.
80626a55 12947 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12948
12949static struct dwo_file *
0ac5b59e
DE
12950open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12951 const char *dwo_name, const char *comp_dir)
3019eac3 12952{
ed2dc618 12953 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 12954
fb1eb2f9 12955 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
80626a55
DE
12956 if (dbfd == NULL)
12957 {
b4f54984 12958 if (dwarf_read_debug)
80626a55
DE
12959 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12960 return NULL;
12961 }
263db9a1 12962
51ac9db5 12963 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12964 dwo_file->dwo_name = dwo_name;
12965 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12966 dwo_file->dbfd = std::move (dbfd);
3019eac3 12967
fb1eb2f9 12968 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
192b62ce 12969 &dwo_file->sections);
3019eac3 12970
ed2dc618
SM
12971 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
12972 dwo_file->cus);
3019eac3 12973
263db9a1 12974 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 12975 dwo_file->sections.types, dwo_file->tus);
3019eac3 12976
b4f54984 12977 if (dwarf_read_debug)
80626a55
DE
12978 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12979
263db9a1 12980 return dwo_file.release ();
3019eac3
DE
12981}
12982
80626a55 12983/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12984 size of each of the DWP debugging sections common to version 1 and 2 that
12985 we are interested in. */
3019eac3 12986
80626a55 12987static void
73869dc2
DE
12988dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12989 void *dwp_file_ptr)
3019eac3 12990{
9a3c8263 12991 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
12992 const struct dwop_section_names *names = &dwop_section_names;
12993 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12994
80626a55 12995 /* Record the ELF section number for later lookup: this is what the
73869dc2 12996 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12997 gdb_assert (elf_section_nr < dwp_file->num_sections);
12998 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12999
80626a55
DE
13000 /* Look for specific sections that we need. */
13001 if (section_is_p (sectp->name, &names->str_dwo))
13002 {
049412e3 13003 dwp_file->sections.str.s.section = sectp;
80626a55
DE
13004 dwp_file->sections.str.size = bfd_get_section_size (sectp);
13005 }
13006 else if (section_is_p (sectp->name, &names->cu_index))
13007 {
049412e3 13008 dwp_file->sections.cu_index.s.section = sectp;
80626a55
DE
13009 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
13010 }
13011 else if (section_is_p (sectp->name, &names->tu_index))
13012 {
049412e3 13013 dwp_file->sections.tu_index.s.section = sectp;
80626a55
DE
13014 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
13015 }
13016}
3019eac3 13017
73869dc2
DE
13018/* This function is mapped across the sections and remembers the offset and
13019 size of each of the DWP version 2 debugging sections that we are interested
13020 in. This is split into a separate function because we don't know if we
13021 have version 1 or 2 until we parse the cu_index/tu_index sections. */
13022
13023static void
13024dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13025{
9a3c8263 13026 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
13027 const struct dwop_section_names *names = &dwop_section_names;
13028 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13029
13030 /* Record the ELF section number for later lookup: this is what the
13031 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13032 gdb_assert (elf_section_nr < dwp_file->num_sections);
13033 dwp_file->elf_sections[elf_section_nr] = sectp;
13034
13035 /* Look for specific sections that we need. */
13036 if (section_is_p (sectp->name, &names->abbrev_dwo))
13037 {
049412e3 13038 dwp_file->sections.abbrev.s.section = sectp;
73869dc2
DE
13039 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
13040 }
13041 else if (section_is_p (sectp->name, &names->info_dwo))
13042 {
049412e3 13043 dwp_file->sections.info.s.section = sectp;
73869dc2
DE
13044 dwp_file->sections.info.size = bfd_get_section_size (sectp);
13045 }
13046 else if (section_is_p (sectp->name, &names->line_dwo))
13047 {
049412e3 13048 dwp_file->sections.line.s.section = sectp;
73869dc2
DE
13049 dwp_file->sections.line.size = bfd_get_section_size (sectp);
13050 }
13051 else if (section_is_p (sectp->name, &names->loc_dwo))
13052 {
049412e3 13053 dwp_file->sections.loc.s.section = sectp;
73869dc2
DE
13054 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
13055 }
13056 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13057 {
049412e3 13058 dwp_file->sections.macinfo.s.section = sectp;
73869dc2
DE
13059 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
13060 }
13061 else if (section_is_p (sectp->name, &names->macro_dwo))
13062 {
049412e3 13063 dwp_file->sections.macro.s.section = sectp;
73869dc2
DE
13064 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
13065 }
13066 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13067 {
049412e3 13068 dwp_file->sections.str_offsets.s.section = sectp;
73869dc2
DE
13069 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
13070 }
13071 else if (section_is_p (sectp->name, &names->types_dwo))
13072 {
049412e3 13073 dwp_file->sections.types.s.section = sectp;
73869dc2
DE
13074 dwp_file->sections.types.size = bfd_get_section_size (sectp);
13075 }
13076}
13077
80626a55 13078/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 13079
80626a55
DE
13080static hashval_t
13081hash_dwp_loaded_cutus (const void *item)
13082{
9a3c8263 13083 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 13084
80626a55
DE
13085 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13086 return dwo_unit->signature;
3019eac3
DE
13087}
13088
80626a55 13089/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 13090
80626a55
DE
13091static int
13092eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 13093{
9a3c8263
SM
13094 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13095 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 13096
80626a55
DE
13097 return dua->signature == dub->signature;
13098}
3019eac3 13099
80626a55 13100/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 13101
80626a55
DE
13102static htab_t
13103allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13104{
13105 return htab_create_alloc_ex (3,
13106 hash_dwp_loaded_cutus,
13107 eq_dwp_loaded_cutus,
13108 NULL,
13109 &objfile->objfile_obstack,
13110 hashtab_obstack_allocate,
13111 dummy_obstack_deallocate);
13112}
3019eac3 13113
ab5088bf
DE
13114/* Try to open DWP file FILE_NAME.
13115 The result is the bfd handle of the file.
13116 If there is a problem finding or opening the file, return NULL.
13117 Upon success, the canonicalized path of the file is stored in the bfd,
13118 same as symfile_bfd_open. */
13119
192b62ce 13120static gdb_bfd_ref_ptr
ed2dc618
SM
13121open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13122 const char *file_name)
ab5088bf 13123{
ed2dc618
SM
13124 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13125 1 /*is_dwp*/,
192b62ce 13126 1 /*search_cwd*/));
6ac97d4c
DE
13127 if (abfd != NULL)
13128 return abfd;
13129
13130 /* Work around upstream bug 15652.
13131 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13132 [Whether that's a "bug" is debatable, but it is getting in our way.]
13133 We have no real idea where the dwp file is, because gdb's realpath-ing
13134 of the executable's path may have discarded the needed info.
13135 [IWBN if the dwp file name was recorded in the executable, akin to
13136 .gnu_debuglink, but that doesn't exist yet.]
13137 Strip the directory from FILE_NAME and search again. */
13138 if (*debug_file_directory != '\0')
13139 {
13140 /* Don't implicitly search the current directory here.
13141 If the user wants to search "." to handle this case,
13142 it must be added to debug-file-directory. */
ed2dc618
SM
13143 return try_open_dwop_file (dwarf2_per_objfile,
13144 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
13145 0 /*search_cwd*/);
13146 }
13147
13148 return NULL;
ab5088bf
DE
13149}
13150
80626a55
DE
13151/* Initialize the use of the DWP file for the current objfile.
13152 By convention the name of the DWP file is ${objfile}.dwp.
13153 The result is NULL if it can't be found. */
a766d390 13154
400174b1 13155static std::unique_ptr<struct dwp_file>
ed2dc618 13156open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
13157{
13158 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 13159
82bf32bc
JK
13160 /* Try to find first .dwp for the binary file before any symbolic links
13161 resolving. */
6c447423
DE
13162
13163 /* If the objfile is a debug file, find the name of the real binary
13164 file and get the name of dwp file from there. */
d721ba37 13165 std::string dwp_name;
6c447423
DE
13166 if (objfile->separate_debug_objfile_backlink != NULL)
13167 {
13168 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13169 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 13170
d721ba37 13171 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
13172 }
13173 else
d721ba37
PA
13174 dwp_name = objfile->original_name;
13175
13176 dwp_name += ".dwp";
80626a55 13177
ed2dc618 13178 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
13179 if (dbfd == NULL
13180 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13181 {
13182 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
13183 dwp_name = objfile_name (objfile);
13184 dwp_name += ".dwp";
ed2dc618 13185 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
13186 }
13187
80626a55
DE
13188 if (dbfd == NULL)
13189 {
b4f54984 13190 if (dwarf_read_debug)
d721ba37 13191 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 13192 return std::unique_ptr<dwp_file> ();
3019eac3 13193 }
400174b1
TT
13194
13195 const char *name = bfd_get_filename (dbfd.get ());
13196 std::unique_ptr<struct dwp_file> dwp_file
13197 (new struct dwp_file (name, std::move (dbfd)));
c906108c 13198
0a0f4c01 13199 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55
DE
13200 dwp_file->elf_sections =
13201 OBSTACK_CALLOC (&objfile->objfile_obstack,
13202 dwp_file->num_sections, asection *);
13203
400174b1
TT
13204 bfd_map_over_sections (dwp_file->dbfd.get (),
13205 dwarf2_locate_common_dwp_sections,
13206 dwp_file.get ());
80626a55 13207
400174b1
TT
13208 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13209 0);
80626a55 13210
400174b1
TT
13211 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13212 1);
80626a55 13213
73869dc2 13214 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
13215 if (dwp_file->cus && dwp_file->tus
13216 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
13217 {
13218 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 13219 pretty bizarre. We use pulongest here because that's the established
4d65956b 13220 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
13221 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13222 " TU version %s [in DWP file %s]"),
13223 pulongest (dwp_file->cus->version),
d721ba37 13224 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 13225 }
08302ed2
DE
13226
13227 if (dwp_file->cus)
13228 dwp_file->version = dwp_file->cus->version;
13229 else if (dwp_file->tus)
13230 dwp_file->version = dwp_file->tus->version;
13231 else
13232 dwp_file->version = 2;
73869dc2
DE
13233
13234 if (dwp_file->version == 2)
400174b1
TT
13235 bfd_map_over_sections (dwp_file->dbfd.get (),
13236 dwarf2_locate_v2_dwp_sections,
13237 dwp_file.get ());
73869dc2 13238
19ac8c2e
DE
13239 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13240 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 13241
b4f54984 13242 if (dwarf_read_debug)
80626a55
DE
13243 {
13244 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13245 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
13246 " %s CUs, %s TUs\n",
13247 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13248 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
13249 }
13250
13251 return dwp_file;
3019eac3 13252}
c906108c 13253
ab5088bf
DE
13254/* Wrapper around open_and_init_dwp_file, only open it once. */
13255
13256static struct dwp_file *
ed2dc618 13257get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf
DE
13258{
13259 if (! dwarf2_per_objfile->dwp_checked)
13260 {
ed2dc618
SM
13261 dwarf2_per_objfile->dwp_file
13262 = open_and_init_dwp_file (dwarf2_per_objfile);
ab5088bf
DE
13263 dwarf2_per_objfile->dwp_checked = 1;
13264 }
400174b1 13265 return dwarf2_per_objfile->dwp_file.get ();
ab5088bf
DE
13266}
13267
80626a55
DE
13268/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13269 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13270 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 13271 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
13272 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13273
13274 This is called, for example, when wanting to read a variable with a
13275 complex location. Therefore we don't want to do file i/o for every call.
13276 Therefore we don't want to look for a DWO file on every call.
13277 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13278 then we check if we've already seen DWO_NAME, and only THEN do we check
13279 for a DWO file.
13280
1c658ad5 13281 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 13282 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 13283
3019eac3 13284static struct dwo_unit *
80626a55
DE
13285lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13286 const char *dwo_name, const char *comp_dir,
13287 ULONGEST signature, int is_debug_types)
3019eac3 13288{
ed2dc618 13289 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
3019eac3 13290 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
13291 const char *kind = is_debug_types ? "TU" : "CU";
13292 void **dwo_file_slot;
3019eac3 13293 struct dwo_file *dwo_file;
80626a55 13294 struct dwp_file *dwp_file;
cb1df416 13295
6a506a2d
DE
13296 /* First see if there's a DWP file.
13297 If we have a DWP file but didn't find the DWO inside it, don't
13298 look for the original DWO file. It makes gdb behave differently
13299 depending on whether one is debugging in the build tree. */
cf2c3c16 13300
ed2dc618 13301 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 13302 if (dwp_file != NULL)
cf2c3c16 13303 {
80626a55
DE
13304 const struct dwp_hash_table *dwp_htab =
13305 is_debug_types ? dwp_file->tus : dwp_file->cus;
13306
13307 if (dwp_htab != NULL)
13308 {
13309 struct dwo_unit *dwo_cutu =
ed2dc618 13310 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 13311 signature, is_debug_types);
80626a55
DE
13312
13313 if (dwo_cutu != NULL)
13314 {
b4f54984 13315 if (dwarf_read_debug)
80626a55
DE
13316 {
13317 fprintf_unfiltered (gdb_stdlog,
13318 "Virtual DWO %s %s found: @%s\n",
13319 kind, hex_string (signature),
13320 host_address_to_string (dwo_cutu));
13321 }
13322 return dwo_cutu;
13323 }
13324 }
13325 }
6a506a2d 13326 else
80626a55 13327 {
6a506a2d 13328 /* No DWP file, look for the DWO file. */
80626a55 13329
ed2dc618
SM
13330 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13331 dwo_name, comp_dir);
6a506a2d 13332 if (*dwo_file_slot == NULL)
80626a55 13333 {
6a506a2d
DE
13334 /* Read in the file and build a table of the CUs/TUs it contains. */
13335 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 13336 }
6a506a2d 13337 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 13338 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 13339
6a506a2d 13340 if (dwo_file != NULL)
19c3d4c9 13341 {
6a506a2d
DE
13342 struct dwo_unit *dwo_cutu = NULL;
13343
13344 if (is_debug_types && dwo_file->tus)
13345 {
13346 struct dwo_unit find_dwo_cutu;
13347
13348 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13349 find_dwo_cutu.signature = signature;
9a3c8263
SM
13350 dwo_cutu
13351 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
6a506a2d 13352 }
33c5cd75 13353 else if (!is_debug_types && dwo_file->cus)
80626a55 13354 {
33c5cd75
DB
13355 struct dwo_unit find_dwo_cutu;
13356
13357 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13358 find_dwo_cutu.signature = signature;
13359 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13360 &find_dwo_cutu);
6a506a2d
DE
13361 }
13362
13363 if (dwo_cutu != NULL)
13364 {
b4f54984 13365 if (dwarf_read_debug)
6a506a2d
DE
13366 {
13367 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13368 kind, dwo_name, hex_string (signature),
13369 host_address_to_string (dwo_cutu));
13370 }
13371 return dwo_cutu;
80626a55
DE
13372 }
13373 }
2e276125 13374 }
9cdd5dbd 13375
80626a55
DE
13376 /* We didn't find it. This could mean a dwo_id mismatch, or
13377 someone deleted the DWO/DWP file, or the search path isn't set up
13378 correctly to find the file. */
13379
b4f54984 13380 if (dwarf_read_debug)
80626a55
DE
13381 {
13382 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13383 kind, dwo_name, hex_string (signature));
13384 }
3019eac3 13385
6656a72d
DE
13386 /* This is a warning and not a complaint because it can be caused by
13387 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
13388 {
13389 /* Print the name of the DWP file if we looked there, helps the user
13390 better diagnose the problem. */
791afaa2 13391 std::string dwp_text;
43942612
DE
13392
13393 if (dwp_file != NULL)
791afaa2
TT
13394 dwp_text = string_printf (" [in DWP file %s]",
13395 lbasename (dwp_file->name));
43942612 13396
9d8780f0 13397 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612
DE
13398 " [in module %s]"),
13399 kind, dwo_name, hex_string (signature),
791afaa2 13400 dwp_text.c_str (),
43942612 13401 this_unit->is_debug_types ? "TU" : "CU",
9d8780f0 13402 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
43942612 13403 }
3019eac3 13404 return NULL;
5fb290d7
DJ
13405}
13406
80626a55
DE
13407/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13408 See lookup_dwo_cutu_unit for details. */
13409
13410static struct dwo_unit *
13411lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13412 const char *dwo_name, const char *comp_dir,
13413 ULONGEST signature)
13414{
13415 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13416}
13417
13418/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13419 See lookup_dwo_cutu_unit for details. */
13420
13421static struct dwo_unit *
13422lookup_dwo_type_unit (struct signatured_type *this_tu,
13423 const char *dwo_name, const char *comp_dir)
13424{
13425 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13426}
13427
89e63ee4
DE
13428/* Traversal function for queue_and_load_all_dwo_tus. */
13429
13430static int
13431queue_and_load_dwo_tu (void **slot, void *info)
13432{
13433 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13434 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13435 ULONGEST signature = dwo_unit->signature;
13436 struct signatured_type *sig_type =
13437 lookup_dwo_signatured_type (per_cu->cu, signature);
13438
13439 if (sig_type != NULL)
13440 {
13441 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13442
13443 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13444 a real dependency of PER_CU on SIG_TYPE. That is detected later
13445 while processing PER_CU. */
13446 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13447 load_full_type_unit (sig_cu);
13448 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
13449 }
13450
13451 return 1;
13452}
13453
13454/* Queue all TUs contained in the DWO of PER_CU to be read in.
13455 The DWO may have the only definition of the type, though it may not be
13456 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13457 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13458
13459static void
13460queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13461{
13462 struct dwo_unit *dwo_unit;
13463 struct dwo_file *dwo_file;
13464
13465 gdb_assert (!per_cu->is_debug_types);
ed2dc618 13466 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
89e63ee4
DE
13467 gdb_assert (per_cu->cu != NULL);
13468
13469 dwo_unit = per_cu->cu->dwo_unit;
13470 gdb_assert (dwo_unit != NULL);
13471
13472 dwo_file = dwo_unit->dwo_file;
13473 if (dwo_file->tus != NULL)
13474 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13475}
13476
3019eac3 13477/* Read in various DIEs. */
348e048f 13478
d389af10 13479/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
13480 Inherit only the children of the DW_AT_abstract_origin DIE not being
13481 already referenced by DW_AT_abstract_origin from the children of the
13482 current DIE. */
d389af10
JK
13483
13484static void
13485inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13486{
13487 struct die_info *child_die;
791afaa2 13488 sect_offset *offsetp;
d389af10
JK
13489 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13490 struct die_info *origin_die;
13491 /* Iterator of the ORIGIN_DIE children. */
13492 struct die_info *origin_child_die;
d389af10 13493 struct attribute *attr;
cd02d79d
PA
13494 struct dwarf2_cu *origin_cu;
13495 struct pending **origin_previous_list_in_scope;
d389af10
JK
13496
13497 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13498 if (!attr)
13499 return;
13500
cd02d79d
PA
13501 /* Note that following die references may follow to a die in a
13502 different cu. */
13503
13504 origin_cu = cu;
13505 origin_die = follow_die_ref (die, attr, &origin_cu);
13506
13507 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13508 symbols in. */
13509 origin_previous_list_in_scope = origin_cu->list_in_scope;
13510 origin_cu->list_in_scope = cu->list_in_scope;
13511
edb3359d
DJ
13512 if (die->tag != origin_die->tag
13513 && !(die->tag == DW_TAG_inlined_subroutine
13514 && origin_die->tag == DW_TAG_subprogram))
b98664d3 13515 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
13516 sect_offset_str (die->sect_off),
13517 sect_offset_str (origin_die->sect_off));
d389af10 13518
791afaa2 13519 std::vector<sect_offset> offsets;
d389af10 13520
3ea89b92
PMR
13521 for (child_die = die->child;
13522 child_die && child_die->tag;
13523 child_die = sibling_die (child_die))
13524 {
13525 struct die_info *child_origin_die;
13526 struct dwarf2_cu *child_origin_cu;
13527
13528 /* We are trying to process concrete instance entries:
216f72a1 13529 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13530 it's not relevant to our analysis here. i.e. detecting DIEs that are
13531 present in the abstract instance but not referenced in the concrete
13532 one. */
216f72a1
JK
13533 if (child_die->tag == DW_TAG_call_site
13534 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
13535 continue;
13536
c38f313d
DJ
13537 /* For each CHILD_DIE, find the corresponding child of
13538 ORIGIN_DIE. If there is more than one layer of
13539 DW_AT_abstract_origin, follow them all; there shouldn't be,
13540 but GCC versions at least through 4.4 generate this (GCC PR
13541 40573). */
3ea89b92
PMR
13542 child_origin_die = child_die;
13543 child_origin_cu = cu;
c38f313d
DJ
13544 while (1)
13545 {
cd02d79d
PA
13546 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13547 child_origin_cu);
c38f313d
DJ
13548 if (attr == NULL)
13549 break;
cd02d79d
PA
13550 child_origin_die = follow_die_ref (child_origin_die, attr,
13551 &child_origin_cu);
c38f313d
DJ
13552 }
13553
d389af10
JK
13554 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13555 counterpart may exist. */
c38f313d 13556 if (child_origin_die != child_die)
d389af10 13557 {
edb3359d
DJ
13558 if (child_die->tag != child_origin_die->tag
13559 && !(child_die->tag == DW_TAG_inlined_subroutine
13560 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 13561 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13562 "different tags"),
9d8780f0
SM
13563 sect_offset_str (child_die->sect_off),
13564 sect_offset_str (child_origin_die->sect_off));
c38f313d 13565 if (child_origin_die->parent != origin_die)
b98664d3 13566 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13567 "different parents"),
9d8780f0
SM
13568 sect_offset_str (child_die->sect_off),
13569 sect_offset_str (child_origin_die->sect_off));
c38f313d 13570 else
791afaa2 13571 offsets.push_back (child_origin_die->sect_off);
d389af10 13572 }
d389af10 13573 }
791afaa2
TT
13574 std::sort (offsets.begin (), offsets.end ());
13575 sect_offset *offsets_end = offsets.data () + offsets.size ();
13576 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13577 if (offsetp[-1] == *offsetp)
b98664d3 13578 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
13579 "to DIE %s as their abstract origin"),
13580 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13581
791afaa2 13582 offsetp = offsets.data ();
d389af10
JK
13583 origin_child_die = origin_die->child;
13584 while (origin_child_die && origin_child_die->tag)
13585 {
13586 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13587 while (offsetp < offsets_end
9c541725 13588 && *offsetp < origin_child_die->sect_off)
d389af10 13589 offsetp++;
b64f50a1 13590 if (offsetp >= offsets_end
9c541725 13591 || *offsetp > origin_child_die->sect_off)
d389af10 13592 {
adde2bff
DE
13593 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13594 Check whether we're already processing ORIGIN_CHILD_DIE.
13595 This can happen with mutually referenced abstract_origins.
13596 PR 16581. */
13597 if (!origin_child_die->in_process)
13598 process_die (origin_child_die, origin_cu);
d389af10
JK
13599 }
13600 origin_child_die = sibling_die (origin_child_die);
13601 }
cd02d79d 13602 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
13603}
13604
c906108c 13605static void
e7c27a73 13606read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13607{
518817b3 13608 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13609 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 13610 struct context_stack *newobj;
c906108c
SS
13611 CORE_ADDR lowpc;
13612 CORE_ADDR highpc;
13613 struct die_info *child_die;
edb3359d 13614 struct attribute *attr, *call_line, *call_file;
15d034d0 13615 const char *name;
e142c38c 13616 CORE_ADDR baseaddr;
801e3a5b 13617 struct block *block;
edb3359d 13618 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13619 std::vector<struct symbol *> template_args;
34eaf542 13620 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13621
13622 if (inlined_func)
13623 {
13624 /* If we do not have call site information, we can't show the
13625 caller of this inlined function. That's too confusing, so
13626 only use the scope for local variables. */
13627 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13628 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13629 if (call_line == NULL || call_file == NULL)
13630 {
13631 read_lexical_block_scope (die, cu);
13632 return;
13633 }
13634 }
c906108c 13635
e142c38c
DJ
13636 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13637
94af9270 13638 name = dwarf2_name (die, cu);
c906108c 13639
e8d05480
JB
13640 /* Ignore functions with missing or empty names. These are actually
13641 illegal according to the DWARF standard. */
13642 if (name == NULL)
13643 {
b98664d3 13644 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13645 sect_offset_str (die->sect_off));
e8d05480
JB
13646 return;
13647 }
13648
13649 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13650 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13651 <= PC_BOUNDS_INVALID)
e8d05480 13652 {
ae4d0c03
PM
13653 attr = dwarf2_attr (die, DW_AT_external, cu);
13654 if (!attr || !DW_UNSND (attr))
b98664d3 13655 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13656 "for subprogram DIE at %s"),
13657 sect_offset_str (die->sect_off));
e8d05480
JB
13658 return;
13659 }
c906108c 13660
3e29f34a
MR
13661 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13662 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13663
34eaf542
TT
13664 /* If we have any template arguments, then we must allocate a
13665 different sort of symbol. */
13666 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13667 {
13668 if (child_die->tag == DW_TAG_template_type_param
13669 || child_die->tag == DW_TAG_template_value_param)
13670 {
e623cf5d 13671 templ_func = allocate_template_symbol (objfile);
cf724bc9 13672 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13673 break;
13674 }
13675 }
13676
c24bdb02 13677 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13678 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13679 (struct symbol *) templ_func);
4c2df51b 13680
81873cc8
TV
13681 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
13682 set_objfile_main_name (objfile, SYMBOL_LINKAGE_NAME (newobj->name),
13683 cu->language);
13684
4cecd739
DJ
13685 /* If there is a location expression for DW_AT_frame_base, record
13686 it. */
e142c38c 13687 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 13688 if (attr)
fe978cb0 13689 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13690
63e43d3a
PMR
13691 /* If there is a location for the static link, record it. */
13692 newobj->static_link = NULL;
13693 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13694 if (attr)
13695 {
224c3ddb
SM
13696 newobj->static_link
13697 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d
AB
13698 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
13699 dwarf2_per_cu_addr_type (cu->per_cu));
63e43d3a
PMR
13700 }
13701
c24bdb02 13702 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13703
639d11d3 13704 if (die->child != NULL)
c906108c 13705 {
639d11d3 13706 child_die = die->child;
c906108c
SS
13707 while (child_die && child_die->tag)
13708 {
34eaf542
TT
13709 if (child_die->tag == DW_TAG_template_type_param
13710 || child_die->tag == DW_TAG_template_value_param)
13711 {
13712 struct symbol *arg = new_symbol (child_die, NULL, cu);
13713
f1078f66 13714 if (arg != NULL)
2f4732b0 13715 template_args.push_back (arg);
34eaf542
TT
13716 }
13717 else
13718 process_die (child_die, cu);
c906108c
SS
13719 child_die = sibling_die (child_die);
13720 }
13721 }
13722
d389af10
JK
13723 inherit_abstract_dies (die, cu);
13724
4a811a97
UW
13725 /* If we have a DW_AT_specification, we might need to import using
13726 directives from the context of the specification DIE. See the
13727 comment in determine_prefix. */
13728 if (cu->language == language_cplus
13729 && dwarf2_attr (die, DW_AT_specification, cu))
13730 {
13731 struct dwarf2_cu *spec_cu = cu;
13732 struct die_info *spec_die = die_specification (die, &spec_cu);
13733
13734 while (spec_die)
13735 {
13736 child_die = spec_die->child;
13737 while (child_die && child_die->tag)
13738 {
13739 if (child_die->tag == DW_TAG_imported_module)
13740 process_die (child_die, spec_cu);
13741 child_die = sibling_die (child_die);
13742 }
13743
13744 /* In some cases, GCC generates specification DIEs that
13745 themselves contain DW_AT_specification attributes. */
13746 spec_die = die_specification (spec_die, &spec_cu);
13747 }
13748 }
13749
c24bdb02 13750 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13751 /* Make a block for the local symbols within. */
c24bdb02 13752 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13753 cstk.static_link, lowpc, highpc);
801e3a5b 13754
df8a16a1 13755 /* For C++, set the block's scope. */
45280282
IB
13756 if ((cu->language == language_cplus
13757 || cu->language == language_fortran
c44af4eb
TT
13758 || cu->language == language_d
13759 || cu->language == language_rust)
4d4ec4e5 13760 && cu->processing_has_namespace_info)
195a3f6c
TT
13761 block_set_scope (block, determine_prefix (die, cu),
13762 &objfile->objfile_obstack);
df8a16a1 13763
801e3a5b
JB
13764 /* If we have address ranges, record them. */
13765 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13766
a60f3166 13767 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13768
34eaf542 13769 /* Attach template arguments to function. */
2f4732b0 13770 if (!template_args.empty ())
34eaf542
TT
13771 {
13772 gdb_assert (templ_func != NULL);
13773
2f4732b0 13774 templ_func->n_template_arguments = template_args.size ();
34eaf542 13775 templ_func->template_arguments
8d749320
SM
13776 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13777 templ_func->n_template_arguments);
34eaf542 13778 memcpy (templ_func->template_arguments,
2f4732b0 13779 template_args.data (),
34eaf542 13780 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13781
13782 /* Make sure that the symtab is set on the new symbols. Even
13783 though they don't appear in this symtab directly, other parts
13784 of gdb assume that symbols do, and this is reasonably
13785 true. */
8634679f 13786 for (symbol *sym : template_args)
3e1d3d8c 13787 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13788 }
13789
208d8187
JB
13790 /* In C++, we can have functions nested inside functions (e.g., when
13791 a function declares a class that has methods). This means that
13792 when we finish processing a function scope, we may need to go
13793 back to building a containing block's symbol lists. */
c24bdb02
KS
13794 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13795 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13796
921e78cf
JB
13797 /* If we've finished processing a top-level function, subsequent
13798 symbols go in the file symbol list. */
c24bdb02
KS
13799 if (cu->get_builder ()->outermost_context_p ())
13800 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13801}
13802
13803/* Process all the DIES contained within a lexical block scope. Start
13804 a new scope, process the dies, and then close the scope. */
13805
13806static void
e7c27a73 13807read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13808{
518817b3 13809 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13810 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
13811 CORE_ADDR lowpc, highpc;
13812 struct die_info *child_die;
e142c38c
DJ
13813 CORE_ADDR baseaddr;
13814
13815 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
13816
13817 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13818 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13819 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13820 be nasty. Might be easier to properly extend generic blocks to
af34e669 13821 describe ranges. */
e385593e
JK
13822 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13823 {
13824 case PC_BOUNDS_NOT_PRESENT:
13825 /* DW_TAG_lexical_block has no attributes, process its children as if
13826 there was no wrapping by that DW_TAG_lexical_block.
13827 GCC does no longer produces such DWARF since GCC r224161. */
13828 for (child_die = die->child;
13829 child_die != NULL && child_die->tag;
13830 child_die = sibling_die (child_die))
13831 process_die (child_die, cu);
13832 return;
13833 case PC_BOUNDS_INVALID:
13834 return;
13835 }
3e29f34a
MR
13836 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13837 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13838
c24bdb02 13839 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13840 if (die->child != NULL)
c906108c 13841 {
639d11d3 13842 child_die = die->child;
c906108c
SS
13843 while (child_die && child_die->tag)
13844 {
e7c27a73 13845 process_die (child_die, cu);
c906108c
SS
13846 child_die = sibling_die (child_die);
13847 }
13848 }
3ea89b92 13849 inherit_abstract_dies (die, cu);
c24bdb02 13850 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13851
c24bdb02
KS
13852 if (*cu->get_builder ()->get_local_symbols () != NULL
13853 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13854 {
801e3a5b 13855 struct block *block
c24bdb02 13856 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13857 cstk.start_addr, highpc);
801e3a5b
JB
13858
13859 /* Note that recording ranges after traversing children, as we
13860 do here, means that recording a parent's ranges entails
13861 walking across all its children's ranges as they appear in
13862 the address map, which is quadratic behavior.
13863
13864 It would be nicer to record the parent's ranges before
13865 traversing its children, simply overriding whatever you find
13866 there. But since we don't even decide whether to create a
13867 block until after we've traversed its children, that's hard
13868 to do. */
13869 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13870 }
c24bdb02
KS
13871 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13872 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13873}
13874
216f72a1 13875/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13876
13877static void
13878read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13879{
518817b3 13880 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
96408a79
SA
13881 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13882 CORE_ADDR pc, baseaddr;
13883 struct attribute *attr;
13884 struct call_site *call_site, call_site_local;
13885 void **slot;
13886 int nparams;
13887 struct die_info *child_die;
13888
13889 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13890
216f72a1
JK
13891 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13892 if (attr == NULL)
13893 {
13894 /* This was a pre-DWARF-5 GNU extension alias
13895 for DW_AT_call_return_pc. */
13896 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13897 }
96408a79
SA
13898 if (!attr)
13899 {
b98664d3 13900 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13901 "DIE %s [in module %s]"),
13902 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13903 return;
13904 }
31aa7e4e 13905 pc = attr_value_as_address (attr) + baseaddr;
3e29f34a 13906 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13907
13908 if (cu->call_site_htab == NULL)
13909 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13910 NULL, &objfile->objfile_obstack,
13911 hashtab_obstack_allocate, NULL);
13912 call_site_local.pc = pc;
13913 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13914 if (*slot != NULL)
13915 {
b98664d3 13916 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13917 "DIE %s [in module %s]"),
13918 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13919 objfile_name (objfile));
96408a79
SA
13920 return;
13921 }
13922
13923 /* Count parameters at the caller. */
13924
13925 nparams = 0;
13926 for (child_die = die->child; child_die && child_die->tag;
13927 child_die = sibling_die (child_die))
13928 {
216f72a1
JK
13929 if (child_die->tag != DW_TAG_call_site_parameter
13930 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13931 {
b98664d3 13932 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13933 "DW_TAG_call_site child DIE %s [in module %s]"),
13934 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13935 objfile_name (objfile));
96408a79
SA
13936 continue;
13937 }
13938
13939 nparams++;
13940 }
13941
224c3ddb
SM
13942 call_site
13943 = ((struct call_site *)
13944 obstack_alloc (&objfile->objfile_obstack,
13945 sizeof (*call_site)
13946 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13947 *slot = call_site;
13948 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13949 call_site->pc = pc;
13950
216f72a1
JK
13951 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13952 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13953 {
13954 struct die_info *func_die;
13955
13956 /* Skip also over DW_TAG_inlined_subroutine. */
13957 for (func_die = die->parent;
13958 func_die && func_die->tag != DW_TAG_subprogram
13959 && func_die->tag != DW_TAG_subroutine_type;
13960 func_die = func_die->parent);
13961
216f72a1
JK
13962 /* DW_AT_call_all_calls is a superset
13963 of DW_AT_call_all_tail_calls. */
96408a79 13964 if (func_die
216f72a1 13965 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 13966 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13967 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13968 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13969 {
13970 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13971 not complete. But keep CALL_SITE for look ups via call_site_htab,
13972 both the initial caller containing the real return address PC and
13973 the final callee containing the current PC of a chain of tail
13974 calls do not need to have the tail call list complete. But any
13975 function candidate for a virtual tail call frame searched via
13976 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13977 determined unambiguously. */
13978 }
13979 else
13980 {
13981 struct type *func_type = NULL;
13982
13983 if (func_die)
13984 func_type = get_die_type (func_die, cu);
13985 if (func_type != NULL)
13986 {
13987 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13988
13989 /* Enlist this call site to the function. */
13990 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13991 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13992 }
13993 else
b98664d3 13994 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13995 "DIE %s [in module %s]"),
13996 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13997 }
13998 }
13999
216f72a1
JK
14000 attr = dwarf2_attr (die, DW_AT_call_target, cu);
14001 if (attr == NULL)
14002 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14003 if (attr == NULL)
14004 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 14005 if (attr == NULL)
216f72a1
JK
14006 {
14007 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14008 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14009 }
96408a79
SA
14010 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
14011 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
14012 /* Keep NULL DWARF_BLOCK. */;
14013 else if (attr_form_is_block (attr))
14014 {
14015 struct dwarf2_locexpr_baton *dlbaton;
14016
8d749320 14017 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
14018 dlbaton->data = DW_BLOCK (attr)->data;
14019 dlbaton->size = DW_BLOCK (attr)->size;
14020 dlbaton->per_cu = cu->per_cu;
14021
14022 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14023 }
7771576e 14024 else if (attr_form_is_ref (attr))
96408a79 14025 {
96408a79
SA
14026 struct dwarf2_cu *target_cu = cu;
14027 struct die_info *target_die;
14028
ac9ec31b 14029 target_die = follow_die_ref (die, attr, &target_cu);
518817b3 14030 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
96408a79
SA
14031 if (die_is_declaration (target_die, target_cu))
14032 {
7d45c7c3 14033 const char *target_physname;
9112db09
JK
14034
14035 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 14036 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 14037 if (target_physname == NULL)
9112db09 14038 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 14039 if (target_physname == NULL)
b98664d3 14040 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
14041 "physname, for referencing DIE %s [in module %s]"),
14042 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14043 else
7d455152 14044 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
14045 }
14046 else
14047 {
14048 CORE_ADDR lowpc;
14049
14050 /* DW_AT_entry_pc should be preferred. */
3a2b436a 14051 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 14052 <= PC_BOUNDS_INVALID)
b98664d3 14053 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
14054 "low pc, for referencing DIE %s [in module %s]"),
14055 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14056 else
3e29f34a
MR
14057 {
14058 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14059 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14060 }
96408a79
SA
14061 }
14062 }
14063 else
b98664d3 14064 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
14065 "block nor reference, for DIE %s [in module %s]"),
14066 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14067
14068 call_site->per_cu = cu->per_cu;
14069
14070 for (child_die = die->child;
14071 child_die && child_die->tag;
14072 child_die = sibling_die (child_die))
14073 {
96408a79 14074 struct call_site_parameter *parameter;
1788b2d3 14075 struct attribute *loc, *origin;
96408a79 14076
216f72a1
JK
14077 if (child_die->tag != DW_TAG_call_site_parameter
14078 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
14079 {
14080 /* Already printed the complaint above. */
14081 continue;
14082 }
14083
14084 gdb_assert (call_site->parameter_count < nparams);
14085 parameter = &call_site->parameter[call_site->parameter_count];
14086
1788b2d3
JK
14087 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14088 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 14089 register is contained in DW_AT_call_value. */
96408a79 14090
24c5c679 14091 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
14092 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14093 if (origin == NULL)
14094 {
14095 /* This was a pre-DWARF-5 GNU extension alias
14096 for DW_AT_call_parameter. */
14097 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14098 }
7771576e 14099 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3 14100 {
1788b2d3 14101 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
14102
14103 sect_offset sect_off
14104 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14105 if (!offset_in_cu_p (&cu->header, sect_off))
d76b7dbc
JK
14106 {
14107 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14108 binding can be done only inside one CU. Such referenced DIE
14109 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 14110 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
14111 "DW_TAG_call_site child DIE %s [in module %s]"),
14112 sect_offset_str (child_die->sect_off),
9c541725 14113 objfile_name (objfile));
d76b7dbc
JK
14114 continue;
14115 }
9c541725
PA
14116 parameter->u.param_cu_off
14117 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3
JK
14118 }
14119 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79 14120 {
b98664d3 14121 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
14122 "DW_TAG_call_site child DIE %s [in module %s]"),
14123 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
14124 continue;
14125 }
24c5c679 14126 else
96408a79 14127 {
24c5c679
JK
14128 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14129 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14130 if (parameter->u.dwarf_reg != -1)
14131 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14132 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14133 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14134 &parameter->u.fb_offset))
14135 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14136 else
14137 {
b98664d3 14138 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 14139 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 14140 "DW_TAG_call_site child DIE %s "
24c5c679 14141 "[in module %s]"),
9d8780f0 14142 sect_offset_str (child_die->sect_off),
9c541725 14143 objfile_name (objfile));
24c5c679
JK
14144 continue;
14145 }
96408a79
SA
14146 }
14147
216f72a1
JK
14148 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14149 if (attr == NULL)
14150 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
96408a79
SA
14151 if (!attr_form_is_block (attr))
14152 {
b98664d3 14153 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
14154 "DW_TAG_call_site child DIE %s [in module %s]"),
14155 sect_offset_str (child_die->sect_off),
9c541725 14156 objfile_name (objfile));
96408a79
SA
14157 continue;
14158 }
14159 parameter->value = DW_BLOCK (attr)->data;
14160 parameter->value_size = DW_BLOCK (attr)->size;
14161
14162 /* Parameters are not pre-cleared by memset above. */
14163 parameter->data_value = NULL;
14164 parameter->data_value_size = 0;
14165 call_site->parameter_count++;
14166
216f72a1
JK
14167 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14168 if (attr == NULL)
14169 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
96408a79
SA
14170 if (attr)
14171 {
14172 if (!attr_form_is_block (attr))
b98664d3 14173 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
14174 "DW_TAG_call_site child DIE %s [in module %s]"),
14175 sect_offset_str (child_die->sect_off),
9c541725 14176 objfile_name (objfile));
96408a79
SA
14177 else
14178 {
14179 parameter->data_value = DW_BLOCK (attr)->data;
14180 parameter->data_value_size = DW_BLOCK (attr)->size;
14181 }
14182 }
14183 }
14184}
14185
71a3c369
TT
14186/* Helper function for read_variable. If DIE represents a virtual
14187 table, then return the type of the concrete object that is
14188 associated with the virtual table. Otherwise, return NULL. */
14189
14190static struct type *
14191rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14192{
14193 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14194 if (attr == NULL)
14195 return NULL;
14196
14197 /* Find the type DIE. */
14198 struct die_info *type_die = NULL;
14199 struct dwarf2_cu *type_cu = cu;
14200
14201 if (attr_form_is_ref (attr))
14202 type_die = follow_die_ref (die, attr, &type_cu);
14203 if (type_die == NULL)
14204 return NULL;
14205
14206 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14207 return NULL;
14208 return die_containing_type (type_die, type_cu);
14209}
14210
14211/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14212
14213static void
14214read_variable (struct die_info *die, struct dwarf2_cu *cu)
14215{
14216 struct rust_vtable_symbol *storage = NULL;
14217
14218 if (cu->language == language_rust)
14219 {
14220 struct type *containing_type = rust_containing_type (die, cu);
14221
14222 if (containing_type != NULL)
14223 {
518817b3 14224 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
71a3c369
TT
14225
14226 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14227 struct rust_vtable_symbol);
14228 initialize_objfile_symbol (storage);
14229 storage->concrete_type = containing_type;
cf724bc9 14230 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
14231 }
14232 }
14233
e4a62c65
TV
14234 struct symbol *res = new_symbol (die, NULL, cu, storage);
14235 struct attribute *abstract_origin
14236 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14237 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14238 if (res == NULL && loc && abstract_origin)
14239 {
14240 /* We have a variable without a name, but with a location and an abstract
14241 origin. This may be a concrete instance of an abstract variable
14242 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14243 later. */
14244 struct dwarf2_cu *origin_cu = cu;
14245 struct die_info *origin_die
14246 = follow_die_ref (die, abstract_origin, &origin_cu);
14247 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
3360b6e7 14248 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
e4a62c65 14249 }
71a3c369
TT
14250}
14251
43988095
JK
14252/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14253 reading .debug_rnglists.
14254 Callback's type should be:
14255 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14256 Return true if the attributes are present and valid, otherwise,
14257 return false. */
14258
14259template <typename Callback>
14260static bool
14261dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14262 Callback &&callback)
14263{
ed2dc618 14264 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14265 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14266 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 14267 bfd *obfd = objfile->obfd;
43988095
JK
14268 /* Base address selection entry. */
14269 CORE_ADDR base;
14270 int found_base;
43988095 14271 const gdb_byte *buffer;
43988095
JK
14272 CORE_ADDR baseaddr;
14273 bool overflow = false;
14274
14275 found_base = cu->base_known;
14276 base = cu->base_address;
14277
14278 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14279 if (offset >= dwarf2_per_objfile->rnglists.size)
14280 {
b98664d3 14281 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
14282 offset);
14283 return false;
14284 }
14285 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14286
14287 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14288
14289 while (1)
14290 {
7814882a
JK
14291 /* Initialize it due to a false compiler warning. */
14292 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
14293 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14294 + dwarf2_per_objfile->rnglists.size);
14295 unsigned int bytes_read;
14296
14297 if (buffer == buf_end)
14298 {
14299 overflow = true;
14300 break;
14301 }
14302 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14303 switch (rlet)
14304 {
14305 case DW_RLE_end_of_list:
14306 break;
14307 case DW_RLE_base_address:
14308 if (buffer + cu->header.addr_size > buf_end)
14309 {
14310 overflow = true;
14311 break;
14312 }
14313 base = read_address (obfd, buffer, cu, &bytes_read);
14314 found_base = 1;
14315 buffer += bytes_read;
14316 break;
14317 case DW_RLE_start_length:
14318 if (buffer + cu->header.addr_size > buf_end)
14319 {
14320 overflow = true;
14321 break;
14322 }
14323 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14324 buffer += bytes_read;
14325 range_end = (range_beginning
14326 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14327 buffer += bytes_read;
14328 if (buffer > buf_end)
14329 {
14330 overflow = true;
14331 break;
14332 }
14333 break;
14334 case DW_RLE_offset_pair:
14335 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14336 buffer += bytes_read;
14337 if (buffer > buf_end)
14338 {
14339 overflow = true;
14340 break;
14341 }
14342 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14343 buffer += bytes_read;
14344 if (buffer > buf_end)
14345 {
14346 overflow = true;
14347 break;
14348 }
14349 break;
14350 case DW_RLE_start_end:
14351 if (buffer + 2 * cu->header.addr_size > buf_end)
14352 {
14353 overflow = true;
14354 break;
14355 }
14356 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14357 buffer += bytes_read;
14358 range_end = read_address (obfd, buffer, cu, &bytes_read);
14359 buffer += bytes_read;
14360 break;
14361 default:
b98664d3 14362 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14363 return false;
14364 }
14365 if (rlet == DW_RLE_end_of_list || overflow)
14366 break;
14367 if (rlet == DW_RLE_base_address)
14368 continue;
14369
14370 if (!found_base)
14371 {
14372 /* We have no valid base address for the ranges
14373 data. */
b98664d3 14374 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14375 return false;
14376 }
14377
14378 if (range_beginning > range_end)
14379 {
14380 /* Inverted range entries are invalid. */
b98664d3 14381 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
14382 return false;
14383 }
14384
14385 /* Empty range entries have no effect. */
14386 if (range_beginning == range_end)
14387 continue;
14388
14389 range_beginning += base;
14390 range_end += base;
14391
14392 /* A not-uncommon case of bad debug info.
14393 Don't pollute the addrmap with bad data. */
14394 if (range_beginning + baseaddr == 0
14395 && !dwarf2_per_objfile->has_section_at_zero)
14396 {
b98664d3 14397 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
14398 " [in module %s]"), objfile_name (objfile));
14399 continue;
14400 }
14401
14402 callback (range_beginning, range_end);
14403 }
14404
14405 if (overflow)
14406 {
b98664d3 14407 complaint (_("Offset %d is not terminated "
43988095
JK
14408 "for DW_AT_ranges attribute"),
14409 offset);
14410 return false;
14411 }
14412
14413 return true;
14414}
14415
14416/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14417 Callback's type should be:
14418 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 14419 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 14420
43988095 14421template <typename Callback>
43039443 14422static int
5f46c5a5 14423dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 14424 Callback &&callback)
43039443 14425{
ed2dc618 14426 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14427 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14428 struct objfile *objfile = dwarf2_per_objfile->objfile;
43039443
JK
14429 struct comp_unit_head *cu_header = &cu->header;
14430 bfd *obfd = objfile->obfd;
14431 unsigned int addr_size = cu_header->addr_size;
14432 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14433 /* Base address selection entry. */
14434 CORE_ADDR base;
14435 int found_base;
14436 unsigned int dummy;
d521ce57 14437 const gdb_byte *buffer;
ff013f42 14438 CORE_ADDR baseaddr;
43039443 14439
43988095
JK
14440 if (cu_header->version >= 5)
14441 return dwarf2_rnglists_process (offset, cu, callback);
14442
d00adf39
DE
14443 found_base = cu->base_known;
14444 base = cu->base_address;
43039443 14445
be391dca 14446 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 14447 if (offset >= dwarf2_per_objfile->ranges.size)
43039443 14448 {
b98664d3 14449 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
14450 offset);
14451 return 0;
14452 }
dce234bc 14453 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 14454
e7030f15 14455 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 14456
43039443
JK
14457 while (1)
14458 {
14459 CORE_ADDR range_beginning, range_end;
14460
14461 range_beginning = read_address (obfd, buffer, cu, &dummy);
14462 buffer += addr_size;
14463 range_end = read_address (obfd, buffer, cu, &dummy);
14464 buffer += addr_size;
14465 offset += 2 * addr_size;
14466
14467 /* An end of list marker is a pair of zero addresses. */
14468 if (range_beginning == 0 && range_end == 0)
14469 /* Found the end of list entry. */
14470 break;
14471
14472 /* Each base address selection entry is a pair of 2 values.
14473 The first is the largest possible address, the second is
14474 the base address. Check for a base address here. */
14475 if ((range_beginning & mask) == mask)
14476 {
28d2bfb9
AB
14477 /* If we found the largest possible address, then we already
14478 have the base address in range_end. */
14479 base = range_end;
43039443
JK
14480 found_base = 1;
14481 continue;
14482 }
14483
14484 if (!found_base)
14485 {
14486 /* We have no valid base address for the ranges
14487 data. */
b98664d3 14488 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
14489 return 0;
14490 }
14491
9277c30c
UW
14492 if (range_beginning > range_end)
14493 {
14494 /* Inverted range entries are invalid. */
b98664d3 14495 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
14496 return 0;
14497 }
14498
14499 /* Empty range entries have no effect. */
14500 if (range_beginning == range_end)
14501 continue;
14502
43039443
JK
14503 range_beginning += base;
14504 range_end += base;
14505
01093045
DE
14506 /* A not-uncommon case of bad debug info.
14507 Don't pollute the addrmap with bad data. */
14508 if (range_beginning + baseaddr == 0
14509 && !dwarf2_per_objfile->has_section_at_zero)
14510 {
b98664d3 14511 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 14512 " [in module %s]"), objfile_name (objfile));
01093045
DE
14513 continue;
14514 }
14515
5f46c5a5
JK
14516 callback (range_beginning, range_end);
14517 }
14518
14519 return 1;
14520}
14521
14522/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14523 Return 1 if the attributes are present and valid, otherwise, return 0.
14524 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14525
14526static int
14527dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14528 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14529 struct partial_symtab *ranges_pst)
14530{
518817b3 14531 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5f46c5a5
JK
14532 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14533 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14534 SECT_OFF_TEXT (objfile));
14535 int low_set = 0;
14536 CORE_ADDR low = 0;
14537 CORE_ADDR high = 0;
14538 int retval;
14539
14540 retval = dwarf2_ranges_process (offset, cu,
14541 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14542 {
9277c30c 14543 if (ranges_pst != NULL)
3e29f34a
MR
14544 {
14545 CORE_ADDR lowpc;
14546 CORE_ADDR highpc;
14547
79748972
TT
14548 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14549 range_beginning + baseaddr)
14550 - baseaddr);
14551 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14552 range_end + baseaddr)
14553 - baseaddr);
d320c2b5
TT
14554 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14555 lowpc, highpc - 1, ranges_pst);
3e29f34a 14556 }
ff013f42 14557
43039443
JK
14558 /* FIXME: This is recording everything as a low-high
14559 segment of consecutive addresses. We should have a
14560 data structure for discontiguous block ranges
14561 instead. */
14562 if (! low_set)
14563 {
14564 low = range_beginning;
14565 high = range_end;
14566 low_set = 1;
14567 }
14568 else
14569 {
14570 if (range_beginning < low)
14571 low = range_beginning;
14572 if (range_end > high)
14573 high = range_end;
14574 }
5f46c5a5
JK
14575 });
14576 if (!retval)
14577 return 0;
43039443
JK
14578
14579 if (! low_set)
14580 /* If the first entry is an end-of-list marker, the range
14581 describes an empty scope, i.e. no instructions. */
14582 return 0;
14583
14584 if (low_return)
14585 *low_return = low;
14586 if (high_return)
14587 *high_return = high;
14588 return 1;
14589}
14590
3a2b436a
JK
14591/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14592 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14593 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14594
3a2b436a 14595static enum pc_bounds_kind
af34e669 14596dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
14597 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14598 struct partial_symtab *pst)
c906108c 14599{
518817b3
SM
14600 struct dwarf2_per_objfile *dwarf2_per_objfile
14601 = cu->per_cu->dwarf2_per_objfile;
c906108c 14602 struct attribute *attr;
91da1414 14603 struct attribute *attr_high;
af34e669
DJ
14604 CORE_ADDR low = 0;
14605 CORE_ADDR high = 0;
e385593e 14606 enum pc_bounds_kind ret;
c906108c 14607
91da1414
MW
14608 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14609 if (attr_high)
af34e669 14610 {
e142c38c 14611 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 14612 if (attr)
91da1414 14613 {
31aa7e4e
JB
14614 low = attr_value_as_address (attr);
14615 high = attr_value_as_address (attr_high);
14616 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14617 high += low;
91da1414 14618 }
af34e669
DJ
14619 else
14620 /* Found high w/o low attribute. */
e385593e 14621 return PC_BOUNDS_INVALID;
af34e669
DJ
14622
14623 /* Found consecutive range of addresses. */
3a2b436a 14624 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14625 }
c906108c 14626 else
af34e669 14627 {
e142c38c 14628 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
14629 if (attr != NULL)
14630 {
ab435259
DE
14631 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14632 We take advantage of the fact that DW_AT_ranges does not appear
14633 in DW_TAG_compile_unit of DWO files. */
14634 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14635 unsigned int ranges_offset = (DW_UNSND (attr)
14636 + (need_ranges_base
14637 ? cu->ranges_base
14638 : 0));
2e3cf129 14639
af34e669 14640 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14641 .debug_ranges section. */
2e3cf129 14642 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 14643 return PC_BOUNDS_INVALID;
43039443 14644 /* Found discontinuous range of addresses. */
3a2b436a 14645 ret = PC_BOUNDS_RANGES;
af34e669 14646 }
e385593e
JK
14647 else
14648 return PC_BOUNDS_NOT_PRESENT;
af34e669 14649 }
c906108c 14650
48fbe735 14651 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14652 if (high <= low)
e385593e 14653 return PC_BOUNDS_INVALID;
c906108c
SS
14654
14655 /* When using the GNU linker, .gnu.linkonce. sections are used to
14656 eliminate duplicate copies of functions and vtables and such.
14657 The linker will arbitrarily choose one and discard the others.
14658 The AT_*_pc values for such functions refer to local labels in
14659 these sections. If the section from that file was discarded, the
14660 labels are not in the output, so the relocs get a value of 0.
14661 If this is a discarded function, mark the pc bounds as invalid,
14662 so that GDB will ignore it. */
72dca2f5 14663 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 14664 return PC_BOUNDS_INVALID;
c906108c
SS
14665
14666 *lowpc = low;
96408a79
SA
14667 if (highpc)
14668 *highpc = high;
af34e669 14669 return ret;
c906108c
SS
14670}
14671
b084d499
JB
14672/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14673 its low and high PC addresses. Do nothing if these addresses could not
14674 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14675 and HIGHPC to the high address if greater than HIGHPC. */
14676
14677static void
14678dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14679 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14680 struct dwarf2_cu *cu)
14681{
14682 CORE_ADDR low, high;
14683 struct die_info *child = die->child;
14684
e385593e 14685 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14686 {
325fac50
PA
14687 *lowpc = std::min (*lowpc, low);
14688 *highpc = std::max (*highpc, high);
b084d499
JB
14689 }
14690
14691 /* If the language does not allow nested subprograms (either inside
14692 subprograms or lexical blocks), we're done. */
14693 if (cu->language != language_ada)
14694 return;
6e70227d 14695
b084d499
JB
14696 /* Check all the children of the given DIE. If it contains nested
14697 subprograms, then check their pc bounds. Likewise, we need to
14698 check lexical blocks as well, as they may also contain subprogram
14699 definitions. */
14700 while (child && child->tag)
14701 {
14702 if (child->tag == DW_TAG_subprogram
14703 || child->tag == DW_TAG_lexical_block)
14704 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14705 child = sibling_die (child);
14706 }
14707}
14708
fae299cd
DC
14709/* Get the low and high pc's represented by the scope DIE, and store
14710 them in *LOWPC and *HIGHPC. If the correct values can't be
14711 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14712
14713static void
14714get_scope_pc_bounds (struct die_info *die,
14715 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14716 struct dwarf2_cu *cu)
14717{
14718 CORE_ADDR best_low = (CORE_ADDR) -1;
14719 CORE_ADDR best_high = (CORE_ADDR) 0;
14720 CORE_ADDR current_low, current_high;
14721
3a2b436a 14722 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14723 >= PC_BOUNDS_RANGES)
fae299cd
DC
14724 {
14725 best_low = current_low;
14726 best_high = current_high;
14727 }
14728 else
14729 {
14730 struct die_info *child = die->child;
14731
14732 while (child && child->tag)
14733 {
14734 switch (child->tag) {
14735 case DW_TAG_subprogram:
b084d499 14736 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14737 break;
14738 case DW_TAG_namespace:
f55ee35c 14739 case DW_TAG_module:
fae299cd
DC
14740 /* FIXME: carlton/2004-01-16: Should we do this for
14741 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14742 that current GCC's always emit the DIEs corresponding
14743 to definitions of methods of classes as children of a
14744 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14745 the DIEs giving the declarations, which could be
14746 anywhere). But I don't see any reason why the
14747 standards says that they have to be there. */
14748 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14749
14750 if (current_low != ((CORE_ADDR) -1))
14751 {
325fac50
PA
14752 best_low = std::min (best_low, current_low);
14753 best_high = std::max (best_high, current_high);
fae299cd
DC
14754 }
14755 break;
14756 default:
0963b4bd 14757 /* Ignore. */
fae299cd
DC
14758 break;
14759 }
14760
14761 child = sibling_die (child);
14762 }
14763 }
14764
14765 *lowpc = best_low;
14766 *highpc = best_high;
14767}
14768
801e3a5b
JB
14769/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14770 in DIE. */
380bca97 14771
801e3a5b
JB
14772static void
14773dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14774 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14775{
518817b3 14776 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 14777 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 14778 struct attribute *attr;
91da1414 14779 struct attribute *attr_high;
801e3a5b 14780
91da1414
MW
14781 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14782 if (attr_high)
801e3a5b 14783 {
801e3a5b
JB
14784 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14785 if (attr)
14786 {
31aa7e4e
JB
14787 CORE_ADDR low = attr_value_as_address (attr);
14788 CORE_ADDR high = attr_value_as_address (attr_high);
14789
14790 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14791 high += low;
9a619af0 14792
3e29f34a
MR
14793 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14794 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14795 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
14796 }
14797 }
14798
14799 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14800 if (attr)
14801 {
ab435259
DE
14802 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14803 We take advantage of the fact that DW_AT_ranges does not appear
14804 in DW_TAG_compile_unit of DWO files. */
14805 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14806
14807 /* The value of the DW_AT_ranges attribute is the offset of the
14808 address range list in the .debug_ranges section. */
ab435259
DE
14809 unsigned long offset = (DW_UNSND (attr)
14810 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14811
2d5f09ec 14812 std::vector<blockrange> blockvec;
5f46c5a5
JK
14813 dwarf2_ranges_process (offset, cu,
14814 [&] (CORE_ADDR start, CORE_ADDR end)
14815 {
58fdfd2c
JK
14816 start += baseaddr;
14817 end += baseaddr;
5f46c5a5
JK
14818 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14819 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14820 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14821 blockvec.emplace_back (start, end);
5f46c5a5 14822 });
2d5f09ec
KB
14823
14824 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14825 }
14826}
14827
685b1105
JK
14828/* Check whether the producer field indicates either of GCC < 4.6, or the
14829 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14830
685b1105
JK
14831static void
14832check_producer (struct dwarf2_cu *cu)
60d5a603 14833{
38360086 14834 int major, minor;
60d5a603
JK
14835
14836 if (cu->producer == NULL)
14837 {
14838 /* For unknown compilers expect their behavior is DWARF version
14839 compliant.
14840
14841 GCC started to support .debug_types sections by -gdwarf-4 since
14842 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14843 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14844 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14845 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14846 }
b1ffba5a 14847 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14848 {
38360086
MW
14849 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14850 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14851 }
5230b05a 14852 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14853 {
14854 cu->producer_is_icc = true;
14855 cu->producer_is_icc_lt_14 = major < 14;
14856 }
c258c396
JD
14857 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14858 cu->producer_is_codewarrior = true;
685b1105
JK
14859 else
14860 {
14861 /* For other non-GCC compilers, expect their behavior is DWARF version
14862 compliant. */
60d5a603
JK
14863 }
14864
9068261f 14865 cu->checked_producer = true;
685b1105 14866}
ba919b58 14867
685b1105
JK
14868/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14869 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14870 during 4.6.0 experimental. */
14871
9068261f 14872static bool
685b1105
JK
14873producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14874{
14875 if (!cu->checked_producer)
14876 check_producer (cu);
14877
14878 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14879}
14880
c258c396
JD
14881
14882/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14883 with incorrect is_stmt attributes. */
14884
14885static bool
14886producer_is_codewarrior (struct dwarf2_cu *cu)
14887{
14888 if (!cu->checked_producer)
14889 check_producer (cu);
14890
14891 return cu->producer_is_codewarrior;
14892}
14893
60d5a603
JK
14894/* Return the default accessibility type if it is not overriden by
14895 DW_AT_accessibility. */
14896
14897static enum dwarf_access_attribute
14898dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14899{
14900 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14901 {
14902 /* The default DWARF 2 accessibility for members is public, the default
14903 accessibility for inheritance is private. */
14904
14905 if (die->tag != DW_TAG_inheritance)
14906 return DW_ACCESS_public;
14907 else
14908 return DW_ACCESS_private;
14909 }
14910 else
14911 {
14912 /* DWARF 3+ defines the default accessibility a different way. The same
14913 rules apply now for DW_TAG_inheritance as for the members and it only
14914 depends on the container kind. */
14915
14916 if (die->parent->tag == DW_TAG_class_type)
14917 return DW_ACCESS_private;
14918 else
14919 return DW_ACCESS_public;
14920 }
14921}
14922
74ac6d43
TT
14923/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14924 offset. If the attribute was not found return 0, otherwise return
14925 1. If it was found but could not properly be handled, set *OFFSET
14926 to 0. */
14927
14928static int
14929handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14930 LONGEST *offset)
14931{
14932 struct attribute *attr;
14933
14934 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14935 if (attr != NULL)
14936 {
14937 *offset = 0;
14938
14939 /* Note that we do not check for a section offset first here.
14940 This is because DW_AT_data_member_location is new in DWARF 4,
14941 so if we see it, we can assume that a constant form is really
14942 a constant and not a section offset. */
14943 if (attr_form_is_constant (attr))
14944 *offset = dwarf2_get_attr_constant_value (attr, 0);
14945 else if (attr_form_is_section_offset (attr))
14946 dwarf2_complex_location_expr_complaint ();
14947 else if (attr_form_is_block (attr))
14948 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14949 else
14950 dwarf2_complex_location_expr_complaint ();
14951
14952 return 1;
14953 }
14954
14955 return 0;
14956}
14957
c906108c
SS
14958/* Add an aggregate field to the field list. */
14959
14960static void
107d2387 14961dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14962 struct dwarf2_cu *cu)
6e70227d 14963{
518817b3 14964 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5e2b427d 14965 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
14966 struct nextfield *new_field;
14967 struct attribute *attr;
14968 struct field *fp;
15d034d0 14969 const char *fieldname = "";
c906108c 14970
7d0ccb61
DJ
14971 if (die->tag == DW_TAG_inheritance)
14972 {
be2daae6
TT
14973 fip->baseclasses.emplace_back ();
14974 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14975 }
14976 else
14977 {
be2daae6
TT
14978 fip->fields.emplace_back ();
14979 new_field = &fip->fields.back ();
7d0ccb61 14980 }
be2daae6 14981
c906108c
SS
14982 fip->nfields++;
14983
e142c38c 14984 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
14985 if (attr)
14986 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
14987 else
14988 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
14989 if (new_field->accessibility != DW_ACCESS_public)
14990 fip->non_public_fields = 1;
60d5a603 14991
e142c38c 14992 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
14993 if (attr)
14994 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
14995 else
14996 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
14997
14998 fp = &new_field->field;
a9a9bd0f 14999
e142c38c 15000 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 15001 {
74ac6d43
TT
15002 LONGEST offset;
15003
a9a9bd0f 15004 /* Data member other than a C++ static data member. */
6e70227d 15005
c906108c 15006 /* Get type of field. */
e7c27a73 15007 fp->type = die_type (die, cu);
c906108c 15008
d6a843b5 15009 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 15010
c906108c 15011 /* Get bit size of field (zero if none). */
e142c38c 15012 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
15013 if (attr)
15014 {
15015 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
15016 }
15017 else
15018 {
15019 FIELD_BITSIZE (*fp) = 0;
15020 }
15021
15022 /* Get bit offset of field. */
74ac6d43
TT
15023 if (handle_data_member_location (die, cu, &offset))
15024 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 15025 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
15026 if (attr)
15027 {
5e2b427d 15028 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
15029 {
15030 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
15031 additional bit offset from the MSB of the containing
15032 anonymous object to the MSB of the field. We don't
15033 have to do anything special since we don't need to
15034 know the size of the anonymous object. */
f41f5e61 15035 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
15036 }
15037 else
15038 {
15039 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
15040 MSB of the anonymous object, subtract off the number of
15041 bits from the MSB of the field to the MSB of the
15042 object, and then subtract off the number of bits of
15043 the field itself. The result is the bit offset of
15044 the LSB of the field. */
c906108c
SS
15045 int anonymous_size;
15046 int bit_offset = DW_UNSND (attr);
15047
e142c38c 15048 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
15049 if (attr)
15050 {
15051 /* The size of the anonymous object containing
15052 the bit field is explicit, so use the
15053 indicated size (in bytes). */
15054 anonymous_size = DW_UNSND (attr);
15055 }
15056 else
15057 {
15058 /* The size of the anonymous object containing
15059 the bit field must be inferred from the type
15060 attribute of the data member containing the
15061 bit field. */
15062 anonymous_size = TYPE_LENGTH (fp->type);
15063 }
f41f5e61
PA
15064 SET_FIELD_BITPOS (*fp,
15065 (FIELD_BITPOS (*fp)
15066 + anonymous_size * bits_per_byte
15067 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
15068 }
15069 }
da5b30da
AA
15070 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15071 if (attr != NULL)
15072 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15073 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
15074
15075 /* Get name of field. */
39cbfefa
DJ
15076 fieldname = dwarf2_name (die, cu);
15077 if (fieldname == NULL)
15078 fieldname = "";
d8151005
DJ
15079
15080 /* The name is already allocated along with this objfile, so we don't
15081 need to duplicate it for the type. */
15082 fp->name = fieldname;
c906108c
SS
15083
15084 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 15085 pointer or virtual base class pointer) to private. */
e142c38c 15086 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 15087 {
d48cc9dd 15088 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
15089 new_field->accessibility = DW_ACCESS_private;
15090 fip->non_public_fields = 1;
15091 }
15092 }
a9a9bd0f 15093 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 15094 {
a9a9bd0f
DC
15095 /* C++ static member. */
15096
15097 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15098 is a declaration, but all versions of G++ as of this writing
15099 (so through at least 3.2.1) incorrectly generate
15100 DW_TAG_variable tags. */
6e70227d 15101
ff355380 15102 const char *physname;
c906108c 15103
a9a9bd0f 15104 /* Get name of field. */
39cbfefa
DJ
15105 fieldname = dwarf2_name (die, cu);
15106 if (fieldname == NULL)
c906108c
SS
15107 return;
15108
254e6b9e 15109 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
15110 if (attr
15111 /* Only create a symbol if this is an external value.
15112 new_symbol checks this and puts the value in the global symbol
15113 table, which we want. If it is not external, new_symbol
15114 will try to put the value in cu->list_in_scope which is wrong. */
15115 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
15116 {
15117 /* A static const member, not much different than an enum as far as
15118 we're concerned, except that we can support more types. */
15119 new_symbol (die, NULL, cu);
15120 }
15121
2df3850c 15122 /* Get physical name. */
ff355380 15123 physname = dwarf2_physname (fieldname, die, cu);
c906108c 15124
d8151005
DJ
15125 /* The name is already allocated along with this objfile, so we don't
15126 need to duplicate it for the type. */
15127 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 15128 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 15129 FIELD_NAME (*fp) = fieldname;
c906108c
SS
15130 }
15131 else if (die->tag == DW_TAG_inheritance)
15132 {
74ac6d43 15133 LONGEST offset;
d4b96c9a 15134
74ac6d43
TT
15135 /* C++ base class field. */
15136 if (handle_data_member_location (die, cu, &offset))
15137 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 15138 FIELD_BITSIZE (*fp) = 0;
e7c27a73 15139 FIELD_TYPE (*fp) = die_type (die, cu);
a737d952 15140 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
c906108c 15141 }
2ddeaf8a
TT
15142 else if (die->tag == DW_TAG_variant_part)
15143 {
15144 /* process_structure_scope will treat this DIE as a union. */
15145 process_structure_scope (die, cu);
15146
15147 /* The variant part is relative to the start of the enclosing
15148 structure. */
15149 SET_FIELD_BITPOS (*fp, 0);
15150 fp->type = get_die_type (die, cu);
15151 fp->artificial = 1;
15152 fp->name = "<<variant>>";
c8c81635
TT
15153
15154 /* Normally a DW_TAG_variant_part won't have a size, but our
15155 representation requires one, so set it to the maximum of the
15156 child sizes. */
15157 if (TYPE_LENGTH (fp->type) == 0)
15158 {
15159 unsigned max = 0;
15160 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
15161 if (TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)) > max)
15162 max = TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i));
15163 TYPE_LENGTH (fp->type) = max;
15164 }
2ddeaf8a
TT
15165 }
15166 else
15167 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
15168}
15169
883fd55a
KS
15170/* Can the type given by DIE define another type? */
15171
15172static bool
15173type_can_define_types (const struct die_info *die)
15174{
15175 switch (die->tag)
15176 {
15177 case DW_TAG_typedef:
15178 case DW_TAG_class_type:
15179 case DW_TAG_structure_type:
15180 case DW_TAG_union_type:
15181 case DW_TAG_enumeration_type:
15182 return true;
15183
15184 default:
15185 return false;
15186 }
15187}
15188
15189/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
15190
15191static void
883fd55a
KS
15192dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15193 struct dwarf2_cu *cu)
6e70227d 15194{
be2daae6
TT
15195 struct decl_field fp;
15196 memset (&fp, 0, sizeof (fp));
98751a41 15197
883fd55a 15198 gdb_assert (type_can_define_types (die));
98751a41 15199
883fd55a 15200 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
15201 fp.name = dwarf2_name (die, cu);
15202 fp.type = read_type_die (die, cu);
98751a41 15203
c191a687
KS
15204 /* Save accessibility. */
15205 enum dwarf_access_attribute accessibility;
15206 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15207 if (attr != NULL)
15208 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15209 else
15210 accessibility = dwarf2_default_access_attribute (die, cu);
15211 switch (accessibility)
15212 {
15213 case DW_ACCESS_public:
15214 /* The assumed value if neither private nor protected. */
15215 break;
15216 case DW_ACCESS_private:
be2daae6 15217 fp.is_private = 1;
c191a687
KS
15218 break;
15219 case DW_ACCESS_protected:
be2daae6 15220 fp.is_protected = 1;
c191a687
KS
15221 break;
15222 default:
b98664d3 15223 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
15224 }
15225
883fd55a 15226 if (die->tag == DW_TAG_typedef)
be2daae6 15227 fip->typedef_field_list.push_back (fp);
883fd55a 15228 else
be2daae6 15229 fip->nested_types_list.push_back (fp);
98751a41
JK
15230}
15231
c906108c
SS
15232/* Create the vector of fields, and attach it to the type. */
15233
15234static void
fba45db2 15235dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15236 struct dwarf2_cu *cu)
c906108c
SS
15237{
15238 int nfields = fip->nfields;
15239
15240 /* Record the field count, allocate space for the array of fields,
15241 and create blank accessibility bitfields if necessary. */
15242 TYPE_NFIELDS (type) = nfields;
15243 TYPE_FIELDS (type) = (struct field *)
be2daae6 15244 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
c906108c 15245
b4ba55a1 15246 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
15247 {
15248 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15249
15250 TYPE_FIELD_PRIVATE_BITS (type) =
15251 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15252 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15253
15254 TYPE_FIELD_PROTECTED_BITS (type) =
15255 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15256 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15257
774b6a14
TT
15258 TYPE_FIELD_IGNORE_BITS (type) =
15259 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15260 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
15261 }
15262
15263 /* If the type has baseclasses, allocate and clear a bit vector for
15264 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 15265 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 15266 {
be2daae6 15267 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 15268 unsigned char *pointer;
c906108c
SS
15269
15270 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 15271 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 15272 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
15273 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15274 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
15275 }
15276
2ddeaf8a
TT
15277 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15278 {
15279 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15280
be2daae6 15281 for (int index = 0; index < nfields; ++index)
2ddeaf8a 15282 {
be2daae6
TT
15283 struct nextfield &field = fip->fields[index];
15284
15285 if (field.variant.is_discriminant)
2ddeaf8a 15286 di->discriminant_index = index;
be2daae6 15287 else if (field.variant.default_branch)
2ddeaf8a
TT
15288 di->default_index = index;
15289 else
be2daae6 15290 di->discriminants[index] = field.variant.discriminant_value;
2ddeaf8a
TT
15291 }
15292 }
15293
be2daae6
TT
15294 /* Copy the saved-up fields into the field vector. */
15295 for (int i = 0; i < nfields; ++i)
c906108c 15296 {
be2daae6
TT
15297 struct nextfield &field
15298 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15299 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 15300
be2daae6
TT
15301 TYPE_FIELD (type, i) = field.field;
15302 switch (field.accessibility)
c906108c 15303 {
c5aa993b 15304 case DW_ACCESS_private:
b4ba55a1 15305 if (cu->language != language_ada)
be2daae6 15306 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 15307 break;
c906108c 15308
c5aa993b 15309 case DW_ACCESS_protected:
b4ba55a1 15310 if (cu->language != language_ada)
be2daae6 15311 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 15312 break;
c906108c 15313
c5aa993b
JM
15314 case DW_ACCESS_public:
15315 break;
c906108c 15316
c5aa993b
JM
15317 default:
15318 /* Unknown accessibility. Complain and treat it as public. */
15319 {
b98664d3 15320 complaint (_("unsupported accessibility %d"),
be2daae6 15321 field.accessibility);
c5aa993b
JM
15322 }
15323 break;
c906108c 15324 }
be2daae6 15325 if (i < fip->baseclasses.size ())
c906108c 15326 {
be2daae6 15327 switch (field.virtuality)
c906108c 15328 {
c5aa993b
JM
15329 case DW_VIRTUALITY_virtual:
15330 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15331 if (cu->language == language_ada)
a73c6dcd 15332 error (_("unexpected virtuality in component of Ada type"));
be2daae6 15333 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 15334 break;
c906108c
SS
15335 }
15336 }
c906108c
SS
15337 }
15338}
15339
7d27a96d
TT
15340/* Return true if this member function is a constructor, false
15341 otherwise. */
15342
15343static int
15344dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15345{
15346 const char *fieldname;
fe978cb0 15347 const char *type_name;
7d27a96d
TT
15348 int len;
15349
15350 if (die->parent == NULL)
15351 return 0;
15352
15353 if (die->parent->tag != DW_TAG_structure_type
15354 && die->parent->tag != DW_TAG_union_type
15355 && die->parent->tag != DW_TAG_class_type)
15356 return 0;
15357
15358 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15359 type_name = dwarf2_name (die->parent, cu);
15360 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15361 return 0;
15362
15363 len = strlen (fieldname);
fe978cb0
PA
15364 return (strncmp (fieldname, type_name, len) == 0
15365 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15366}
15367
c906108c
SS
15368/* Add a member function to the proper fieldlist. */
15369
15370static void
107d2387 15371dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15372 struct type *type, struct dwarf2_cu *cu)
c906108c 15373{
518817b3 15374 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15375 struct attribute *attr;
c906108c 15376 int i;
be2daae6 15377 struct fnfieldlist *flp = nullptr;
c906108c 15378 struct fn_field *fnp;
15d034d0 15379 const char *fieldname;
f792889a 15380 struct type *this_type;
60d5a603 15381 enum dwarf_access_attribute accessibility;
c906108c 15382
b4ba55a1 15383 if (cu->language == language_ada)
a73c6dcd 15384 error (_("unexpected member function in Ada type"));
b4ba55a1 15385
2df3850c 15386 /* Get name of member function. */
39cbfefa
DJ
15387 fieldname = dwarf2_name (die, cu);
15388 if (fieldname == NULL)
2df3850c 15389 return;
c906108c 15390
c906108c 15391 /* Look up member function name in fieldlist. */
be2daae6 15392 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15393 {
27bfe10e 15394 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15395 {
15396 flp = &fip->fnfieldlists[i];
15397 break;
15398 }
c906108c
SS
15399 }
15400
be2daae6
TT
15401 /* Create a new fnfieldlist if necessary. */
15402 if (flp == nullptr)
c906108c 15403 {
be2daae6
TT
15404 fip->fnfieldlists.emplace_back ();
15405 flp = &fip->fnfieldlists.back ();
c906108c 15406 flp->name = fieldname;
be2daae6 15407 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15408 }
15409
be2daae6
TT
15410 /* Create a new member function field and add it to the vector of
15411 fnfieldlists. */
15412 flp->fnfields.emplace_back ();
15413 fnp = &flp->fnfields.back ();
3da10d80
KS
15414
15415 /* Delay processing of the physname until later. */
9c37b5ae 15416 if (cu->language == language_cplus)
be2daae6
TT
15417 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15418 die, cu);
3da10d80
KS
15419 else
15420 {
1d06ead6 15421 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15422 fnp->physname = physname ? physname : "";
15423 }
15424
c906108c 15425 fnp->type = alloc_type (objfile);
f792889a
DJ
15426 this_type = read_type_die (die, cu);
15427 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 15428 {
f792889a 15429 int nparams = TYPE_NFIELDS (this_type);
c906108c 15430
f792889a 15431 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15432 of the method itself (TYPE_CODE_METHOD). */
15433 smash_to_method_type (fnp->type, type,
f792889a
DJ
15434 TYPE_TARGET_TYPE (this_type),
15435 TYPE_FIELDS (this_type),
15436 TYPE_NFIELDS (this_type),
15437 TYPE_VARARGS (this_type));
c906108c
SS
15438
15439 /* Handle static member functions.
c5aa993b 15440 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15441 member functions. G++ helps GDB by marking the first
15442 parameter for non-static member functions (which is the this
15443 pointer) as artificial. We obtain this information from
15444 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15445 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15446 fnp->voffset = VOFFSET_STATIC;
15447 }
15448 else
b98664d3 15449 complaint (_("member function type missing for '%s'"),
3da10d80 15450 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15451
15452 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15453 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15454 fnp->fcontext = die_containing_type (die, cu);
c906108c 15455
3e43a32a
MS
15456 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15457 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15458
15459 /* Get accessibility. */
e142c38c 15460 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 15461 if (attr)
aead7601 15462 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
15463 else
15464 accessibility = dwarf2_default_access_attribute (die, cu);
15465 switch (accessibility)
c906108c 15466 {
60d5a603
JK
15467 case DW_ACCESS_private:
15468 fnp->is_private = 1;
15469 break;
15470 case DW_ACCESS_protected:
15471 fnp->is_protected = 1;
15472 break;
c906108c
SS
15473 }
15474
b02dede2 15475 /* Check for artificial methods. */
e142c38c 15476 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15477 if (attr && DW_UNSND (attr) != 0)
15478 fnp->is_artificial = 1;
15479
7d27a96d
TT
15480 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15481
0d564a31 15482 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15483 function. For older versions of GCC, this is an offset in the
15484 appropriate virtual table, as specified by DW_AT_containing_type.
15485 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15486 to the object address. */
15487
e142c38c 15488 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 15489 if (attr)
8e19ed76 15490 {
aec5aa8b 15491 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 15492 {
aec5aa8b
TT
15493 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15494 {
15495 /* Old-style GCC. */
15496 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15497 }
15498 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15499 || (DW_BLOCK (attr)->size > 1
15500 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15501 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15502 {
aec5aa8b
TT
15503 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15504 if ((fnp->voffset % cu->header.addr_size) != 0)
15505 dwarf2_complex_location_expr_complaint ();
15506 else
15507 fnp->voffset /= cu->header.addr_size;
15508 fnp->voffset += 2;
15509 }
15510 else
15511 dwarf2_complex_location_expr_complaint ();
15512
15513 if (!fnp->fcontext)
7e993ebf
KS
15514 {
15515 /* If there is no `this' field and no DW_AT_containing_type,
15516 we cannot actually find a base class context for the
15517 vtable! */
15518 if (TYPE_NFIELDS (this_type) == 0
15519 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15520 {
b98664d3 15521 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15522 "function \"%s\" (offset %s)"),
15523 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15524 }
15525 else
15526 {
15527 fnp->fcontext
15528 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15529 }
15530 }
aec5aa8b 15531 }
3690dd37 15532 else if (attr_form_is_section_offset (attr))
8e19ed76 15533 {
4d3c2250 15534 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15535 }
15536 else
15537 {
4d3c2250
KB
15538 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15539 fieldname);
8e19ed76 15540 }
0d564a31 15541 }
d48cc9dd
DJ
15542 else
15543 {
15544 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15545 if (attr && DW_UNSND (attr))
15546 {
15547 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15548 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15549 "but the vtable offset is not specified"),
9d8780f0 15550 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15551 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15552 TYPE_CPLUS_DYNAMIC (type) = 1;
15553 }
15554 }
c906108c
SS
15555}
15556
15557/* Create the vector of member function fields, and attach it to the type. */
15558
15559static void
fba45db2 15560dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15561 struct dwarf2_cu *cu)
c906108c 15562{
b4ba55a1 15563 if (cu->language == language_ada)
a73c6dcd 15564 error (_("unexpected member functions in Ada type"));
b4ba55a1 15565
c906108c
SS
15566 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15567 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15568 TYPE_ALLOC (type,
15569 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15570
be2daae6 15571 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15572 {
be2daae6 15573 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15574 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15575
be2daae6
TT
15576 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15577 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15578 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15579 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15580
15581 for (int k = 0; k < nf.fnfields.size (); ++k)
15582 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15583 }
15584
be2daae6 15585 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15586}
15587
1168df01
JB
15588/* Returns non-zero if NAME is the name of a vtable member in CU's
15589 language, zero otherwise. */
15590static int
15591is_vtable_name (const char *name, struct dwarf2_cu *cu)
15592{
15593 static const char vptr[] = "_vptr";
15594
9c37b5ae
TT
15595 /* Look for the C++ form of the vtable. */
15596 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15597 return 1;
15598
15599 return 0;
15600}
15601
c0dd20ea 15602/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15603 functions, with the ABI-specified layout. If TYPE describes
15604 such a structure, smash it into a member function type.
61049d3b
DJ
15605
15606 GCC shouldn't do this; it should just output pointer to member DIEs.
15607 This is GCC PR debug/28767. */
c0dd20ea 15608
0b92b5bb
TT
15609static void
15610quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15611{
09e2d7c7 15612 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15613
15614 /* Check for a structure with no name and two children. */
0b92b5bb
TT
15615 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15616 return;
c0dd20ea
DJ
15617
15618 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15619 if (TYPE_FIELD_NAME (type, 0) == NULL
15620 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15621 || TYPE_FIELD_NAME (type, 1) == NULL
15622 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15623 return;
c0dd20ea
DJ
15624
15625 /* Find the type of the method. */
0b92b5bb 15626 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
15627 if (pfn_type == NULL
15628 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15629 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 15630 return;
c0dd20ea
DJ
15631
15632 /* Look for the "this" argument. */
15633 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15634 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 15635 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 15636 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 15637 return;
c0dd20ea 15638
09e2d7c7 15639 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 15640 new_type = alloc_type (objfile);
09e2d7c7 15641 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
15642 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15643 TYPE_VARARGS (pfn_type));
0b92b5bb 15644 smash_to_methodptr_type (type, new_type);
c0dd20ea 15645}
1168df01 15646
2b4424c3
TT
15647/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15648 appropriate error checking and issuing complaints if there is a
15649 problem. */
15650
15651static ULONGEST
15652get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15653{
15654 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15655
15656 if (attr == nullptr)
15657 return 0;
15658
15659 if (!attr_form_is_constant (attr))
15660 {
b98664d3 15661 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15662 " - DIE at %s [in module %s]"),
15663 sect_offset_str (die->sect_off),
15664 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15665 return 0;
15666 }
15667
15668 ULONGEST align;
15669 if (attr->form == DW_FORM_sdata)
15670 {
15671 LONGEST val = DW_SND (attr);
15672 if (val < 0)
15673 {
b98664d3 15674 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
15675 " - DIE at %s [in module %s]"),
15676 sect_offset_str (die->sect_off),
15677 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15678 return 0;
15679 }
15680 align = val;
15681 }
15682 else
15683 align = DW_UNSND (attr);
15684
15685 if (align == 0)
15686 {
b98664d3 15687 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15688 " - DIE at %s [in module %s]"),
15689 sect_offset_str (die->sect_off),
15690 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15691 return 0;
15692 }
15693 if ((align & (align - 1)) != 0)
15694 {
b98664d3 15695 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15696 " - DIE at %s [in module %s]"),
15697 sect_offset_str (die->sect_off),
15698 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15699 return 0;
15700 }
15701
15702 return align;
15703}
15704
15705/* If the DIE has a DW_AT_alignment attribute, use its value to set
15706 the alignment for TYPE. */
15707
15708static void
15709maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15710 struct type *type)
15711{
15712 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15713 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15714 " - DIE at %s [in module %s]"),
15715 sect_offset_str (die->sect_off),
15716 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15717}
685b1105 15718
c906108c 15719/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15720 (definition) to create a type for the structure or union. Fill in
15721 the type's name and general properties; the members will not be
83655187
DE
15722 processed until process_structure_scope. A symbol table entry for
15723 the type will also not be done until process_structure_scope (assuming
15724 the type has a name).
c906108c 15725
c767944b
DJ
15726 NOTE: we need to call these functions regardless of whether or not the
15727 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15728 structure or union. This gets the type entered into our set of
83655187 15729 user defined types. */
c906108c 15730
f792889a 15731static struct type *
134d01f1 15732read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15733{
518817b3 15734 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
15735 struct type *type;
15736 struct attribute *attr;
15d034d0 15737 const char *name;
c906108c 15738
348e048f
DE
15739 /* If the definition of this type lives in .debug_types, read that type.
15740 Don't follow DW_AT_specification though, that will take us back up
15741 the chain and we want to go down. */
45e58e77 15742 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
15743 if (attr)
15744 {
ac9ec31b 15745 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15746
ac9ec31b 15747 /* The type's CU may not be the same as CU.
02142a6c 15748 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15749 return set_die_type (die, type, cu);
15750 }
15751
c0dd20ea 15752 type = alloc_type (objfile);
c906108c 15753 INIT_CPLUS_SPECIFIC (type);
93311388 15754
39cbfefa
DJ
15755 name = dwarf2_name (die, cu);
15756 if (name != NULL)
c906108c 15757 {
987504bb 15758 if (cu->language == language_cplus
c44af4eb
TT
15759 || cu->language == language_d
15760 || cu->language == language_rust)
63d06c5c 15761 {
15d034d0 15762 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15763
15764 /* dwarf2_full_name might have already finished building the DIE's
15765 type. If so, there is no need to continue. */
15766 if (get_die_type (die, cu) != NULL)
15767 return get_die_type (die, cu);
15768
e86ca25f 15769 TYPE_NAME (type) = full_name;
63d06c5c
DC
15770 }
15771 else
15772 {
d8151005
DJ
15773 /* The name is already allocated along with this objfile, so
15774 we don't need to duplicate it for the type. */
e86ca25f 15775 TYPE_NAME (type) = name;
63d06c5c 15776 }
c906108c
SS
15777 }
15778
15779 if (die->tag == DW_TAG_structure_type)
15780 {
15781 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15782 }
15783 else if (die->tag == DW_TAG_union_type)
15784 {
15785 TYPE_CODE (type) = TYPE_CODE_UNION;
15786 }
2ddeaf8a
TT
15787 else if (die->tag == DW_TAG_variant_part)
15788 {
15789 TYPE_CODE (type) = TYPE_CODE_UNION;
15790 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15791 }
c906108c
SS
15792 else
15793 {
4753d33b 15794 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
15795 }
15796
0cc2414c
TT
15797 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15798 TYPE_DECLARED_CLASS (type) = 1;
15799
e142c38c 15800 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
15801 if (attr)
15802 {
155bfbd3
JB
15803 if (attr_form_is_constant (attr))
15804 TYPE_LENGTH (type) = DW_UNSND (attr);
15805 else
15806 {
15807 /* For the moment, dynamic type sizes are not supported
15808 by GDB's struct type. The actual size is determined
15809 on-demand when resolving the type of a given object,
15810 so set the type's length to zero for now. Otherwise,
15811 we record an expression as the length, and that expression
15812 could lead to a very large value, which could eventually
15813 lead to us trying to allocate that much memory when creating
15814 a value of that type. */
15815 TYPE_LENGTH (type) = 0;
15816 }
c906108c
SS
15817 }
15818 else
15819 {
15820 TYPE_LENGTH (type) = 0;
15821 }
15822
2b4424c3
TT
15823 maybe_set_alignment (cu, die, type);
15824
5230b05a 15825 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15826 {
5230b05a
WT
15827 /* ICC<14 does not output the required DW_AT_declaration on
15828 incomplete types, but gives them a size of zero. */
422b1cb0 15829 TYPE_STUB (type) = 1;
685b1105
JK
15830 }
15831 else
15832 TYPE_STUB_SUPPORTED (type) = 1;
15833
dc718098 15834 if (die_is_declaration (die, cu))
876cecd0 15835 TYPE_STUB (type) = 1;
a6c727b2
DJ
15836 else if (attr == NULL && die->child == NULL
15837 && producer_is_realview (cu->producer))
15838 /* RealView does not output the required DW_AT_declaration
15839 on incomplete types. */
15840 TYPE_STUB (type) = 1;
dc718098 15841
c906108c
SS
15842 /* We need to add the type field to the die immediately so we don't
15843 infinitely recurse when dealing with pointers to the structure
0963b4bd 15844 type within the structure itself. */
1c379e20 15845 set_die_type (die, type, cu);
c906108c 15846
7e314c57
JK
15847 /* set_die_type should be already done. */
15848 set_descriptive_type (type, die, cu);
15849
c767944b
DJ
15850 return type;
15851}
15852
2ddeaf8a
TT
15853/* A helper for process_structure_scope that handles a single member
15854 DIE. */
15855
15856static void
15857handle_struct_member_die (struct die_info *child_die, struct type *type,
15858 struct field_info *fi,
15859 std::vector<struct symbol *> *template_args,
15860 struct dwarf2_cu *cu)
15861{
15862 if (child_die->tag == DW_TAG_member
15863 || child_die->tag == DW_TAG_variable
15864 || child_die->tag == DW_TAG_variant_part)
15865 {
15866 /* NOTE: carlton/2002-11-05: A C++ static data member
15867 should be a DW_TAG_member that is a declaration, but
15868 all versions of G++ as of this writing (so through at
15869 least 3.2.1) incorrectly generate DW_TAG_variable
15870 tags for them instead. */
15871 dwarf2_add_field (fi, child_die, cu);
15872 }
15873 else if (child_die->tag == DW_TAG_subprogram)
15874 {
15875 /* Rust doesn't have member functions in the C++ sense.
15876 However, it does emit ordinary functions as children
15877 of a struct DIE. */
15878 if (cu->language == language_rust)
15879 read_func_scope (child_die, cu);
15880 else
15881 {
15882 /* C++ member function. */
15883 dwarf2_add_member_fn (fi, child_die, type, cu);
15884 }
15885 }
15886 else if (child_die->tag == DW_TAG_inheritance)
15887 {
15888 /* C++ base class field. */
15889 dwarf2_add_field (fi, child_die, cu);
15890 }
15891 else if (type_can_define_types (child_die))
15892 dwarf2_add_type_defn (fi, child_die, cu);
15893 else if (child_die->tag == DW_TAG_template_type_param
15894 || child_die->tag == DW_TAG_template_value_param)
15895 {
15896 struct symbol *arg = new_symbol (child_die, NULL, cu);
15897
15898 if (arg != NULL)
15899 template_args->push_back (arg);
15900 }
15901 else if (child_die->tag == DW_TAG_variant)
15902 {
15903 /* In a variant we want to get the discriminant and also add a
15904 field for our sole member child. */
15905 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15906
bde09ab7 15907 for (die_info *variant_child = child_die->child;
2ddeaf8a
TT
15908 variant_child != NULL;
15909 variant_child = sibling_die (variant_child))
15910 {
15911 if (variant_child->tag == DW_TAG_member)
15912 {
15913 handle_struct_member_die (variant_child, type, fi,
15914 template_args, cu);
15915 /* Only handle the one. */
15916 break;
15917 }
15918 }
15919
15920 /* We don't handle this but we might as well report it if we see
15921 it. */
15922 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
b98664d3 15923 complaint (_("DW_AT_discr_list is not supported yet"
2ddeaf8a
TT
15924 " - DIE at %s [in module %s]"),
15925 sect_offset_str (child_die->sect_off),
15926 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15927
15928 /* The first field was just added, so we can stash the
15929 discriminant there. */
be2daae6 15930 gdb_assert (!fi->fields.empty ());
2ddeaf8a 15931 if (discr == NULL)
be2daae6 15932 fi->fields.back ().variant.default_branch = true;
2ddeaf8a 15933 else
be2daae6 15934 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
2ddeaf8a
TT
15935 }
15936}
15937
c767944b
DJ
15938/* Finish creating a structure or union type, including filling in
15939 its members and creating a symbol for it. */
15940
15941static void
15942process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15943{
518817b3 15944 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
ca040673 15945 struct die_info *child_die;
c767944b
DJ
15946 struct type *type;
15947
15948 type = get_die_type (die, cu);
15949 if (type == NULL)
15950 type = read_structure_type (die, cu);
15951
2ddeaf8a
TT
15952 /* When reading a DW_TAG_variant_part, we need to notice when we
15953 read the discriminant member, so we can record it later in the
15954 discriminant_info. */
15955 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
15956 sect_offset discr_offset;
3e1d3d8c 15957 bool has_template_parameters = false;
2ddeaf8a
TT
15958
15959 if (is_variant_part)
15960 {
15961 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15962 if (discr == NULL)
15963 {
15964 /* Maybe it's a univariant form, an extension we support.
15965 In this case arrange not to check the offset. */
15966 is_variant_part = false;
15967 }
15968 else if (attr_form_is_ref (discr))
15969 {
15970 struct dwarf2_cu *target_cu = cu;
15971 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15972
15973 discr_offset = target_die->sect_off;
15974 }
15975 else
15976 {
b98664d3 15977 complaint (_("DW_AT_discr does not have DIE reference form"
2ddeaf8a
TT
15978 " - DIE at %s [in module %s]"),
15979 sect_offset_str (die->sect_off),
15980 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15981 is_variant_part = false;
15982 }
15983 }
15984
e142c38c 15985 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15986 {
15987 struct field_info fi;
2f4732b0 15988 std::vector<struct symbol *> template_args;
c906108c 15989
639d11d3 15990 child_die = die->child;
c906108c
SS
15991
15992 while (child_die && child_die->tag)
15993 {
2ddeaf8a 15994 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
34eaf542 15995
2ddeaf8a 15996 if (is_variant_part && discr_offset == child_die->sect_off)
be2daae6 15997 fi.fields.back ().variant.is_discriminant = true;
34eaf542 15998
c906108c
SS
15999 child_die = sibling_die (child_die);
16000 }
16001
34eaf542 16002 /* Attach template arguments to type. */
2f4732b0 16003 if (!template_args.empty ())
34eaf542 16004 {
3e1d3d8c 16005 has_template_parameters = true;
34eaf542 16006 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 16007 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 16008 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
16009 = XOBNEWVEC (&objfile->objfile_obstack,
16010 struct symbol *,
16011 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 16012 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 16013 template_args.data (),
34eaf542
TT
16014 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16015 * sizeof (struct symbol *)));
34eaf542
TT
16016 }
16017
c906108c
SS
16018 /* Attach fields and member functions to the type. */
16019 if (fi.nfields)
e7c27a73 16020 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 16021 if (!fi.fnfieldlists.empty ())
c906108c 16022 {
e7c27a73 16023 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 16024
c5aa993b 16025 /* Get the type which refers to the base class (possibly this
c906108c 16026 class itself) which contains the vtable pointer for the current
0d564a31
DJ
16027 class from the DW_AT_containing_type attribute. This use of
16028 DW_AT_containing_type is a GNU extension. */
c906108c 16029
e142c38c 16030 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 16031 {
e7c27a73 16032 struct type *t = die_containing_type (die, cu);
c906108c 16033
ae6ae975 16034 set_type_vptr_basetype (type, t);
c906108c
SS
16035 if (type == t)
16036 {
c906108c
SS
16037 int i;
16038
16039 /* Our own class provides vtbl ptr. */
16040 for (i = TYPE_NFIELDS (t) - 1;
16041 i >= TYPE_N_BASECLASSES (t);
16042 --i)
16043 {
0d5cff50 16044 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 16045
1168df01 16046 if (is_vtable_name (fieldname, cu))
c906108c 16047 {
ae6ae975 16048 set_type_vptr_fieldno (type, i);
c906108c
SS
16049 break;
16050 }
16051 }
16052
16053 /* Complain if virtual function table field not found. */
16054 if (i < TYPE_N_BASECLASSES (t))
b98664d3 16055 complaint (_("virtual function table pointer "
3e43a32a 16056 "not found when defining class '%s'"),
e86ca25f 16057 TYPE_NAME (type) ? TYPE_NAME (type) : "");
c906108c
SS
16058 }
16059 else
16060 {
ae6ae975 16061 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
16062 }
16063 }
f6235d4c 16064 else if (cu->producer
61012eef 16065 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
16066 {
16067 /* The IBM XLC compiler does not provide direct indication
16068 of the containing type, but the vtable pointer is
16069 always named __vfp. */
16070
16071 int i;
16072
16073 for (i = TYPE_NFIELDS (type) - 1;
16074 i >= TYPE_N_BASECLASSES (type);
16075 --i)
16076 {
16077 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16078 {
ae6ae975
DE
16079 set_type_vptr_fieldno (type, i);
16080 set_type_vptr_basetype (type, type);
f6235d4c
EZ
16081 break;
16082 }
16083 }
16084 }
c906108c 16085 }
98751a41
JK
16086
16087 /* Copy fi.typedef_field_list linked list elements content into the
16088 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 16089 if (!fi.typedef_field_list.empty ())
98751a41 16090 {
be2daae6 16091 int count = fi.typedef_field_list.size ();
98751a41 16092
a0d7a4ff 16093 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 16094 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 16095 = ((struct decl_field *)
be2daae6
TT
16096 TYPE_ALLOC (type,
16097 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16098 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 16099
be2daae6
TT
16100 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16101 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 16102 }
c767944b 16103
883fd55a
KS
16104 /* Copy fi.nested_types_list linked list elements content into the
16105 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 16106 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 16107 {
be2daae6 16108 int count = fi.nested_types_list.size ();
883fd55a
KS
16109
16110 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16111 TYPE_NESTED_TYPES_ARRAY (type)
16112 = ((struct decl_field *)
be2daae6
TT
16113 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16114 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 16115
be2daae6
TT
16116 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16117 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 16118 }
c906108c 16119 }
63d06c5c 16120
bb5ed363 16121 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
16122 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16123 cu->rust_unions.push_back (type);
0b92b5bb 16124
90aeadfc
DC
16125 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16126 snapshots) has been known to create a die giving a declaration
16127 for a class that has, as a child, a die giving a definition for a
16128 nested class. So we have to process our children even if the
16129 current die is a declaration. Normally, of course, a declaration
16130 won't have any children at all. */
134d01f1 16131
ca040673
DE
16132 child_die = die->child;
16133
90aeadfc
DC
16134 while (child_die != NULL && child_die->tag)
16135 {
16136 if (child_die->tag == DW_TAG_member
16137 || child_die->tag == DW_TAG_variable
34eaf542
TT
16138 || child_die->tag == DW_TAG_inheritance
16139 || child_die->tag == DW_TAG_template_value_param
16140 || child_die->tag == DW_TAG_template_type_param)
134d01f1 16141 {
90aeadfc 16142 /* Do nothing. */
134d01f1 16143 }
90aeadfc
DC
16144 else
16145 process_die (child_die, cu);
134d01f1 16146
90aeadfc 16147 child_die = sibling_die (child_die);
134d01f1
DJ
16148 }
16149
fa4028e9
JB
16150 /* Do not consider external references. According to the DWARF standard,
16151 these DIEs are identified by the fact that they have no byte_size
16152 attribute, and a declaration attribute. */
16153 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16154 || !die_is_declaration (die, cu))
3e1d3d8c
TT
16155 {
16156 struct symbol *sym = new_symbol (die, type, cu);
16157
16158 if (has_template_parameters)
16159 {
a776957c
TT
16160 struct symtab *symtab;
16161 if (sym != nullptr)
16162 symtab = symbol_symtab (sym);
16163 else if (cu->line_header != nullptr)
16164 {
16165 /* Any related symtab will do. */
16166 symtab
16167 = cu->line_header->file_name_at (file_name_index (1))->symtab;
16168 }
16169 else
16170 {
16171 symtab = nullptr;
16172 complaint (_("could not find suitable "
16173 "symtab for template parameter"
16174 " - DIE at %s [in module %s]"),
16175 sect_offset_str (die->sect_off),
16176 objfile_name (objfile));
16177 }
16178
16179 if (symtab != nullptr)
16180 {
16181 /* Make sure that the symtab is set on the new symbols.
16182 Even though they don't appear in this symtab directly,
16183 other parts of gdb assume that symbols do, and this is
16184 reasonably true. */
16185 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16186 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16187 }
3e1d3d8c
TT
16188 }
16189 }
134d01f1
DJ
16190}
16191
55426c9d
JB
16192/* Assuming DIE is an enumeration type, and TYPE is its associated type,
16193 update TYPE using some information only available in DIE's children. */
16194
16195static void
16196update_enumeration_type_from_children (struct die_info *die,
16197 struct type *type,
16198 struct dwarf2_cu *cu)
16199{
60f7655a 16200 struct die_info *child_die;
55426c9d
JB
16201 int unsigned_enum = 1;
16202 int flag_enum = 1;
16203 ULONGEST mask = 0;
55426c9d 16204
8268c778 16205 auto_obstack obstack;
55426c9d 16206
60f7655a
DE
16207 for (child_die = die->child;
16208 child_die != NULL && child_die->tag;
16209 child_die = sibling_die (child_die))
55426c9d
JB
16210 {
16211 struct attribute *attr;
16212 LONGEST value;
16213 const gdb_byte *bytes;
16214 struct dwarf2_locexpr_baton *baton;
16215 const char *name;
60f7655a 16216
55426c9d
JB
16217 if (child_die->tag != DW_TAG_enumerator)
16218 continue;
16219
16220 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16221 if (attr == NULL)
16222 continue;
16223
16224 name = dwarf2_name (child_die, cu);
16225 if (name == NULL)
16226 name = "<anonymous enumerator>";
16227
16228 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16229 &value, &bytes, &baton);
16230 if (value < 0)
16231 {
16232 unsigned_enum = 0;
16233 flag_enum = 0;
16234 }
16235 else if ((mask & value) != 0)
16236 flag_enum = 0;
16237 else
16238 mask |= value;
16239
16240 /* If we already know that the enum type is neither unsigned, nor
16241 a flag type, no need to look at the rest of the enumerates. */
16242 if (!unsigned_enum && !flag_enum)
16243 break;
55426c9d
JB
16244 }
16245
16246 if (unsigned_enum)
16247 TYPE_UNSIGNED (type) = 1;
16248 if (flag_enum)
16249 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16250}
16251
134d01f1
DJ
16252/* Given a DW_AT_enumeration_type die, set its type. We do not
16253 complete the type's fields yet, or create any symbols. */
c906108c 16254
f792889a 16255static struct type *
134d01f1 16256read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16257{
518817b3 16258 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16259 struct type *type;
c906108c 16260 struct attribute *attr;
0114d602 16261 const char *name;
134d01f1 16262
348e048f
DE
16263 /* If the definition of this type lives in .debug_types, read that type.
16264 Don't follow DW_AT_specification though, that will take us back up
16265 the chain and we want to go down. */
45e58e77 16266 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
16267 if (attr)
16268 {
ac9ec31b 16269 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16270
ac9ec31b 16271 /* The type's CU may not be the same as CU.
02142a6c 16272 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16273 return set_die_type (die, type, cu);
16274 }
16275
c906108c
SS
16276 type = alloc_type (objfile);
16277
16278 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 16279 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16280 if (name != NULL)
e86ca25f 16281 TYPE_NAME (type) = name;
c906108c 16282
0626fc76
TT
16283 attr = dwarf2_attr (die, DW_AT_type, cu);
16284 if (attr != NULL)
16285 {
16286 struct type *underlying_type = die_type (die, cu);
16287
16288 TYPE_TARGET_TYPE (type) = underlying_type;
16289 }
16290
e142c38c 16291 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
16292 if (attr)
16293 {
16294 TYPE_LENGTH (type) = DW_UNSND (attr);
16295 }
16296 else
16297 {
16298 TYPE_LENGTH (type) = 0;
16299 }
16300
2b4424c3
TT
16301 maybe_set_alignment (cu, die, type);
16302
137033e9
JB
16303 /* The enumeration DIE can be incomplete. In Ada, any type can be
16304 declared as private in the package spec, and then defined only
16305 inside the package body. Such types are known as Taft Amendment
16306 Types. When another package uses such a type, an incomplete DIE
16307 may be generated by the compiler. */
02eb380e 16308 if (die_is_declaration (die, cu))
876cecd0 16309 TYPE_STUB (type) = 1;
02eb380e 16310
0626fc76
TT
16311 /* Finish the creation of this type by using the enum's children.
16312 We must call this even when the underlying type has been provided
16313 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
16314 update_enumeration_type_from_children (die, type, cu);
16315
0626fc76
TT
16316 /* If this type has an underlying type that is not a stub, then we
16317 may use its attributes. We always use the "unsigned" attribute
16318 in this situation, because ordinarily we guess whether the type
16319 is unsigned -- but the guess can be wrong and the underlying type
16320 can tell us the reality. However, we defer to a local size
16321 attribute if one exists, because this lets the compiler override
16322 the underlying type if needed. */
16323 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16324 {
16325 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16326 if (TYPE_LENGTH (type) == 0)
16327 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
2b4424c3
TT
16328 if (TYPE_RAW_ALIGN (type) == 0
16329 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16330 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
0626fc76
TT
16331 }
16332
3d567982
TT
16333 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16334
f792889a 16335 return set_die_type (die, type, cu);
134d01f1
DJ
16336}
16337
16338/* Given a pointer to a die which begins an enumeration, process all
16339 the dies that define the members of the enumeration, and create the
16340 symbol for the enumeration type.
16341
16342 NOTE: We reverse the order of the element list. */
16343
16344static void
16345process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16346{
f792889a 16347 struct type *this_type;
134d01f1 16348
f792889a
DJ
16349 this_type = get_die_type (die, cu);
16350 if (this_type == NULL)
16351 this_type = read_enumeration_type (die, cu);
9dc481d3 16352
639d11d3 16353 if (die->child != NULL)
c906108c 16354 {
9dc481d3
DE
16355 struct die_info *child_die;
16356 struct symbol *sym;
16357 struct field *fields = NULL;
16358 int num_fields = 0;
15d034d0 16359 const char *name;
9dc481d3 16360
639d11d3 16361 child_die = die->child;
c906108c
SS
16362 while (child_die && child_die->tag)
16363 {
16364 if (child_die->tag != DW_TAG_enumerator)
16365 {
e7c27a73 16366 process_die (child_die, cu);
c906108c
SS
16367 }
16368 else
16369 {
39cbfefa
DJ
16370 name = dwarf2_name (child_die, cu);
16371 if (name)
c906108c 16372 {
f792889a 16373 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
16374
16375 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16376 {
16377 fields = (struct field *)
16378 xrealloc (fields,
16379 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 16380 * sizeof (struct field));
c906108c
SS
16381 }
16382
3567439c 16383 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 16384 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 16385 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
16386 FIELD_BITSIZE (fields[num_fields]) = 0;
16387
16388 num_fields++;
16389 }
16390 }
16391
16392 child_die = sibling_die (child_die);
16393 }
16394
16395 if (num_fields)
16396 {
f792889a
DJ
16397 TYPE_NFIELDS (this_type) = num_fields;
16398 TYPE_FIELDS (this_type) = (struct field *)
16399 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16400 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 16401 sizeof (struct field) * num_fields);
b8c9b27d 16402 xfree (fields);
c906108c 16403 }
c906108c 16404 }
134d01f1 16405
6c83ed52
TT
16406 /* If we are reading an enum from a .debug_types unit, and the enum
16407 is a declaration, and the enum is not the signatured type in the
16408 unit, then we do not want to add a symbol for it. Adding a
16409 symbol would in some cases obscure the true definition of the
16410 enum, giving users an incomplete type when the definition is
16411 actually available. Note that we do not want to do this for all
16412 enums which are just declarations, because C++0x allows forward
16413 enum declarations. */
3019eac3 16414 if (cu->per_cu->is_debug_types
6c83ed52
TT
16415 && die_is_declaration (die, cu))
16416 {
52dc124a 16417 struct signatured_type *sig_type;
6c83ed52 16418
c0f78cd4 16419 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16420 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16421 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16422 return;
16423 }
16424
f792889a 16425 new_symbol (die, this_type, cu);
c906108c
SS
16426}
16427
16428/* Extract all information from a DW_TAG_array_type DIE and put it in
16429 the DIE's type field. For now, this only handles one dimensional
16430 arrays. */
16431
f792889a 16432static struct type *
e7c27a73 16433read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16434{
518817b3 16435 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16436 struct die_info *child_die;
7e314c57 16437 struct type *type;
c906108c 16438 struct type *element_type, *range_type, *index_type;
c906108c 16439 struct attribute *attr;
15d034d0 16440 const char *name;
a405673c 16441 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16442 unsigned int bit_stride = 0;
c906108c 16443
e7c27a73 16444 element_type = die_type (die, cu);
c906108c 16445
7e314c57
JK
16446 /* The die_type call above may have already set the type for this DIE. */
16447 type = get_die_type (die, cu);
16448 if (type)
16449 return type;
16450
dc53a7ad
JB
16451 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16452 if (attr != NULL)
a405673c
JB
16453 {
16454 int stride_ok;
9a49df9d
AB
16455 struct type *prop_type
16456 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
a405673c
JB
16457
16458 byte_stride_prop
16459 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
16460 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16461 prop_type);
a405673c
JB
16462 if (!stride_ok)
16463 {
b98664d3 16464 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16465 " - DIE at %s [in module %s]"),
16466 sect_offset_str (die->sect_off),
518817b3 16467 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a405673c
JB
16468 /* Ignore this attribute. We will likely not be able to print
16469 arrays of this type correctly, but there is little we can do
16470 to help if we cannot read the attribute's value. */
16471 byte_stride_prop = NULL;
16472 }
16473 }
dc53a7ad
JB
16474
16475 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16476 if (attr != NULL)
16477 bit_stride = DW_UNSND (attr);
16478
c906108c
SS
16479 /* Irix 6.2 native cc creates array types without children for
16480 arrays with unspecified length. */
639d11d3 16481 if (die->child == NULL)
c906108c 16482 {
46bf5051 16483 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16484 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16485 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16486 byte_stride_prop, bit_stride);
f792889a 16487 return set_die_type (die, type, cu);
c906108c
SS
16488 }
16489
791afaa2 16490 std::vector<struct type *> range_types;
639d11d3 16491 child_die = die->child;
c906108c
SS
16492 while (child_die && child_die->tag)
16493 {
16494 if (child_die->tag == DW_TAG_subrange_type)
16495 {
f792889a 16496 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16497
f792889a 16498 if (child_type != NULL)
a02abb62 16499 {
0963b4bd
MS
16500 /* The range type was succesfully read. Save it for the
16501 array type creation. */
791afaa2 16502 range_types.push_back (child_type);
a02abb62 16503 }
c906108c
SS
16504 }
16505 child_die = sibling_die (child_die);
16506 }
16507
16508 /* Dwarf2 dimensions are output from left to right, create the
16509 necessary array types in backwards order. */
7ca2d3a3 16510
c906108c 16511 type = element_type;
7ca2d3a3
DL
16512
16513 if (read_array_order (die, cu) == DW_ORD_col_major)
16514 {
16515 int i = 0;
9a619af0 16516
791afaa2 16517 while (i < range_types.size ())
dc53a7ad 16518 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16519 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16520 }
16521 else
16522 {
791afaa2 16523 size_t ndim = range_types.size ();
7ca2d3a3 16524 while (ndim-- > 0)
dc53a7ad 16525 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16526 byte_stride_prop, bit_stride);
7ca2d3a3 16527 }
c906108c 16528
f5f8a009
EZ
16529 /* Understand Dwarf2 support for vector types (like they occur on
16530 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16531 array type. This is not part of the Dwarf2/3 standard yet, but a
16532 custom vendor extension. The main difference between a regular
16533 array and the vector variant is that vectors are passed by value
16534 to functions. */
e142c38c 16535 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 16536 if (attr)
ea37ba09 16537 make_vector_type (type);
f5f8a009 16538
dbc98a8b
KW
16539 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16540 implementation may choose to implement triple vectors using this
16541 attribute. */
16542 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16543 if (attr)
16544 {
16545 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16546 TYPE_LENGTH (type) = DW_UNSND (attr);
16547 else
b98664d3 16548 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16549 "than the total size of elements"));
dbc98a8b
KW
16550 }
16551
39cbfefa
DJ
16552 name = dwarf2_name (die, cu);
16553 if (name)
16554 TYPE_NAME (type) = name;
6e70227d 16555
2b4424c3
TT
16556 maybe_set_alignment (cu, die, type);
16557
0963b4bd 16558 /* Install the type in the die. */
7e314c57
JK
16559 set_die_type (die, type, cu);
16560
16561 /* set_die_type should be already done. */
b4ba55a1
JB
16562 set_descriptive_type (type, die, cu);
16563
7e314c57 16564 return type;
c906108c
SS
16565}
16566
7ca2d3a3 16567static enum dwarf_array_dim_ordering
6e70227d 16568read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16569{
16570 struct attribute *attr;
16571
16572 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16573
aead7601
SM
16574 if (attr)
16575 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16576
0963b4bd
MS
16577 /* GNU F77 is a special case, as at 08/2004 array type info is the
16578 opposite order to the dwarf2 specification, but data is still
16579 laid out as per normal fortran.
7ca2d3a3 16580
0963b4bd
MS
16581 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16582 version checking. */
7ca2d3a3 16583
905e0470
PM
16584 if (cu->language == language_fortran
16585 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16586 {
16587 return DW_ORD_row_major;
16588 }
16589
6e70227d 16590 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16591 {
16592 case array_column_major:
16593 return DW_ORD_col_major;
16594 case array_row_major:
16595 default:
16596 return DW_ORD_row_major;
16597 };
16598}
16599
72019c9c 16600/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16601 the DIE's type field. */
72019c9c 16602
f792889a 16603static struct type *
72019c9c
GM
16604read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16605{
7e314c57
JK
16606 struct type *domain_type, *set_type;
16607 struct attribute *attr;
f792889a 16608
7e314c57
JK
16609 domain_type = die_type (die, cu);
16610
16611 /* The die_type call above may have already set the type for this DIE. */
16612 set_type = get_die_type (die, cu);
16613 if (set_type)
16614 return set_type;
16615
16616 set_type = create_set_type (NULL, domain_type);
16617
16618 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
16619 if (attr)
16620 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16621
2b4424c3
TT
16622 maybe_set_alignment (cu, die, set_type);
16623
f792889a 16624 return set_die_type (die, set_type, cu);
72019c9c 16625}
7ca2d3a3 16626
0971de02
TT
16627/* A helper for read_common_block that creates a locexpr baton.
16628 SYM is the symbol which we are marking as computed.
16629 COMMON_DIE is the DIE for the common block.
16630 COMMON_LOC is the location expression attribute for the common
16631 block itself.
16632 MEMBER_LOC is the location expression attribute for the particular
16633 member of the common block that we are processing.
16634 CU is the CU from which the above come. */
16635
16636static void
16637mark_common_block_symbol_computed (struct symbol *sym,
16638 struct die_info *common_die,
16639 struct attribute *common_loc,
16640 struct attribute *member_loc,
16641 struct dwarf2_cu *cu)
16642{
518817b3
SM
16643 struct dwarf2_per_objfile *dwarf2_per_objfile
16644 = cu->per_cu->dwarf2_per_objfile;
0971de02
TT
16645 struct objfile *objfile = dwarf2_per_objfile->objfile;
16646 struct dwarf2_locexpr_baton *baton;
16647 gdb_byte *ptr;
16648 unsigned int cu_off;
16649 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16650 LONGEST offset = 0;
16651
16652 gdb_assert (common_loc && member_loc);
16653 gdb_assert (attr_form_is_block (common_loc));
16654 gdb_assert (attr_form_is_block (member_loc)
16655 || attr_form_is_constant (member_loc));
16656
8d749320 16657 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
16658 baton->per_cu = cu->per_cu;
16659 gdb_assert (baton->per_cu);
16660
16661 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16662
16663 if (attr_form_is_constant (member_loc))
16664 {
16665 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16666 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16667 }
16668 else
16669 baton->size += DW_BLOCK (member_loc)->size;
16670
224c3ddb 16671 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16672 baton->data = ptr;
16673
16674 *ptr++ = DW_OP_call4;
9c541725 16675 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16676 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16677 ptr += 4;
16678
16679 if (attr_form_is_constant (member_loc))
16680 {
16681 *ptr++ = DW_OP_addr;
16682 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16683 ptr += cu->header.addr_size;
16684 }
16685 else
16686 {
16687 /* We have to copy the data here, because DW_OP_call4 will only
16688 use a DW_AT_location attribute. */
16689 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16690 ptr += DW_BLOCK (member_loc)->size;
16691 }
16692
16693 *ptr++ = DW_OP_plus;
16694 gdb_assert (ptr - baton->data == baton->size);
16695
0971de02 16696 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16697 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16698}
16699
4357ac6c
TT
16700/* Create appropriate locally-scoped variables for all the
16701 DW_TAG_common_block entries. Also create a struct common_block
16702 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16703 is used to sepate the common blocks name namespace from regular
16704 variable names. */
c906108c
SS
16705
16706static void
e7c27a73 16707read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16708{
0971de02
TT
16709 struct attribute *attr;
16710
16711 attr = dwarf2_attr (die, DW_AT_location, cu);
16712 if (attr)
16713 {
16714 /* Support the .debug_loc offsets. */
16715 if (attr_form_is_block (attr))
16716 {
16717 /* Ok. */
16718 }
16719 else if (attr_form_is_section_offset (attr))
16720 {
16721 dwarf2_complex_location_expr_complaint ();
16722 attr = NULL;
16723 }
16724 else
16725 {
16726 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16727 "common block member");
16728 attr = NULL;
16729 }
16730 }
16731
639d11d3 16732 if (die->child != NULL)
c906108c 16733 {
518817b3 16734 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
4357ac6c
TT
16735 struct die_info *child_die;
16736 size_t n_entries = 0, size;
16737 struct common_block *common_block;
16738 struct symbol *sym;
74ac6d43 16739
4357ac6c
TT
16740 for (child_die = die->child;
16741 child_die && child_die->tag;
16742 child_die = sibling_die (child_die))
16743 ++n_entries;
16744
16745 size = (sizeof (struct common_block)
16746 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16747 common_block
16748 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16749 size);
4357ac6c
TT
16750 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16751 common_block->n_entries = 0;
16752
16753 for (child_die = die->child;
16754 child_die && child_die->tag;
16755 child_die = sibling_die (child_die))
16756 {
16757 /* Create the symbol in the DW_TAG_common_block block in the current
16758 symbol scope. */
e7c27a73 16759 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16760 if (sym != NULL)
16761 {
16762 struct attribute *member_loc;
16763
16764 common_block->contents[common_block->n_entries++] = sym;
16765
16766 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16767 cu);
16768 if (member_loc)
16769 {
16770 /* GDB has handled this for a long time, but it is
16771 not specified by DWARF. It seems to have been
16772 emitted by gfortran at least as recently as:
16773 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16774 complaint (_("Variable in common block has "
0971de02 16775 "DW_AT_data_member_location "
9d8780f0
SM
16776 "- DIE at %s [in module %s]"),
16777 sect_offset_str (child_die->sect_off),
518817b3 16778 objfile_name (objfile));
0971de02
TT
16779
16780 if (attr_form_is_section_offset (member_loc))
16781 dwarf2_complex_location_expr_complaint ();
16782 else if (attr_form_is_constant (member_loc)
16783 || attr_form_is_block (member_loc))
16784 {
16785 if (attr)
16786 mark_common_block_symbol_computed (sym, die, attr,
16787 member_loc, cu);
16788 }
16789 else
16790 dwarf2_complex_location_expr_complaint ();
16791 }
16792 }
c906108c 16793 }
4357ac6c
TT
16794
16795 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16796 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16797 }
16798}
16799
0114d602 16800/* Create a type for a C++ namespace. */
d9fa45fe 16801
0114d602
DJ
16802static struct type *
16803read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16804{
518817b3 16805 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16806 const char *previous_prefix, *name;
9219021c 16807 int is_anonymous;
0114d602
DJ
16808 struct type *type;
16809
16810 /* For extensions, reuse the type of the original namespace. */
16811 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16812 {
16813 struct die_info *ext_die;
16814 struct dwarf2_cu *ext_cu = cu;
9a619af0 16815
0114d602
DJ
16816 ext_die = dwarf2_extension (die, &ext_cu);
16817 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16818
16819 /* EXT_CU may not be the same as CU.
02142a6c 16820 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16821 return set_die_type (die, type, cu);
16822 }
9219021c 16823
e142c38c 16824 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16825
16826 /* Now build the name of the current namespace. */
16827
0114d602
DJ
16828 previous_prefix = determine_prefix (die, cu);
16829 if (previous_prefix[0] != '\0')
16830 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16831 previous_prefix, name, 0, cu);
0114d602
DJ
16832
16833 /* Create the type. */
19f392bc 16834 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16835
60531b24 16836 return set_die_type (die, type, cu);
0114d602
DJ
16837}
16838
22cee43f 16839/* Read a namespace scope. */
0114d602
DJ
16840
16841static void
16842read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16843{
518817b3 16844 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16845 int is_anonymous;
9219021c 16846
5c4e30ca
DC
16847 /* Add a symbol associated to this if we haven't seen the namespace
16848 before. Also, add a using directive if it's an anonymous
16849 namespace. */
9219021c 16850
f2f0e013 16851 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16852 {
16853 struct type *type;
16854
0114d602 16855 type = read_type_die (die, cu);
e7c27a73 16856 new_symbol (die, type, cu);
5c4e30ca 16857
e8e80198 16858 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16859 if (is_anonymous)
0114d602
DJ
16860 {
16861 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16862
eb1e02fd 16863 std::vector<const char *> excludes;
804d2729 16864 add_using_directive (using_directives (cu),
22cee43f 16865 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 16866 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16867 }
5c4e30ca 16868 }
9219021c 16869
639d11d3 16870 if (die->child != NULL)
d9fa45fe 16871 {
639d11d3 16872 struct die_info *child_die = die->child;
6e70227d 16873
d9fa45fe
DC
16874 while (child_die && child_die->tag)
16875 {
e7c27a73 16876 process_die (child_die, cu);
d9fa45fe
DC
16877 child_die = sibling_die (child_die);
16878 }
16879 }
38d518c9
EZ
16880}
16881
f55ee35c
JK
16882/* Read a Fortran module as type. This DIE can be only a declaration used for
16883 imported module. Still we need that type as local Fortran "use ... only"
16884 declaration imports depend on the created type in determine_prefix. */
16885
16886static struct type *
16887read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16888{
518817b3 16889 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15d034d0 16890 const char *module_name;
f55ee35c
JK
16891 struct type *type;
16892
16893 module_name = dwarf2_name (die, cu);
19f392bc 16894 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 16895
f55ee35c
JK
16896 return set_die_type (die, type, cu);
16897}
16898
5d7cb8df
JK
16899/* Read a Fortran module. */
16900
16901static void
16902read_module (struct die_info *die, struct dwarf2_cu *cu)
16903{
16904 struct die_info *child_die = die->child;
530e8392
KB
16905 struct type *type;
16906
16907 type = read_type_die (die, cu);
16908 new_symbol (die, type, cu);
5d7cb8df 16909
5d7cb8df
JK
16910 while (child_die && child_die->tag)
16911 {
16912 process_die (child_die, cu);
16913 child_die = sibling_die (child_die);
16914 }
16915}
16916
38d518c9
EZ
16917/* Return the name of the namespace represented by DIE. Set
16918 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16919 namespace. */
16920
16921static const char *
e142c38c 16922namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16923{
16924 struct die_info *current_die;
16925 const char *name = NULL;
16926
16927 /* Loop through the extensions until we find a name. */
16928
16929 for (current_die = die;
16930 current_die != NULL;
f2f0e013 16931 current_die = dwarf2_extension (die, &cu))
38d518c9 16932 {
96553a0c
DE
16933 /* We don't use dwarf2_name here so that we can detect the absence
16934 of a name -> anonymous namespace. */
7d45c7c3 16935 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16936
38d518c9
EZ
16937 if (name != NULL)
16938 break;
16939 }
16940
16941 /* Is it an anonymous namespace? */
16942
16943 *is_anonymous = (name == NULL);
16944 if (*is_anonymous)
2b1dbab0 16945 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16946
16947 return name;
d9fa45fe
DC
16948}
16949
c906108c
SS
16950/* Extract all information from a DW_TAG_pointer_type DIE and add to
16951 the user defined type vector. */
16952
f792889a 16953static struct type *
e7c27a73 16954read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16955{
518817b3
SM
16956 struct gdbarch *gdbarch
16957 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
e7c27a73 16958 struct comp_unit_head *cu_header = &cu->header;
c906108c 16959 struct type *type;
8b2dbe47
KB
16960 struct attribute *attr_byte_size;
16961 struct attribute *attr_address_class;
16962 int byte_size, addr_class;
7e314c57
JK
16963 struct type *target_type;
16964
16965 target_type = die_type (die, cu);
c906108c 16966
7e314c57
JK
16967 /* The die_type call above may have already set the type for this DIE. */
16968 type = get_die_type (die, cu);
16969 if (type)
16970 return type;
16971
16972 type = lookup_pointer_type (target_type);
8b2dbe47 16973
e142c38c 16974 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
16975 if (attr_byte_size)
16976 byte_size = DW_UNSND (attr_byte_size);
c906108c 16977 else
8b2dbe47
KB
16978 byte_size = cu_header->addr_size;
16979
e142c38c 16980 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
16981 if (attr_address_class)
16982 addr_class = DW_UNSND (attr_address_class);
16983 else
16984 addr_class = DW_ADDR_none;
16985
2b4424c3
TT
16986 ULONGEST alignment = get_alignment (cu, die);
16987
16988 /* If the pointer size, alignment, or address class is different
16989 than the default, create a type variant marked as such and set
16990 the length accordingly. */
16991 if (TYPE_LENGTH (type) != byte_size
16992 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16993 && alignment != TYPE_RAW_ALIGN (type))
16994 || addr_class != DW_ADDR_none)
c906108c 16995 {
5e2b427d 16996 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
16997 {
16998 int type_flags;
16999
849957d9 17000 type_flags = gdbarch_address_class_type_flags
5e2b427d 17001 (gdbarch, byte_size, addr_class);
876cecd0
TT
17002 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17003 == 0);
8b2dbe47
KB
17004 type = make_type_with_address_space (type, type_flags);
17005 }
17006 else if (TYPE_LENGTH (type) != byte_size)
17007 {
b98664d3 17008 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 17009 }
2b4424c3
TT
17010 else if (TYPE_RAW_ALIGN (type) != alignment)
17011 {
b98664d3 17012 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
17013 " - DIE at %s [in module %s]"),
17014 sect_offset_str (die->sect_off),
17015 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17016 }
6e70227d 17017 else
9a619af0
MS
17018 {
17019 /* Should we also complain about unhandled address classes? */
17020 }
c906108c 17021 }
8b2dbe47
KB
17022
17023 TYPE_LENGTH (type) = byte_size;
2b4424c3 17024 set_type_align (type, alignment);
f792889a 17025 return set_die_type (die, type, cu);
c906108c
SS
17026}
17027
17028/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17029 the user defined type vector. */
17030
f792889a 17031static struct type *
e7c27a73 17032read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
17033{
17034 struct type *type;
17035 struct type *to_type;
17036 struct type *domain;
17037
e7c27a73
DJ
17038 to_type = die_type (die, cu);
17039 domain = die_containing_type (die, cu);
0d5de010 17040
7e314c57
JK
17041 /* The calls above may have already set the type for this DIE. */
17042 type = get_die_type (die, cu);
17043 if (type)
17044 return type;
17045
0d5de010
DJ
17046 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
17047 type = lookup_methodptr_type (to_type);
7078baeb
TT
17048 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
17049 {
518817b3
SM
17050 struct type *new_type
17051 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
7078baeb
TT
17052
17053 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
17054 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
17055 TYPE_VARARGS (to_type));
17056 type = lookup_methodptr_type (new_type);
17057 }
0d5de010
DJ
17058 else
17059 type = lookup_memberptr_type (to_type, domain);
c906108c 17060
f792889a 17061 return set_die_type (die, type, cu);
c906108c
SS
17062}
17063
4297a3f0 17064/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
17065 the user defined type vector. */
17066
f792889a 17067static struct type *
4297a3f0
AV
17068read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
17069 enum type_code refcode)
c906108c 17070{
e7c27a73 17071 struct comp_unit_head *cu_header = &cu->header;
7e314c57 17072 struct type *type, *target_type;
c906108c
SS
17073 struct attribute *attr;
17074
4297a3f0
AV
17075 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17076
7e314c57
JK
17077 target_type = die_type (die, cu);
17078
17079 /* The die_type call above may have already set the type for this DIE. */
17080 type = get_die_type (die, cu);
17081 if (type)
17082 return type;
17083
4297a3f0 17084 type = lookup_reference_type (target_type, refcode);
e142c38c 17085 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
17086 if (attr)
17087 {
17088 TYPE_LENGTH (type) = DW_UNSND (attr);
17089 }
17090 else
17091 {
107d2387 17092 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 17093 }
2b4424c3 17094 maybe_set_alignment (cu, die, type);
f792889a 17095 return set_die_type (die, type, cu);
c906108c
SS
17096}
17097
cf363f18
MW
17098/* Add the given cv-qualifiers to the element type of the array. GCC
17099 outputs DWARF type qualifiers that apply to an array, not the
17100 element type. But GDB relies on the array element type to carry
17101 the cv-qualifiers. This mimics section 6.7.3 of the C99
17102 specification. */
17103
17104static struct type *
17105add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17106 struct type *base_type, int cnst, int voltl)
17107{
17108 struct type *el_type, *inner_array;
17109
17110 base_type = copy_type (base_type);
17111 inner_array = base_type;
17112
17113 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17114 {
17115 TYPE_TARGET_TYPE (inner_array) =
17116 copy_type (TYPE_TARGET_TYPE (inner_array));
17117 inner_array = TYPE_TARGET_TYPE (inner_array);
17118 }
17119
17120 el_type = TYPE_TARGET_TYPE (inner_array);
17121 cnst |= TYPE_CONST (el_type);
17122 voltl |= TYPE_VOLATILE (el_type);
17123 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17124
17125 return set_die_type (die, base_type, cu);
17126}
17127
f792889a 17128static struct type *
e7c27a73 17129read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17130{
f792889a 17131 struct type *base_type, *cv_type;
c906108c 17132
e7c27a73 17133 base_type = die_type (die, cu);
7e314c57
JK
17134
17135 /* The die_type call above may have already set the type for this DIE. */
17136 cv_type = get_die_type (die, cu);
17137 if (cv_type)
17138 return cv_type;
17139
2f608a3a
KW
17140 /* In case the const qualifier is applied to an array type, the element type
17141 is so qualified, not the array type (section 6.7.3 of C99). */
17142 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 17143 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 17144
f792889a
DJ
17145 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17146 return set_die_type (die, cv_type, cu);
c906108c
SS
17147}
17148
f792889a 17149static struct type *
e7c27a73 17150read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17151{
f792889a 17152 struct type *base_type, *cv_type;
c906108c 17153
e7c27a73 17154 base_type = die_type (die, cu);
7e314c57
JK
17155
17156 /* The die_type call above may have already set the type for this DIE. */
17157 cv_type = get_die_type (die, cu);
17158 if (cv_type)
17159 return cv_type;
17160
cf363f18
MW
17161 /* In case the volatile qualifier is applied to an array type, the
17162 element type is so qualified, not the array type (section 6.7.3
17163 of C99). */
17164 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17165 return add_array_cv_type (die, cu, base_type, 0, 1);
17166
f792889a
DJ
17167 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17168 return set_die_type (die, cv_type, cu);
c906108c
SS
17169}
17170
06d66ee9
TT
17171/* Handle DW_TAG_restrict_type. */
17172
17173static struct type *
17174read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17175{
17176 struct type *base_type, *cv_type;
17177
17178 base_type = die_type (die, cu);
17179
17180 /* The die_type call above may have already set the type for this DIE. */
17181 cv_type = get_die_type (die, cu);
17182 if (cv_type)
17183 return cv_type;
17184
17185 cv_type = make_restrict_type (base_type);
17186 return set_die_type (die, cv_type, cu);
17187}
17188
a2c2acaf
MW
17189/* Handle DW_TAG_atomic_type. */
17190
17191static struct type *
17192read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17193{
17194 struct type *base_type, *cv_type;
17195
17196 base_type = die_type (die, cu);
17197
17198 /* The die_type call above may have already set the type for this DIE. */
17199 cv_type = get_die_type (die, cu);
17200 if (cv_type)
17201 return cv_type;
17202
17203 cv_type = make_atomic_type (base_type);
17204 return set_die_type (die, cv_type, cu);
17205}
17206
c906108c
SS
17207/* Extract all information from a DW_TAG_string_type DIE and add to
17208 the user defined type vector. It isn't really a user defined type,
17209 but it behaves like one, with other DIE's using an AT_user_def_type
17210 attribute to reference it. */
17211
f792889a 17212static struct type *
e7c27a73 17213read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17214{
518817b3 17215 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3b7538c0 17216 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
17217 struct type *type, *range_type, *index_type, *char_type;
17218 struct attribute *attr;
17219 unsigned int length;
17220
e142c38c 17221 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
17222 if (attr)
17223 {
17224 length = DW_UNSND (attr);
17225 }
17226 else
17227 {
0963b4bd 17228 /* Check for the DW_AT_byte_size attribute. */
e142c38c 17229 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
17230 if (attr)
17231 {
17232 length = DW_UNSND (attr);
17233 }
17234 else
17235 {
17236 length = 1;
17237 }
c906108c 17238 }
6ccb9162 17239
46bf5051 17240 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 17241 range_type = create_static_range_type (NULL, index_type, 1, length);
3b7538c0
UW
17242 char_type = language_string_char_type (cu->language_defn, gdbarch);
17243 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17244
f792889a 17245 return set_die_type (die, type, cu);
c906108c
SS
17246}
17247
4d804846
JB
17248/* Assuming that DIE corresponds to a function, returns nonzero
17249 if the function is prototyped. */
17250
17251static int
17252prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17253{
17254 struct attribute *attr;
17255
17256 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17257 if (attr && (DW_UNSND (attr) != 0))
17258 return 1;
17259
17260 /* The DWARF standard implies that the DW_AT_prototyped attribute
17261 is only meaninful for C, but the concept also extends to other
17262 languages that allow unprototyped functions (Eg: Objective C).
17263 For all other languages, assume that functions are always
17264 prototyped. */
17265 if (cu->language != language_c
17266 && cu->language != language_objc
17267 && cu->language != language_opencl)
17268 return 1;
17269
17270 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17271 prototyped and unprototyped functions; default to prototyped,
17272 since that is more common in modern code (and RealView warns
17273 about unprototyped functions). */
17274 if (producer_is_realview (cu->producer))
17275 return 1;
17276
17277 return 0;
17278}
17279
c906108c
SS
17280/* Handle DIES due to C code like:
17281
17282 struct foo
c5aa993b
JM
17283 {
17284 int (*funcp)(int a, long l);
17285 int b;
17286 };
c906108c 17287
0963b4bd 17288 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17289
f792889a 17290static struct type *
e7c27a73 17291read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17292{
518817b3 17293 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0963b4bd
MS
17294 struct type *type; /* Type that this function returns. */
17295 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17296 struct attribute *attr;
17297
e7c27a73 17298 type = die_type (die, cu);
7e314c57
JK
17299
17300 /* The die_type call above may have already set the type for this DIE. */
17301 ftype = get_die_type (die, cu);
17302 if (ftype)
17303 return ftype;
17304
0c8b41f1 17305 ftype = lookup_function_type (type);
c906108c 17306
4d804846 17307 if (prototyped_function_p (die, cu))
a6c727b2 17308 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17309
c055b101
CV
17310 /* Store the calling convention in the type if it's available in
17311 the subroutine die. Otherwise set the calling convention to
17312 the default value DW_CC_normal. */
17313 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
17314 if (attr)
17315 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17316 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17317 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17318 else
17319 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17320
743649fd
MW
17321 /* Record whether the function returns normally to its caller or not
17322 if the DWARF producer set that information. */
17323 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17324 if (attr && (DW_UNSND (attr) != 0))
17325 TYPE_NO_RETURN (ftype) = 1;
17326
76c10ea2
GM
17327 /* We need to add the subroutine type to the die immediately so
17328 we don't infinitely recurse when dealing with parameters
0963b4bd 17329 declared as the same subroutine type. */
76c10ea2 17330 set_die_type (die, ftype, cu);
6e70227d 17331
639d11d3 17332 if (die->child != NULL)
c906108c 17333 {
bb5ed363 17334 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17335 struct die_info *child_die;
8072405b 17336 int nparams, iparams;
c906108c
SS
17337
17338 /* Count the number of parameters.
17339 FIXME: GDB currently ignores vararg functions, but knows about
17340 vararg member functions. */
8072405b 17341 nparams = 0;
639d11d3 17342 child_die = die->child;
c906108c
SS
17343 while (child_die && child_die->tag)
17344 {
17345 if (child_die->tag == DW_TAG_formal_parameter)
17346 nparams++;
17347 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17348 TYPE_VARARGS (ftype) = 1;
c906108c
SS
17349 child_die = sibling_die (child_die);
17350 }
17351
17352 /* Allocate storage for parameters and fill them in. */
17353 TYPE_NFIELDS (ftype) = nparams;
17354 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 17355 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 17356
8072405b
JK
17357 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17358 even if we error out during the parameters reading below. */
17359 for (iparams = 0; iparams < nparams; iparams++)
17360 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17361
17362 iparams = 0;
639d11d3 17363 child_die = die->child;
c906108c
SS
17364 while (child_die && child_die->tag)
17365 {
17366 if (child_die->tag == DW_TAG_formal_parameter)
17367 {
3ce3b1ba
PA
17368 struct type *arg_type;
17369
17370 /* DWARF version 2 has no clean way to discern C++
17371 static and non-static member functions. G++ helps
17372 GDB by marking the first parameter for non-static
17373 member functions (which is the this pointer) as
17374 artificial. We pass this information to
17375 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17376
17377 DWARF version 3 added DW_AT_object_pointer, which GCC
17378 4.5 does not yet generate. */
e142c38c 17379 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
17380 if (attr)
17381 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17382 else
9c37b5ae 17383 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17384 arg_type = die_type (child_die, cu);
17385
17386 /* RealView does not mark THIS as const, which the testsuite
17387 expects. GCC marks THIS as const in method definitions,
17388 but not in the class specifications (GCC PR 43053). */
17389 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17390 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17391 {
17392 int is_this = 0;
17393 struct dwarf2_cu *arg_cu = cu;
17394 const char *name = dwarf2_name (child_die, cu);
17395
17396 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17397 if (attr)
17398 {
17399 /* If the compiler emits this, use it. */
17400 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17401 is_this = 1;
17402 }
17403 else if (name && strcmp (name, "this") == 0)
17404 /* Function definitions will have the argument names. */
17405 is_this = 1;
17406 else if (name == NULL && iparams == 0)
17407 /* Declarations may not have the names, so like
17408 elsewhere in GDB, assume an artificial first
17409 argument is "this". */
17410 is_this = 1;
17411
17412 if (is_this)
17413 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17414 arg_type, 0);
17415 }
17416
17417 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
17418 iparams++;
17419 }
17420 child_die = sibling_die (child_die);
17421 }
17422 }
17423
76c10ea2 17424 return ftype;
c906108c
SS
17425}
17426
f792889a 17427static struct type *
e7c27a73 17428read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17429{
518817b3 17430 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 17431 const char *name = NULL;
3c8e0968 17432 struct type *this_type, *target_type;
c906108c 17433
94af9270 17434 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17435 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17436 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17437 set_die_type (die, this_type, cu);
3c8e0968
DE
17438 target_type = die_type (die, cu);
17439 if (target_type != this_type)
17440 TYPE_TARGET_TYPE (this_type) = target_type;
17441 else
17442 {
17443 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17444 spec and cause infinite loops in GDB. */
b98664d3 17445 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17446 "- DIE at %s [in module %s]"),
17447 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17448 TYPE_TARGET_TYPE (this_type) = NULL;
17449 }
f792889a 17450 return this_type;
c906108c
SS
17451}
17452
9b790ce7
UW
17453/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17454 (which may be different from NAME) to the architecture back-end to allow
17455 it to guess the correct format if necessary. */
17456
17457static struct type *
17458dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17459 const char *name_hint)
17460{
17461 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17462 const struct floatformat **format;
17463 struct type *type;
17464
17465 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17466 if (format)
17467 type = init_float_type (objfile, bits, name, format);
17468 else
77b7c781 17469 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17470
17471 return type;
17472}
17473
eb77c9df
AB
17474/* Allocate an integer type of size BITS and name NAME. */
17475
17476static struct type *
17477dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17478 int bits, int unsigned_p, const char *name)
17479{
17480 struct type *type;
17481
17482 /* Versions of Intel's C Compiler generate an integer type called "void"
17483 instead of using DW_TAG_unspecified_type. This has been seen on
17484 at least versions 14, 17, and 18. */
35ee2dc2
AB
17485 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17486 && strcmp (name, "void") == 0)
eb77c9df
AB
17487 type = objfile_type (objfile)->builtin_void;
17488 else
17489 type = init_integer_type (objfile, bits, unsigned_p, name);
17490
17491 return type;
17492}
17493
8bdc1658
AB
17494/* Initialise and return a floating point type of size BITS suitable for
17495 use as a component of a complex number. The NAME_HINT is passed through
17496 when initialising the floating point type and is the name of the complex
17497 type.
17498
17499 As DWARF doesn't currently provide an explicit name for the components
17500 of a complex number, but it can be helpful to have these components
17501 named, we try to select a suitable name based on the size of the
17502 component. */
17503static struct type *
17504dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17505 struct objfile *objfile,
17506 int bits, const char *name_hint)
17507{
17508 gdbarch *gdbarch = get_objfile_arch (objfile);
17509 struct type *tt = nullptr;
17510
35add35e
AB
17511 /* Try to find a suitable floating point builtin type of size BITS.
17512 We're going to use the name of this type as the name for the complex
17513 target type that we are about to create. */
1db455a7 17514 switch (cu->language)
8bdc1658 17515 {
1db455a7
AB
17516 case language_fortran:
17517 switch (bits)
17518 {
17519 case 32:
17520 tt = builtin_f_type (gdbarch)->builtin_real;
17521 break;
17522 case 64:
17523 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17524 break;
17525 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17526 case 128:
17527 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17528 break;
17529 }
8bdc1658 17530 break;
1db455a7
AB
17531 default:
17532 switch (bits)
17533 {
17534 case 32:
17535 tt = builtin_type (gdbarch)->builtin_float;
17536 break;
17537 case 64:
17538 tt = builtin_type (gdbarch)->builtin_double;
17539 break;
17540 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17541 case 128:
17542 tt = builtin_type (gdbarch)->builtin_long_double;
17543 break;
17544 }
8bdc1658
AB
17545 break;
17546 }
17547
35add35e
AB
17548 /* If the type we found doesn't match the size we were looking for, then
17549 pretend we didn't find a type at all, the complex target type we
17550 create will then be nameless. */
a12e5744 17551 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
17552 tt = nullptr;
17553
8bdc1658
AB
17554 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
17555 return dwarf2_init_float_type (objfile, bits, name, name_hint);
17556}
17557
c906108c
SS
17558/* Find a representation of a given base type and install
17559 it in the TYPE field of the die. */
17560
f792889a 17561static struct type *
e7c27a73 17562read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17563{
518817b3 17564 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
17565 struct type *type;
17566 struct attribute *attr;
19f392bc 17567 int encoding = 0, bits = 0;
15d034d0 17568 const char *name;
c906108c 17569
e142c38c 17570 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
17571 if (attr)
17572 {
17573 encoding = DW_UNSND (attr);
17574 }
e142c38c 17575 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
17576 if (attr)
17577 {
19f392bc 17578 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
c906108c 17579 }
39cbfefa 17580 name = dwarf2_name (die, cu);
6ccb9162 17581 if (!name)
c906108c 17582 {
b98664d3 17583 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
c906108c 17584 }
6ccb9162
UW
17585
17586 switch (encoding)
c906108c 17587 {
6ccb9162
UW
17588 case DW_ATE_address:
17589 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17590 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17591 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17592 break;
17593 case DW_ATE_boolean:
19f392bc 17594 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17595 break;
17596 case DW_ATE_complex_float:
8bdc1658 17597 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name);
19f392bc 17598 type = init_complex_type (objfile, name, type);
6ccb9162
UW
17599 break;
17600 case DW_ATE_decimal_float:
19f392bc 17601 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17602 break;
17603 case DW_ATE_float:
9b790ce7 17604 type = dwarf2_init_float_type (objfile, bits, name, name);
6ccb9162
UW
17605 break;
17606 case DW_ATE_signed:
eb77c9df 17607 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17608 break;
17609 case DW_ATE_unsigned:
3b2b8fea
TT
17610 if (cu->language == language_fortran
17611 && name
61012eef 17612 && startswith (name, "character("))
19f392bc
UW
17613 type = init_character_type (objfile, bits, 1, name);
17614 else
eb77c9df 17615 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
17616 break;
17617 case DW_ATE_signed_char:
6e70227d 17618 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17619 || cu->language == language_pascal
17620 || cu->language == language_fortran)
19f392bc
UW
17621 type = init_character_type (objfile, bits, 0, name);
17622 else
eb77c9df 17623 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17624 break;
17625 case DW_ATE_unsigned_char:
868a0084 17626 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17627 || cu->language == language_pascal
c44af4eb
TT
17628 || cu->language == language_fortran
17629 || cu->language == language_rust)
19f392bc
UW
17630 type = init_character_type (objfile, bits, 1, name);
17631 else
eb77c9df 17632 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 17633 break;
75079b2b 17634 case DW_ATE_UTF:
53e710ac
PA
17635 {
17636 gdbarch *arch = get_objfile_arch (objfile);
17637
17638 if (bits == 16)
17639 type = builtin_type (arch)->builtin_char16;
17640 else if (bits == 32)
17641 type = builtin_type (arch)->builtin_char32;
17642 else
17643 {
b98664d3 17644 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 17645 bits);
eb77c9df 17646 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
17647 }
17648 return set_die_type (die, type, cu);
17649 }
75079b2b
TT
17650 break;
17651
6ccb9162 17652 default:
b98664d3 17653 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 17654 dwarf_type_encoding_name (encoding));
77b7c781 17655 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17656 break;
c906108c 17657 }
6ccb9162 17658
0114d602 17659 if (name && strcmp (name, "char") == 0)
876cecd0 17660 TYPE_NOSIGN (type) = 1;
0114d602 17661
2b4424c3
TT
17662 maybe_set_alignment (cu, die, type);
17663
f792889a 17664 return set_die_type (die, type, cu);
c906108c
SS
17665}
17666
80180f79
SA
17667/* Parse dwarf attribute if it's a block, reference or constant and put the
17668 resulting value of the attribute into struct bound_prop.
17669 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17670
17671static int
17672attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
17673 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17674 struct type *default_type)
80180f79
SA
17675{
17676 struct dwarf2_property_baton *baton;
518817b3
SM
17677 struct obstack *obstack
17678 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
80180f79 17679
9a49df9d
AB
17680 gdb_assert (default_type != NULL);
17681
80180f79
SA
17682 if (attr == NULL || prop == NULL)
17683 return 0;
17684
17685 if (attr_form_is_block (attr))
17686 {
8d749320 17687 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17688 baton->property_type = default_type;
80180f79
SA
17689 baton->locexpr.per_cu = cu->per_cu;
17690 baton->locexpr.size = DW_BLOCK (attr)->size;
17691 baton->locexpr.data = DW_BLOCK (attr)->data;
9a49df9d 17692 baton->locexpr.is_reference = false;
80180f79
SA
17693 prop->data.baton = baton;
17694 prop->kind = PROP_LOCEXPR;
17695 gdb_assert (prop->data.baton != NULL);
17696 }
17697 else if (attr_form_is_ref (attr))
17698 {
17699 struct dwarf2_cu *target_cu = cu;
17700 struct die_info *target_die;
17701 struct attribute *target_attr;
17702
17703 target_die = follow_die_ref (die, attr, &target_cu);
17704 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17705 if (target_attr == NULL)
17706 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17707 target_cu);
80180f79
SA
17708 if (target_attr == NULL)
17709 return 0;
17710
df25ebbd 17711 switch (target_attr->name)
80180f79 17712 {
df25ebbd
JB
17713 case DW_AT_location:
17714 if (attr_form_is_section_offset (target_attr))
17715 {
8d749320 17716 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17717 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17718 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17719 prop->data.baton = baton;
17720 prop->kind = PROP_LOCLIST;
17721 gdb_assert (prop->data.baton != NULL);
17722 }
17723 else if (attr_form_is_block (target_attr))
17724 {
8d749320 17725 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17726 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17727 baton->locexpr.per_cu = cu->per_cu;
17728 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17729 baton->locexpr.data = DW_BLOCK (target_attr)->data;
9a49df9d 17730 baton->locexpr.is_reference = true;
df25ebbd
JB
17731 prop->data.baton = baton;
17732 prop->kind = PROP_LOCEXPR;
17733 gdb_assert (prop->data.baton != NULL);
17734 }
17735 else
17736 {
17737 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17738 "dynamic property");
17739 return 0;
17740 }
17741 break;
17742 case DW_AT_data_member_location:
17743 {
17744 LONGEST offset;
17745
17746 if (!handle_data_member_location (target_die, target_cu,
17747 &offset))
17748 return 0;
17749
8d749320 17750 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17751 baton->property_type = read_type_die (target_die->parent,
6ad395a7 17752 target_cu);
df25ebbd
JB
17753 baton->offset_info.offset = offset;
17754 baton->offset_info.type = die_type (target_die, target_cu);
17755 prop->data.baton = baton;
17756 prop->kind = PROP_ADDR_OFFSET;
17757 break;
17758 }
80180f79
SA
17759 }
17760 }
17761 else if (attr_form_is_constant (attr))
17762 {
17763 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17764 prop->kind = PROP_CONST;
17765 }
17766 else
17767 {
17768 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17769 dwarf2_name (die, cu));
17770 return 0;
17771 }
17772
17773 return 1;
17774}
17775
9a49df9d
AB
17776/* Find an integer type the same size as the address size given in the
17777 compilation unit header for PER_CU. UNSIGNED_P controls if the integer
17778 is unsigned or not. */
17779
17780static struct type *
17781dwarf2_per_cu_addr_sized_int_type (struct dwarf2_per_cu_data *per_cu,
17782 bool unsigned_p)
17783{
17784 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
17785 int addr_size = dwarf2_per_cu_addr_size (per_cu);
17786 struct type *int_type;
17787
17788 /* Helper macro to examine the various builtin types. */
17789#define TRY_TYPE(F) \
17790 int_type = (unsigned_p \
17791 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17792 : objfile_type (objfile)->builtin_ ## F); \
17793 if (int_type != NULL && TYPE_LENGTH (int_type) == addr_size) \
17794 return int_type
17795
17796 TRY_TYPE (char);
17797 TRY_TYPE (short);
17798 TRY_TYPE (int);
17799 TRY_TYPE (long);
17800 TRY_TYPE (long_long);
17801
17802#undef TRY_TYPE
17803
17804 gdb_assert_not_reached ("unable to find suitable integer type");
17805}
17806
b86352cf
AB
17807/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17808 present (which is valid) then compute the default type based on the
17809 compilation units address size. */
17810
17811static struct type *
17812read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17813{
17814 struct type *index_type = die_type (die, cu);
17815
17816 /* Dwarf-2 specifications explicitly allows to create subrange types
17817 without specifying a base type.
17818 In that case, the base type must be set to the type of
17819 the lower bound, upper bound or count, in that order, if any of these
17820 three attributes references an object that has a type.
17821 If no base type is found, the Dwarf-2 specifications say that
17822 a signed integer type of size equal to the size of an address should
17823 be used.
17824 For the following C code: `extern char gdb_int [];'
17825 GCC produces an empty range DIE.
17826 FIXME: muller/2010-05-28: Possible references to object for low bound,
17827 high bound or count are not yet handled by this code. */
17828 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
9a49df9d 17829 index_type = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
b86352cf
AB
17830
17831 return index_type;
17832}
17833
a02abb62
JB
17834/* Read the given DW_AT_subrange DIE. */
17835
f792889a 17836static struct type *
a02abb62
JB
17837read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17838{
4c9ad8c2 17839 struct type *base_type, *orig_base_type;
a02abb62
JB
17840 struct type *range_type;
17841 struct attribute *attr;
729efb13 17842 struct dynamic_prop low, high;
4fae6e18 17843 int low_default_is_valid;
c451ebe5 17844 int high_bound_is_count = 0;
15d034d0 17845 const char *name;
d359392f 17846 ULONGEST negative_mask;
e77813c8 17847
b86352cf
AB
17848 orig_base_type = read_subrange_index_type (die, cu);
17849
4c9ad8c2
TT
17850 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17851 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17852 creating the range type, but we use the result of check_typedef
17853 when examining properties of the type. */
17854 base_type = check_typedef (orig_base_type);
a02abb62 17855
7e314c57
JK
17856 /* The die_type call above may have already set the type for this DIE. */
17857 range_type = get_die_type (die, cu);
17858 if (range_type)
17859 return range_type;
17860
729efb13
SA
17861 low.kind = PROP_CONST;
17862 high.kind = PROP_CONST;
17863 high.data.const_val = 0;
17864
4fae6e18
JK
17865 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17866 omitting DW_AT_lower_bound. */
17867 switch (cu->language)
6e70227d 17868 {
4fae6e18
JK
17869 case language_c:
17870 case language_cplus:
729efb13 17871 low.data.const_val = 0;
4fae6e18
JK
17872 low_default_is_valid = 1;
17873 break;
17874 case language_fortran:
729efb13 17875 low.data.const_val = 1;
4fae6e18
JK
17876 low_default_is_valid = 1;
17877 break;
17878 case language_d:
4fae6e18 17879 case language_objc:
c44af4eb 17880 case language_rust:
729efb13 17881 low.data.const_val = 0;
4fae6e18
JK
17882 low_default_is_valid = (cu->header.version >= 4);
17883 break;
17884 case language_ada:
17885 case language_m2:
17886 case language_pascal:
729efb13 17887 low.data.const_val = 1;
4fae6e18
JK
17888 low_default_is_valid = (cu->header.version >= 4);
17889 break;
17890 default:
729efb13 17891 low.data.const_val = 0;
4fae6e18
JK
17892 low_default_is_valid = 0;
17893 break;
a02abb62
JB
17894 }
17895
e142c38c 17896 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 17897 if (attr)
9a49df9d 17898 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 17899 else if (!low_default_is_valid)
b98664d3 17900 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
17901 "- DIE at %s [in module %s]"),
17902 sect_offset_str (die->sect_off),
518817b3 17903 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a02abb62 17904
506f5c41
TV
17905 struct attribute *attr_ub, *attr_count;
17906 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 17907 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 17908 {
506f5c41 17909 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 17910 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 17911 {
c451ebe5
SA
17912 /* If bounds are constant do the final calculation here. */
17913 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17914 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17915 else
17916 high_bound_is_count = 1;
c2ff108b 17917 }
506f5c41
TV
17918 else
17919 {
17920 if (attr_ub != NULL)
17921 complaint (_("Unresolved DW_AT_upper_bound "
17922 "- DIE at %s [in module %s]"),
17923 sect_offset_str (die->sect_off),
17924 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17925 if (attr_count != NULL)
17926 complaint (_("Unresolved DW_AT_count "
17927 "- DIE at %s [in module %s]"),
17928 sect_offset_str (die->sect_off),
17929 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17930 }
e77813c8 17931 }
a02abb62 17932
4e962e74
TT
17933 LONGEST bias = 0;
17934 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
17935 if (bias_attr != nullptr && attr_form_is_constant (bias_attr))
17936 bias = dwarf2_get_attr_constant_value (bias_attr, 0);
17937
dbb9c2b1
JB
17938 /* Normally, the DWARF producers are expected to use a signed
17939 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17940 But this is unfortunately not always the case, as witnessed
17941 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17942 is used instead. To work around that ambiguity, we treat
17943 the bounds as signed, and thus sign-extend their values, when
17944 the base type is signed. */
6e70227d 17945 negative_mask =
d359392f 17946 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17947 if (low.kind == PROP_CONST
17948 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17949 low.data.const_val |= negative_mask;
17950 if (high.kind == PROP_CONST
17951 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17952 high.data.const_val |= negative_mask;
43bbcdc2 17953
4e962e74 17954 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 17955
c451ebe5
SA
17956 if (high_bound_is_count)
17957 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17958
c2ff108b
JK
17959 /* Ada expects an empty array on no boundary attributes. */
17960 if (attr == NULL && cu->language != language_ada)
729efb13 17961 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 17962
39cbfefa
DJ
17963 name = dwarf2_name (die, cu);
17964 if (name)
17965 TYPE_NAME (range_type) = name;
6e70227d 17966
e142c38c 17967 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
17968 if (attr)
17969 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17970
2b4424c3
TT
17971 maybe_set_alignment (cu, die, range_type);
17972
7e314c57
JK
17973 set_die_type (die, range_type, cu);
17974
17975 /* set_die_type should be already done. */
b4ba55a1
JB
17976 set_descriptive_type (range_type, die, cu);
17977
7e314c57 17978 return range_type;
a02abb62 17979}
6e70227d 17980
f792889a 17981static struct type *
81a17f79
JB
17982read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17983{
17984 struct type *type;
81a17f79 17985
518817b3
SM
17986 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17987 NULL);
0114d602 17988 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 17989
74a2f8ff
JB
17990 /* In Ada, an unspecified type is typically used when the description
17991 of the type is defered to a different unit. When encountering
17992 such a type, we treat it as a stub, and try to resolve it later on,
17993 when needed. */
17994 if (cu->language == language_ada)
17995 TYPE_STUB (type) = 1;
17996
f792889a 17997 return set_die_type (die, type, cu);
81a17f79 17998}
a02abb62 17999
639d11d3
DC
18000/* Read a single die and all its descendents. Set the die's sibling
18001 field to NULL; set other fields in the die correctly, and set all
18002 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
18003 location of the info_ptr after reading all of those dies. PARENT
18004 is the parent of the die in question. */
18005
18006static struct die_info *
dee91e82 18007read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
18008 const gdb_byte *info_ptr,
18009 const gdb_byte **new_info_ptr,
dee91e82 18010 struct die_info *parent)
639d11d3
DC
18011{
18012 struct die_info *die;
d521ce57 18013 const gdb_byte *cur_ptr;
639d11d3
DC
18014 int has_children;
18015
bf6af496 18016 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
18017 if (die == NULL)
18018 {
18019 *new_info_ptr = cur_ptr;
18020 return NULL;
18021 }
93311388 18022 store_in_ref_table (die, reader->cu);
639d11d3
DC
18023
18024 if (has_children)
bf6af496 18025 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
18026 else
18027 {
18028 die->child = NULL;
18029 *new_info_ptr = cur_ptr;
18030 }
18031
18032 die->sibling = NULL;
18033 die->parent = parent;
18034 return die;
18035}
18036
18037/* Read a die, all of its descendents, and all of its siblings; set
18038 all of the fields of all of the dies correctly. Arguments are as
18039 in read_die_and_children. */
18040
18041static struct die_info *
bf6af496 18042read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
18043 const gdb_byte *info_ptr,
18044 const gdb_byte **new_info_ptr,
bf6af496 18045 struct die_info *parent)
639d11d3
DC
18046{
18047 struct die_info *first_die, *last_sibling;
d521ce57 18048 const gdb_byte *cur_ptr;
639d11d3 18049
c906108c 18050 cur_ptr = info_ptr;
639d11d3
DC
18051 first_die = last_sibling = NULL;
18052
18053 while (1)
c906108c 18054 {
639d11d3 18055 struct die_info *die
dee91e82 18056 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 18057
1d325ec1 18058 if (die == NULL)
c906108c 18059 {
639d11d3
DC
18060 *new_info_ptr = cur_ptr;
18061 return first_die;
c906108c 18062 }
1d325ec1
DJ
18063
18064 if (!first_die)
18065 first_die = die;
c906108c 18066 else
1d325ec1
DJ
18067 last_sibling->sibling = die;
18068
18069 last_sibling = die;
c906108c 18070 }
c906108c
SS
18071}
18072
bf6af496
DE
18073/* Read a die, all of its descendents, and all of its siblings; set
18074 all of the fields of all of the dies correctly. Arguments are as
18075 in read_die_and_children.
18076 This the main entry point for reading a DIE and all its children. */
18077
18078static struct die_info *
18079read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
18080 const gdb_byte *info_ptr,
18081 const gdb_byte **new_info_ptr,
bf6af496
DE
18082 struct die_info *parent)
18083{
18084 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
18085 new_info_ptr, parent);
18086
b4f54984 18087 if (dwarf_die_debug)
bf6af496
DE
18088 {
18089 fprintf_unfiltered (gdb_stdlog,
18090 "Read die from %s@0x%x of %s:\n",
a32a8923 18091 get_section_name (reader->die_section),
bf6af496
DE
18092 (unsigned) (info_ptr - reader->die_section->buffer),
18093 bfd_get_filename (reader->abfd));
b4f54984 18094 dump_die (die, dwarf_die_debug);
bf6af496
DE
18095 }
18096
18097 return die;
18098}
18099
3019eac3
DE
18100/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18101 attributes.
18102 The caller is responsible for filling in the extra attributes
18103 and updating (*DIEP)->num_attrs.
18104 Set DIEP to point to a newly allocated die with its information,
18105 except for its child, sibling, and parent fields.
18106 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 18107
d521ce57 18108static const gdb_byte *
3019eac3 18109read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 18110 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 18111 int *has_children, int num_extra_attrs)
93311388 18112{
b64f50a1 18113 unsigned int abbrev_number, bytes_read, i;
93311388
DE
18114 struct abbrev_info *abbrev;
18115 struct die_info *die;
18116 struct dwarf2_cu *cu = reader->cu;
18117 bfd *abfd = reader->abfd;
18118
9c541725 18119 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
18120 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18121 info_ptr += bytes_read;
18122 if (!abbrev_number)
18123 {
18124 *diep = NULL;
18125 *has_children = 0;
18126 return info_ptr;
18127 }
18128
685af9cd 18129 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 18130 if (!abbrev)
348e048f
DE
18131 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18132 abbrev_number,
18133 bfd_get_filename (abfd));
18134
3019eac3 18135 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 18136 die->sect_off = sect_off;
93311388
DE
18137 die->tag = abbrev->tag;
18138 die->abbrev = abbrev_number;
18139
3019eac3
DE
18140 /* Make the result usable.
18141 The caller needs to update num_attrs after adding the extra
18142 attributes. */
93311388
DE
18143 die->num_attrs = abbrev->num_attrs;
18144
18145 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
18146 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18147 info_ptr);
93311388
DE
18148
18149 *diep = die;
18150 *has_children = abbrev->has_children;
18151 return info_ptr;
18152}
18153
3019eac3
DE
18154/* Read a die and all its attributes.
18155 Set DIEP to point to a newly allocated die with its information,
18156 except for its child, sibling, and parent fields.
18157 Set HAS_CHILDREN to tell whether the die has children or not. */
18158
d521ce57 18159static const gdb_byte *
3019eac3 18160read_full_die (const struct die_reader_specs *reader,
d521ce57 18161 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
18162 int *has_children)
18163{
d521ce57 18164 const gdb_byte *result;
bf6af496
DE
18165
18166 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
18167
b4f54984 18168 if (dwarf_die_debug)
bf6af496
DE
18169 {
18170 fprintf_unfiltered (gdb_stdlog,
18171 "Read die from %s@0x%x of %s:\n",
a32a8923 18172 get_section_name (reader->die_section),
bf6af496
DE
18173 (unsigned) (info_ptr - reader->die_section->buffer),
18174 bfd_get_filename (reader->abfd));
b4f54984 18175 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
18176 }
18177
18178 return result;
3019eac3 18179}
433df2d4
DE
18180\f
18181/* Abbreviation tables.
3019eac3 18182
433df2d4 18183 In DWARF version 2, the description of the debugging information is
c906108c
SS
18184 stored in a separate .debug_abbrev section. Before we read any
18185 dies from a section we read in all abbreviations and install them
433df2d4
DE
18186 in a hash table. */
18187
18188/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
18189
685af9cd
TT
18190struct abbrev_info *
18191abbrev_table::alloc_abbrev ()
433df2d4
DE
18192{
18193 struct abbrev_info *abbrev;
18194
685af9cd 18195 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
433df2d4 18196 memset (abbrev, 0, sizeof (struct abbrev_info));
8d749320 18197
433df2d4
DE
18198 return abbrev;
18199}
18200
18201/* Add an abbreviation to the table. */
c906108c 18202
685af9cd
TT
18203void
18204abbrev_table::add_abbrev (unsigned int abbrev_number,
18205 struct abbrev_info *abbrev)
433df2d4
DE
18206{
18207 unsigned int hash_number;
18208
18209 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768
YQ
18210 abbrev->next = m_abbrevs[hash_number];
18211 m_abbrevs[hash_number] = abbrev;
433df2d4 18212}
dee91e82 18213
433df2d4
DE
18214/* Look up an abbrev in the table.
18215 Returns NULL if the abbrev is not found. */
18216
685af9cd
TT
18217struct abbrev_info *
18218abbrev_table::lookup_abbrev (unsigned int abbrev_number)
c906108c 18219{
433df2d4
DE
18220 unsigned int hash_number;
18221 struct abbrev_info *abbrev;
18222
18223 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768 18224 abbrev = m_abbrevs[hash_number];
433df2d4
DE
18225
18226 while (abbrev)
18227 {
18228 if (abbrev->number == abbrev_number)
18229 return abbrev;
18230 abbrev = abbrev->next;
18231 }
18232 return NULL;
18233}
18234
18235/* Read in an abbrev table. */
18236
685af9cd 18237static abbrev_table_up
ed2dc618
SM
18238abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18239 struct dwarf2_section_info *section,
9c541725 18240 sect_offset sect_off)
433df2d4
DE
18241{
18242 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 18243 bfd *abfd = get_section_bfd_owner (section);
d521ce57 18244 const gdb_byte *abbrev_ptr;
c906108c
SS
18245 struct abbrev_info *cur_abbrev;
18246 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 18247 unsigned int abbrev_form;
f3dd6933
DJ
18248 struct attr_abbrev *cur_attrs;
18249 unsigned int allocated_attrs;
c906108c 18250
685af9cd 18251 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
c906108c 18252
433df2d4 18253 dwarf2_read_section (objfile, section);
9c541725 18254 abbrev_ptr = section->buffer + to_underlying (sect_off);
c906108c
SS
18255 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18256 abbrev_ptr += bytes_read;
18257
f3dd6933 18258 allocated_attrs = ATTR_ALLOC_CHUNK;
8d749320 18259 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
6e70227d 18260
0963b4bd 18261 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
18262 while (abbrev_number)
18263 {
685af9cd 18264 cur_abbrev = abbrev_table->alloc_abbrev ();
c906108c
SS
18265
18266 /* read in abbrev header */
18267 cur_abbrev->number = abbrev_number;
aead7601
SM
18268 cur_abbrev->tag
18269 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
c906108c
SS
18270 abbrev_ptr += bytes_read;
18271 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18272 abbrev_ptr += 1;
18273
18274 /* now read in declarations */
22d2f3ab 18275 for (;;)
c906108c 18276 {
43988095
JK
18277 LONGEST implicit_const;
18278
22d2f3ab
JK
18279 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18280 abbrev_ptr += bytes_read;
18281 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18282 abbrev_ptr += bytes_read;
43988095
JK
18283 if (abbrev_form == DW_FORM_implicit_const)
18284 {
18285 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18286 &bytes_read);
18287 abbrev_ptr += bytes_read;
18288 }
18289 else
18290 {
18291 /* Initialize it due to a false compiler warning. */
18292 implicit_const = -1;
18293 }
22d2f3ab
JK
18294
18295 if (abbrev_name == 0)
18296 break;
18297
f3dd6933 18298 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 18299 {
f3dd6933
DJ
18300 allocated_attrs += ATTR_ALLOC_CHUNK;
18301 cur_attrs
224c3ddb 18302 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
c906108c 18303 }
ae038cb0 18304
aead7601
SM
18305 cur_attrs[cur_abbrev->num_attrs].name
18306 = (enum dwarf_attribute) abbrev_name;
22d2f3ab 18307 cur_attrs[cur_abbrev->num_attrs].form
aead7601 18308 = (enum dwarf_form) abbrev_form;
43988095 18309 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
22d2f3ab 18310 ++cur_abbrev->num_attrs;
c906108c
SS
18311 }
18312
8d749320
SM
18313 cur_abbrev->attrs =
18314 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18315 cur_abbrev->num_attrs);
f3dd6933
DJ
18316 memcpy (cur_abbrev->attrs, cur_attrs,
18317 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18318
685af9cd 18319 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
c906108c
SS
18320
18321 /* Get next abbreviation.
18322 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
18323 always properly terminated with an abbrev number of 0.
18324 Exit loop if we encounter an abbreviation which we have
18325 already read (which means we are about to read the abbreviations
18326 for the next compile unit) or if the end of the abbreviation
18327 table is reached. */
433df2d4 18328 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
18329 break;
18330 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18331 abbrev_ptr += bytes_read;
685af9cd 18332 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
c906108c
SS
18333 break;
18334 }
f3dd6933
DJ
18335
18336 xfree (cur_attrs);
433df2d4 18337 return abbrev_table;
c906108c
SS
18338}
18339
72bf9492
DJ
18340/* Returns nonzero if TAG represents a type that we might generate a partial
18341 symbol for. */
18342
18343static int
18344is_type_tag_for_partial (int tag)
18345{
18346 switch (tag)
18347 {
18348#if 0
18349 /* Some types that would be reasonable to generate partial symbols for,
18350 that we don't at present. */
18351 case DW_TAG_array_type:
18352 case DW_TAG_file_type:
18353 case DW_TAG_ptr_to_member_type:
18354 case DW_TAG_set_type:
18355 case DW_TAG_string_type:
18356 case DW_TAG_subroutine_type:
18357#endif
18358 case DW_TAG_base_type:
18359 case DW_TAG_class_type:
680b30c7 18360 case DW_TAG_interface_type:
72bf9492
DJ
18361 case DW_TAG_enumeration_type:
18362 case DW_TAG_structure_type:
18363 case DW_TAG_subrange_type:
18364 case DW_TAG_typedef:
18365 case DW_TAG_union_type:
18366 return 1;
18367 default:
18368 return 0;
18369 }
18370}
18371
18372/* Load all DIEs that are interesting for partial symbols into memory. */
18373
18374static struct partial_die_info *
dee91e82 18375load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18376 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18377{
dee91e82 18378 struct dwarf2_cu *cu = reader->cu;
518817b3 18379 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
72bf9492 18380 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18381 unsigned int bytes_read;
5afb4e99 18382 unsigned int load_all = 0;
72bf9492
DJ
18383 int nesting_level = 1;
18384
18385 parent_die = NULL;
18386 last_die = NULL;
18387
7adf1e79
DE
18388 gdb_assert (cu->per_cu != NULL);
18389 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18390 load_all = 1;
18391
72bf9492
DJ
18392 cu->partial_dies
18393 = htab_create_alloc_ex (cu->header.length / 12,
18394 partial_die_hash,
18395 partial_die_eq,
18396 NULL,
18397 &cu->comp_unit_obstack,
18398 hashtab_obstack_allocate,
18399 dummy_obstack_deallocate);
18400
72bf9492
DJ
18401 while (1)
18402 {
685af9cd 18403 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18404
18405 /* A NULL abbrev means the end of a series of children. */
18406 if (abbrev == NULL)
18407 {
18408 if (--nesting_level == 0)
cd9983dd
YQ
18409 return first_die;
18410
72bf9492
DJ
18411 info_ptr += bytes_read;
18412 last_die = parent_die;
18413 parent_die = parent_die->die_parent;
18414 continue;
18415 }
18416
98bfdba5
PA
18417 /* Check for template arguments. We never save these; if
18418 they're seen, we just mark the parent, and go on our way. */
18419 if (parent_die != NULL
18420 && cu->language == language_cplus
18421 && (abbrev->tag == DW_TAG_template_type_param
18422 || abbrev->tag == DW_TAG_template_value_param))
18423 {
18424 parent_die->has_template_arguments = 1;
18425
18426 if (!load_all)
18427 {
18428 /* We don't need a partial DIE for the template argument. */
dee91e82 18429 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18430 continue;
18431 }
18432 }
18433
0d99eb77 18434 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18435 Skip their other children. */
18436 if (!load_all
18437 && cu->language == language_cplus
18438 && parent_die != NULL
18439 && parent_die->tag == DW_TAG_subprogram)
18440 {
dee91e82 18441 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18442 continue;
18443 }
18444
5afb4e99
DJ
18445 /* Check whether this DIE is interesting enough to save. Normally
18446 we would not be interested in members here, but there may be
18447 later variables referencing them via DW_AT_specification (for
18448 static members). */
18449 if (!load_all
18450 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18451 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18452 && abbrev->tag != DW_TAG_enumerator
18453 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18454 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18455 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18456 && abbrev->tag != DW_TAG_variable
5afb4e99 18457 && abbrev->tag != DW_TAG_namespace
f55ee35c 18458 && abbrev->tag != DW_TAG_module
95554aad 18459 && abbrev->tag != DW_TAG_member
74921315
KS
18460 && abbrev->tag != DW_TAG_imported_unit
18461 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18462 {
18463 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18464 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18465 continue;
18466 }
18467
6f06d47b
YQ
18468 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18469 abbrev);
cd9983dd 18470
48fbe735 18471 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18472
18473 /* This two-pass algorithm for processing partial symbols has a
18474 high cost in cache pressure. Thus, handle some simple cases
18475 here which cover the majority of C partial symbols. DIEs
18476 which neither have specification tags in them, nor could have
18477 specification tags elsewhere pointing at them, can simply be
18478 processed and discarded.
18479
18480 This segment is also optional; scan_partial_symbols and
18481 add_partial_symbol will handle these DIEs if we chain
18482 them in normally. When compilers which do not emit large
18483 quantities of duplicate debug information are more common,
18484 this code can probably be removed. */
18485
18486 /* Any complete simple types at the top level (pretty much all
18487 of them, for a language without namespaces), can be processed
18488 directly. */
18489 if (parent_die == NULL
cd9983dd
YQ
18490 && pdi.has_specification == 0
18491 && pdi.is_declaration == 0
18492 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18493 || pdi.tag == DW_TAG_base_type
18494 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18495 {
cd9983dd
YQ
18496 if (building_psymtab && pdi.name != NULL)
18497 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
79748972 18498 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 18499 psymbol_placement::STATIC,
1762568f 18500 0, cu->language, objfile);
cd9983dd 18501 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18502 continue;
18503 }
18504
d8228535
JK
18505 /* The exception for DW_TAG_typedef with has_children above is
18506 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 18507 type_name_or_error will error on such types later.
d8228535
JK
18508
18509 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18510 it could not find the child DIEs referenced later, this is checked
18511 above. In correct DWARF DW_TAG_typedef should have no children. */
18512
cd9983dd 18513 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 18514 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18515 "- DIE at %s [in module %s]"),
cd9983dd 18516 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18517
72bf9492
DJ
18518 /* If we're at the second level, and we're an enumerator, and
18519 our parent has no specification (meaning possibly lives in a
18520 namespace elsewhere), then we can add the partial symbol now
18521 instead of queueing it. */
cd9983dd 18522 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18523 && parent_die != NULL
18524 && parent_die->die_parent == NULL
18525 && parent_die->tag == DW_TAG_enumeration_type
18526 && parent_die->has_specification == 0)
18527 {
cd9983dd 18528 if (pdi.name == NULL)
b98664d3 18529 complaint (_("malformed enumerator DIE ignored"));
72bf9492 18530 else if (building_psymtab)
cd9983dd 18531 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
79748972 18532 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 18533 cu->language == language_cplus
75aedd27
TT
18534 ? psymbol_placement::GLOBAL
18535 : psymbol_placement::STATIC,
1762568f 18536 0, cu->language, objfile);
72bf9492 18537
cd9983dd 18538 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18539 continue;
18540 }
18541
cd9983dd 18542 struct partial_die_info *part_die
6f06d47b 18543 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18544
72bf9492
DJ
18545 /* We'll save this DIE so link it in. */
18546 part_die->die_parent = parent_die;
18547 part_die->die_sibling = NULL;
18548 part_die->die_child = NULL;
18549
18550 if (last_die && last_die == parent_die)
18551 last_die->die_child = part_die;
18552 else if (last_die)
18553 last_die->die_sibling = part_die;
18554
18555 last_die = part_die;
18556
18557 if (first_die == NULL)
18558 first_die = part_die;
18559
18560 /* Maybe add the DIE to the hash table. Not all DIEs that we
18561 find interesting need to be in the hash table, because we
18562 also have the parent/sibling/child chains; only those that we
18563 might refer to by offset later during partial symbol reading.
18564
18565 For now this means things that might have be the target of a
18566 DW_AT_specification, DW_AT_abstract_origin, or
18567 DW_AT_extension. DW_AT_extension will refer only to
18568 namespaces; DW_AT_abstract_origin refers to functions (and
18569 many things under the function DIE, but we do not recurse
18570 into function DIEs during partial symbol reading) and
18571 possibly variables as well; DW_AT_specification refers to
18572 declarations. Declarations ought to have the DW_AT_declaration
18573 flag. It happens that GCC forgets to put it in sometimes, but
18574 only for functions, not for types.
18575
18576 Adding more things than necessary to the hash table is harmless
18577 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18578 wasted time in find_partial_die, when we reread the compilation
18579 unit with load_all_dies set. */
72bf9492 18580
5afb4e99 18581 if (load_all
72929c62 18582 || abbrev->tag == DW_TAG_constant
5afb4e99 18583 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18584 || abbrev->tag == DW_TAG_variable
18585 || abbrev->tag == DW_TAG_namespace
18586 || part_die->is_declaration)
18587 {
18588 void **slot;
18589
18590 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18591 to_underlying (part_die->sect_off),
18592 INSERT);
72bf9492
DJ
18593 *slot = part_die;
18594 }
18595
72bf9492 18596 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18597 we have no reason to follow the children of structures; for other
98bfdba5
PA
18598 languages we have to, so that we can get at method physnames
18599 to infer fully qualified class names, for DW_AT_specification,
18600 and for C++ template arguments. For C++, we also look one level
18601 inside functions to find template arguments (if the name of the
18602 function does not already contain the template arguments).
bc30ff58
JB
18603
18604 For Ada, we need to scan the children of subprograms and lexical
18605 blocks as well because Ada allows the definition of nested
18606 entities that could be interesting for the debugger, such as
18607 nested subprograms for instance. */
72bf9492 18608 if (last_die->has_children
5afb4e99
DJ
18609 && (load_all
18610 || last_die->tag == DW_TAG_namespace
f55ee35c 18611 || last_die->tag == DW_TAG_module
72bf9492 18612 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18613 || (cu->language == language_cplus
18614 && last_die->tag == DW_TAG_subprogram
18615 && (last_die->name == NULL
18616 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
18617 || (cu->language != language_c
18618 && (last_die->tag == DW_TAG_class_type
680b30c7 18619 || last_die->tag == DW_TAG_interface_type
72bf9492 18620 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
18621 || last_die->tag == DW_TAG_union_type))
18622 || (cu->language == language_ada
18623 && (last_die->tag == DW_TAG_subprogram
18624 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18625 {
18626 nesting_level++;
18627 parent_die = last_die;
18628 continue;
18629 }
18630
18631 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18632 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18633
18634 /* Back to the top, do it again. */
18635 }
18636}
18637
6f06d47b
YQ
18638partial_die_info::partial_die_info (sect_offset sect_off_,
18639 struct abbrev_info *abbrev)
18640 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18641{
18642}
18643
35cc7ed7
YQ
18644/* Read a minimal amount of information into the minimal die structure.
18645 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 18646
48fbe735
YQ
18647const gdb_byte *
18648partial_die_info::read (const struct die_reader_specs *reader,
18649 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 18650{
dee91e82 18651 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18652 struct dwarf2_per_objfile *dwarf2_per_objfile
18653 = cu->per_cu->dwarf2_per_objfile;
fa238c03 18654 unsigned int i;
c5aa993b 18655 int has_low_pc_attr = 0;
c906108c 18656 int has_high_pc_attr = 0;
91da1414 18657 int high_pc_relative = 0;
c906108c 18658
fd0a254f 18659 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 18660 {
48fbe735
YQ
18661 struct attribute attr;
18662
fd0a254f 18663 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
c906108c
SS
18664
18665 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18666 partial symbol table. */
c906108c
SS
18667 switch (attr.name)
18668 {
18669 case DW_AT_name:
48fbe735 18670 switch (tag)
71c25dea
TT
18671 {
18672 case DW_TAG_compile_unit:
95554aad 18673 case DW_TAG_partial_unit:
348e048f 18674 case DW_TAG_type_unit:
71c25dea
TT
18675 /* Compilation units have a DW_AT_name that is a filename, not
18676 a source language identifier. */
18677 case DW_TAG_enumeration_type:
18678 case DW_TAG_enumerator:
18679 /* These tags always have simple identifiers already; no need
18680 to canonicalize them. */
48fbe735 18681 name = DW_STRING (&attr);
71c25dea
TT
18682 break;
18683 default:
48fbe735
YQ
18684 {
18685 struct objfile *objfile = dwarf2_per_objfile->objfile;
18686
18687 name
18688 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18689 &objfile->per_bfd->storage_obstack);
18690 }
71c25dea
TT
18691 break;
18692 }
c906108c 18693 break;
31ef98ae 18694 case DW_AT_linkage_name:
c906108c 18695 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18696 /* Note that both forms of linkage name might appear. We
18697 assume they will be the same, and we only store the last
18698 one we see. */
48fbe735 18699 linkage_name = DW_STRING (&attr);
c906108c
SS
18700 break;
18701 case DW_AT_low_pc:
18702 has_low_pc_attr = 1;
48fbe735 18703 lowpc = attr_value_as_address (&attr);
c906108c
SS
18704 break;
18705 case DW_AT_high_pc:
18706 has_high_pc_attr = 1;
48fbe735 18707 highpc = attr_value_as_address (&attr);
31aa7e4e
JB
18708 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18709 high_pc_relative = 1;
c906108c
SS
18710 break;
18711 case DW_AT_location:
0963b4bd 18712 /* Support the .debug_loc offsets. */
8e19ed76
PS
18713 if (attr_form_is_block (&attr))
18714 {
48fbe735 18715 d.locdesc = DW_BLOCK (&attr);
8e19ed76 18716 }
3690dd37 18717 else if (attr_form_is_section_offset (&attr))
8e19ed76 18718 {
4d3c2250 18719 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18720 }
18721 else
18722 {
4d3c2250
KB
18723 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18724 "partial symbol information");
8e19ed76 18725 }
c906108c 18726 break;
c906108c 18727 case DW_AT_external:
48fbe735 18728 is_external = DW_UNSND (&attr);
c906108c
SS
18729 break;
18730 case DW_AT_declaration:
48fbe735 18731 is_declaration = DW_UNSND (&attr);
c906108c
SS
18732 break;
18733 case DW_AT_type:
48fbe735 18734 has_type = 1;
c906108c
SS
18735 break;
18736 case DW_AT_abstract_origin:
18737 case DW_AT_specification:
72bf9492 18738 case DW_AT_extension:
48fbe735
YQ
18739 has_specification = 1;
18740 spec_offset = dwarf2_get_ref_die_offset (&attr);
18741 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 18742 || cu->per_cu->is_dwz);
c906108c
SS
18743 break;
18744 case DW_AT_sibling:
18745 /* Ignore absolute siblings, they might point outside of
18746 the current compile unit. */
18747 if (attr.form == DW_FORM_ref_addr)
b98664d3 18748 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 18749 else
b9502d3f 18750 {
48fbe735 18751 const gdb_byte *buffer = reader->buffer;
9c541725
PA
18752 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18753 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18754
18755 if (sibling_ptr < info_ptr)
b98664d3 18756 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
18757 else if (sibling_ptr > reader->buffer_end)
18758 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f 18759 else
48fbe735 18760 sibling = sibling_ptr;
b9502d3f 18761 }
c906108c 18762 break;
fa4028e9 18763 case DW_AT_byte_size:
48fbe735 18764 has_byte_size = 1;
fa4028e9 18765 break;
ff908ebf 18766 case DW_AT_const_value:
48fbe735 18767 has_const_value = 1;
ff908ebf 18768 break;
68511cec
CES
18769 case DW_AT_calling_convention:
18770 /* DWARF doesn't provide a way to identify a program's source-level
18771 entry point. DW_AT_calling_convention attributes are only meant
18772 to describe functions' calling conventions.
18773
18774 However, because it's a necessary piece of information in
0c1b455e
TT
18775 Fortran, and before DWARF 4 DW_CC_program was the only
18776 piece of debugging information whose definition refers to
18777 a 'main program' at all, several compilers marked Fortran
18778 main programs with DW_CC_program --- even when those
18779 functions use the standard calling conventions.
18780
18781 Although DWARF now specifies a way to provide this
18782 information, we support this practice for backward
18783 compatibility. */
68511cec 18784 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 18785 && cu->language == language_fortran)
48fbe735 18786 main_subprogram = 1;
68511cec 18787 break;
481860b3
GB
18788 case DW_AT_inline:
18789 if (DW_UNSND (&attr) == DW_INL_inlined
18790 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 18791 may_be_inlined = 1;
481860b3 18792 break;
95554aad
TT
18793
18794 case DW_AT_import:
48fbe735 18795 if (tag == DW_TAG_imported_unit)
36586728 18796 {
48fbe735
YQ
18797 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18798 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
18799 || cu->per_cu->is_dwz);
18800 }
95554aad
TT
18801 break;
18802
0c1b455e 18803 case DW_AT_main_subprogram:
48fbe735 18804 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
18805 break;
18806
05caa1d2
TT
18807 case DW_AT_ranges:
18808 {
18809 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18810 but that requires a full DIE, so instead we just
18811 reimplement it. */
18812 int need_ranges_base = tag != DW_TAG_compile_unit;
18813 unsigned int ranges_offset = (DW_UNSND (&attr)
18814 + (need_ranges_base
18815 ? cu->ranges_base
18816 : 0));
18817
18818 /* Value of the DW_AT_ranges attribute is the offset in the
18819 .debug_ranges section. */
18820 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18821 nullptr))
18822 has_pc_info = 1;
18823 }
18824 break;
18825
c906108c
SS
18826 default:
18827 break;
18828 }
18829 }
18830
10d06d82
TT
18831 /* For Ada, if both the name and the linkage name appear, we prefer
18832 the latter. This lets "catch exception" work better, regardless
18833 of the order in which the name and linkage name were emitted.
18834 Really, though, this is just a workaround for the fact that gdb
18835 doesn't store both the name and the linkage name. */
18836 if (cu->language == language_ada && linkage_name != nullptr)
18837 name = linkage_name;
18838
91da1414 18839 if (high_pc_relative)
48fbe735 18840 highpc += lowpc;
91da1414 18841
9373cf26
JK
18842 if (has_low_pc_attr && has_high_pc_attr)
18843 {
18844 /* When using the GNU linker, .gnu.linkonce. sections are used to
18845 eliminate duplicate copies of functions and vtables and such.
18846 The linker will arbitrarily choose one and discard the others.
18847 The AT_*_pc values for such functions refer to local labels in
18848 these sections. If the section from that file was discarded, the
18849 labels are not in the output, so the relocs get a value of 0.
18850 If this is a discarded function, mark the pc bounds as invalid,
18851 so that GDB will ignore it. */
48fbe735 18852 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9373cf26 18853 {
48fbe735 18854 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18855 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 18856
b98664d3 18857 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 18858 "for DIE at %s [in module %s]"),
48fbe735
YQ
18859 paddress (gdbarch, lowpc),
18860 sect_offset_str (sect_off),
9d8780f0 18861 objfile_name (objfile));
9373cf26
JK
18862 }
18863 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 18864 else if (lowpc >= highpc)
9373cf26 18865 {
48fbe735 18866 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18867 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 18868
b98664d3 18869 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 18870 "for DIE at %s [in module %s]"),
48fbe735
YQ
18871 paddress (gdbarch, lowpc),
18872 paddress (gdbarch, highpc),
18873 sect_offset_str (sect_off),
9c541725 18874 objfile_name (objfile));
9373cf26
JK
18875 }
18876 else
48fbe735 18877 has_pc_info = 1;
9373cf26 18878 }
85cbf3d3 18879
c906108c
SS
18880 return info_ptr;
18881}
18882
72bf9492
DJ
18883/* Find a cached partial DIE at OFFSET in CU. */
18884
d590ff25
YQ
18885struct partial_die_info *
18886dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
18887{
18888 struct partial_die_info *lookup_die = NULL;
6f06d47b 18889 struct partial_die_info part_die (sect_off);
72bf9492 18890
9a3c8263 18891 lookup_die = ((struct partial_die_info *)
d590ff25 18892 htab_find_with_hash (partial_dies, &part_die,
9c541725 18893 to_underlying (sect_off)));
72bf9492 18894
72bf9492
DJ
18895 return lookup_die;
18896}
18897
348e048f
DE
18898/* Find a partial DIE at OFFSET, which may or may not be in CU,
18899 except in the case of .debug_types DIEs which do not reference
18900 outside their CU (they do however referencing other types via
55f1336d 18901 DW_FORM_ref_sig8). */
72bf9492 18902
122cf0f2 18903static const struct cu_partial_die_info
9c541725 18904find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18905{
518817b3
SM
18906 struct dwarf2_per_objfile *dwarf2_per_objfile
18907 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18908 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
18909 struct dwarf2_per_cu_data *per_cu = NULL;
18910 struct partial_die_info *pd = NULL;
72bf9492 18911
36586728 18912 if (offset_in_dwz == cu->per_cu->is_dwz
9c541725 18913 && offset_in_cu_p (&cu->header, sect_off))
5afb4e99 18914 {
d590ff25 18915 pd = cu->find_partial_die (sect_off);
5afb4e99 18916 if (pd != NULL)
fb816e8b 18917 return { cu, pd };
0d99eb77
DE
18918 /* We missed recording what we needed.
18919 Load all dies and try again. */
18920 per_cu = cu->per_cu;
5afb4e99 18921 }
0d99eb77
DE
18922 else
18923 {
18924 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18925 if (cu->per_cu->is_debug_types)
0d99eb77 18926 {
9d8780f0
SM
18927 error (_("Dwarf Error: Type Unit at offset %s contains"
18928 " external reference to offset %s [in module %s].\n"),
18929 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
18930 bfd_get_filename (objfile->obfd));
18931 }
9c541725 18932 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 18933 dwarf2_per_objfile);
72bf9492 18934
0d99eb77
DE
18935 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18936 load_partial_comp_unit (per_cu);
ae038cb0 18937
0d99eb77 18938 per_cu->cu->last_used = 0;
d590ff25 18939 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 18940 }
5afb4e99 18941
dee91e82
DE
18942 /* If we didn't find it, and not all dies have been loaded,
18943 load them all and try again. */
18944
5afb4e99
DJ
18945 if (pd == NULL && per_cu->load_all_dies == 0)
18946 {
5afb4e99 18947 per_cu->load_all_dies = 1;
fd820528
DE
18948
18949 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18950 THIS_CU->cu may already be in use. So we can't just free it and
18951 replace its DIEs with the ones we read in. Instead, we leave those
18952 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18953 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18954 set. */
dee91e82 18955 load_partial_comp_unit (per_cu);
5afb4e99 18956
d590ff25 18957 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
18958 }
18959
18960 if (pd == NULL)
18961 internal_error (__FILE__, __LINE__,
9d8780f0 18962 _("could not find partial DIE %s "
3e43a32a 18963 "in cache [from module %s]\n"),
9d8780f0 18964 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
fb816e8b 18965 return { per_cu->cu, pd };
72bf9492
DJ
18966}
18967
abc72ce4
DE
18968/* See if we can figure out if the class lives in a namespace. We do
18969 this by looking for a member function; its demangled name will
18970 contain namespace info, if there is any. */
18971
18972static void
18973guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18974 struct dwarf2_cu *cu)
18975{
18976 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18977 what template types look like, because the demangler
18978 frequently doesn't give the same name as the debug info. We
18979 could fix this by only using the demangled name to get the
18980 prefix (but see comment in read_structure_type). */
18981
18982 struct partial_die_info *real_pdi;
18983 struct partial_die_info *child_pdi;
18984
18985 /* If this DIE (this DIE's specification, if any) has a parent, then
18986 we should not do this. We'll prepend the parent's fully qualified
18987 name when we create the partial symbol. */
18988
18989 real_pdi = struct_pdi;
18990 while (real_pdi->has_specification)
fb816e8b 18991 {
122cf0f2
AB
18992 auto res = find_partial_die (real_pdi->spec_offset,
18993 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
18994 real_pdi = res.pdi;
18995 cu = res.cu;
18996 }
abc72ce4
DE
18997
18998 if (real_pdi->die_parent != NULL)
18999 return;
19000
19001 for (child_pdi = struct_pdi->die_child;
19002 child_pdi != NULL;
19003 child_pdi = child_pdi->die_sibling)
19004 {
19005 if (child_pdi->tag == DW_TAG_subprogram
19006 && child_pdi->linkage_name != NULL)
19007 {
19008 char *actual_class_name
19009 = language_class_name_from_physname (cu->language_defn,
19010 child_pdi->linkage_name);
19011 if (actual_class_name != NULL)
19012 {
518817b3 19013 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4 19014 struct_pdi->name
021887d8
TT
19015 = obstack_strdup (&objfile->per_bfd->storage_obstack,
19016 actual_class_name);
abc72ce4
DE
19017 xfree (actual_class_name);
19018 }
19019 break;
19020 }
19021 }
19022}
19023
52356b79
YQ
19024void
19025partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 19026{
abc72ce4
DE
19027 /* Once we've fixed up a die, there's no point in doing so again.
19028 This also avoids a memory leak if we were to call
19029 guess_partial_die_structure_name multiple times. */
52356b79 19030 if (fixup_called)
abc72ce4
DE
19031 return;
19032
72bf9492
DJ
19033 /* If we found a reference attribute and the DIE has no name, try
19034 to find a name in the referred to DIE. */
19035
52356b79 19036 if (name == NULL && has_specification)
72bf9492
DJ
19037 {
19038 struct partial_die_info *spec_die;
72bf9492 19039
122cf0f2 19040 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
19041 spec_die = res.pdi;
19042 cu = res.cu;
72bf9492 19043
52356b79 19044 spec_die->fixup (cu);
72bf9492
DJ
19045
19046 if (spec_die->name)
19047 {
52356b79 19048 name = spec_die->name;
72bf9492
DJ
19049
19050 /* Copy DW_AT_external attribute if it is set. */
19051 if (spec_die->is_external)
52356b79 19052 is_external = spec_die->is_external;
72bf9492
DJ
19053 }
19054 }
19055
19056 /* Set default names for some unnamed DIEs. */
72bf9492 19057
52356b79
YQ
19058 if (name == NULL && tag == DW_TAG_namespace)
19059 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 19060
abc72ce4
DE
19061 /* If there is no parent die to provide a namespace, and there are
19062 children, see if we can determine the namespace from their linkage
122d1940 19063 name. */
abc72ce4 19064 if (cu->language == language_cplus
fd5866f6 19065 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
52356b79
YQ
19066 && die_parent == NULL
19067 && has_children
19068 && (tag == DW_TAG_class_type
19069 || tag == DW_TAG_structure_type
19070 || tag == DW_TAG_union_type))
19071 guess_partial_die_structure_name (this, cu);
abc72ce4 19072
53832f31
TT
19073 /* GCC might emit a nameless struct or union that has a linkage
19074 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
19075 if (name == NULL
19076 && (tag == DW_TAG_class_type
19077 || tag == DW_TAG_interface_type
19078 || tag == DW_TAG_structure_type
19079 || tag == DW_TAG_union_type)
19080 && linkage_name != NULL)
53832f31
TT
19081 {
19082 char *demangled;
19083
52356b79 19084 demangled = gdb_demangle (linkage_name, DMGL_TYPES);
53832f31
TT
19085 if (demangled)
19086 {
96408a79
SA
19087 const char *base;
19088
19089 /* Strip any leading namespaces/classes, keep only the base name.
19090 DW_AT_name for named DIEs does not contain the prefixes. */
19091 base = strrchr (demangled, ':');
19092 if (base && base > demangled && base[-1] == ':')
19093 base++;
19094 else
19095 base = demangled;
19096
518817b3 19097 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
021887d8 19098 name = obstack_strdup (&objfile->per_bfd->storage_obstack, base);
53832f31
TT
19099 xfree (demangled);
19100 }
19101 }
19102
52356b79 19103 fixup_called = 1;
72bf9492
DJ
19104}
19105
a8329558 19106/* Read an attribute value described by an attribute form. */
c906108c 19107
d521ce57 19108static const gdb_byte *
dee91e82
DE
19109read_attribute_value (const struct die_reader_specs *reader,
19110 struct attribute *attr, unsigned form,
43988095 19111 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 19112{
dee91e82 19113 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
19114 struct dwarf2_per_objfile *dwarf2_per_objfile
19115 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 19116 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 19117 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 19118 bfd *abfd = reader->abfd;
e7c27a73 19119 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19120 unsigned int bytes_read;
19121 struct dwarf_block *blk;
19122
aead7601 19123 attr->form = (enum dwarf_form) form;
a8329558 19124 switch (form)
c906108c 19125 {
c906108c 19126 case DW_FORM_ref_addr:
ae411497 19127 if (cu->header.version == 2)
4568ecf9 19128 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 19129 else
4568ecf9
DE
19130 DW_UNSND (attr) = read_offset (abfd, info_ptr,
19131 &cu->header, &bytes_read);
ae411497
TT
19132 info_ptr += bytes_read;
19133 break;
36586728
TT
19134 case DW_FORM_GNU_ref_alt:
19135 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19136 info_ptr += bytes_read;
19137 break;
ae411497 19138 case DW_FORM_addr:
e7c27a73 19139 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 19140 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 19141 info_ptr += bytes_read;
c906108c
SS
19142 break;
19143 case DW_FORM_block2:
7b5a2f43 19144 blk = dwarf_alloc_block (cu);
c906108c
SS
19145 blk->size = read_2_bytes (abfd, info_ptr);
19146 info_ptr += 2;
19147 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19148 info_ptr += blk->size;
19149 DW_BLOCK (attr) = blk;
19150 break;
19151 case DW_FORM_block4:
7b5a2f43 19152 blk = dwarf_alloc_block (cu);
c906108c
SS
19153 blk->size = read_4_bytes (abfd, info_ptr);
19154 info_ptr += 4;
19155 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19156 info_ptr += blk->size;
19157 DW_BLOCK (attr) = blk;
19158 break;
19159 case DW_FORM_data2:
19160 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19161 info_ptr += 2;
19162 break;
19163 case DW_FORM_data4:
19164 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19165 info_ptr += 4;
19166 break;
19167 case DW_FORM_data8:
19168 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19169 info_ptr += 8;
19170 break;
0224619f
JK
19171 case DW_FORM_data16:
19172 blk = dwarf_alloc_block (cu);
19173 blk->size = 16;
19174 blk->data = read_n_bytes (abfd, info_ptr, 16);
19175 info_ptr += 16;
19176 DW_BLOCK (attr) = blk;
19177 break;
2dc7f7b3
TT
19178 case DW_FORM_sec_offset:
19179 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
19180 info_ptr += bytes_read;
19181 break;
c906108c 19182 case DW_FORM_string:
9b1c24c8 19183 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 19184 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
19185 info_ptr += bytes_read;
19186 break;
4bdf3d34 19187 case DW_FORM_strp:
36586728
TT
19188 if (!cu->per_cu->is_dwz)
19189 {
ed2dc618
SM
19190 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19191 abfd, info_ptr, cu_header,
36586728
TT
19192 &bytes_read);
19193 DW_STRING_IS_CANONICAL (attr) = 0;
19194 info_ptr += bytes_read;
19195 break;
19196 }
19197 /* FALLTHROUGH */
43988095
JK
19198 case DW_FORM_line_strp:
19199 if (!cu->per_cu->is_dwz)
19200 {
ed2dc618
SM
19201 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
19202 abfd, info_ptr,
43988095
JK
19203 cu_header, &bytes_read);
19204 DW_STRING_IS_CANONICAL (attr) = 0;
19205 info_ptr += bytes_read;
19206 break;
19207 }
19208 /* FALLTHROUGH */
36586728
TT
19209 case DW_FORM_GNU_strp_alt:
19210 {
ed2dc618 19211 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
19212 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
19213 &bytes_read);
19214
ed2dc618
SM
19215 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
19216 dwz, str_offset);
36586728
TT
19217 DW_STRING_IS_CANONICAL (attr) = 0;
19218 info_ptr += bytes_read;
19219 }
4bdf3d34 19220 break;
2dc7f7b3 19221 case DW_FORM_exprloc:
c906108c 19222 case DW_FORM_block:
7b5a2f43 19223 blk = dwarf_alloc_block (cu);
c906108c
SS
19224 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19225 info_ptr += bytes_read;
19226 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19227 info_ptr += blk->size;
19228 DW_BLOCK (attr) = blk;
19229 break;
19230 case DW_FORM_block1:
7b5a2f43 19231 blk = dwarf_alloc_block (cu);
c906108c
SS
19232 blk->size = read_1_byte (abfd, info_ptr);
19233 info_ptr += 1;
19234 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19235 info_ptr += blk->size;
19236 DW_BLOCK (attr) = blk;
19237 break;
19238 case DW_FORM_data1:
19239 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19240 info_ptr += 1;
19241 break;
19242 case DW_FORM_flag:
19243 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19244 info_ptr += 1;
19245 break;
2dc7f7b3
TT
19246 case DW_FORM_flag_present:
19247 DW_UNSND (attr) = 1;
19248 break;
c906108c
SS
19249 case DW_FORM_sdata:
19250 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19251 info_ptr += bytes_read;
19252 break;
19253 case DW_FORM_udata:
19254 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19255 info_ptr += bytes_read;
19256 break;
19257 case DW_FORM_ref1:
9c541725 19258 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19259 + read_1_byte (abfd, info_ptr));
c906108c
SS
19260 info_ptr += 1;
19261 break;
19262 case DW_FORM_ref2:
9c541725 19263 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19264 + read_2_bytes (abfd, info_ptr));
c906108c
SS
19265 info_ptr += 2;
19266 break;
19267 case DW_FORM_ref4:
9c541725 19268 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19269 + read_4_bytes (abfd, info_ptr));
c906108c
SS
19270 info_ptr += 4;
19271 break;
613e1657 19272 case DW_FORM_ref8:
9c541725 19273 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19274 + read_8_bytes (abfd, info_ptr));
613e1657
KB
19275 info_ptr += 8;
19276 break;
55f1336d 19277 case DW_FORM_ref_sig8:
ac9ec31b 19278 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
19279 info_ptr += 8;
19280 break;
c906108c 19281 case DW_FORM_ref_udata:
9c541725 19282 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19283 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19284 info_ptr += bytes_read;
19285 break;
c906108c 19286 case DW_FORM_indirect:
a8329558
KW
19287 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19288 info_ptr += bytes_read;
43988095
JK
19289 if (form == DW_FORM_implicit_const)
19290 {
19291 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19292 info_ptr += bytes_read;
19293 }
19294 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19295 info_ptr);
19296 break;
19297 case DW_FORM_implicit_const:
19298 DW_SND (attr) = implicit_const;
a8329558 19299 break;
336d760d 19300 case DW_FORM_addrx:
3019eac3
DE
19301 case DW_FORM_GNU_addr_index:
19302 if (reader->dwo_file == NULL)
19303 {
19304 /* For now flag a hard error.
19305 Later we can turn this into a complaint. */
19306 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19307 dwarf_form_name (form),
19308 bfd_get_filename (abfd));
19309 }
19310 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19311 info_ptr += bytes_read;
19312 break;
cf532bd1 19313 case DW_FORM_strx:
15f18d14
AT
19314 case DW_FORM_strx1:
19315 case DW_FORM_strx2:
19316 case DW_FORM_strx3:
19317 case DW_FORM_strx4:
3019eac3
DE
19318 case DW_FORM_GNU_str_index:
19319 if (reader->dwo_file == NULL)
19320 {
19321 /* For now flag a hard error.
19322 Later we can turn this into a complaint if warranted. */
19323 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19324 dwarf_form_name (form),
19325 bfd_get_filename (abfd));
19326 }
19327 {
15f18d14
AT
19328 ULONGEST str_index;
19329 if (form == DW_FORM_strx1)
19330 {
19331 str_index = read_1_byte (abfd, info_ptr);
19332 info_ptr += 1;
19333 }
19334 else if (form == DW_FORM_strx2)
19335 {
19336 str_index = read_2_bytes (abfd, info_ptr);
19337 info_ptr += 2;
19338 }
19339 else if (form == DW_FORM_strx3)
19340 {
19341 str_index = read_3_bytes (abfd, info_ptr);
19342 info_ptr += 3;
19343 }
19344 else if (form == DW_FORM_strx4)
19345 {
19346 str_index = read_4_bytes (abfd, info_ptr);
19347 info_ptr += 4;
19348 }
19349 else
19350 {
19351 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19352 info_ptr += bytes_read;
19353 }
342587c4 19354 DW_STRING (attr) = read_str_index (reader, str_index);
3019eac3 19355 DW_STRING_IS_CANONICAL (attr) = 0;
3019eac3
DE
19356 }
19357 break;
c906108c 19358 default:
8a3fe4f8 19359 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19360 dwarf_form_name (form),
19361 bfd_get_filename (abfd));
c906108c 19362 }
28e94949 19363
36586728 19364 /* Super hack. */
7771576e 19365 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
19366 attr->form = DW_FORM_GNU_ref_alt;
19367
28e94949
JB
19368 /* We have seen instances where the compiler tried to emit a byte
19369 size attribute of -1 which ended up being encoded as an unsigned
19370 0xffffffff. Although 0xffffffff is technically a valid size value,
19371 an object of this size seems pretty unlikely so we can relatively
19372 safely treat these cases as if the size attribute was invalid and
19373 treat them as zero by default. */
19374 if (attr->name == DW_AT_byte_size
19375 && form == DW_FORM_data4
19376 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19377 {
19378 complaint
b98664d3 19379 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 19380 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19381 DW_UNSND (attr) = 0;
19382 }
28e94949 19383
c906108c
SS
19384 return info_ptr;
19385}
19386
a8329558
KW
19387/* Read an attribute described by an abbreviated attribute. */
19388
d521ce57 19389static const gdb_byte *
dee91e82
DE
19390read_attribute (const struct die_reader_specs *reader,
19391 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 19392 const gdb_byte *info_ptr)
a8329558
KW
19393{
19394 attr->name = abbrev->name;
43988095
JK
19395 return read_attribute_value (reader, attr, abbrev->form,
19396 abbrev->implicit_const, info_ptr);
a8329558
KW
19397}
19398
0963b4bd 19399/* Read dwarf information from a buffer. */
c906108c
SS
19400
19401static unsigned int
a1855c1d 19402read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19403{
fe1b8b76 19404 return bfd_get_8 (abfd, buf);
c906108c
SS
19405}
19406
19407static int
a1855c1d 19408read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19409{
fe1b8b76 19410 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
19411}
19412
19413static unsigned int
a1855c1d 19414read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19415{
fe1b8b76 19416 return bfd_get_16 (abfd, buf);
c906108c
SS
19417}
19418
21ae7a4d 19419static int
a1855c1d 19420read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19421{
19422 return bfd_get_signed_16 (abfd, buf);
19423}
19424
15f18d14
AT
19425static unsigned int
19426read_3_bytes (bfd *abfd, const gdb_byte *buf)
19427{
19428 unsigned int result = 0;
19429 for (int i = 0; i < 3; ++i)
19430 {
19431 unsigned char byte = bfd_get_8 (abfd, buf);
19432 buf++;
19433 result |= ((unsigned int) byte << (i * 8));
19434 }
19435 return result;
19436}
19437
c906108c 19438static unsigned int
a1855c1d 19439read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19440{
fe1b8b76 19441 return bfd_get_32 (abfd, buf);
c906108c
SS
19442}
19443
21ae7a4d 19444static int
a1855c1d 19445read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19446{
19447 return bfd_get_signed_32 (abfd, buf);
19448}
19449
93311388 19450static ULONGEST
a1855c1d 19451read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19452{
fe1b8b76 19453 return bfd_get_64 (abfd, buf);
c906108c
SS
19454}
19455
19456static CORE_ADDR
d521ce57 19457read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 19458 unsigned int *bytes_read)
c906108c 19459{
e7c27a73 19460 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19461 CORE_ADDR retval = 0;
19462
107d2387 19463 if (cu_header->signed_addr_p)
c906108c 19464 {
107d2387
AC
19465 switch (cu_header->addr_size)
19466 {
19467 case 2:
fe1b8b76 19468 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
19469 break;
19470 case 4:
fe1b8b76 19471 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
19472 break;
19473 case 8:
fe1b8b76 19474 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
19475 break;
19476 default:
8e65ff28 19477 internal_error (__FILE__, __LINE__,
e2e0b3e5 19478 _("read_address: bad switch, signed [in module %s]"),
659b0389 19479 bfd_get_filename (abfd));
107d2387
AC
19480 }
19481 }
19482 else
19483 {
19484 switch (cu_header->addr_size)
19485 {
19486 case 2:
fe1b8b76 19487 retval = bfd_get_16 (abfd, buf);
107d2387
AC
19488 break;
19489 case 4:
fe1b8b76 19490 retval = bfd_get_32 (abfd, buf);
107d2387
AC
19491 break;
19492 case 8:
fe1b8b76 19493 retval = bfd_get_64 (abfd, buf);
107d2387
AC
19494 break;
19495 default:
8e65ff28 19496 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
19497 _("read_address: bad switch, "
19498 "unsigned [in module %s]"),
659b0389 19499 bfd_get_filename (abfd));
107d2387 19500 }
c906108c 19501 }
64367e0a 19502
107d2387
AC
19503 *bytes_read = cu_header->addr_size;
19504 return retval;
c906108c
SS
19505}
19506
f7ef9339 19507/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
19508 specification allows the initial length to take up either 4 bytes
19509 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19510 bytes describe the length and all offsets will be 8 bytes in length
19511 instead of 4.
19512
f7ef9339
KB
19513 An older, non-standard 64-bit format is also handled by this
19514 function. The older format in question stores the initial length
19515 as an 8-byte quantity without an escape value. Lengths greater
19516 than 2^32 aren't very common which means that the initial 4 bytes
19517 is almost always zero. Since a length value of zero doesn't make
19518 sense for the 32-bit format, this initial zero can be considered to
19519 be an escape value which indicates the presence of the older 64-bit
19520 format. As written, the code can't detect (old format) lengths
917c78fc
MK
19521 greater than 4GB. If it becomes necessary to handle lengths
19522 somewhat larger than 4GB, we could allow other small values (such
19523 as the non-sensical values of 1, 2, and 3) to also be used as
19524 escape values indicating the presence of the old format.
f7ef9339 19525
917c78fc
MK
19526 The value returned via bytes_read should be used to increment the
19527 relevant pointer after calling read_initial_length().
c764a876 19528
613e1657
KB
19529 [ Note: read_initial_length() and read_offset() are based on the
19530 document entitled "DWARF Debugging Information Format", revision
f7ef9339 19531 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
19532 from:
19533
f7ef9339 19534 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 19535
613e1657
KB
19536 This document is only a draft and is subject to change. (So beware.)
19537
f7ef9339 19538 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
19539 determined empirically by examining 64-bit ELF files produced by
19540 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
19541
19542 - Kevin, July 16, 2002
613e1657
KB
19543 ] */
19544
19545static LONGEST
d521ce57 19546read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 19547{
fe1b8b76 19548 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 19549
dd373385 19550 if (length == 0xffffffff)
613e1657 19551 {
fe1b8b76 19552 length = bfd_get_64 (abfd, buf + 4);
613e1657 19553 *bytes_read = 12;
613e1657 19554 }
dd373385 19555 else if (length == 0)
f7ef9339 19556 {
dd373385 19557 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 19558 length = bfd_get_64 (abfd, buf);
f7ef9339 19559 *bytes_read = 8;
f7ef9339 19560 }
613e1657
KB
19561 else
19562 {
19563 *bytes_read = 4;
613e1657
KB
19564 }
19565
c764a876
DE
19566 return length;
19567}
dd373385 19568
c764a876
DE
19569/* Cover function for read_initial_length.
19570 Returns the length of the object at BUF, and stores the size of the
19571 initial length in *BYTES_READ and stores the size that offsets will be in
19572 *OFFSET_SIZE.
19573 If the initial length size is not equivalent to that specified in
19574 CU_HEADER then issue a complaint.
19575 This is useful when reading non-comp-unit headers. */
dd373385 19576
c764a876 19577static LONGEST
d521ce57 19578read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
19579 const struct comp_unit_head *cu_header,
19580 unsigned int *bytes_read,
19581 unsigned int *offset_size)
19582{
19583 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19584
19585 gdb_assert (cu_header->initial_length_size == 4
19586 || cu_header->initial_length_size == 8
19587 || cu_header->initial_length_size == 12);
19588
19589 if (cu_header->initial_length_size != *bytes_read)
b98664d3 19590 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 19591
c764a876 19592 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 19593 return length;
613e1657
KB
19594}
19595
19596/* Read an offset from the data stream. The size of the offset is
917c78fc 19597 given by cu_header->offset_size. */
613e1657
KB
19598
19599static LONGEST
d521ce57
TT
19600read_offset (bfd *abfd, const gdb_byte *buf,
19601 const struct comp_unit_head *cu_header,
891d2f0b 19602 unsigned int *bytes_read)
c764a876
DE
19603{
19604 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 19605
c764a876
DE
19606 *bytes_read = cu_header->offset_size;
19607 return offset;
19608}
19609
19610/* Read an offset from the data stream. */
19611
19612static LONGEST
d521ce57 19613read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
19614{
19615 LONGEST retval = 0;
19616
c764a876 19617 switch (offset_size)
613e1657
KB
19618 {
19619 case 4:
fe1b8b76 19620 retval = bfd_get_32 (abfd, buf);
613e1657
KB
19621 break;
19622 case 8:
fe1b8b76 19623 retval = bfd_get_64 (abfd, buf);
613e1657
KB
19624 break;
19625 default:
8e65ff28 19626 internal_error (__FILE__, __LINE__,
c764a876 19627 _("read_offset_1: bad switch [in module %s]"),
659b0389 19628 bfd_get_filename (abfd));
613e1657
KB
19629 }
19630
917c78fc 19631 return retval;
613e1657
KB
19632}
19633
d521ce57
TT
19634static const gdb_byte *
19635read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
19636{
19637 /* If the size of a host char is 8 bits, we can return a pointer
19638 to the buffer, otherwise we have to copy the data to a buffer
19639 allocated on the temporary obstack. */
4bdf3d34 19640 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 19641 return buf;
c906108c
SS
19642}
19643
d521ce57
TT
19644static const char *
19645read_direct_string (bfd *abfd, const gdb_byte *buf,
19646 unsigned int *bytes_read_ptr)
c906108c
SS
19647{
19648 /* If the size of a host char is 8 bits, we can return a pointer
19649 to the string, otherwise we have to copy the string to a buffer
19650 allocated on the temporary obstack. */
4bdf3d34 19651 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
19652 if (*buf == '\0')
19653 {
19654 *bytes_read_ptr = 1;
19655 return NULL;
19656 }
d521ce57
TT
19657 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19658 return (const char *) buf;
4bdf3d34
JJ
19659}
19660
43988095
JK
19661/* Return pointer to string at section SECT offset STR_OFFSET with error
19662 reporting strings FORM_NAME and SECT_NAME. */
19663
d521ce57 19664static const char *
ed2dc618
SM
19665read_indirect_string_at_offset_from (struct objfile *objfile,
19666 bfd *abfd, LONGEST str_offset,
43988095
JK
19667 struct dwarf2_section_info *sect,
19668 const char *form_name,
19669 const char *sect_name)
19670{
ed2dc618 19671 dwarf2_read_section (objfile, sect);
43988095
JK
19672 if (sect->buffer == NULL)
19673 error (_("%s used without %s section [in module %s]"),
19674 form_name, sect_name, bfd_get_filename (abfd));
19675 if (str_offset >= sect->size)
19676 error (_("%s pointing outside of %s section [in module %s]"),
19677 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 19678 gdb_assert (HOST_CHAR_BIT == 8);
43988095 19679 if (sect->buffer[str_offset] == '\0')
4bdf3d34 19680 return NULL;
43988095
JK
19681 return (const char *) (sect->buffer + str_offset);
19682}
19683
19684/* Return pointer to string at .debug_str offset STR_OFFSET. */
19685
19686static const char *
ed2dc618
SM
19687read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19688 bfd *abfd, LONGEST str_offset)
43988095 19689{
ed2dc618
SM
19690 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19691 abfd, str_offset,
43988095
JK
19692 &dwarf2_per_objfile->str,
19693 "DW_FORM_strp", ".debug_str");
19694}
19695
19696/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19697
19698static const char *
ed2dc618
SM
19699read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19700 bfd *abfd, LONGEST str_offset)
43988095 19701{
ed2dc618
SM
19702 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19703 abfd, str_offset,
43988095
JK
19704 &dwarf2_per_objfile->line_str,
19705 "DW_FORM_line_strp",
19706 ".debug_line_str");
c906108c
SS
19707}
19708
36586728
TT
19709/* Read a string at offset STR_OFFSET in the .debug_str section from
19710 the .dwz file DWZ. Throw an error if the offset is too large. If
19711 the string consists of a single NUL byte, return NULL; otherwise
19712 return a pointer to the string. */
19713
d521ce57 19714static const char *
ed2dc618
SM
19715read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19716 LONGEST str_offset)
36586728 19717{
ed2dc618 19718 dwarf2_read_section (objfile, &dwz->str);
36586728
TT
19719
19720 if (dwz->str.buffer == NULL)
19721 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19722 "section [in module %s]"),
19723 bfd_get_filename (dwz->dwz_bfd));
19724 if (str_offset >= dwz->str.size)
19725 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19726 ".debug_str section [in module %s]"),
19727 bfd_get_filename (dwz->dwz_bfd));
19728 gdb_assert (HOST_CHAR_BIT == 8);
19729 if (dwz->str.buffer[str_offset] == '\0')
19730 return NULL;
d521ce57 19731 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
19732}
19733
43988095
JK
19734/* Return pointer to string at .debug_str offset as read from BUF.
19735 BUF is assumed to be in a compilation unit described by CU_HEADER.
19736 Return *BYTES_READ_PTR count of bytes read from BUF. */
19737
d521ce57 19738static const char *
ed2dc618
SM
19739read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19740 const gdb_byte *buf,
cf2c3c16
TT
19741 const struct comp_unit_head *cu_header,
19742 unsigned int *bytes_read_ptr)
19743{
19744 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19745
ed2dc618 19746 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
cf2c3c16
TT
19747}
19748
43988095
JK
19749/* Return pointer to string at .debug_line_str offset as read from BUF.
19750 BUF is assumed to be in a compilation unit described by CU_HEADER.
19751 Return *BYTES_READ_PTR count of bytes read from BUF. */
19752
19753static const char *
ed2dc618
SM
19754read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19755 bfd *abfd, const gdb_byte *buf,
43988095
JK
19756 const struct comp_unit_head *cu_header,
19757 unsigned int *bytes_read_ptr)
19758{
19759 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19760
ed2dc618
SM
19761 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19762 str_offset);
43988095
JK
19763}
19764
19765ULONGEST
d521ce57 19766read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
43988095 19767 unsigned int *bytes_read_ptr)
c906108c 19768{
12df843f 19769 ULONGEST result;
ce5d95e1 19770 unsigned int num_read;
870f88f7 19771 int shift;
c906108c
SS
19772 unsigned char byte;
19773
19774 result = 0;
19775 shift = 0;
19776 num_read = 0;
c906108c
SS
19777 while (1)
19778 {
fe1b8b76 19779 byte = bfd_get_8 (abfd, buf);
c906108c
SS
19780 buf++;
19781 num_read++;
12df843f 19782 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
19783 if ((byte & 128) == 0)
19784 {
19785 break;
19786 }
19787 shift += 7;
19788 }
19789 *bytes_read_ptr = num_read;
19790 return result;
19791}
19792
12df843f 19793static LONGEST
d521ce57
TT
19794read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19795 unsigned int *bytes_read_ptr)
c906108c 19796{
4dd1b460 19797 ULONGEST result;
870f88f7 19798 int shift, num_read;
c906108c
SS
19799 unsigned char byte;
19800
19801 result = 0;
19802 shift = 0;
c906108c 19803 num_read = 0;
c906108c
SS
19804 while (1)
19805 {
fe1b8b76 19806 byte = bfd_get_8 (abfd, buf);
c906108c
SS
19807 buf++;
19808 num_read++;
4dd1b460 19809 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
19810 shift += 7;
19811 if ((byte & 128) == 0)
19812 {
19813 break;
19814 }
19815 }
77e0b926 19816 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
4dd1b460 19817 result |= -(((ULONGEST) 1) << shift);
c906108c
SS
19818 *bytes_read_ptr = num_read;
19819 return result;
19820}
19821
3019eac3
DE
19822/* Given index ADDR_INDEX in .debug_addr, fetch the value.
19823 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
19824 ADDR_SIZE is the size of addresses from the CU header. */
19825
19826static CORE_ADDR
ed2dc618
SM
19827read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19828 unsigned int addr_index, ULONGEST addr_base, int addr_size)
3019eac3
DE
19829{
19830 struct objfile *objfile = dwarf2_per_objfile->objfile;
19831 bfd *abfd = objfile->obfd;
19832 const gdb_byte *info_ptr;
19833
19834 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
19835 if (dwarf2_per_objfile->addr.buffer == NULL)
19836 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 19837 objfile_name (objfile));
3019eac3
DE
19838 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
19839 error (_("DW_FORM_addr_index pointing outside of "
19840 ".debug_addr section [in module %s]"),
4262abfb 19841 objfile_name (objfile));
3019eac3
DE
19842 info_ptr = (dwarf2_per_objfile->addr.buffer
19843 + addr_base + addr_index * addr_size);
19844 if (addr_size == 4)
19845 return bfd_get_32 (abfd, info_ptr);
19846 else
19847 return bfd_get_64 (abfd, info_ptr);
19848}
19849
19850/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19851
19852static CORE_ADDR
19853read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19854{
518817b3
SM
19855 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19856 cu->addr_base, cu->header.addr_size);
3019eac3
DE
19857}
19858
19859/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19860
19861static CORE_ADDR
d521ce57 19862read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
19863 unsigned int *bytes_read)
19864{
518817b3 19865 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
3019eac3
DE
19866 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19867
19868 return read_addr_index (cu, addr_index);
19869}
19870
19871/* Data structure to pass results from dwarf2_read_addr_index_reader
19872 back to dwarf2_read_addr_index. */
19873
19874struct dwarf2_read_addr_index_data
19875{
19876 ULONGEST addr_base;
19877 int addr_size;
19878};
19879
19880/* die_reader_func for dwarf2_read_addr_index. */
19881
19882static void
19883dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 19884 const gdb_byte *info_ptr,
3019eac3
DE
19885 struct die_info *comp_unit_die,
19886 int has_children,
19887 void *data)
19888{
19889 struct dwarf2_cu *cu = reader->cu;
19890 struct dwarf2_read_addr_index_data *aidata =
19891 (struct dwarf2_read_addr_index_data *) data;
19892
19893 aidata->addr_base = cu->addr_base;
19894 aidata->addr_size = cu->header.addr_size;
19895}
19896
19897/* Given an index in .debug_addr, fetch the value.
19898 NOTE: This can be called during dwarf expression evaluation,
19899 long after the debug information has been read, and thus per_cu->cu
19900 may no longer exist. */
19901
19902CORE_ADDR
19903dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19904 unsigned int addr_index)
19905{
ed2dc618 19906 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3
DE
19907 struct dwarf2_cu *cu = per_cu->cu;
19908 ULONGEST addr_base;
19909 int addr_size;
19910
3019eac3
DE
19911 /* We need addr_base and addr_size.
19912 If we don't have PER_CU->cu, we have to get it.
19913 Nasty, but the alternative is storing the needed info in PER_CU,
19914 which at this point doesn't seem justified: it's not clear how frequently
19915 it would get used and it would increase the size of every PER_CU.
19916 Entry points like dwarf2_per_cu_addr_size do a similar thing
19917 so we're not in uncharted territory here.
19918 Alas we need to be a bit more complicated as addr_base is contained
19919 in the DIE.
19920
19921 We don't need to read the entire CU(/TU).
19922 We just need the header and top level die.
a1b64ce1 19923
3019eac3 19924 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 19925 For now we skip this optimization. */
3019eac3
DE
19926
19927 if (cu != NULL)
19928 {
19929 addr_base = cu->addr_base;
19930 addr_size = cu->header.addr_size;
19931 }
19932 else
19933 {
19934 struct dwarf2_read_addr_index_data aidata;
19935
a1b64ce1
DE
19936 /* Note: We can't use init_cutu_and_read_dies_simple here,
19937 we need addr_base. */
58f0c718 19938 init_cutu_and_read_dies (per_cu, NULL, 0, 0, false,
a1b64ce1 19939 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
19940 addr_base = aidata.addr_base;
19941 addr_size = aidata.addr_size;
19942 }
19943
ed2dc618
SM
19944 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19945 addr_size);
3019eac3
DE
19946}
19947
cf532bd1 19948/* Given a DW_FORM_GNU_str_index or DW_FORM_strx, fetch the string.
57d63ce2 19949 This is only used by the Fission support. */
3019eac3 19950
d521ce57 19951static const char *
342587c4 19952read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
3019eac3 19953{
ed2dc618 19954 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
19955 struct dwarf2_per_objfile *dwarf2_per_objfile
19956 = cu->per_cu->dwarf2_per_objfile;
3019eac3 19957 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 19958 const char *objf_name = objfile_name (objfile);
3019eac3 19959 bfd *abfd = objfile->obfd;
73869dc2
DE
19960 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
19961 struct dwarf2_section_info *str_offsets_section =
19962 &reader->dwo_file->sections.str_offsets;
d521ce57 19963 const gdb_byte *info_ptr;
3019eac3 19964 ULONGEST str_offset;
cf532bd1 19965 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 19966
73869dc2
DE
19967 dwarf2_read_section (objfile, str_section);
19968 dwarf2_read_section (objfile, str_offsets_section);
19969 if (str_section->buffer == NULL)
57d63ce2 19970 error (_("%s used without .debug_str.dwo section"
9d8780f0
SM
19971 " in CU at offset %s [in module %s]"),
19972 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19973 if (str_offsets_section->buffer == NULL)
57d63ce2 19974 error (_("%s used without .debug_str_offsets.dwo section"
9d8780f0
SM
19975 " in CU at offset %s [in module %s]"),
19976 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19977 if (str_index * cu->header.offset_size >= str_offsets_section->size)
57d63ce2 19978 error (_("%s pointing outside of .debug_str_offsets.dwo"
9d8780f0
SM
19979 " section in CU at offset %s [in module %s]"),
19980 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19981 info_ptr = (str_offsets_section->buffer
3019eac3
DE
19982 + str_index * cu->header.offset_size);
19983 if (cu->header.offset_size == 4)
19984 str_offset = bfd_get_32 (abfd, info_ptr);
19985 else
19986 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 19987 if (str_offset >= str_section->size)
57d63ce2 19988 error (_("Offset from %s pointing outside of"
9d8780f0
SM
19989 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19990 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19991 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
19992}
19993
3019eac3
DE
19994/* Return the length of an LEB128 number in BUF. */
19995
19996static int
19997leb128_size (const gdb_byte *buf)
19998{
19999 const gdb_byte *begin = buf;
20000 gdb_byte byte;
20001
20002 while (1)
20003 {
20004 byte = *buf++;
20005 if ((byte & 128) == 0)
20006 return buf - begin;
20007 }
20008}
20009
c906108c 20010static void
e142c38c 20011set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
20012{
20013 switch (lang)
20014 {
20015 case DW_LANG_C89:
76bee0cc 20016 case DW_LANG_C99:
0cfd832f 20017 case DW_LANG_C11:
c906108c 20018 case DW_LANG_C:
d1be3247 20019 case DW_LANG_UPC:
e142c38c 20020 cu->language = language_c;
c906108c 20021 break;
9c37b5ae 20022 case DW_LANG_Java:
c906108c 20023 case DW_LANG_C_plus_plus:
0cfd832f
MW
20024 case DW_LANG_C_plus_plus_11:
20025 case DW_LANG_C_plus_plus_14:
e142c38c 20026 cu->language = language_cplus;
c906108c 20027 break;
6aecb9c2
JB
20028 case DW_LANG_D:
20029 cu->language = language_d;
20030 break;
c906108c
SS
20031 case DW_LANG_Fortran77:
20032 case DW_LANG_Fortran90:
b21b22e0 20033 case DW_LANG_Fortran95:
f7de9aab
MW
20034 case DW_LANG_Fortran03:
20035 case DW_LANG_Fortran08:
e142c38c 20036 cu->language = language_fortran;
c906108c 20037 break;
a766d390
DE
20038 case DW_LANG_Go:
20039 cu->language = language_go;
20040 break;
c906108c 20041 case DW_LANG_Mips_Assembler:
e142c38c 20042 cu->language = language_asm;
c906108c
SS
20043 break;
20044 case DW_LANG_Ada83:
8aaf0b47 20045 case DW_LANG_Ada95:
bc5f45f8
JB
20046 cu->language = language_ada;
20047 break;
72019c9c
GM
20048 case DW_LANG_Modula2:
20049 cu->language = language_m2;
20050 break;
fe8e67fd
PM
20051 case DW_LANG_Pascal83:
20052 cu->language = language_pascal;
20053 break;
22566fbd
DJ
20054 case DW_LANG_ObjC:
20055 cu->language = language_objc;
20056 break;
c44af4eb
TT
20057 case DW_LANG_Rust:
20058 case DW_LANG_Rust_old:
20059 cu->language = language_rust;
20060 break;
c906108c
SS
20061 case DW_LANG_Cobol74:
20062 case DW_LANG_Cobol85:
c906108c 20063 default:
e142c38c 20064 cu->language = language_minimal;
c906108c
SS
20065 break;
20066 }
e142c38c 20067 cu->language_defn = language_def (cu->language);
c906108c
SS
20068}
20069
20070/* Return the named attribute or NULL if not there. */
20071
20072static struct attribute *
e142c38c 20073dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 20074{
a48e046c 20075 for (;;)
c906108c 20076 {
a48e046c
TT
20077 unsigned int i;
20078 struct attribute *spec = NULL;
20079
20080 for (i = 0; i < die->num_attrs; ++i)
20081 {
20082 if (die->attrs[i].name == name)
20083 return &die->attrs[i];
20084 if (die->attrs[i].name == DW_AT_specification
20085 || die->attrs[i].name == DW_AT_abstract_origin)
20086 spec = &die->attrs[i];
20087 }
20088
20089 if (!spec)
20090 break;
c906108c 20091
f2f0e013 20092 die = follow_die_ref (die, spec, &cu);
f2f0e013 20093 }
c5aa993b 20094
c906108c
SS
20095 return NULL;
20096}
20097
348e048f
DE
20098/* Return the named attribute or NULL if not there,
20099 but do not follow DW_AT_specification, etc.
20100 This is for use in contexts where we're reading .debug_types dies.
20101 Following DW_AT_specification, DW_AT_abstract_origin will take us
20102 back up the chain, and we want to go down. */
20103
20104static struct attribute *
45e58e77 20105dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
20106{
20107 unsigned int i;
20108
20109 for (i = 0; i < die->num_attrs; ++i)
20110 if (die->attrs[i].name == name)
20111 return &die->attrs[i];
20112
20113 return NULL;
20114}
20115
7d45c7c3
KB
20116/* Return the string associated with a string-typed attribute, or NULL if it
20117 is either not found or is of an incorrect type. */
20118
20119static const char *
20120dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20121{
20122 struct attribute *attr;
20123 const char *str = NULL;
20124
20125 attr = dwarf2_attr (die, name, cu);
20126
20127 if (attr != NULL)
20128 {
43988095 20129 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438 20130 || attr->form == DW_FORM_string
cf532bd1 20131 || attr->form == DW_FORM_strx
b3340438 20132 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 20133 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
20134 str = DW_STRING (attr);
20135 else
b98664d3 20136 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
20137 "DIE at %s in module %s"),
20138 dwarf_attr_name (name), sect_offset_str (die->sect_off),
518817b3 20139 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7d45c7c3
KB
20140 }
20141
20142 return str;
20143}
20144
a084a2a6
AT
20145/* Return the dwo name or NULL if not present. If present, it is in either
20146 DW_AT_GNU_dwo_name or DW_AT_dwo_name atrribute. */
20147static const char *
20148dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20149{
20150 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20151 if (dwo_name == nullptr)
20152 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20153 return dwo_name;
20154}
20155
05cf31d1
JB
20156/* Return non-zero iff the attribute NAME is defined for the given DIE,
20157 and holds a non-zero value. This function should only be used for
2dc7f7b3 20158 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
20159
20160static int
20161dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20162{
20163 struct attribute *attr = dwarf2_attr (die, name, cu);
20164
20165 return (attr && DW_UNSND (attr));
20166}
20167
3ca72b44 20168static int
e142c38c 20169die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 20170{
05cf31d1
JB
20171 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20172 which value is non-zero. However, we have to be careful with
20173 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20174 (via dwarf2_flag_true_p) follows this attribute. So we may
20175 end up accidently finding a declaration attribute that belongs
20176 to a different DIE referenced by the specification attribute,
20177 even though the given DIE does not have a declaration attribute. */
20178 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20179 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
20180}
20181
63d06c5c 20182/* Return the die giving the specification for DIE, if there is
f2f0e013 20183 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
20184 containing the return value on output. If there is no
20185 specification, but there is an abstract origin, that is
20186 returned. */
63d06c5c
DC
20187
20188static struct die_info *
f2f0e013 20189die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 20190{
f2f0e013
DJ
20191 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20192 *spec_cu);
63d06c5c 20193
edb3359d
DJ
20194 if (spec_attr == NULL)
20195 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20196
63d06c5c
DC
20197 if (spec_attr == NULL)
20198 return NULL;
20199 else
f2f0e013 20200 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 20201}
c906108c 20202
527f3840
JK
20203/* Stub for free_line_header to match void * callback types. */
20204
20205static void
20206free_line_header_voidp (void *arg)
20207{
9a3c8263 20208 struct line_header *lh = (struct line_header *) arg;
527f3840 20209
fff8551c 20210 delete lh;
527f3840
JK
20211}
20212
fff8551c
PA
20213void
20214line_header::add_include_dir (const char *include_dir)
c906108c 20215{
27e0867f 20216 if (dwarf_line_debug >= 2)
fff8551c
PA
20217 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
20218 include_dirs.size () + 1, include_dir);
27e0867f 20219
fff8551c 20220 include_dirs.push_back (include_dir);
debd256d 20221}
6e70227d 20222
fff8551c
PA
20223void
20224line_header::add_file_name (const char *name,
ecfb656c 20225 dir_index d_index,
fff8551c
PA
20226 unsigned int mod_time,
20227 unsigned int length)
debd256d 20228{
27e0867f
DE
20229 if (dwarf_line_debug >= 2)
20230 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
fff8551c 20231 (unsigned) file_names.size () + 1, name);
27e0867f 20232
ecfb656c 20233 file_names.emplace_back (name, d_index, mod_time, length);
debd256d 20234}
6e70227d 20235
83769d0b 20236/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
20237
20238static struct dwarf2_section_info *
20239get_debug_line_section (struct dwarf2_cu *cu)
20240{
20241 struct dwarf2_section_info *section;
518817b3
SM
20242 struct dwarf2_per_objfile *dwarf2_per_objfile
20243 = cu->per_cu->dwarf2_per_objfile;
36586728
TT
20244
20245 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20246 DWO file. */
20247 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20248 section = &cu->dwo_unit->dwo_file->sections.line;
20249 else if (cu->per_cu->is_dwz)
20250 {
ed2dc618 20251 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
20252
20253 section = &dwz->line;
20254 }
20255 else
20256 section = &dwarf2_per_objfile->line;
20257
20258 return section;
20259}
20260
43988095
JK
20261/* Read directory or file name entry format, starting with byte of
20262 format count entries, ULEB128 pairs of entry formats, ULEB128 of
20263 entries count and the entries themselves in the described entry
20264 format. */
20265
20266static void
ed2dc618
SM
20267read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
20268 bfd *abfd, const gdb_byte **bufp,
43988095
JK
20269 struct line_header *lh,
20270 const struct comp_unit_head *cu_header,
20271 void (*callback) (struct line_header *lh,
20272 const char *name,
ecfb656c 20273 dir_index d_index,
43988095
JK
20274 unsigned int mod_time,
20275 unsigned int length))
20276{
20277 gdb_byte format_count, formati;
20278 ULONGEST data_count, datai;
20279 const gdb_byte *buf = *bufp;
20280 const gdb_byte *format_header_data;
43988095
JK
20281 unsigned int bytes_read;
20282
20283 format_count = read_1_byte (abfd, buf);
20284 buf += 1;
20285 format_header_data = buf;
20286 for (formati = 0; formati < format_count; formati++)
20287 {
20288 read_unsigned_leb128 (abfd, buf, &bytes_read);
20289 buf += bytes_read;
20290 read_unsigned_leb128 (abfd, buf, &bytes_read);
20291 buf += bytes_read;
20292 }
20293
20294 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20295 buf += bytes_read;
20296 for (datai = 0; datai < data_count; datai++)
20297 {
20298 const gdb_byte *format = format_header_data;
20299 struct file_entry fe;
20300
43988095
JK
20301 for (formati = 0; formati < format_count; formati++)
20302 {
ecfb656c 20303 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20304 format += bytes_read;
43988095 20305
ecfb656c 20306 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20307 format += bytes_read;
ecfb656c
PA
20308
20309 gdb::optional<const char *> string;
20310 gdb::optional<unsigned int> uint;
20311
43988095
JK
20312 switch (form)
20313 {
20314 case DW_FORM_string:
ecfb656c 20315 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
20316 buf += bytes_read;
20317 break;
20318
20319 case DW_FORM_line_strp:
ed2dc618
SM
20320 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20321 abfd, buf,
ecfb656c
PA
20322 cu_header,
20323 &bytes_read));
43988095
JK
20324 buf += bytes_read;
20325 break;
20326
20327 case DW_FORM_data1:
ecfb656c 20328 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
20329 buf += 1;
20330 break;
20331
20332 case DW_FORM_data2:
ecfb656c 20333 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
20334 buf += 2;
20335 break;
20336
20337 case DW_FORM_data4:
ecfb656c 20338 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
20339 buf += 4;
20340 break;
20341
20342 case DW_FORM_data8:
ecfb656c 20343 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
20344 buf += 8;
20345 break;
20346
20347 case DW_FORM_udata:
ecfb656c 20348 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
20349 buf += bytes_read;
20350 break;
20351
20352 case DW_FORM_block:
20353 /* It is valid only for DW_LNCT_timestamp which is ignored by
20354 current GDB. */
20355 break;
20356 }
ecfb656c
PA
20357
20358 switch (content_type)
20359 {
20360 case DW_LNCT_path:
20361 if (string.has_value ())
20362 fe.name = *string;
20363 break;
20364 case DW_LNCT_directory_index:
20365 if (uint.has_value ())
20366 fe.d_index = (dir_index) *uint;
20367 break;
20368 case DW_LNCT_timestamp:
20369 if (uint.has_value ())
20370 fe.mod_time = *uint;
20371 break;
20372 case DW_LNCT_size:
20373 if (uint.has_value ())
20374 fe.length = *uint;
20375 break;
20376 case DW_LNCT_MD5:
20377 break;
20378 default:
b98664d3 20379 complaint (_("Unknown format content type %s"),
ecfb656c
PA
20380 pulongest (content_type));
20381 }
43988095
JK
20382 }
20383
ecfb656c 20384 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
20385 }
20386
20387 *bufp = buf;
20388}
20389
debd256d 20390/* Read the statement program header starting at OFFSET in
3019eac3 20391 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 20392 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
20393 Returns NULL if there is a problem reading the header, e.g., if it
20394 has a version we don't understand.
debd256d
JB
20395
20396 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
20397 the returned object point into the dwarf line section buffer,
20398 and must not be freed. */
ae2de4f8 20399
fff8551c 20400static line_header_up
9c541725 20401dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 20402{
d521ce57 20403 const gdb_byte *line_ptr;
c764a876 20404 unsigned int bytes_read, offset_size;
debd256d 20405 int i;
d521ce57 20406 const char *cur_dir, *cur_file;
3019eac3
DE
20407 struct dwarf2_section_info *section;
20408 bfd *abfd;
518817b3
SM
20409 struct dwarf2_per_objfile *dwarf2_per_objfile
20410 = cu->per_cu->dwarf2_per_objfile;
3019eac3 20411
36586728 20412 section = get_debug_line_section (cu);
3019eac3
DE
20413 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20414 if (section->buffer == NULL)
debd256d 20415 {
3019eac3 20416 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 20417 complaint (_("missing .debug_line.dwo section"));
3019eac3 20418 else
b98664d3 20419 complaint (_("missing .debug_line section"));
debd256d
JB
20420 return 0;
20421 }
20422
fceca515
DE
20423 /* We can't do this until we know the section is non-empty.
20424 Only then do we know we have such a section. */
a32a8923 20425 abfd = get_section_bfd_owner (section);
fceca515 20426
a738430d
MK
20427 /* Make sure that at least there's room for the total_length field.
20428 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 20429 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 20430 {
4d3c2250 20431 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20432 return 0;
20433 }
20434
fff8551c 20435 line_header_up lh (new line_header ());
debd256d 20436
9c541725 20437 lh->sect_off = sect_off;
527f3840
JK
20438 lh->offset_in_dwz = cu->per_cu->is_dwz;
20439
9c541725 20440 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 20441
a738430d 20442 /* Read in the header. */
6e70227d 20443 lh->total_length =
c764a876
DE
20444 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20445 &bytes_read, &offset_size);
debd256d 20446 line_ptr += bytes_read;
3019eac3 20447 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 20448 {
4d3c2250 20449 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20450 return 0;
20451 }
20452 lh->statement_program_end = line_ptr + lh->total_length;
20453 lh->version = read_2_bytes (abfd, line_ptr);
20454 line_ptr += 2;
43988095 20455 if (lh->version > 5)
cd366ee8
DE
20456 {
20457 /* This is a version we don't understand. The format could have
20458 changed in ways we don't handle properly so just punt. */
b98664d3 20459 complaint (_("unsupported version in .debug_line section"));
cd366ee8
DE
20460 return NULL;
20461 }
43988095
JK
20462 if (lh->version >= 5)
20463 {
20464 gdb_byte segment_selector_size;
20465
20466 /* Skip address size. */
20467 read_1_byte (abfd, line_ptr);
20468 line_ptr += 1;
20469
20470 segment_selector_size = read_1_byte (abfd, line_ptr);
20471 line_ptr += 1;
20472 if (segment_selector_size != 0)
20473 {
b98664d3 20474 complaint (_("unsupported segment selector size %u "
43988095
JK
20475 "in .debug_line section"),
20476 segment_selector_size);
20477 return NULL;
20478 }
20479 }
c764a876
DE
20480 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20481 line_ptr += offset_size;
debd256d
JB
20482 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20483 line_ptr += 1;
2dc7f7b3
TT
20484 if (lh->version >= 4)
20485 {
20486 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20487 line_ptr += 1;
20488 }
20489 else
20490 lh->maximum_ops_per_instruction = 1;
20491
20492 if (lh->maximum_ops_per_instruction == 0)
20493 {
20494 lh->maximum_ops_per_instruction = 1;
b98664d3 20495 complaint (_("invalid maximum_ops_per_instruction "
3e43a32a 20496 "in `.debug_line' section"));
2dc7f7b3
TT
20497 }
20498
debd256d
JB
20499 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20500 line_ptr += 1;
20501 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20502 line_ptr += 1;
20503 lh->line_range = read_1_byte (abfd, line_ptr);
20504 line_ptr += 1;
20505 lh->opcode_base = read_1_byte (abfd, line_ptr);
20506 line_ptr += 1;
fff8551c 20507 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
20508
20509 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20510 for (i = 1; i < lh->opcode_base; ++i)
20511 {
20512 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20513 line_ptr += 1;
20514 }
20515
43988095 20516 if (lh->version >= 5)
debd256d 20517 {
43988095 20518 /* Read directory table. */
ed2dc618
SM
20519 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20520 &cu->header,
b926417a 20521 [] (struct line_header *header, const char *name,
ecfb656c 20522 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20523 unsigned int length)
20524 {
b926417a 20525 header->add_include_dir (name);
fff8551c 20526 });
debd256d 20527
43988095 20528 /* Read file name table. */
ed2dc618
SM
20529 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20530 &cu->header,
b926417a 20531 [] (struct line_header *header, const char *name,
ecfb656c 20532 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20533 unsigned int length)
20534 {
b926417a 20535 header->add_file_name (name, d_index, mod_time, length);
fff8551c 20536 });
43988095
JK
20537 }
20538 else
debd256d 20539 {
43988095
JK
20540 /* Read directory table. */
20541 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20542 {
20543 line_ptr += bytes_read;
fff8551c 20544 lh->add_include_dir (cur_dir);
43988095 20545 }
debd256d
JB
20546 line_ptr += bytes_read;
20547
43988095
JK
20548 /* Read file name table. */
20549 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20550 {
ecfb656c
PA
20551 unsigned int mod_time, length;
20552 dir_index d_index;
43988095
JK
20553
20554 line_ptr += bytes_read;
ecfb656c 20555 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
20556 line_ptr += bytes_read;
20557 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20558 line_ptr += bytes_read;
20559 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20560 line_ptr += bytes_read;
20561
ecfb656c 20562 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
20563 }
20564 line_ptr += bytes_read;
debd256d 20565 }
6e70227d 20566 lh->statement_program_start = line_ptr;
debd256d 20567
3019eac3 20568 if (line_ptr > (section->buffer + section->size))
b98664d3 20569 complaint (_("line number info header doesn't "
3e43a32a 20570 "fit in `.debug_line' section"));
debd256d 20571
debd256d
JB
20572 return lh;
20573}
c906108c 20574
c6da4cef
DE
20575/* Subroutine of dwarf_decode_lines to simplify it.
20576 Return the file name of the psymtab for included file FILE_INDEX
20577 in line header LH of PST.
20578 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
20579 If space for the result is malloc'd, *NAME_HOLDER will be set.
20580 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 20581
d521ce57 20582static const char *
c6da4cef
DE
20583psymtab_include_file_name (const struct line_header *lh, int file_index,
20584 const struct partial_symtab *pst,
c89b44cd
TT
20585 const char *comp_dir,
20586 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 20587{
8c43009f 20588 const file_entry &fe = lh->file_names[file_index];
d521ce57
TT
20589 const char *include_name = fe.name;
20590 const char *include_name_to_compare = include_name;
72b9f47f 20591 const char *pst_filename;
c6da4cef
DE
20592 int file_is_pst;
20593
8c43009f 20594 const char *dir_name = fe.include_dir (lh);
c6da4cef 20595
c89b44cd 20596 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
20597 if (!IS_ABSOLUTE_PATH (include_name)
20598 && (dir_name != NULL || comp_dir != NULL))
20599 {
20600 /* Avoid creating a duplicate psymtab for PST.
20601 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20602 Before we do the comparison, however, we need to account
20603 for DIR_NAME and COMP_DIR.
20604 First prepend dir_name (if non-NULL). If we still don't
20605 have an absolute path prepend comp_dir (if non-NULL).
20606 However, the directory we record in the include-file's
20607 psymtab does not contain COMP_DIR (to match the
20608 corresponding symtab(s)).
20609
20610 Example:
20611
20612 bash$ cd /tmp
20613 bash$ gcc -g ./hello.c
20614 include_name = "hello.c"
20615 dir_name = "."
20616 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
20617 DW_AT_name = "./hello.c"
20618
20619 */
c6da4cef
DE
20620
20621 if (dir_name != NULL)
20622 {
c89b44cd
TT
20623 name_holder->reset (concat (dir_name, SLASH_STRING,
20624 include_name, (char *) NULL));
20625 include_name = name_holder->get ();
c6da4cef 20626 include_name_to_compare = include_name;
c6da4cef
DE
20627 }
20628 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20629 {
c89b44cd
TT
20630 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20631 include_name, (char *) NULL));
20632 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
20633 }
20634 }
20635
20636 pst_filename = pst->filename;
c89b44cd 20637 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
20638 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20639 {
c89b44cd
TT
20640 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20641 pst_filename, (char *) NULL));
20642 pst_filename = copied_name.get ();
c6da4cef
DE
20643 }
20644
1e3fad37 20645 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 20646
c6da4cef
DE
20647 if (file_is_pst)
20648 return NULL;
20649 return include_name;
20650}
20651
d9b3de22
DE
20652/* State machine to track the state of the line number program. */
20653
6f77053d 20654class lnp_state_machine
d9b3de22 20655{
6f77053d
PA
20656public:
20657 /* Initialize a machine state for the start of a line number
20658 program. */
804d2729
TT
20659 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20660 bool record_lines_p);
6f77053d 20661
8c43009f
PA
20662 file_entry *current_file ()
20663 {
20664 /* lh->file_names is 0-based, but the file name numbers in the
20665 statement program are 1-based. */
6f77053d
PA
20666 return m_line_header->file_name_at (m_file);
20667 }
20668
20669 /* Record the line in the state machine. END_SEQUENCE is true if
20670 we're processing the end of a sequence. */
20671 void record_line (bool end_sequence);
20672
7ab6656f
OJ
20673 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
20674 nop-out rest of the lines in this sequence. */
6f77053d
PA
20675 void check_line_address (struct dwarf2_cu *cu,
20676 const gdb_byte *line_ptr,
7ab6656f 20677 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
20678
20679 void handle_set_discriminator (unsigned int discriminator)
20680 {
20681 m_discriminator = discriminator;
20682 m_line_has_non_zero_discriminator |= discriminator != 0;
20683 }
20684
20685 /* Handle DW_LNE_set_address. */
20686 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20687 {
20688 m_op_index = 0;
20689 address += baseaddr;
20690 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20691 }
20692
20693 /* Handle DW_LNS_advance_pc. */
20694 void handle_advance_pc (CORE_ADDR adjust);
20695
20696 /* Handle a special opcode. */
20697 void handle_special_opcode (unsigned char op_code);
20698
20699 /* Handle DW_LNS_advance_line. */
20700 void handle_advance_line (int line_delta)
20701 {
20702 advance_line (line_delta);
20703 }
20704
20705 /* Handle DW_LNS_set_file. */
20706 void handle_set_file (file_name_index file);
20707
20708 /* Handle DW_LNS_negate_stmt. */
20709 void handle_negate_stmt ()
20710 {
20711 m_is_stmt = !m_is_stmt;
20712 }
20713
20714 /* Handle DW_LNS_const_add_pc. */
20715 void handle_const_add_pc ();
20716
20717 /* Handle DW_LNS_fixed_advance_pc. */
20718 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20719 {
20720 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20721 m_op_index = 0;
20722 }
20723
20724 /* Handle DW_LNS_copy. */
20725 void handle_copy ()
20726 {
20727 record_line (false);
20728 m_discriminator = 0;
20729 }
20730
20731 /* Handle DW_LNE_end_sequence. */
20732 void handle_end_sequence ()
20733 {
804d2729 20734 m_currently_recording_lines = true;
6f77053d
PA
20735 }
20736
20737private:
20738 /* Advance the line by LINE_DELTA. */
20739 void advance_line (int line_delta)
20740 {
20741 m_line += line_delta;
20742
20743 if (line_delta != 0)
20744 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
20745 }
20746
804d2729
TT
20747 struct dwarf2_cu *m_cu;
20748
6f77053d
PA
20749 gdbarch *m_gdbarch;
20750
20751 /* True if we're recording lines.
20752 Otherwise we're building partial symtabs and are just interested in
20753 finding include files mentioned by the line number program. */
20754 bool m_record_lines_p;
20755
8c43009f 20756 /* The line number header. */
6f77053d 20757 line_header *m_line_header;
8c43009f 20758
6f77053d
PA
20759 /* These are part of the standard DWARF line number state machine,
20760 and initialized according to the DWARF spec. */
d9b3de22 20761
6f77053d 20762 unsigned char m_op_index = 0;
8c43009f 20763 /* The line table index (1-based) of the current file. */
6f77053d
PA
20764 file_name_index m_file = (file_name_index) 1;
20765 unsigned int m_line = 1;
20766
20767 /* These are initialized in the constructor. */
20768
20769 CORE_ADDR m_address;
20770 bool m_is_stmt;
20771 unsigned int m_discriminator;
d9b3de22
DE
20772
20773 /* Additional bits of state we need to track. */
20774
20775 /* The last file that we called dwarf2_start_subfile for.
20776 This is only used for TLLs. */
6f77053d 20777 unsigned int m_last_file = 0;
d9b3de22 20778 /* The last file a line number was recorded for. */
6f77053d 20779 struct subfile *m_last_subfile = NULL;
d9b3de22 20780
804d2729
TT
20781 /* When true, record the lines we decode. */
20782 bool m_currently_recording_lines = false;
d9b3de22
DE
20783
20784 /* The last line number that was recorded, used to coalesce
20785 consecutive entries for the same line. This can happen, for
20786 example, when discriminators are present. PR 17276. */
6f77053d
PA
20787 unsigned int m_last_line = 0;
20788 bool m_line_has_non_zero_discriminator = false;
8c43009f 20789};
d9b3de22 20790
6f77053d
PA
20791void
20792lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20793{
20794 CORE_ADDR addr_adj = (((m_op_index + adjust)
20795 / m_line_header->maximum_ops_per_instruction)
20796 * m_line_header->minimum_instruction_length);
20797 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20798 m_op_index = ((m_op_index + adjust)
20799 % m_line_header->maximum_ops_per_instruction);
20800}
d9b3de22 20801
6f77053d
PA
20802void
20803lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 20804{
6f77053d
PA
20805 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20806 CORE_ADDR addr_adj = (((m_op_index
20807 + (adj_opcode / m_line_header->line_range))
20808 / m_line_header->maximum_ops_per_instruction)
20809 * m_line_header->minimum_instruction_length);
20810 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20811 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20812 % m_line_header->maximum_ops_per_instruction);
d9b3de22 20813
6f77053d
PA
20814 int line_delta = (m_line_header->line_base
20815 + (adj_opcode % m_line_header->line_range));
20816 advance_line (line_delta);
20817 record_line (false);
20818 m_discriminator = 0;
20819}
d9b3de22 20820
6f77053d
PA
20821void
20822lnp_state_machine::handle_set_file (file_name_index file)
20823{
20824 m_file = file;
20825
20826 const file_entry *fe = current_file ();
20827 if (fe == NULL)
20828 dwarf2_debug_line_missing_file_complaint ();
20829 else if (m_record_lines_p)
20830 {
20831 const char *dir = fe->include_dir (m_line_header);
20832
c24bdb02 20833 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20834 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 20835 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
20836 }
20837}
20838
20839void
20840lnp_state_machine::handle_const_add_pc ()
20841{
20842 CORE_ADDR adjust
20843 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20844
20845 CORE_ADDR addr_adj
20846 = (((m_op_index + adjust)
20847 / m_line_header->maximum_ops_per_instruction)
20848 * m_line_header->minimum_instruction_length);
20849
20850 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20851 m_op_index = ((m_op_index + adjust)
20852 % m_line_header->maximum_ops_per_instruction);
20853}
d9b3de22 20854
a05a36a5
DE
20855/* Return non-zero if we should add LINE to the line number table.
20856 LINE is the line to add, LAST_LINE is the last line that was added,
20857 LAST_SUBFILE is the subfile for LAST_LINE.
20858 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20859 had a non-zero discriminator.
20860
20861 We have to be careful in the presence of discriminators.
20862 E.g., for this line:
20863
20864 for (i = 0; i < 100000; i++);
20865
20866 clang can emit four line number entries for that one line,
20867 each with a different discriminator.
20868 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20869
20870 However, we want gdb to coalesce all four entries into one.
20871 Otherwise the user could stepi into the middle of the line and
20872 gdb would get confused about whether the pc really was in the
20873 middle of the line.
20874
20875 Things are further complicated by the fact that two consecutive
20876 line number entries for the same line is a heuristic used by gcc
20877 to denote the end of the prologue. So we can't just discard duplicate
20878 entries, we have to be selective about it. The heuristic we use is
20879 that we only collapse consecutive entries for the same line if at least
20880 one of those entries has a non-zero discriminator. PR 17276.
20881
20882 Note: Addresses in the line number state machine can never go backwards
20883 within one sequence, thus this coalescing is ok. */
20884
20885static int
804d2729
TT
20886dwarf_record_line_p (struct dwarf2_cu *cu,
20887 unsigned int line, unsigned int last_line,
a05a36a5
DE
20888 int line_has_non_zero_discriminator,
20889 struct subfile *last_subfile)
20890{
c24bdb02 20891 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
20892 return 1;
20893 if (line != last_line)
20894 return 1;
20895 /* Same line for the same file that we've seen already.
20896 As a last check, for pr 17276, only record the line if the line
20897 has never had a non-zero discriminator. */
20898 if (!line_has_non_zero_discriminator)
20899 return 1;
20900 return 0;
20901}
20902
804d2729
TT
20903/* Use the CU's builder to record line number LINE beginning at
20904 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
20905
20906static void
d9b3de22
DE
20907dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20908 unsigned int line, CORE_ADDR address,
804d2729 20909 struct dwarf2_cu *cu)
252a6764
DE
20910{
20911 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20912
27e0867f
DE
20913 if (dwarf_line_debug)
20914 {
20915 fprintf_unfiltered (gdb_stdlog,
20916 "Recording line %u, file %s, address %s\n",
20917 line, lbasename (subfile->name),
20918 paddress (gdbarch, address));
20919 }
20920
804d2729 20921 if (cu != nullptr)
c24bdb02 20922 cu->get_builder ()->record_line (subfile, line, addr);
252a6764
DE
20923}
20924
20925/* Subroutine of dwarf_decode_lines_1 to simplify it.
20926 Mark the end of a set of line number records.
d9b3de22 20927 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
20928 If SUBFILE is NULL the request is ignored. */
20929
20930static void
20931dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 20932 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 20933{
27e0867f
DE
20934 if (subfile == NULL)
20935 return;
20936
20937 if (dwarf_line_debug)
20938 {
20939 fprintf_unfiltered (gdb_stdlog,
20940 "Finishing current line, file %s, address %s\n",
20941 lbasename (subfile->name),
20942 paddress (gdbarch, address));
20943 }
20944
804d2729 20945 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
d9b3de22
DE
20946}
20947
6f77053d
PA
20948void
20949lnp_state_machine::record_line (bool end_sequence)
d9b3de22 20950{
d9b3de22
DE
20951 if (dwarf_line_debug)
20952 {
20953 fprintf_unfiltered (gdb_stdlog,
20954 "Processing actual line %u: file %u,"
20955 " address %s, is_stmt %u, discrim %u\n",
6f77053d
PA
20956 m_line, to_underlying (m_file),
20957 paddress (m_gdbarch, m_address),
20958 m_is_stmt, m_discriminator);
d9b3de22
DE
20959 }
20960
6f77053d 20961 file_entry *fe = current_file ();
8c43009f
PA
20962
20963 if (fe == NULL)
d9b3de22
DE
20964 dwarf2_debug_line_missing_file_complaint ();
20965 /* For now we ignore lines not starting on an instruction boundary.
20966 But not when processing end_sequence for compatibility with the
20967 previous version of the code. */
6f77053d 20968 else if (m_op_index == 0 || end_sequence)
d9b3de22 20969 {
8c43009f 20970 fe->included_p = 1;
c258c396 20971 if (m_record_lines_p && (producer_is_codewarrior (m_cu) || m_is_stmt))
d9b3de22 20972 {
c24bdb02 20973 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
804d2729 20974 || end_sequence)
d9b3de22 20975 {
804d2729
TT
20976 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20977 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
20978 }
20979
20980 if (!end_sequence)
20981 {
804d2729 20982 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
20983 m_line_has_non_zero_discriminator,
20984 m_last_subfile))
d9b3de22 20985 {
c24bdb02 20986 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 20987 dwarf_record_line_1 (m_gdbarch,
c24bdb02 20988 builder->get_current_subfile (),
6f77053d 20989 m_line, m_address,
804d2729 20990 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 20991 }
c24bdb02 20992 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20993 m_last_line = m_line;
d9b3de22
DE
20994 }
20995 }
20996 }
20997}
20998
804d2729
TT
20999lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21000 line_header *lh, bool record_lines_p)
d9b3de22 21001{
804d2729 21002 m_cu = cu;
6f77053d
PA
21003 m_gdbarch = arch;
21004 m_record_lines_p = record_lines_p;
21005 m_line_header = lh;
d9b3de22 21006
804d2729 21007 m_currently_recording_lines = true;
d9b3de22 21008
d9b3de22
DE
21009 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21010 was a line entry for it so that the backend has a chance to adjust it
21011 and also record it in case it needs it. This is currently used by MIPS
21012 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
21013 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21014 m_is_stmt = lh->default_is_stmt;
21015 m_discriminator = 0;
252a6764
DE
21016}
21017
6f77053d
PA
21018void
21019lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21020 const gdb_byte *line_ptr,
7ab6656f 21021 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 21022{
7ab6656f
OJ
21023 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
21024 the pc range of the CU. However, we restrict the test to only ADDRESS
21025 values of zero to preserve GDB's previous behaviour which is to handle
21026 the specific case of a function being GC'd by the linker. */
924c2928 21027
7ab6656f 21028 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
21029 {
21030 /* This line table is for a function which has been
21031 GCd by the linker. Ignore it. PR gdb/12528 */
21032
518817b3 21033 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
924c2928
DE
21034 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21035
b98664d3 21036 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 21037 line_offset, objfile_name (objfile));
804d2729
TT
21038 m_currently_recording_lines = false;
21039 /* Note: m_currently_recording_lines is left as false until we see
21040 DW_LNE_end_sequence. */
924c2928
DE
21041 }
21042}
21043
f3f5162e 21044/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
21045 Process the line number information in LH.
21046 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21047 program in order to set included_p for every referenced header. */
debd256d 21048
c906108c 21049static void
43f3e411
DE
21050dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21051 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 21052{
d521ce57
TT
21053 const gdb_byte *line_ptr, *extended_end;
21054 const gdb_byte *line_end;
a8c50c1f 21055 unsigned int bytes_read, extended_len;
699ca60a 21056 unsigned char op_code, extended_op;
e142c38c 21057 CORE_ADDR baseaddr;
518817b3 21058 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 21059 bfd *abfd = objfile->obfd;
fbf65064 21060 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
21061 /* True if we're recording line info (as opposed to building partial
21062 symtabs and just interested in finding include files mentioned by
21063 the line number program). */
21064 bool record_lines_p = !decode_for_pst_p;
e142c38c
DJ
21065
21066 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 21067
debd256d
JB
21068 line_ptr = lh->statement_program_start;
21069 line_end = lh->statement_program_end;
c906108c
SS
21070
21071 /* Read the statement sequences until there's nothing left. */
21072 while (line_ptr < line_end)
21073 {
6f77053d
PA
21074 /* The DWARF line number program state machine. Reset the state
21075 machine at the start of each sequence. */
804d2729 21076 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 21077 bool end_sequence = false;
d9b3de22 21078
8c43009f 21079 if (record_lines_p)
c906108c 21080 {
8c43009f
PA
21081 /* Start a subfile for the current file of the state
21082 machine. */
21083 const file_entry *fe = state_machine.current_file ();
21084
21085 if (fe != NULL)
804d2729 21086 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
21087 }
21088
a738430d 21089 /* Decode the table. */
d9b3de22 21090 while (line_ptr < line_end && !end_sequence)
c906108c
SS
21091 {
21092 op_code = read_1_byte (abfd, line_ptr);
21093 line_ptr += 1;
9aa1fe7e 21094
debd256d 21095 if (op_code >= lh->opcode_base)
6e70227d 21096 {
8e07a239 21097 /* Special opcode. */
6f77053d 21098 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
21099 }
21100 else switch (op_code)
c906108c
SS
21101 {
21102 case DW_LNS_extended_op:
3e43a32a
MS
21103 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21104 &bytes_read);
473b7be6 21105 line_ptr += bytes_read;
a8c50c1f 21106 extended_end = line_ptr + extended_len;
c906108c
SS
21107 extended_op = read_1_byte (abfd, line_ptr);
21108 line_ptr += 1;
21109 switch (extended_op)
21110 {
21111 case DW_LNE_end_sequence:
6f77053d
PA
21112 state_machine.handle_end_sequence ();
21113 end_sequence = true;
c906108c
SS
21114 break;
21115 case DW_LNE_set_address:
d9b3de22
DE
21116 {
21117 CORE_ADDR address
21118 = read_address (abfd, line_ptr, cu, &bytes_read);
d9b3de22 21119 line_ptr += bytes_read;
6f77053d
PA
21120
21121 state_machine.check_line_address (cu, line_ptr,
7ab6656f 21122 lowpc - baseaddr, address);
6f77053d 21123 state_machine.handle_set_address (baseaddr, address);
d9b3de22 21124 }
c906108c
SS
21125 break;
21126 case DW_LNE_define_file:
debd256d 21127 {
d521ce57 21128 const char *cur_file;
ecfb656c
PA
21129 unsigned int mod_time, length;
21130 dir_index dindex;
6e70227d 21131
3e43a32a
MS
21132 cur_file = read_direct_string (abfd, line_ptr,
21133 &bytes_read);
debd256d 21134 line_ptr += bytes_read;
ecfb656c 21135 dindex = (dir_index)
debd256d
JB
21136 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21137 line_ptr += bytes_read;
21138 mod_time =
21139 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21140 line_ptr += bytes_read;
21141 length =
21142 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21143 line_ptr += bytes_read;
ecfb656c 21144 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 21145 }
c906108c 21146 break;
d0c6ba3d 21147 case DW_LNE_set_discriminator:
6f77053d
PA
21148 {
21149 /* The discriminator is not interesting to the
21150 debugger; just ignore it. We still need to
21151 check its value though:
21152 if there are consecutive entries for the same
21153 (non-prologue) line we want to coalesce them.
21154 PR 17276. */
21155 unsigned int discr
21156 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21157 line_ptr += bytes_read;
21158
21159 state_machine.handle_set_discriminator (discr);
21160 }
d0c6ba3d 21161 break;
c906108c 21162 default:
b98664d3 21163 complaint (_("mangled .debug_line section"));
debd256d 21164 return;
c906108c 21165 }
a8c50c1f
DJ
21166 /* Make sure that we parsed the extended op correctly. If e.g.
21167 we expected a different address size than the producer used,
21168 we may have read the wrong number of bytes. */
21169 if (line_ptr != extended_end)
21170 {
b98664d3 21171 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
21172 return;
21173 }
c906108c
SS
21174 break;
21175 case DW_LNS_copy:
6f77053d 21176 state_machine.handle_copy ();
c906108c
SS
21177 break;
21178 case DW_LNS_advance_pc:
2dc7f7b3
TT
21179 {
21180 CORE_ADDR adjust
21181 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 21182 line_ptr += bytes_read;
6f77053d
PA
21183
21184 state_machine.handle_advance_pc (adjust);
2dc7f7b3 21185 }
c906108c
SS
21186 break;
21187 case DW_LNS_advance_line:
a05a36a5
DE
21188 {
21189 int line_delta
21190 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 21191 line_ptr += bytes_read;
6f77053d
PA
21192
21193 state_machine.handle_advance_line (line_delta);
a05a36a5 21194 }
c906108c
SS
21195 break;
21196 case DW_LNS_set_file:
d9b3de22 21197 {
6f77053d 21198 file_name_index file
ecfb656c
PA
21199 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21200 &bytes_read);
d9b3de22 21201 line_ptr += bytes_read;
8c43009f 21202
6f77053d 21203 state_machine.handle_set_file (file);
d9b3de22 21204 }
c906108c
SS
21205 break;
21206 case DW_LNS_set_column:
0ad93d4f 21207 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
21208 line_ptr += bytes_read;
21209 break;
21210 case DW_LNS_negate_stmt:
6f77053d 21211 state_machine.handle_negate_stmt ();
c906108c
SS
21212 break;
21213 case DW_LNS_set_basic_block:
c906108c 21214 break;
c2c6d25f
JM
21215 /* Add to the address register of the state machine the
21216 address increment value corresponding to special opcode
a738430d
MK
21217 255. I.e., this value is scaled by the minimum
21218 instruction length since special opcode 255 would have
b021a221 21219 scaled the increment. */
c906108c 21220 case DW_LNS_const_add_pc:
6f77053d 21221 state_machine.handle_const_add_pc ();
c906108c
SS
21222 break;
21223 case DW_LNS_fixed_advance_pc:
3e29f34a 21224 {
6f77053d 21225 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 21226 line_ptr += 2;
6f77053d
PA
21227
21228 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 21229 }
c906108c 21230 break;
9aa1fe7e 21231 default:
a738430d
MK
21232 {
21233 /* Unknown standard opcode, ignore it. */
9aa1fe7e 21234 int i;
a738430d 21235
debd256d 21236 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
21237 {
21238 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21239 line_ptr += bytes_read;
21240 }
21241 }
c906108c
SS
21242 }
21243 }
d9b3de22
DE
21244
21245 if (!end_sequence)
21246 dwarf2_debug_line_missing_end_sequence_complaint ();
21247
21248 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21249 in which case we still finish recording the last line). */
6f77053d 21250 state_machine.record_line (true);
c906108c 21251 }
f3f5162e
DE
21252}
21253
21254/* Decode the Line Number Program (LNP) for the given line_header
21255 structure and CU. The actual information extracted and the type
21256 of structures created from the LNP depends on the value of PST.
21257
21258 1. If PST is NULL, then this procedure uses the data from the program
21259 to create all necessary symbol tables, and their linetables.
21260
21261 2. If PST is not NULL, this procedure reads the program to determine
21262 the list of files included by the unit represented by PST, and
21263 builds all the associated partial symbol tables.
21264
21265 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21266 It is used for relative paths in the line table.
21267 NOTE: When processing partial symtabs (pst != NULL),
21268 comp_dir == pst->dirname.
21269
21270 NOTE: It is important that psymtabs have the same file name (via strcmp)
21271 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21272 symtab we don't use it in the name of the psymtabs we create.
21273 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
21274 A good testcase for this is mb-inline.exp.
21275
527f3840
JK
21276 LOWPC is the lowest address in CU (or 0 if not known).
21277
21278 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21279 for its PC<->lines mapping information. Otherwise only the filename
21280 table is read in. */
f3f5162e
DE
21281
21282static void
21283dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
c3b7b696 21284 struct dwarf2_cu *cu, struct partial_symtab *pst,
527f3840 21285 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 21286{
518817b3 21287 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 21288 const int decode_for_pst_p = (pst != NULL);
f3f5162e 21289
527f3840
JK
21290 if (decode_mapping)
21291 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
21292
21293 if (decode_for_pst_p)
21294 {
21295 int file_index;
21296
21297 /* Now that we're done scanning the Line Header Program, we can
21298 create the psymtab of each included file. */
fff8551c 21299 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
aaa75496
JB
21300 if (lh->file_names[file_index].included_p == 1)
21301 {
c89b44cd 21302 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 21303 const char *include_name =
c89b44cd
TT
21304 psymtab_include_file_name (lh, file_index, pst, comp_dir,
21305 &name_holder);
c6da4cef 21306 if (include_name != NULL)
aaa75496
JB
21307 dwarf2_create_include_psymtab (include_name, pst, objfile);
21308 }
21309 }
cb1df416
DJ
21310 else
21311 {
21312 /* Make sure a symtab is created for every file, even files
21313 which contain only variables (i.e. no code with associated
21314 line numbers). */
c24bdb02
KS
21315 buildsym_compunit *builder = cu->get_builder ();
21316 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 21317 int i;
cb1df416 21318
fff8551c 21319 for (i = 0; i < lh->file_names.size (); i++)
cb1df416 21320 {
8c43009f 21321 file_entry &fe = lh->file_names[i];
9a619af0 21322
804d2729 21323 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
cb1df416 21324
c24bdb02 21325 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 21326 {
c24bdb02 21327 builder->get_current_subfile ()->symtab
804d2729 21328 = allocate_symtab (cust,
c24bdb02 21329 builder->get_current_subfile ()->name);
43f3e411 21330 }
c24bdb02 21331 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
21332 }
21333 }
c906108c
SS
21334}
21335
21336/* Start a subfile for DWARF. FILENAME is the name of the file and
21337 DIRNAME the name of the source directory which contains FILENAME
4d663531 21338 or NULL if not known.
c906108c
SS
21339 This routine tries to keep line numbers from identical absolute and
21340 relative file names in a common subfile.
21341
21342 Using the `list' example from the GDB testsuite, which resides in
21343 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21344 of /srcdir/list0.c yields the following debugging information for list0.c:
21345
c5aa993b 21346 DW_AT_name: /srcdir/list0.c
4d663531 21347 DW_AT_comp_dir: /compdir
357e46e7 21348 files.files[0].name: list0.h
c5aa993b 21349 files.files[0].dir: /srcdir
357e46e7 21350 files.files[1].name: list0.c
c5aa993b 21351 files.files[1].dir: /srcdir
c906108c
SS
21352
21353 The line number information for list0.c has to end up in a single
4f1520fb
FR
21354 subfile, so that `break /srcdir/list0.c:1' works as expected.
21355 start_subfile will ensure that this happens provided that we pass the
21356 concatenation of files.files[1].dir and files.files[1].name as the
21357 subfile's name. */
c906108c
SS
21358
21359static void
804d2729
TT
21360dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21361 const char *dirname)
c906108c 21362{
d521ce57 21363 char *copy = NULL;
4f1520fb 21364
4d663531 21365 /* In order not to lose the line information directory,
4f1520fb
FR
21366 we concatenate it to the filename when it makes sense.
21367 Note that the Dwarf3 standard says (speaking of filenames in line
21368 information): ``The directory index is ignored for file names
21369 that represent full path names''. Thus ignoring dirname in the
21370 `else' branch below isn't an issue. */
c906108c 21371
d5166ae1 21372 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
21373 {
21374 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21375 filename = copy;
21376 }
c906108c 21377
c24bdb02 21378 cu->get_builder ()->start_subfile (filename);
4f1520fb 21379
d521ce57
TT
21380 if (copy != NULL)
21381 xfree (copy);
c906108c
SS
21382}
21383
804d2729
TT
21384/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21385 buildsym_compunit constructor. */
f4dc4d17 21386
c24bdb02
KS
21387struct compunit_symtab *
21388dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21389 CORE_ADDR low_pc)
f4dc4d17 21390{
c24bdb02 21391 gdb_assert (m_builder == nullptr);
43f3e411 21392
c24bdb02
KS
21393 m_builder.reset (new struct buildsym_compunit
21394 (per_cu->dwarf2_per_objfile->objfile,
21395 name, comp_dir, language, low_pc));
93b8bea4 21396
c24bdb02 21397 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 21398
c24bdb02
KS
21399 get_builder ()->record_debugformat ("DWARF 2");
21400 get_builder ()->record_producer (producer);
f4dc4d17 21401
c24bdb02 21402 processing_has_namespace_info = false;
43f3e411 21403
c24bdb02 21404 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
21405}
21406
4c2df51b
DJ
21407static void
21408var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 21409 struct dwarf2_cu *cu)
4c2df51b 21410{
518817b3 21411 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e7c27a73
DJ
21412 struct comp_unit_head *cu_header = &cu->header;
21413
4c2df51b
DJ
21414 /* NOTE drow/2003-01-30: There used to be a comment and some special
21415 code here to turn a symbol with DW_AT_external and a
21416 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21417 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21418 with some versions of binutils) where shared libraries could have
21419 relocations against symbols in their debug information - the
21420 minimal symbol would have the right address, but the debug info
21421 would not. It's no longer necessary, because we will explicitly
21422 apply relocations when we read in the debug information now. */
21423
21424 /* A DW_AT_location attribute with no contents indicates that a
21425 variable has been optimized away. */
21426 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21427 {
f1e6e072 21428 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
21429 return;
21430 }
21431
21432 /* Handle one degenerate form of location expression specially, to
21433 preserve GDB's previous behavior when section offsets are
336d760d
AT
21434 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21435 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
21436
21437 if (attr_form_is_block (attr)
3019eac3
DE
21438 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21439 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
336d760d
AT
21440 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21441 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
3019eac3
DE
21442 && (DW_BLOCK (attr)->size
21443 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 21444 {
891d2f0b 21445 unsigned int dummy;
4c2df51b 21446
3019eac3
DE
21447 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21448 SYMBOL_VALUE_ADDRESS (sym) =
21449 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
21450 else
21451 SYMBOL_VALUE_ADDRESS (sym) =
21452 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 21453 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
21454 fixup_symbol_section (sym, objfile);
21455 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
21456 SYMBOL_SECTION (sym));
4c2df51b
DJ
21457 return;
21458 }
21459
21460 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21461 expression evaluator, and use LOC_COMPUTED only when necessary
21462 (i.e. when the value of a register or memory location is
21463 referenced, or a thread-local block, etc.). Then again, it might
21464 not be worthwhile. I'm assuming that it isn't unless performance
21465 or memory numbers show me otherwise. */
21466
f1e6e072 21467 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 21468
f1e6e072 21469 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 21470 cu->has_loclist = true;
4c2df51b
DJ
21471}
21472
c906108c
SS
21473/* Given a pointer to a DWARF information entry, figure out if we need
21474 to make a symbol table entry for it, and if so, create a new entry
21475 and return a pointer to it.
21476 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
21477 used the passed type.
21478 If SPACE is not NULL, use it to hold the new symbol. If it is
21479 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
21480
21481static struct symbol *
5e2db402
TT
21482new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21483 struct symbol *space)
c906108c 21484{
518817b3
SM
21485 struct dwarf2_per_objfile *dwarf2_per_objfile
21486 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21487 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 21488 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 21489 struct symbol *sym = NULL;
15d034d0 21490 const char *name;
c906108c
SS
21491 struct attribute *attr = NULL;
21492 struct attribute *attr2 = NULL;
e142c38c 21493 CORE_ADDR baseaddr;
e37fd15a
SW
21494 struct pending **list_to_add = NULL;
21495
edb3359d 21496 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
21497
21498 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 21499
94af9270 21500 name = dwarf2_name (die, cu);
c906108c
SS
21501 if (name)
21502 {
94af9270 21503 const char *linkagename;
34eaf542 21504 int suppress_add = 0;
94af9270 21505
34eaf542
TT
21506 if (space)
21507 sym = space;
21508 else
e623cf5d 21509 sym = allocate_symbol (objfile);
c906108c 21510 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
21511
21512 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 21513 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
21514 linkagename = dwarf2_physname (name, die, cu);
21515 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 21516
f55ee35c
JK
21517 /* Fortran does not have mangling standard and the mangling does differ
21518 between gfortran, iFort etc. */
21519 if (cu->language == language_fortran
b250c185 21520 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 21521 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 21522 dwarf2_full_name (name, die, cu),
29df156d 21523 NULL);
f55ee35c 21524
c906108c 21525 /* Default assumptions.
c5aa993b 21526 Use the passed type or decode it from the die. */
176620f1 21527 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 21528 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
21529 if (type != NULL)
21530 SYMBOL_TYPE (sym) = type;
21531 else
e7c27a73 21532 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
21533 attr = dwarf2_attr (die,
21534 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21535 cu);
c906108c
SS
21536 if (attr)
21537 {
21538 SYMBOL_LINE (sym) = DW_UNSND (attr);
21539 }
cb1df416 21540
edb3359d
DJ
21541 attr = dwarf2_attr (die,
21542 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21543 cu);
cb1df416
DJ
21544 if (attr)
21545 {
ecfb656c 21546 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 21547 struct file_entry *fe;
9a619af0 21548
ecfb656c
PA
21549 if (cu->line_header != NULL)
21550 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
21551 else
21552 fe = NULL;
21553
21554 if (fe == NULL)
b98664d3 21555 complaint (_("file index out of range"));
8c43009f
PA
21556 else
21557 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
21558 }
21559
c906108c
SS
21560 switch (die->tag)
21561 {
21562 case DW_TAG_label:
e142c38c 21563 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c 21564 if (attr)
3e29f34a
MR
21565 {
21566 CORE_ADDR addr;
21567
21568 addr = attr_value_as_address (attr);
21569 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21570 SYMBOL_VALUE_ADDRESS (sym) = addr;
21571 }
0f5238ed
TT
21572 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21573 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 21574 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
d3cb6808 21575 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
21576 break;
21577 case DW_TAG_subprogram:
21578 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21579 finish_block. */
f1e6e072 21580 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 21581 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
21582 if ((attr2 && (DW_UNSND (attr2) != 0))
21583 || cu->language == language_ada)
c906108c 21584 {
2cfa0c8d
JB
21585 /* Subprograms marked external are stored as a global symbol.
21586 Ada subprograms, whether marked external or not, are always
21587 stored as a global symbol, because we want to be able to
21588 access them globally. For instance, we want to be able
21589 to break on a nested subprogram without having to
21590 specify the context. */
c24bdb02 21591 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
21592 }
21593 else
21594 {
e37fd15a 21595 list_to_add = cu->list_in_scope;
c906108c
SS
21596 }
21597 break;
edb3359d
DJ
21598 case DW_TAG_inlined_subroutine:
21599 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21600 finish_block. */
f1e6e072 21601 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 21602 SYMBOL_INLINED (sym) = 1;
481860b3 21603 list_to_add = cu->list_in_scope;
edb3359d 21604 break;
34eaf542
TT
21605 case DW_TAG_template_value_param:
21606 suppress_add = 1;
21607 /* Fall through. */
72929c62 21608 case DW_TAG_constant:
c906108c 21609 case DW_TAG_variable:
254e6b9e 21610 case DW_TAG_member:
0963b4bd
MS
21611 /* Compilation with minimal debug info may result in
21612 variables with missing type entries. Change the
21613 misleading `void' type to something sensible. */
c906108c 21614 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 21615 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 21616
e142c38c 21617 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
21618 /* In the case of DW_TAG_member, we should only be called for
21619 static const members. */
21620 if (die->tag == DW_TAG_member)
21621 {
3863f96c
DE
21622 /* dwarf2_add_field uses die_is_declaration,
21623 so we do the same. */
254e6b9e
DE
21624 gdb_assert (die_is_declaration (die, cu));
21625 gdb_assert (attr);
21626 }
c906108c
SS
21627 if (attr)
21628 {
e7c27a73 21629 dwarf2_const_value (attr, sym, cu);
e142c38c 21630 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 21631 if (!suppress_add)
34eaf542
TT
21632 {
21633 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 21634 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 21635 else
e37fd15a 21636 list_to_add = cu->list_in_scope;
34eaf542 21637 }
c906108c
SS
21638 break;
21639 }
e142c38c 21640 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
21641 if (attr)
21642 {
e7c27a73 21643 var_decode_location (attr, sym, cu);
e142c38c 21644 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
21645
21646 /* Fortran explicitly imports any global symbols to the local
21647 scope by DW_TAG_common_block. */
21648 if (cu->language == language_fortran && die->parent
21649 && die->parent->tag == DW_TAG_common_block)
21650 attr2 = NULL;
21651
caac4577
JG
21652 if (SYMBOL_CLASS (sym) == LOC_STATIC
21653 && SYMBOL_VALUE_ADDRESS (sym) == 0
21654 && !dwarf2_per_objfile->has_section_at_zero)
21655 {
21656 /* When a static variable is eliminated by the linker,
21657 the corresponding debug information is not stripped
21658 out, but the variable address is set to null;
21659 do not add such variables into symbol table. */
21660 }
21661 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 21662 {
f55ee35c
JK
21663 /* Workaround gfortran PR debug/40040 - it uses
21664 DW_AT_location for variables in -fPIC libraries which may
21665 get overriden by other libraries/executable and get
21666 a different address. Resolve it by the minimal symbol
21667 which may come from inferior's executable using copy
21668 relocation. Make this workaround only for gfortran as for
21669 other compilers GDB cannot guess the minimal symbol
21670 Fortran mangling kind. */
21671 if (cu->language == language_fortran && die->parent
21672 && die->parent->tag == DW_TAG_module
21673 && cu->producer
28586665 21674 && startswith (cu->producer, "GNU Fortran"))
f1e6e072 21675 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 21676
1c809c68
TT
21677 /* A variable with DW_AT_external is never static,
21678 but it may be block-scoped. */
804d2729 21679 list_to_add
c24bdb02
KS
21680 = ((cu->list_in_scope
21681 == cu->get_builder ()->get_file_symbols ())
21682 ? cu->get_builder ()->get_global_symbols ()
804d2729 21683 : cu->list_in_scope);
1c809c68 21684 }
c906108c 21685 else
e37fd15a 21686 list_to_add = cu->list_in_scope;
c906108c
SS
21687 }
21688 else
21689 {
21690 /* We do not know the address of this symbol.
c5aa993b
JM
21691 If it is an external symbol and we have type information
21692 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21693 The address of the variable will then be determined from
21694 the minimal symbol table whenever the variable is
21695 referenced. */
e142c38c 21696 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
21697
21698 /* Fortran explicitly imports any global symbols to the local
21699 scope by DW_TAG_common_block. */
21700 if (cu->language == language_fortran && die->parent
21701 && die->parent->tag == DW_TAG_common_block)
21702 {
21703 /* SYMBOL_CLASS doesn't matter here because
21704 read_common_block is going to reset it. */
21705 if (!suppress_add)
21706 list_to_add = cu->list_in_scope;
21707 }
21708 else if (attr2 && (DW_UNSND (attr2) != 0)
21709 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 21710 {
0fe7935b
DJ
21711 /* A variable with DW_AT_external is never static, but it
21712 may be block-scoped. */
804d2729 21713 list_to_add
c24bdb02
KS
21714 = ((cu->list_in_scope
21715 == cu->get_builder ()->get_file_symbols ())
21716 ? cu->get_builder ()->get_global_symbols ()
804d2729 21717 : cu->list_in_scope);
0fe7935b 21718
f1e6e072 21719 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 21720 }
442ddf59
JK
21721 else if (!die_is_declaration (die, cu))
21722 {
21723 /* Use the default LOC_OPTIMIZED_OUT class. */
21724 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
21725 if (!suppress_add)
21726 list_to_add = cu->list_in_scope;
442ddf59 21727 }
c906108c
SS
21728 }
21729 break;
21730 case DW_TAG_formal_parameter:
a60f3166
TT
21731 {
21732 /* If we are inside a function, mark this as an argument. If
21733 not, we might be looking at an argument to an inlined function
21734 when we do not have enough information to show inlined frames;
21735 pretend it's a local variable in that case so that the user can
21736 still see it. */
804d2729 21737 struct context_stack *curr
c24bdb02 21738 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
21739 if (curr != nullptr && curr->name != nullptr)
21740 SYMBOL_IS_ARGUMENT (sym) = 1;
21741 attr = dwarf2_attr (die, DW_AT_location, cu);
21742 if (attr)
21743 {
21744 var_decode_location (attr, sym, cu);
21745 }
21746 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21747 if (attr)
21748 {
21749 dwarf2_const_value (attr, sym, cu);
21750 }
f346a30d 21751
a60f3166
TT
21752 list_to_add = cu->list_in_scope;
21753 }
c906108c
SS
21754 break;
21755 case DW_TAG_unspecified_parameters:
21756 /* From varargs functions; gdb doesn't seem to have any
21757 interest in this information, so just ignore it for now.
21758 (FIXME?) */
21759 break;
34eaf542
TT
21760 case DW_TAG_template_type_param:
21761 suppress_add = 1;
21762 /* Fall through. */
c906108c 21763 case DW_TAG_class_type:
680b30c7 21764 case DW_TAG_interface_type:
c906108c
SS
21765 case DW_TAG_structure_type:
21766 case DW_TAG_union_type:
72019c9c 21767 case DW_TAG_set_type:
c906108c 21768 case DW_TAG_enumeration_type:
f1e6e072 21769 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21770 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 21771
63d06c5c 21772 {
9c37b5ae 21773 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
21774 really ever be static objects: otherwise, if you try
21775 to, say, break of a class's method and you're in a file
21776 which doesn't mention that class, it won't work unless
21777 the check for all static symbols in lookup_symbol_aux
21778 saves you. See the OtherFileClass tests in
21779 gdb.c++/namespace.exp. */
21780
e37fd15a 21781 if (!suppress_add)
34eaf542 21782 {
c24bdb02 21783 buildsym_compunit *builder = cu->get_builder ();
804d2729 21784 list_to_add
c24bdb02 21785 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 21786 && cu->language == language_cplus
c24bdb02 21787 ? builder->get_global_symbols ()
804d2729 21788 : cu->list_in_scope);
63d06c5c 21789
64382290 21790 /* The semantics of C++ state that "struct foo {
9c37b5ae 21791 ... }" also defines a typedef for "foo". */
64382290 21792 if (cu->language == language_cplus
45280282 21793 || cu->language == language_ada
c44af4eb
TT
21794 || cu->language == language_d
21795 || cu->language == language_rust)
64382290
TT
21796 {
21797 /* The symbol's name is already allocated along
21798 with this objfile, so we don't need to
21799 duplicate it for the type. */
21800 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21801 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21802 }
63d06c5c
DC
21803 }
21804 }
c906108c
SS
21805 break;
21806 case DW_TAG_typedef:
f1e6e072 21807 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 21808 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21809 list_to_add = cu->list_in_scope;
63d06c5c 21810 break;
c906108c 21811 case DW_TAG_base_type:
a02abb62 21812 case DW_TAG_subrange_type:
f1e6e072 21813 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21814 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21815 list_to_add = cu->list_in_scope;
c906108c
SS
21816 break;
21817 case DW_TAG_enumerator:
e142c38c 21818 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
21819 if (attr)
21820 {
e7c27a73 21821 dwarf2_const_value (attr, sym, cu);
c906108c 21822 }
63d06c5c
DC
21823 {
21824 /* NOTE: carlton/2003-11-10: See comment above in the
21825 DW_TAG_class_type, etc. block. */
21826
804d2729 21827 list_to_add
c24bdb02 21828 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 21829 && cu->language == language_cplus
c24bdb02 21830 ? cu->get_builder ()->get_global_symbols ()
804d2729 21831 : cu->list_in_scope);
63d06c5c 21832 }
c906108c 21833 break;
74921315 21834 case DW_TAG_imported_declaration:
5c4e30ca 21835 case DW_TAG_namespace:
f1e6e072 21836 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 21837 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 21838 break;
530e8392
KB
21839 case DW_TAG_module:
21840 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21841 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 21842 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 21843 break;
4357ac6c 21844 case DW_TAG_common_block:
f1e6e072 21845 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 21846 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 21847 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 21848 break;
c906108c
SS
21849 default:
21850 /* Not a tag we recognize. Hopefully we aren't processing
21851 trash data, but since we must specifically ignore things
21852 we don't recognize, there is nothing else we should do at
0963b4bd 21853 this point. */
b98664d3 21854 complaint (_("unsupported tag: '%s'"),
4d3c2250 21855 dwarf_tag_name (die->tag));
c906108c
SS
21856 break;
21857 }
df8a16a1 21858
e37fd15a
SW
21859 if (suppress_add)
21860 {
21861 sym->hash_next = objfile->template_symbols;
21862 objfile->template_symbols = sym;
21863 list_to_add = NULL;
21864 }
21865
21866 if (list_to_add != NULL)
d3cb6808 21867 add_symbol_to_list (sym, list_to_add);
e37fd15a 21868
df8a16a1
DJ
21869 /* For the benefit of old versions of GCC, check for anonymous
21870 namespaces based on the demangled name. */
4d4ec4e5 21871 if (!cu->processing_has_namespace_info
94af9270 21872 && cu->language == language_cplus)
c24bdb02 21873 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
21874 }
21875 return (sym);
21876}
21877
98bfdba5
PA
21878/* Given an attr with a DW_FORM_dataN value in host byte order,
21879 zero-extend it as appropriate for the symbol's type. The DWARF
21880 standard (v4) is not entirely clear about the meaning of using
21881 DW_FORM_dataN for a constant with a signed type, where the type is
21882 wider than the data. The conclusion of a discussion on the DWARF
21883 list was that this is unspecified. We choose to always zero-extend
21884 because that is the interpretation long in use by GCC. */
c906108c 21885
98bfdba5 21886static gdb_byte *
ff39bb5e 21887dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 21888 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 21889{
518817b3 21890 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e17a4113
UW
21891 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21892 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
21893 LONGEST l = DW_UNSND (attr);
21894
21895 if (bits < sizeof (*value) * 8)
21896 {
21897 l &= ((LONGEST) 1 << bits) - 1;
21898 *value = l;
21899 }
21900 else if (bits == sizeof (*value) * 8)
21901 *value = l;
21902 else
21903 {
224c3ddb 21904 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
21905 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21906 return bytes;
21907 }
21908
21909 return NULL;
21910}
21911
21912/* Read a constant value from an attribute. Either set *VALUE, or if
21913 the value does not fit in *VALUE, set *BYTES - either already
21914 allocated on the objfile obstack, or newly allocated on OBSTACK,
21915 or, set *BATON, if we translated the constant to a location
21916 expression. */
21917
21918static void
ff39bb5e 21919dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
21920 const char *name, struct obstack *obstack,
21921 struct dwarf2_cu *cu,
d521ce57 21922 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
21923 struct dwarf2_locexpr_baton **baton)
21924{
518817b3 21925 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
98bfdba5 21926 struct comp_unit_head *cu_header = &cu->header;
c906108c 21927 struct dwarf_block *blk;
98bfdba5
PA
21928 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21929 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21930
21931 *value = 0;
21932 *bytes = NULL;
21933 *baton = NULL;
c906108c
SS
21934
21935 switch (attr->form)
21936 {
21937 case DW_FORM_addr:
336d760d 21938 case DW_FORM_addrx:
3019eac3 21939 case DW_FORM_GNU_addr_index:
ac56253d 21940 {
ac56253d
TT
21941 gdb_byte *data;
21942
98bfdba5
PA
21943 if (TYPE_LENGTH (type) != cu_header->addr_size)
21944 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 21945 cu_header->addr_size,
98bfdba5 21946 TYPE_LENGTH (type));
ac56253d
TT
21947 /* Symbols of this form are reasonably rare, so we just
21948 piggyback on the existing location code rather than writing
21949 a new implementation of symbol_computed_ops. */
8d749320 21950 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
21951 (*baton)->per_cu = cu->per_cu;
21952 gdb_assert ((*baton)->per_cu);
ac56253d 21953
98bfdba5 21954 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 21955 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 21956 (*baton)->data = data;
ac56253d
TT
21957
21958 data[0] = DW_OP_addr;
21959 store_unsigned_integer (&data[1], cu_header->addr_size,
21960 byte_order, DW_ADDR (attr));
21961 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 21962 }
c906108c 21963 break;
4ac36638 21964 case DW_FORM_string:
93b5768b 21965 case DW_FORM_strp:
cf532bd1 21966 case DW_FORM_strx:
3019eac3 21967 case DW_FORM_GNU_str_index:
36586728 21968 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
21969 /* DW_STRING is already allocated on the objfile obstack, point
21970 directly to it. */
d521ce57 21971 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 21972 break;
c906108c
SS
21973 case DW_FORM_block1:
21974 case DW_FORM_block2:
21975 case DW_FORM_block4:
21976 case DW_FORM_block:
2dc7f7b3 21977 case DW_FORM_exprloc:
0224619f 21978 case DW_FORM_data16:
c906108c 21979 blk = DW_BLOCK (attr);
98bfdba5
PA
21980 if (TYPE_LENGTH (type) != blk->size)
21981 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21982 TYPE_LENGTH (type));
21983 *bytes = blk->data;
c906108c 21984 break;
2df3850c
JM
21985
21986 /* The DW_AT_const_value attributes are supposed to carry the
21987 symbol's value "represented as it would be on the target
21988 architecture." By the time we get here, it's already been
21989 converted to host endianness, so we just need to sign- or
21990 zero-extend it as appropriate. */
21991 case DW_FORM_data1:
3aef2284 21992 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 21993 break;
c906108c 21994 case DW_FORM_data2:
3aef2284 21995 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 21996 break;
c906108c 21997 case DW_FORM_data4:
3aef2284 21998 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 21999 break;
c906108c 22000 case DW_FORM_data8:
3aef2284 22001 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
22002 break;
22003
c906108c 22004 case DW_FORM_sdata:
663c44ac 22005 case DW_FORM_implicit_const:
98bfdba5 22006 *value = DW_SND (attr);
2df3850c
JM
22007 break;
22008
c906108c 22009 case DW_FORM_udata:
98bfdba5 22010 *value = DW_UNSND (attr);
c906108c 22011 break;
2df3850c 22012
c906108c 22013 default:
b98664d3 22014 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 22015 dwarf_form_name (attr->form));
98bfdba5 22016 *value = 0;
c906108c
SS
22017 break;
22018 }
22019}
22020
2df3850c 22021
98bfdba5
PA
22022/* Copy constant value from an attribute to a symbol. */
22023
2df3850c 22024static void
ff39bb5e 22025dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 22026 struct dwarf2_cu *cu)
2df3850c 22027{
518817b3 22028 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12df843f 22029 LONGEST value;
d521ce57 22030 const gdb_byte *bytes;
98bfdba5 22031 struct dwarf2_locexpr_baton *baton;
2df3850c 22032
98bfdba5
PA
22033 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
22034 SYMBOL_PRINT_NAME (sym),
22035 &objfile->objfile_obstack, cu,
22036 &value, &bytes, &baton);
2df3850c 22037
98bfdba5
PA
22038 if (baton != NULL)
22039 {
98bfdba5 22040 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 22041 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
22042 }
22043 else if (bytes != NULL)
22044 {
22045 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 22046 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
22047 }
22048 else
22049 {
22050 SYMBOL_VALUE (sym) = value;
f1e6e072 22051 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 22052 }
2df3850c
JM
22053}
22054
c906108c
SS
22055/* Return the type of the die in question using its DW_AT_type attribute. */
22056
22057static struct type *
e7c27a73 22058die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22059{
c906108c 22060 struct attribute *type_attr;
c906108c 22061
e142c38c 22062 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
22063 if (!type_attr)
22064 {
518817b3 22065 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 22066 /* A missing DW_AT_type represents a void type. */
518817b3 22067 return objfile_type (objfile)->builtin_void;
c906108c 22068 }
348e048f 22069
673bfd45 22070 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22071}
22072
b4ba55a1
JB
22073/* True iff CU's producer generates GNAT Ada auxiliary information
22074 that allows to find parallel types through that information instead
22075 of having to do expensive parallel lookups by type name. */
22076
22077static int
22078need_gnat_info (struct dwarf2_cu *cu)
22079{
de4cb04a
JB
22080 /* Assume that the Ada compiler was GNAT, which always produces
22081 the auxiliary information. */
22082 return (cu->language == language_ada);
b4ba55a1
JB
22083}
22084
b4ba55a1
JB
22085/* Return the auxiliary type of the die in question using its
22086 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22087 attribute is not present. */
22088
22089static struct type *
22090die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22091{
b4ba55a1 22092 struct attribute *type_attr;
b4ba55a1
JB
22093
22094 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22095 if (!type_attr)
22096 return NULL;
22097
673bfd45 22098 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
22099}
22100
22101/* If DIE has a descriptive_type attribute, then set the TYPE's
22102 descriptive type accordingly. */
22103
22104static void
22105set_descriptive_type (struct type *type, struct die_info *die,
22106 struct dwarf2_cu *cu)
22107{
22108 struct type *descriptive_type = die_descriptive_type (die, cu);
22109
22110 if (descriptive_type)
22111 {
22112 ALLOCATE_GNAT_AUX_TYPE (type);
22113 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22114 }
22115}
22116
c906108c
SS
22117/* Return the containing type of the die in question using its
22118 DW_AT_containing_type attribute. */
22119
22120static struct type *
e7c27a73 22121die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22122{
c906108c 22123 struct attribute *type_attr;
518817b3 22124 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 22125
e142c38c 22126 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
22127 if (!type_attr)
22128 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 22129 "[in module %s]"), objfile_name (objfile));
33ac96f0 22130
673bfd45 22131 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22132}
22133
ac9ec31b
DE
22134/* Return an error marker type to use for the ill formed type in DIE/CU. */
22135
22136static struct type *
22137build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22138{
518817b3
SM
22139 struct dwarf2_per_objfile *dwarf2_per_objfile
22140 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b 22141 struct objfile *objfile = dwarf2_per_objfile->objfile;
528e1572 22142 char *saved;
ac9ec31b 22143
528e1572
SM
22144 std::string message
22145 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22146 objfile_name (objfile),
22147 sect_offset_str (cu->header.sect_off),
22148 sect_offset_str (die->sect_off));
efba19b0 22149 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 22150
19f392bc 22151 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
22152}
22153
673bfd45 22154/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
22155 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22156 DW_AT_containing_type.
673bfd45
DE
22157 If there is no type substitute an error marker. */
22158
c906108c 22159static struct type *
ff39bb5e 22160lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 22161 struct dwarf2_cu *cu)
c906108c 22162{
518817b3
SM
22163 struct dwarf2_per_objfile *dwarf2_per_objfile
22164 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 22165 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
22166 struct type *this_type;
22167
ac9ec31b
DE
22168 gdb_assert (attr->name == DW_AT_type
22169 || attr->name == DW_AT_GNAT_descriptive_type
22170 || attr->name == DW_AT_containing_type);
22171
673bfd45
DE
22172 /* First see if we have it cached. */
22173
36586728
TT
22174 if (attr->form == DW_FORM_GNU_ref_alt)
22175 {
22176 struct dwarf2_per_cu_data *per_cu;
9c541725 22177 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 22178
ed2dc618
SM
22179 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
22180 dwarf2_per_objfile);
9c541725 22181 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 22182 }
7771576e 22183 else if (attr_form_is_ref (attr))
673bfd45 22184 {
9c541725 22185 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 22186
9c541725 22187 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 22188 }
55f1336d 22189 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 22190 {
ac9ec31b 22191 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 22192
ac9ec31b 22193 return get_signatured_type (die, signature, cu);
673bfd45
DE
22194 }
22195 else
22196 {
b98664d3 22197 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
22198 " at %s [in module %s]"),
22199 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 22200 objfile_name (objfile));
ac9ec31b 22201 return build_error_marker_type (cu, die);
673bfd45
DE
22202 }
22203
22204 /* If not cached we need to read it in. */
22205
22206 if (this_type == NULL)
22207 {
ac9ec31b 22208 struct die_info *type_die = NULL;
673bfd45
DE
22209 struct dwarf2_cu *type_cu = cu;
22210
7771576e 22211 if (attr_form_is_ref (attr))
ac9ec31b
DE
22212 type_die = follow_die_ref (die, attr, &type_cu);
22213 if (type_die == NULL)
22214 return build_error_marker_type (cu, die);
22215 /* If we find the type now, it's probably because the type came
3019eac3
DE
22216 from an inter-CU reference and the type's CU got expanded before
22217 ours. */
ac9ec31b 22218 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
22219 }
22220
22221 /* If we still don't have a type use an error marker. */
22222
22223 if (this_type == NULL)
ac9ec31b 22224 return build_error_marker_type (cu, die);
673bfd45 22225
f792889a 22226 return this_type;
c906108c
SS
22227}
22228
673bfd45
DE
22229/* Return the type in DIE, CU.
22230 Returns NULL for invalid types.
22231
02142a6c 22232 This first does a lookup in die_type_hash,
673bfd45
DE
22233 and only reads the die in if necessary.
22234
22235 NOTE: This can be called when reading in partial or full symbols. */
22236
f792889a 22237static struct type *
e7c27a73 22238read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22239{
f792889a
DJ
22240 struct type *this_type;
22241
22242 this_type = get_die_type (die, cu);
22243 if (this_type)
22244 return this_type;
22245
673bfd45
DE
22246 return read_type_die_1 (die, cu);
22247}
22248
22249/* Read the type in DIE, CU.
22250 Returns NULL for invalid types. */
22251
22252static struct type *
22253read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22254{
22255 struct type *this_type = NULL;
22256
c906108c
SS
22257 switch (die->tag)
22258 {
22259 case DW_TAG_class_type:
680b30c7 22260 case DW_TAG_interface_type:
c906108c
SS
22261 case DW_TAG_structure_type:
22262 case DW_TAG_union_type:
f792889a 22263 this_type = read_structure_type (die, cu);
c906108c
SS
22264 break;
22265 case DW_TAG_enumeration_type:
f792889a 22266 this_type = read_enumeration_type (die, cu);
c906108c
SS
22267 break;
22268 case DW_TAG_subprogram:
22269 case DW_TAG_subroutine_type:
edb3359d 22270 case DW_TAG_inlined_subroutine:
f792889a 22271 this_type = read_subroutine_type (die, cu);
c906108c
SS
22272 break;
22273 case DW_TAG_array_type:
f792889a 22274 this_type = read_array_type (die, cu);
c906108c 22275 break;
72019c9c 22276 case DW_TAG_set_type:
f792889a 22277 this_type = read_set_type (die, cu);
72019c9c 22278 break;
c906108c 22279 case DW_TAG_pointer_type:
f792889a 22280 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
22281 break;
22282 case DW_TAG_ptr_to_member_type:
f792889a 22283 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
22284 break;
22285 case DW_TAG_reference_type:
4297a3f0
AV
22286 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22287 break;
22288 case DW_TAG_rvalue_reference_type:
22289 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
22290 break;
22291 case DW_TAG_const_type:
f792889a 22292 this_type = read_tag_const_type (die, cu);
c906108c
SS
22293 break;
22294 case DW_TAG_volatile_type:
f792889a 22295 this_type = read_tag_volatile_type (die, cu);
c906108c 22296 break;
06d66ee9
TT
22297 case DW_TAG_restrict_type:
22298 this_type = read_tag_restrict_type (die, cu);
22299 break;
c906108c 22300 case DW_TAG_string_type:
f792889a 22301 this_type = read_tag_string_type (die, cu);
c906108c
SS
22302 break;
22303 case DW_TAG_typedef:
f792889a 22304 this_type = read_typedef (die, cu);
c906108c 22305 break;
a02abb62 22306 case DW_TAG_subrange_type:
f792889a 22307 this_type = read_subrange_type (die, cu);
a02abb62 22308 break;
c906108c 22309 case DW_TAG_base_type:
f792889a 22310 this_type = read_base_type (die, cu);
c906108c 22311 break;
81a17f79 22312 case DW_TAG_unspecified_type:
f792889a 22313 this_type = read_unspecified_type (die, cu);
81a17f79 22314 break;
0114d602
DJ
22315 case DW_TAG_namespace:
22316 this_type = read_namespace_type (die, cu);
22317 break;
f55ee35c
JK
22318 case DW_TAG_module:
22319 this_type = read_module_type (die, cu);
22320 break;
a2c2acaf
MW
22321 case DW_TAG_atomic_type:
22322 this_type = read_tag_atomic_type (die, cu);
22323 break;
c906108c 22324 default:
b98664d3 22325 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 22326 dwarf_tag_name (die->tag));
c906108c
SS
22327 break;
22328 }
63d06c5c 22329
f792889a 22330 return this_type;
63d06c5c
DC
22331}
22332
abc72ce4
DE
22333/* See if we can figure out if the class lives in a namespace. We do
22334 this by looking for a member function; its demangled name will
22335 contain namespace info, if there is any.
22336 Return the computed name or NULL.
22337 Space for the result is allocated on the objfile's obstack.
22338 This is the full-die version of guess_partial_die_structure_name.
22339 In this case we know DIE has no useful parent. */
22340
22341static char *
22342guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22343{
22344 struct die_info *spec_die;
22345 struct dwarf2_cu *spec_cu;
22346 struct die_info *child;
518817b3 22347 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4
DE
22348
22349 spec_cu = cu;
22350 spec_die = die_specification (die, &spec_cu);
22351 if (spec_die != NULL)
22352 {
22353 die = spec_die;
22354 cu = spec_cu;
22355 }
22356
22357 for (child = die->child;
22358 child != NULL;
22359 child = child->sibling)
22360 {
22361 if (child->tag == DW_TAG_subprogram)
22362 {
73b9be8b 22363 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 22364
7d45c7c3 22365 if (linkage_name != NULL)
abc72ce4
DE
22366 {
22367 char *actual_name
22368 = language_class_name_from_physname (cu->language_defn,
7d45c7c3 22369 linkage_name);
abc72ce4
DE
22370 char *name = NULL;
22371
22372 if (actual_name != NULL)
22373 {
15d034d0 22374 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
22375
22376 if (die_name != NULL
22377 && strcmp (die_name, actual_name) != 0)
22378 {
22379 /* Strip off the class name from the full name.
22380 We want the prefix. */
22381 int die_name_len = strlen (die_name);
22382 int actual_name_len = strlen (actual_name);
22383
22384 /* Test for '::' as a sanity check. */
22385 if (actual_name_len > die_name_len + 2
3e43a32a
MS
22386 && actual_name[actual_name_len
22387 - die_name_len - 1] == ':')
0cf9feb9 22388 name = obstack_strndup (
e3b94546 22389 &objfile->per_bfd->storage_obstack,
224c3ddb 22390 actual_name, actual_name_len - die_name_len - 2);
abc72ce4
DE
22391 }
22392 }
22393 xfree (actual_name);
22394 return name;
22395 }
22396 }
22397 }
22398
22399 return NULL;
22400}
22401
96408a79
SA
22402/* GCC might emit a nameless typedef that has a linkage name. Determine the
22403 prefix part in such case. See
22404 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22405
a121b7c1 22406static const char *
96408a79
SA
22407anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22408{
22409 struct attribute *attr;
e6a959d6 22410 const char *base;
96408a79
SA
22411
22412 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22413 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22414 return NULL;
22415
7d45c7c3 22416 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
22417 return NULL;
22418
73b9be8b 22419 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
22420 if (attr == NULL || DW_STRING (attr) == NULL)
22421 return NULL;
22422
22423 /* dwarf2_name had to be already called. */
22424 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22425
22426 /* Strip the base name, keep any leading namespaces/classes. */
22427 base = strrchr (DW_STRING (attr), ':');
22428 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22429 return "";
22430
518817b3 22431 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0cf9feb9
TT
22432 return obstack_strndup (&objfile->per_bfd->storage_obstack,
22433 DW_STRING (attr),
22434 &base[-1] - DW_STRING (attr));
96408a79
SA
22435}
22436
fdde2d81 22437/* Return the name of the namespace/class that DIE is defined within,
0114d602 22438 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 22439
0114d602
DJ
22440 For example, if we're within the method foo() in the following
22441 code:
22442
22443 namespace N {
22444 class C {
22445 void foo () {
22446 }
22447 };
22448 }
22449
22450 then determine_prefix on foo's die will return "N::C". */
fdde2d81 22451
0d5cff50 22452static const char *
e142c38c 22453determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 22454{
518817b3
SM
22455 struct dwarf2_per_objfile *dwarf2_per_objfile
22456 = cu->per_cu->dwarf2_per_objfile;
0114d602
DJ
22457 struct die_info *parent, *spec_die;
22458 struct dwarf2_cu *spec_cu;
22459 struct type *parent_type;
a121b7c1 22460 const char *retval;
63d06c5c 22461
9c37b5ae 22462 if (cu->language != language_cplus
c44af4eb
TT
22463 && cu->language != language_fortran && cu->language != language_d
22464 && cu->language != language_rust)
0114d602
DJ
22465 return "";
22466
96408a79
SA
22467 retval = anonymous_struct_prefix (die, cu);
22468 if (retval)
22469 return retval;
22470
0114d602
DJ
22471 /* We have to be careful in the presence of DW_AT_specification.
22472 For example, with GCC 3.4, given the code
22473
22474 namespace N {
22475 void foo() {
22476 // Definition of N::foo.
22477 }
22478 }
22479
22480 then we'll have a tree of DIEs like this:
22481
22482 1: DW_TAG_compile_unit
22483 2: DW_TAG_namespace // N
22484 3: DW_TAG_subprogram // declaration of N::foo
22485 4: DW_TAG_subprogram // definition of N::foo
22486 DW_AT_specification // refers to die #3
22487
22488 Thus, when processing die #4, we have to pretend that we're in
22489 the context of its DW_AT_specification, namely the contex of die
22490 #3. */
22491 spec_cu = cu;
22492 spec_die = die_specification (die, &spec_cu);
22493 if (spec_die == NULL)
22494 parent = die->parent;
22495 else
63d06c5c 22496 {
0114d602
DJ
22497 parent = spec_die->parent;
22498 cu = spec_cu;
63d06c5c 22499 }
0114d602
DJ
22500
22501 if (parent == NULL)
22502 return "";
98bfdba5
PA
22503 else if (parent->building_fullname)
22504 {
22505 const char *name;
22506 const char *parent_name;
22507
22508 /* It has been seen on RealView 2.2 built binaries,
22509 DW_TAG_template_type_param types actually _defined_ as
22510 children of the parent class:
22511
22512 enum E {};
22513 template class <class Enum> Class{};
22514 Class<enum E> class_e;
22515
22516 1: DW_TAG_class_type (Class)
22517 2: DW_TAG_enumeration_type (E)
22518 3: DW_TAG_enumerator (enum1:0)
22519 3: DW_TAG_enumerator (enum2:1)
22520 ...
22521 2: DW_TAG_template_type_param
22522 DW_AT_type DW_FORM_ref_udata (E)
22523
22524 Besides being broken debug info, it can put GDB into an
22525 infinite loop. Consider:
22526
22527 When we're building the full name for Class<E>, we'll start
22528 at Class, and go look over its template type parameters,
22529 finding E. We'll then try to build the full name of E, and
22530 reach here. We're now trying to build the full name of E,
22531 and look over the parent DIE for containing scope. In the
22532 broken case, if we followed the parent DIE of E, we'd again
22533 find Class, and once again go look at its template type
22534 arguments, etc., etc. Simply don't consider such parent die
22535 as source-level parent of this die (it can't be, the language
22536 doesn't allow it), and break the loop here. */
22537 name = dwarf2_name (die, cu);
22538 parent_name = dwarf2_name (parent, cu);
b98664d3 22539 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
22540 name ? name : "<unknown>",
22541 parent_name ? parent_name : "<unknown>");
22542 return "";
22543 }
63d06c5c 22544 else
0114d602
DJ
22545 switch (parent->tag)
22546 {
63d06c5c 22547 case DW_TAG_namespace:
0114d602 22548 parent_type = read_type_die (parent, cu);
acebe513
UW
22549 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22550 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22551 Work around this problem here. */
22552 if (cu->language == language_cplus
e86ca25f 22553 && strcmp (TYPE_NAME (parent_type), "::") == 0)
acebe513 22554 return "";
0114d602 22555 /* We give a name to even anonymous namespaces. */
e86ca25f 22556 return TYPE_NAME (parent_type);
63d06c5c 22557 case DW_TAG_class_type:
680b30c7 22558 case DW_TAG_interface_type:
63d06c5c 22559 case DW_TAG_structure_type:
0114d602 22560 case DW_TAG_union_type:
f55ee35c 22561 case DW_TAG_module:
0114d602 22562 parent_type = read_type_die (parent, cu);
e86ca25f
TT
22563 if (TYPE_NAME (parent_type) != NULL)
22564 return TYPE_NAME (parent_type);
0114d602
DJ
22565 else
22566 /* An anonymous structure is only allowed non-static data
22567 members; no typedefs, no member functions, et cetera.
22568 So it does not need a prefix. */
22569 return "";
abc72ce4 22570 case DW_TAG_compile_unit:
95554aad 22571 case DW_TAG_partial_unit:
abc72ce4
DE
22572 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22573 if (cu->language == language_cplus
fd5866f6 22574 && !dwarf2_per_objfile->types.empty ()
abc72ce4
DE
22575 && die->child != NULL
22576 && (die->tag == DW_TAG_class_type
22577 || die->tag == DW_TAG_structure_type
22578 || die->tag == DW_TAG_union_type))
22579 {
22580 char *name = guess_full_die_structure_name (die, cu);
22581 if (name != NULL)
22582 return name;
22583 }
22584 return "";
3d567982
TT
22585 case DW_TAG_enumeration_type:
22586 parent_type = read_type_die (parent, cu);
22587 if (TYPE_DECLARED_CLASS (parent_type))
22588 {
e86ca25f
TT
22589 if (TYPE_NAME (parent_type) != NULL)
22590 return TYPE_NAME (parent_type);
3d567982
TT
22591 return "";
22592 }
22593 /* Fall through. */
63d06c5c 22594 default:
8176b9b8 22595 return determine_prefix (parent, cu);
63d06c5c 22596 }
63d06c5c
DC
22597}
22598
3e43a32a
MS
22599/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22600 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22601 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22602 an obconcat, otherwise allocate storage for the result. The CU argument is
22603 used to determine the language and hence, the appropriate separator. */
987504bb 22604
f55ee35c 22605#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
22606
22607static char *
f55ee35c
JK
22608typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22609 int physname, struct dwarf2_cu *cu)
63d06c5c 22610{
f55ee35c 22611 const char *lead = "";
5c315b68 22612 const char *sep;
63d06c5c 22613
3e43a32a
MS
22614 if (suffix == NULL || suffix[0] == '\0'
22615 || prefix == NULL || prefix[0] == '\0')
987504bb 22616 sep = "";
45280282
IB
22617 else if (cu->language == language_d)
22618 {
22619 /* For D, the 'main' function could be defined in any module, but it
22620 should never be prefixed. */
22621 if (strcmp (suffix, "D main") == 0)
22622 {
22623 prefix = "";
22624 sep = "";
22625 }
22626 else
22627 sep = ".";
22628 }
f55ee35c
JK
22629 else if (cu->language == language_fortran && physname)
22630 {
22631 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22632 DW_AT_MIPS_linkage_name is preferred and used instead. */
22633
22634 lead = "__";
22635 sep = "_MOD_";
22636 }
987504bb
JJ
22637 else
22638 sep = "::";
63d06c5c 22639
6dd47d34
DE
22640 if (prefix == NULL)
22641 prefix = "";
22642 if (suffix == NULL)
22643 suffix = "";
22644
987504bb
JJ
22645 if (obs == NULL)
22646 {
3e43a32a 22647 char *retval
224c3ddb
SM
22648 = ((char *)
22649 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 22650
f55ee35c
JK
22651 strcpy (retval, lead);
22652 strcat (retval, prefix);
6dd47d34
DE
22653 strcat (retval, sep);
22654 strcat (retval, suffix);
63d06c5c
DC
22655 return retval;
22656 }
987504bb
JJ
22657 else
22658 {
22659 /* We have an obstack. */
f55ee35c 22660 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 22661 }
63d06c5c
DC
22662}
22663
c906108c
SS
22664/* Return sibling of die, NULL if no sibling. */
22665
f9aca02d 22666static struct die_info *
fba45db2 22667sibling_die (struct die_info *die)
c906108c 22668{
639d11d3 22669 return die->sibling;
c906108c
SS
22670}
22671
71c25dea
TT
22672/* Get name of a die, return NULL if not found. */
22673
15d034d0
TT
22674static const char *
22675dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
22676 struct obstack *obstack)
22677{
22678 if (name && cu->language == language_cplus)
22679 {
2f408ecb 22680 std::string canon_name = cp_canonicalize_string (name);
71c25dea 22681
2f408ecb 22682 if (!canon_name.empty ())
71c25dea 22683 {
2f408ecb 22684 if (canon_name != name)
efba19b0 22685 name = obstack_strdup (obstack, canon_name);
71c25dea
TT
22686 }
22687 }
22688
22689 return name;
c906108c
SS
22690}
22691
96553a0c
DE
22692/* Get name of a die, return NULL if not found.
22693 Anonymous namespaces are converted to their magic string. */
9219021c 22694
15d034d0 22695static const char *
e142c38c 22696dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
22697{
22698 struct attribute *attr;
518817b3 22699 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9219021c 22700
e142c38c 22701 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 22702 if ((!attr || !DW_STRING (attr))
96553a0c 22703 && die->tag != DW_TAG_namespace
53832f31
TT
22704 && die->tag != DW_TAG_class_type
22705 && die->tag != DW_TAG_interface_type
22706 && die->tag != DW_TAG_structure_type
22707 && die->tag != DW_TAG_union_type)
71c25dea
TT
22708 return NULL;
22709
22710 switch (die->tag)
22711 {
22712 case DW_TAG_compile_unit:
95554aad 22713 case DW_TAG_partial_unit:
71c25dea
TT
22714 /* Compilation units have a DW_AT_name that is a filename, not
22715 a source language identifier. */
22716 case DW_TAG_enumeration_type:
22717 case DW_TAG_enumerator:
22718 /* These tags always have simple identifiers already; no need
22719 to canonicalize them. */
22720 return DW_STRING (attr);
907af001 22721
96553a0c
DE
22722 case DW_TAG_namespace:
22723 if (attr != NULL && DW_STRING (attr) != NULL)
22724 return DW_STRING (attr);
22725 return CP_ANONYMOUS_NAMESPACE_STR;
22726
907af001
UW
22727 case DW_TAG_class_type:
22728 case DW_TAG_interface_type:
22729 case DW_TAG_structure_type:
22730 case DW_TAG_union_type:
22731 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22732 structures or unions. These were of the form "._%d" in GCC 4.1,
22733 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22734 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 22735 if (attr && DW_STRING (attr)
61012eef
GB
22736 && (startswith (DW_STRING (attr), "._")
22737 || startswith (DW_STRING (attr), "<anonymous")))
907af001 22738 return NULL;
53832f31
TT
22739
22740 /* GCC might emit a nameless typedef that has a linkage name. See
22741 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22742 if (!attr || DW_STRING (attr) == NULL)
22743 {
df5c6c50 22744 char *demangled = NULL;
53832f31 22745
73b9be8b 22746 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
22747 if (attr == NULL || DW_STRING (attr) == NULL)
22748 return NULL;
22749
df5c6c50
JK
22750 /* Avoid demangling DW_STRING (attr) the second time on a second
22751 call for the same DIE. */
22752 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 22753 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
22754
22755 if (demangled)
22756 {
e6a959d6 22757 const char *base;
96408a79 22758
53832f31 22759 /* FIXME: we already did this for the partial symbol... */
34a68019 22760 DW_STRING (attr)
021887d8
TT
22761 = obstack_strdup (&objfile->per_bfd->storage_obstack,
22762 demangled);
53832f31
TT
22763 DW_STRING_IS_CANONICAL (attr) = 1;
22764 xfree (demangled);
96408a79
SA
22765
22766 /* Strip any leading namespaces/classes, keep only the base name.
22767 DW_AT_name for named DIEs does not contain the prefixes. */
22768 base = strrchr (DW_STRING (attr), ':');
22769 if (base && base > DW_STRING (attr) && base[-1] == ':')
22770 return &base[1];
22771 else
22772 return DW_STRING (attr);
53832f31
TT
22773 }
22774 }
907af001
UW
22775 break;
22776
71c25dea 22777 default:
907af001
UW
22778 break;
22779 }
22780
22781 if (!DW_STRING_IS_CANONICAL (attr))
22782 {
22783 DW_STRING (attr)
22784 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
e3b94546 22785 &objfile->per_bfd->storage_obstack);
907af001 22786 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 22787 }
907af001 22788 return DW_STRING (attr);
9219021c
DC
22789}
22790
22791/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
22792 is none. *EXT_CU is the CU containing DIE on input, and the CU
22793 containing the return value on output. */
9219021c
DC
22794
22795static struct die_info *
f2f0e013 22796dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
22797{
22798 struct attribute *attr;
9219021c 22799
f2f0e013 22800 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
22801 if (attr == NULL)
22802 return NULL;
22803
f2f0e013 22804 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
22805}
22806
fa9c3fa0
TT
22807/* A convenience function that returns an "unknown" DWARF name,
22808 including the value of V. STR is the name of the entity being
22809 printed, e.g., "TAG". */
22810
22811static const char *
22812dwarf_unknown (const char *str, unsigned v)
22813{
22814 char *cell = get_print_cell ();
22815 xsnprintf (cell, PRINT_CELL_SIZE, "DW_%s_<unknown: %u>", str, v);
22816 return cell;
22817}
22818
c906108c
SS
22819/* Convert a DIE tag into its string name. */
22820
f39c6ffd 22821static const char *
aa1ee363 22822dwarf_tag_name (unsigned tag)
c906108c 22823{
f39c6ffd
TT
22824 const char *name = get_DW_TAG_name (tag);
22825
22826 if (name == NULL)
fa9c3fa0 22827 return dwarf_unknown ("TAG", tag);
f39c6ffd
TT
22828
22829 return name;
c906108c
SS
22830}
22831
22832/* Convert a DWARF attribute code into its string name. */
22833
f39c6ffd 22834static const char *
aa1ee363 22835dwarf_attr_name (unsigned attr)
c906108c 22836{
f39c6ffd
TT
22837 const char *name;
22838
c764a876 22839#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
22840 if (attr == DW_AT_MIPS_fde)
22841 return "DW_AT_MIPS_fde";
22842#else
22843 if (attr == DW_AT_HP_block_index)
22844 return "DW_AT_HP_block_index";
c764a876 22845#endif
f39c6ffd
TT
22846
22847 name = get_DW_AT_name (attr);
22848
22849 if (name == NULL)
fa9c3fa0 22850 return dwarf_unknown ("AT", attr);
f39c6ffd
TT
22851
22852 return name;
c906108c
SS
22853}
22854
a084a2a6
AT
22855/* Convert a unit type to corresponding DW_UT name. */
22856
22857static const char *
22858dwarf_unit_type_name (int unit_type) {
22859 switch (unit_type)
22860 {
22861 case 0x01:
22862 return "DW_UT_compile (0x01)";
22863 case 0x02:
22864 return "DW_UT_type (0x02)";
22865 case 0x03:
22866 return "DW_UT_partial (0x03)";
22867 case 0x04:
22868 return "DW_UT_skeleton (0x04)";
22869 case 0x05:
22870 return "DW_UT_split_compile (0x05)";
22871 case 0x06:
22872 return "DW_UT_split_type (0x06)";
22873 case 0x80:
22874 return "DW_UT_lo_user (0x80)";
22875 case 0xff:
22876 return "DW_UT_hi_user (0xff)";
22877 default:
22878 return nullptr;
22879 }
22880}
22881
c906108c
SS
22882/* Convert a DWARF value form code into its string name. */
22883
f39c6ffd 22884static const char *
aa1ee363 22885dwarf_form_name (unsigned form)
c906108c 22886{
f39c6ffd
TT
22887 const char *name = get_DW_FORM_name (form);
22888
22889 if (name == NULL)
fa9c3fa0 22890 return dwarf_unknown ("FORM", form);
f39c6ffd
TT
22891
22892 return name;
c906108c
SS
22893}
22894
a121b7c1 22895static const char *
fba45db2 22896dwarf_bool_name (unsigned mybool)
c906108c
SS
22897{
22898 if (mybool)
22899 return "TRUE";
22900 else
22901 return "FALSE";
22902}
22903
22904/* Convert a DWARF type code into its string name. */
22905
f39c6ffd 22906static const char *
aa1ee363 22907dwarf_type_encoding_name (unsigned enc)
c906108c 22908{
f39c6ffd 22909 const char *name = get_DW_ATE_name (enc);
c906108c 22910
f39c6ffd 22911 if (name == NULL)
fa9c3fa0 22912 return dwarf_unknown ("ATE", enc);
c906108c 22913
f39c6ffd 22914 return name;
c906108c 22915}
c906108c 22916
f9aca02d 22917static void
d97bc12b 22918dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
22919{
22920 unsigned int i;
22921
d97bc12b 22922 print_spaces (indent, f);
9d8780f0 22923 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 22924 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 22925 sect_offset_str (die->sect_off));
d97bc12b
DE
22926
22927 if (die->parent != NULL)
22928 {
22929 print_spaces (indent, f);
9d8780f0
SM
22930 fprintf_unfiltered (f, " parent at offset: %s\n",
22931 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
22932 }
22933
22934 print_spaces (indent, f);
22935 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 22936 dwarf_bool_name (die->child != NULL));
c906108c 22937
d97bc12b
DE
22938 print_spaces (indent, f);
22939 fprintf_unfiltered (f, " attributes:\n");
22940
c906108c
SS
22941 for (i = 0; i < die->num_attrs; ++i)
22942 {
d97bc12b
DE
22943 print_spaces (indent, f);
22944 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
22945 dwarf_attr_name (die->attrs[i].name),
22946 dwarf_form_name (die->attrs[i].form));
d97bc12b 22947
c906108c
SS
22948 switch (die->attrs[i].form)
22949 {
c906108c 22950 case DW_FORM_addr:
336d760d 22951 case DW_FORM_addrx:
3019eac3 22952 case DW_FORM_GNU_addr_index:
d97bc12b 22953 fprintf_unfiltered (f, "address: ");
5af949e3 22954 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
22955 break;
22956 case DW_FORM_block2:
22957 case DW_FORM_block4:
22958 case DW_FORM_block:
22959 case DW_FORM_block1:
56eb65bd
SP
22960 fprintf_unfiltered (f, "block: size %s",
22961 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 22962 break;
2dc7f7b3 22963 case DW_FORM_exprloc:
56eb65bd
SP
22964 fprintf_unfiltered (f, "expression: size %s",
22965 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 22966 break;
0224619f
JK
22967 case DW_FORM_data16:
22968 fprintf_unfiltered (f, "constant of 16 bytes");
22969 break;
4568ecf9
DE
22970 case DW_FORM_ref_addr:
22971 fprintf_unfiltered (f, "ref address: ");
22972 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22973 break;
36586728
TT
22974 case DW_FORM_GNU_ref_alt:
22975 fprintf_unfiltered (f, "alt ref address: ");
22976 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22977 break;
10b3939b
DJ
22978 case DW_FORM_ref1:
22979 case DW_FORM_ref2:
22980 case DW_FORM_ref4:
4568ecf9
DE
22981 case DW_FORM_ref8:
22982 case DW_FORM_ref_udata:
d97bc12b 22983 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 22984 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 22985 break;
c906108c
SS
22986 case DW_FORM_data1:
22987 case DW_FORM_data2:
22988 case DW_FORM_data4:
ce5d95e1 22989 case DW_FORM_data8:
c906108c
SS
22990 case DW_FORM_udata:
22991 case DW_FORM_sdata:
43bbcdc2
PH
22992 fprintf_unfiltered (f, "constant: %s",
22993 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 22994 break;
2dc7f7b3
TT
22995 case DW_FORM_sec_offset:
22996 fprintf_unfiltered (f, "section offset: %s",
22997 pulongest (DW_UNSND (&die->attrs[i])));
22998 break;
55f1336d 22999 case DW_FORM_ref_sig8:
ac9ec31b
DE
23000 fprintf_unfiltered (f, "signature: %s",
23001 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 23002 break;
c906108c 23003 case DW_FORM_string:
4bdf3d34 23004 case DW_FORM_strp:
43988095 23005 case DW_FORM_line_strp:
cf532bd1 23006 case DW_FORM_strx:
3019eac3 23007 case DW_FORM_GNU_str_index:
36586728 23008 case DW_FORM_GNU_strp_alt:
8285870a 23009 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 23010 DW_STRING (&die->attrs[i])
8285870a
JK
23011 ? DW_STRING (&die->attrs[i]) : "",
23012 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
23013 break;
23014 case DW_FORM_flag:
23015 if (DW_UNSND (&die->attrs[i]))
d97bc12b 23016 fprintf_unfiltered (f, "flag: TRUE");
c906108c 23017 else
d97bc12b 23018 fprintf_unfiltered (f, "flag: FALSE");
c906108c 23019 break;
2dc7f7b3
TT
23020 case DW_FORM_flag_present:
23021 fprintf_unfiltered (f, "flag: TRUE");
23022 break;
a8329558 23023 case DW_FORM_indirect:
0963b4bd
MS
23024 /* The reader will have reduced the indirect form to
23025 the "base form" so this form should not occur. */
3e43a32a
MS
23026 fprintf_unfiltered (f,
23027 "unexpected attribute form: DW_FORM_indirect");
a8329558 23028 break;
663c44ac
JK
23029 case DW_FORM_implicit_const:
23030 fprintf_unfiltered (f, "constant: %s",
23031 plongest (DW_SND (&die->attrs[i])));
23032 break;
c906108c 23033 default:
d97bc12b 23034 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 23035 die->attrs[i].form);
d97bc12b 23036 break;
c906108c 23037 }
d97bc12b 23038 fprintf_unfiltered (f, "\n");
c906108c
SS
23039 }
23040}
23041
f9aca02d 23042static void
d97bc12b 23043dump_die_for_error (struct die_info *die)
c906108c 23044{
d97bc12b
DE
23045 dump_die_shallow (gdb_stderr, 0, die);
23046}
23047
23048static void
23049dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23050{
23051 int indent = level * 4;
23052
23053 gdb_assert (die != NULL);
23054
23055 if (level >= max_level)
23056 return;
23057
23058 dump_die_shallow (f, indent, die);
23059
23060 if (die->child != NULL)
c906108c 23061 {
d97bc12b
DE
23062 print_spaces (indent, f);
23063 fprintf_unfiltered (f, " Children:");
23064 if (level + 1 < max_level)
23065 {
23066 fprintf_unfiltered (f, "\n");
23067 dump_die_1 (f, level + 1, max_level, die->child);
23068 }
23069 else
23070 {
3e43a32a
MS
23071 fprintf_unfiltered (f,
23072 " [not printed, max nesting level reached]\n");
d97bc12b
DE
23073 }
23074 }
23075
23076 if (die->sibling != NULL && level > 0)
23077 {
23078 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
23079 }
23080}
23081
d97bc12b
DE
23082/* This is called from the pdie macro in gdbinit.in.
23083 It's not static so gcc will keep a copy callable from gdb. */
23084
23085void
23086dump_die (struct die_info *die, int max_level)
23087{
23088 dump_die_1 (gdb_stdlog, 0, max_level, die);
23089}
23090
f9aca02d 23091static void
51545339 23092store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 23093{
51545339 23094 void **slot;
c906108c 23095
9c541725
PA
23096 slot = htab_find_slot_with_hash (cu->die_hash, die,
23097 to_underlying (die->sect_off),
b64f50a1 23098 INSERT);
51545339
DJ
23099
23100 *slot = die;
c906108c
SS
23101}
23102
b64f50a1
JK
23103/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
23104 required kind. */
23105
23106static sect_offset
ff39bb5e 23107dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 23108{
7771576e 23109 if (attr_form_is_ref (attr))
9c541725 23110 return (sect_offset) DW_UNSND (attr);
93311388 23111
b98664d3 23112 complaint (_("unsupported die ref attribute form: '%s'"),
93311388 23113 dwarf_form_name (attr->form));
9c541725 23114 return {};
c906108c
SS
23115}
23116
43bbcdc2
PH
23117/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
23118 * the value held by the attribute is not constant. */
a02abb62 23119
43bbcdc2 23120static LONGEST
ff39bb5e 23121dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 23122{
663c44ac 23123 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
23124 return DW_SND (attr);
23125 else if (attr->form == DW_FORM_udata
23126 || attr->form == DW_FORM_data1
23127 || attr->form == DW_FORM_data2
23128 || attr->form == DW_FORM_data4
23129 || attr->form == DW_FORM_data8)
23130 return DW_UNSND (attr);
23131 else
23132 {
0224619f 23133 /* For DW_FORM_data16 see attr_form_is_constant. */
b98664d3 23134 complaint (_("Attribute value is not a constant (%s)"),
a02abb62
JB
23135 dwarf_form_name (attr->form));
23136 return default_value;
23137 }
23138}
23139
348e048f
DE
23140/* Follow reference or signature attribute ATTR of SRC_DIE.
23141 On entry *REF_CU is the CU of SRC_DIE.
23142 On exit *REF_CU is the CU of the result. */
23143
23144static struct die_info *
ff39bb5e 23145follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
23146 struct dwarf2_cu **ref_cu)
23147{
23148 struct die_info *die;
23149
7771576e 23150 if (attr_form_is_ref (attr))
348e048f 23151 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 23152 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
23153 die = follow_die_sig (src_die, attr, ref_cu);
23154 else
23155 {
23156 dump_die_for_error (src_die);
23157 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
518817b3 23158 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
348e048f
DE
23159 }
23160
23161 return die;
03dd20cc
DJ
23162}
23163
5c631832 23164/* Follow reference OFFSET.
673bfd45
DE
23165 On entry *REF_CU is the CU of the source die referencing OFFSET.
23166 On exit *REF_CU is the CU of the result.
23167 Returns NULL if OFFSET is invalid. */
f504f079 23168
f9aca02d 23169static struct die_info *
9c541725 23170follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 23171 struct dwarf2_cu **ref_cu)
c906108c 23172{
10b3939b 23173 struct die_info temp_die;
f2f0e013 23174 struct dwarf2_cu *target_cu, *cu = *ref_cu;
518817b3
SM
23175 struct dwarf2_per_objfile *dwarf2_per_objfile
23176 = cu->per_cu->dwarf2_per_objfile;
10b3939b 23177
348e048f
DE
23178 gdb_assert (cu->per_cu != NULL);
23179
98bfdba5
PA
23180 target_cu = cu;
23181
3019eac3 23182 if (cu->per_cu->is_debug_types)
348e048f
DE
23183 {
23184 /* .debug_types CUs cannot reference anything outside their CU.
23185 If they need to, they have to reference a signatured type via
55f1336d 23186 DW_FORM_ref_sig8. */
9c541725 23187 if (!offset_in_cu_p (&cu->header, sect_off))
5c631832 23188 return NULL;
348e048f 23189 }
36586728 23190 else if (offset_in_dwz != cu->per_cu->is_dwz
9c541725 23191 || !offset_in_cu_p (&cu->header, sect_off))
10b3939b
DJ
23192 {
23193 struct dwarf2_per_cu_data *per_cu;
9a619af0 23194
9c541725 23195 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 23196 dwarf2_per_objfile);
03dd20cc
DJ
23197
23198 /* If necessary, add it to the queue and load its DIEs. */
95554aad 23199 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 23200 load_full_comp_unit (per_cu, false, cu->language);
03dd20cc 23201
10b3939b
DJ
23202 target_cu = per_cu->cu;
23203 }
98bfdba5
PA
23204 else if (cu->dies == NULL)
23205 {
23206 /* We're loading full DIEs during partial symbol reading. */
23207 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
58f0c718 23208 load_full_comp_unit (cu->per_cu, false, language_minimal);
98bfdba5 23209 }
c906108c 23210
f2f0e013 23211 *ref_cu = target_cu;
9c541725 23212 temp_die.sect_off = sect_off;
c24bdb02
KS
23213
23214 if (target_cu != cu)
23215 target_cu->ancestor = cu;
23216
9a3c8263 23217 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
23218 &temp_die,
23219 to_underlying (sect_off));
5c631832 23220}
10b3939b 23221
5c631832
JK
23222/* Follow reference attribute ATTR of SRC_DIE.
23223 On entry *REF_CU is the CU of SRC_DIE.
23224 On exit *REF_CU is the CU of the result. */
23225
23226static struct die_info *
ff39bb5e 23227follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
23228 struct dwarf2_cu **ref_cu)
23229{
9c541725 23230 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
23231 struct dwarf2_cu *cu = *ref_cu;
23232 struct die_info *die;
23233
9c541725 23234 die = follow_die_offset (sect_off,
36586728
TT
23235 (attr->form == DW_FORM_GNU_ref_alt
23236 || cu->per_cu->is_dwz),
23237 ref_cu);
5c631832 23238 if (!die)
9d8780f0
SM
23239 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23240 "at %s [in module %s]"),
23241 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
518817b3 23242 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
348e048f 23243
5c631832
JK
23244 return die;
23245}
23246
9c541725 23247/* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
d83e736b 23248 Returned value is intended for DW_OP_call*. Returned
e3b94546
SM
23249 dwarf2_locexpr_baton->data has lifetime of
23250 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
5c631832
JK
23251
23252struct dwarf2_locexpr_baton
9c541725 23253dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
8b9737bf
TT
23254 struct dwarf2_per_cu_data *per_cu,
23255 CORE_ADDR (*get_frame_pc) (void *baton),
e4a62c65 23256 void *baton, bool resolve_abstract_p)
5c631832 23257{
918dd910 23258 struct dwarf2_cu *cu;
5c631832
JK
23259 struct die_info *die;
23260 struct attribute *attr;
23261 struct dwarf2_locexpr_baton retval;
12359b5e
SM
23262 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
23263 struct objfile *objfile = dwarf2_per_objfile->objfile;
8cf6f0b1 23264
918dd910 23265 if (per_cu->cu == NULL)
58f0c718 23266 load_cu (per_cu, false);
918dd910 23267 cu = per_cu->cu;
cc12ce38
DE
23268 if (cu == NULL)
23269 {
23270 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23271 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23272 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23273 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23274 }
918dd910 23275
9c541725 23276 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 23277 if (!die)
9d8780f0
SM
23278 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23279 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23280
23281 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 23282 if (!attr && resolve_abstract_p
3360b6e7 23283 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
e4a62c65
TV
23284 != dwarf2_per_objfile->abstract_to_concrete.end ()))
23285 {
23286 CORE_ADDR pc = (*get_frame_pc) (baton);
eba4caf2
TV
23287 CORE_ADDR baseaddr
23288 = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
23289 struct gdbarch *gdbarch = get_objfile_arch (objfile);
e4a62c65 23290
3360b6e7
TV
23291 for (const auto &cand_off
23292 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
e4a62c65 23293 {
3360b6e7
TV
23294 struct dwarf2_cu *cand_cu = cu;
23295 struct die_info *cand
23296 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23297 if (!cand
23298 || !cand->parent
e4a62c65
TV
23299 || cand->parent->tag != DW_TAG_subprogram)
23300 continue;
23301
23302 CORE_ADDR pc_low, pc_high;
23303 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
23304 if (pc_low == ((CORE_ADDR) -1))
23305 continue;
23306 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23307 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23308 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
23309 continue;
23310
23311 die = cand;
23312 attr = dwarf2_attr (die, DW_AT_location, cu);
23313 break;
23314 }
23315 }
23316
5c631832
JK
23317 if (!attr)
23318 {
e103e986
JK
23319 /* DWARF: "If there is no such attribute, then there is no effect.".
23320 DATA is ignored if SIZE is 0. */
5c631832 23321
e103e986 23322 retval.data = NULL;
5c631832
JK
23323 retval.size = 0;
23324 }
8cf6f0b1
TT
23325 else if (attr_form_is_section_offset (attr))
23326 {
23327 struct dwarf2_loclist_baton loclist_baton;
23328 CORE_ADDR pc = (*get_frame_pc) (baton);
23329 size_t size;
23330
23331 fill_in_loclist_baton (cu, &loclist_baton, attr);
23332
23333 retval.data = dwarf2_find_location_expression (&loclist_baton,
23334 &size, pc);
23335 retval.size = size;
23336 }
5c631832
JK
23337 else
23338 {
23339 if (!attr_form_is_block (attr))
9d8780f0 23340 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 23341 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 23342 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23343
23344 retval.data = DW_BLOCK (attr)->data;
23345 retval.size = DW_BLOCK (attr)->size;
23346 }
23347 retval.per_cu = cu->per_cu;
918dd910 23348
ed2dc618 23349 age_cached_comp_units (dwarf2_per_objfile);
918dd910 23350
5c631832 23351 return retval;
348e048f
DE
23352}
23353
8b9737bf
TT
23354/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
23355 offset. */
23356
23357struct dwarf2_locexpr_baton
23358dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
23359 struct dwarf2_per_cu_data *per_cu,
23360 CORE_ADDR (*get_frame_pc) (void *baton),
23361 void *baton)
23362{
9c541725 23363 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 23364
9c541725 23365 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
23366}
23367
b6807d98
TT
23368/* Write a constant of a given type as target-ordered bytes into
23369 OBSTACK. */
23370
23371static const gdb_byte *
23372write_constant_as_bytes (struct obstack *obstack,
23373 enum bfd_endian byte_order,
23374 struct type *type,
23375 ULONGEST value,
23376 LONGEST *len)
23377{
23378 gdb_byte *result;
23379
23380 *len = TYPE_LENGTH (type);
224c3ddb 23381 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23382 store_unsigned_integer (result, *len, byte_order, value);
23383
23384 return result;
23385}
23386
23387/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23388 pointer to the constant bytes and set LEN to the length of the
23389 data. If memory is needed, allocate it on OBSTACK. If the DIE
23390 does not have a DW_AT_const_value, return NULL. */
23391
23392const gdb_byte *
9c541725 23393dwarf2_fetch_constant_bytes (sect_offset sect_off,
b6807d98
TT
23394 struct dwarf2_per_cu_data *per_cu,
23395 struct obstack *obstack,
23396 LONGEST *len)
23397{
23398 struct dwarf2_cu *cu;
23399 struct die_info *die;
23400 struct attribute *attr;
23401 const gdb_byte *result = NULL;
23402 struct type *type;
23403 LONGEST value;
23404 enum bfd_endian byte_order;
e3b94546 23405 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 23406
b6807d98 23407 if (per_cu->cu == NULL)
58f0c718 23408 load_cu (per_cu, false);
b6807d98 23409 cu = per_cu->cu;
cc12ce38
DE
23410 if (cu == NULL)
23411 {
23412 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23413 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23414 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23415 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23416 }
b6807d98 23417
9c541725 23418 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 23419 if (!die)
9d8780f0
SM
23420 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23421 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
23422
23423 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23424 if (attr == NULL)
23425 return NULL;
23426
e3b94546 23427 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
23428 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23429
23430 switch (attr->form)
23431 {
23432 case DW_FORM_addr:
336d760d 23433 case DW_FORM_addrx:
b6807d98
TT
23434 case DW_FORM_GNU_addr_index:
23435 {
23436 gdb_byte *tem;
23437
23438 *len = cu->header.addr_size;
224c3ddb 23439 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23440 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23441 result = tem;
23442 }
23443 break;
23444 case DW_FORM_string:
23445 case DW_FORM_strp:
cf532bd1 23446 case DW_FORM_strx:
b6807d98
TT
23447 case DW_FORM_GNU_str_index:
23448 case DW_FORM_GNU_strp_alt:
23449 /* DW_STRING is already allocated on the objfile obstack, point
23450 directly to it. */
23451 result = (const gdb_byte *) DW_STRING (attr);
23452 *len = strlen (DW_STRING (attr));
23453 break;
23454 case DW_FORM_block1:
23455 case DW_FORM_block2:
23456 case DW_FORM_block4:
23457 case DW_FORM_block:
23458 case DW_FORM_exprloc:
0224619f 23459 case DW_FORM_data16:
b6807d98
TT
23460 result = DW_BLOCK (attr)->data;
23461 *len = DW_BLOCK (attr)->size;
23462 break;
23463
23464 /* The DW_AT_const_value attributes are supposed to carry the
23465 symbol's value "represented as it would be on the target
23466 architecture." By the time we get here, it's already been
23467 converted to host endianness, so we just need to sign- or
23468 zero-extend it as appropriate. */
23469 case DW_FORM_data1:
23470 type = die_type (die, cu);
23471 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23472 if (result == NULL)
23473 result = write_constant_as_bytes (obstack, byte_order,
23474 type, value, len);
23475 break;
23476 case DW_FORM_data2:
23477 type = die_type (die, cu);
23478 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23479 if (result == NULL)
23480 result = write_constant_as_bytes (obstack, byte_order,
23481 type, value, len);
23482 break;
23483 case DW_FORM_data4:
23484 type = die_type (die, cu);
23485 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23486 if (result == NULL)
23487 result = write_constant_as_bytes (obstack, byte_order,
23488 type, value, len);
23489 break;
23490 case DW_FORM_data8:
23491 type = die_type (die, cu);
23492 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23493 if (result == NULL)
23494 result = write_constant_as_bytes (obstack, byte_order,
23495 type, value, len);
23496 break;
23497
23498 case DW_FORM_sdata:
663c44ac 23499 case DW_FORM_implicit_const:
b6807d98
TT
23500 type = die_type (die, cu);
23501 result = write_constant_as_bytes (obstack, byte_order,
23502 type, DW_SND (attr), len);
23503 break;
23504
23505 case DW_FORM_udata:
23506 type = die_type (die, cu);
23507 result = write_constant_as_bytes (obstack, byte_order,
23508 type, DW_UNSND (attr), len);
23509 break;
23510
23511 default:
b98664d3 23512 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
23513 dwarf_form_name (attr->form));
23514 break;
23515 }
23516
23517 return result;
23518}
23519
7942e96e
AA
23520/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23521 valid type for this die is found. */
23522
23523struct type *
9c541725 23524dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
7942e96e
AA
23525 struct dwarf2_per_cu_data *per_cu)
23526{
23527 struct dwarf2_cu *cu;
23528 struct die_info *die;
23529
7942e96e 23530 if (per_cu->cu == NULL)
58f0c718 23531 load_cu (per_cu, false);
7942e96e
AA
23532 cu = per_cu->cu;
23533 if (!cu)
23534 return NULL;
23535
9c541725 23536 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
23537 if (!die)
23538 return NULL;
23539
23540 return die_type (die, cu);
23541}
23542
8a9b8146
TT
23543/* Return the type of the DIE at DIE_OFFSET in the CU named by
23544 PER_CU. */
23545
23546struct type *
b64f50a1 23547dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
23548 struct dwarf2_per_cu_data *per_cu)
23549{
9c541725 23550 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 23551 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
23552}
23553
ac9ec31b 23554/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 23555 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
23556 On exit *REF_CU is the CU of the result.
23557 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
23558
23559static struct die_info *
ac9ec31b
DE
23560follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23561 struct dwarf2_cu **ref_cu)
348e048f 23562{
348e048f 23563 struct die_info temp_die;
c24bdb02 23564 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f
DE
23565 struct die_info *die;
23566
ac9ec31b
DE
23567 /* While it might be nice to assert sig_type->type == NULL here,
23568 we can get here for DW_AT_imported_declaration where we need
23569 the DIE not the type. */
348e048f
DE
23570
23571 /* If necessary, add it to the queue and load its DIEs. */
23572
95554aad 23573 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 23574 read_signatured_type (sig_type);
348e048f 23575
348e048f 23576 sig_cu = sig_type->per_cu.cu;
69d751e3 23577 gdb_assert (sig_cu != NULL);
9c541725
PA
23578 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23579 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 23580 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 23581 to_underlying (temp_die.sect_off));
348e048f
DE
23582 if (die)
23583 {
ed2dc618 23584 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 23585 = (*ref_cu)->per_cu->dwarf2_per_objfile;
ed2dc618 23586
796a7ff8
DE
23587 /* For .gdb_index version 7 keep track of included TUs.
23588 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23589 if (dwarf2_per_objfile->index_table != NULL
23590 && dwarf2_per_objfile->index_table->version <= 7)
23591 {
23592 VEC_safe_push (dwarf2_per_cu_ptr,
23593 (*ref_cu)->per_cu->imported_symtabs,
23594 sig_cu->per_cu);
23595 }
23596
348e048f 23597 *ref_cu = sig_cu;
c24bdb02
KS
23598 if (sig_cu != cu)
23599 sig_cu->ancestor = cu;
23600
348e048f
DE
23601 return die;
23602 }
23603
ac9ec31b
DE
23604 return NULL;
23605}
23606
23607/* Follow signatured type referenced by ATTR in SRC_DIE.
23608 On entry *REF_CU is the CU of SRC_DIE.
23609 On exit *REF_CU is the CU of the result.
23610 The result is the DIE of the type.
23611 If the referenced type cannot be found an error is thrown. */
23612
23613static struct die_info *
ff39bb5e 23614follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
23615 struct dwarf2_cu **ref_cu)
23616{
23617 ULONGEST signature = DW_SIGNATURE (attr);
23618 struct signatured_type *sig_type;
23619 struct die_info *die;
23620
23621 gdb_assert (attr->form == DW_FORM_ref_sig8);
23622
a2ce51a0 23623 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
23624 /* sig_type will be NULL if the signatured type is missing from
23625 the debug info. */
23626 if (sig_type == NULL)
23627 {
23628 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23629 " from DIE at %s [in module %s]"),
23630 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23631 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23632 }
23633
23634 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23635 if (die == NULL)
23636 {
23637 dump_die_for_error (src_die);
23638 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23639 " from DIE at %s [in module %s]"),
23640 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23641 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23642 }
23643
23644 return die;
23645}
23646
23647/* Get the type specified by SIGNATURE referenced in DIE/CU,
23648 reading in and processing the type unit if necessary. */
23649
23650static struct type *
23651get_signatured_type (struct die_info *die, ULONGEST signature,
23652 struct dwarf2_cu *cu)
23653{
518817b3
SM
23654 struct dwarf2_per_objfile *dwarf2_per_objfile
23655 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
23656 struct signatured_type *sig_type;
23657 struct dwarf2_cu *type_cu;
23658 struct die_info *type_die;
23659 struct type *type;
23660
a2ce51a0 23661 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
23662 /* sig_type will be NULL if the signatured type is missing from
23663 the debug info. */
23664 if (sig_type == NULL)
23665 {
b98664d3 23666 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23667 " from DIE at %s [in module %s]"),
23668 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23669 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23670 return build_error_marker_type (cu, die);
23671 }
23672
23673 /* If we already know the type we're done. */
23674 if (sig_type->type != NULL)
23675 return sig_type->type;
23676
23677 type_cu = cu;
23678 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23679 if (type_die != NULL)
23680 {
23681 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23682 is created. This is important, for example, because for c++ classes
23683 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23684 type = read_type_die (type_die, type_cu);
23685 if (type == NULL)
23686 {
b98664d3 23687 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
23688 " referenced from DIE at %s [in module %s]"),
23689 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23690 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23691 type = build_error_marker_type (cu, die);
23692 }
23693 }
23694 else
23695 {
b98664d3 23696 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23697 " from DIE at %s [in module %s]"),
23698 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23699 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23700 type = build_error_marker_type (cu, die);
23701 }
23702 sig_type->type = type;
23703
23704 return type;
23705}
23706
23707/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23708 reading in and processing the type unit if necessary. */
23709
23710static struct type *
ff39bb5e 23711get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 23712 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
23713{
23714 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 23715 if (attr_form_is_ref (attr))
ac9ec31b
DE
23716 {
23717 struct dwarf2_cu *type_cu = cu;
23718 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23719
23720 return read_type_die (type_die, type_cu);
23721 }
23722 else if (attr->form == DW_FORM_ref_sig8)
23723 {
23724 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23725 }
23726 else
23727 {
518817b3
SM
23728 struct dwarf2_per_objfile *dwarf2_per_objfile
23729 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 23730
b98664d3 23731 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
23732 " at %s [in module %s]"),
23733 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 23734 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23735 return build_error_marker_type (cu, die);
23736 }
348e048f
DE
23737}
23738
e5fe5e75 23739/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
23740
23741static void
e5fe5e75 23742load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 23743{
52dc124a 23744 struct signatured_type *sig_type;
348e048f 23745
f4dc4d17
DE
23746 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23747 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23748
6721b2ec
DE
23749 /* We have the per_cu, but we need the signatured_type.
23750 Fortunately this is an easy translation. */
23751 gdb_assert (per_cu->is_debug_types);
23752 sig_type = (struct signatured_type *) per_cu;
348e048f 23753
6721b2ec 23754 gdb_assert (per_cu->cu == NULL);
348e048f 23755
52dc124a 23756 read_signatured_type (sig_type);
348e048f 23757
6721b2ec 23758 gdb_assert (per_cu->cu != NULL);
348e048f
DE
23759}
23760
dee91e82
DE
23761/* die_reader_func for read_signatured_type.
23762 This is identical to load_full_comp_unit_reader,
23763 but is kept separate for now. */
348e048f
DE
23764
23765static void
dee91e82 23766read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 23767 const gdb_byte *info_ptr,
dee91e82
DE
23768 struct die_info *comp_unit_die,
23769 int has_children,
23770 void *data)
348e048f 23771{
dee91e82 23772 struct dwarf2_cu *cu = reader->cu;
348e048f 23773
dee91e82
DE
23774 gdb_assert (cu->die_hash == NULL);
23775 cu->die_hash =
23776 htab_create_alloc_ex (cu->header.length / 12,
23777 die_hash,
23778 die_eq,
23779 NULL,
23780 &cu->comp_unit_obstack,
23781 hashtab_obstack_allocate,
23782 dummy_obstack_deallocate);
348e048f 23783
dee91e82
DE
23784 if (has_children)
23785 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23786 &info_ptr, comp_unit_die);
23787 cu->dies = comp_unit_die;
23788 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
23789
23790 /* We try not to read any attributes in this function, because not
9cdd5dbd 23791 all CUs needed for references have been loaded yet, and symbol
348e048f 23792 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
23793 or we won't be able to build types correctly.
23794 Similarly, if we do not read the producer, we can not apply
23795 producer-specific interpretation. */
95554aad 23796 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 23797}
348e048f 23798
3019eac3
DE
23799/* Read in a signatured type and build its CU and DIEs.
23800 If the type is a stub for the real type in a DWO file,
23801 read in the real type from the DWO file as well. */
dee91e82
DE
23802
23803static void
23804read_signatured_type (struct signatured_type *sig_type)
23805{
23806 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 23807
3019eac3 23808 gdb_assert (per_cu->is_debug_types);
dee91e82 23809 gdb_assert (per_cu->cu == NULL);
348e048f 23810
58f0c718 23811 init_cutu_and_read_dies (per_cu, NULL, 0, 1, false,
f4dc4d17 23812 read_signatured_type_reader, NULL);
7ee85ab1 23813 sig_type->per_cu.tu_read = 1;
c906108c
SS
23814}
23815
c906108c
SS
23816/* Decode simple location descriptions.
23817 Given a pointer to a dwarf block that defines a location, compute
23818 the location and return the value.
23819
4cecd739
DJ
23820 NOTE drow/2003-11-18: This function is called in two situations
23821 now: for the address of static or global variables (partial symbols
23822 only) and for offsets into structures which are expected to be
23823 (more or less) constant. The partial symbol case should go away,
23824 and only the constant case should remain. That will let this
23825 function complain more accurately. A few special modes are allowed
23826 without complaint for global variables (for instance, global
23827 register values and thread-local values).
c906108c
SS
23828
23829 A location description containing no operations indicates that the
4cecd739 23830 object is optimized out. The return value is 0 for that case.
6b992462
DJ
23831 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23832 callers will only want a very basic result and this can become a
21ae7a4d
JK
23833 complaint.
23834
23835 Note that stack[0] is unused except as a default error return. */
c906108c
SS
23836
23837static CORE_ADDR
e7c27a73 23838decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 23839{
518817b3 23840 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
56eb65bd
SP
23841 size_t i;
23842 size_t size = blk->size;
d521ce57 23843 const gdb_byte *data = blk->data;
21ae7a4d
JK
23844 CORE_ADDR stack[64];
23845 int stacki;
23846 unsigned int bytes_read, unsnd;
23847 gdb_byte op;
c906108c 23848
21ae7a4d
JK
23849 i = 0;
23850 stacki = 0;
23851 stack[stacki] = 0;
23852 stack[++stacki] = 0;
23853
23854 while (i < size)
23855 {
23856 op = data[i++];
23857 switch (op)
23858 {
23859 case DW_OP_lit0:
23860 case DW_OP_lit1:
23861 case DW_OP_lit2:
23862 case DW_OP_lit3:
23863 case DW_OP_lit4:
23864 case DW_OP_lit5:
23865 case DW_OP_lit6:
23866 case DW_OP_lit7:
23867 case DW_OP_lit8:
23868 case DW_OP_lit9:
23869 case DW_OP_lit10:
23870 case DW_OP_lit11:
23871 case DW_OP_lit12:
23872 case DW_OP_lit13:
23873 case DW_OP_lit14:
23874 case DW_OP_lit15:
23875 case DW_OP_lit16:
23876 case DW_OP_lit17:
23877 case DW_OP_lit18:
23878 case DW_OP_lit19:
23879 case DW_OP_lit20:
23880 case DW_OP_lit21:
23881 case DW_OP_lit22:
23882 case DW_OP_lit23:
23883 case DW_OP_lit24:
23884 case DW_OP_lit25:
23885 case DW_OP_lit26:
23886 case DW_OP_lit27:
23887 case DW_OP_lit28:
23888 case DW_OP_lit29:
23889 case DW_OP_lit30:
23890 case DW_OP_lit31:
23891 stack[++stacki] = op - DW_OP_lit0;
23892 break;
f1bea926 23893
21ae7a4d
JK
23894 case DW_OP_reg0:
23895 case DW_OP_reg1:
23896 case DW_OP_reg2:
23897 case DW_OP_reg3:
23898 case DW_OP_reg4:
23899 case DW_OP_reg5:
23900 case DW_OP_reg6:
23901 case DW_OP_reg7:
23902 case DW_OP_reg8:
23903 case DW_OP_reg9:
23904 case DW_OP_reg10:
23905 case DW_OP_reg11:
23906 case DW_OP_reg12:
23907 case DW_OP_reg13:
23908 case DW_OP_reg14:
23909 case DW_OP_reg15:
23910 case DW_OP_reg16:
23911 case DW_OP_reg17:
23912 case DW_OP_reg18:
23913 case DW_OP_reg19:
23914 case DW_OP_reg20:
23915 case DW_OP_reg21:
23916 case DW_OP_reg22:
23917 case DW_OP_reg23:
23918 case DW_OP_reg24:
23919 case DW_OP_reg25:
23920 case DW_OP_reg26:
23921 case DW_OP_reg27:
23922 case DW_OP_reg28:
23923 case DW_OP_reg29:
23924 case DW_OP_reg30:
23925 case DW_OP_reg31:
23926 stack[++stacki] = op - DW_OP_reg0;
23927 if (i < size)
23928 dwarf2_complex_location_expr_complaint ();
23929 break;
c906108c 23930
21ae7a4d
JK
23931 case DW_OP_regx:
23932 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23933 i += bytes_read;
23934 stack[++stacki] = unsnd;
23935 if (i < size)
23936 dwarf2_complex_location_expr_complaint ();
23937 break;
c906108c 23938
21ae7a4d
JK
23939 case DW_OP_addr:
23940 stack[++stacki] = read_address (objfile->obfd, &data[i],
23941 cu, &bytes_read);
23942 i += bytes_read;
23943 break;
d53d4ac5 23944
21ae7a4d
JK
23945 case DW_OP_const1u:
23946 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23947 i += 1;
23948 break;
23949
23950 case DW_OP_const1s:
23951 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23952 i += 1;
23953 break;
23954
23955 case DW_OP_const2u:
23956 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23957 i += 2;
23958 break;
23959
23960 case DW_OP_const2s:
23961 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23962 i += 2;
23963 break;
d53d4ac5 23964
21ae7a4d
JK
23965 case DW_OP_const4u:
23966 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23967 i += 4;
23968 break;
23969
23970 case DW_OP_const4s:
23971 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23972 i += 4;
23973 break;
23974
585861ea
JK
23975 case DW_OP_const8u:
23976 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23977 i += 8;
23978 break;
23979
21ae7a4d
JK
23980 case DW_OP_constu:
23981 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23982 &bytes_read);
23983 i += bytes_read;
23984 break;
23985
23986 case DW_OP_consts:
23987 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23988 i += bytes_read;
23989 break;
23990
23991 case DW_OP_dup:
23992 stack[stacki + 1] = stack[stacki];
23993 stacki++;
23994 break;
23995
23996 case DW_OP_plus:
23997 stack[stacki - 1] += stack[stacki];
23998 stacki--;
23999 break;
24000
24001 case DW_OP_plus_uconst:
24002 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24003 &bytes_read);
24004 i += bytes_read;
24005 break;
24006
24007 case DW_OP_minus:
24008 stack[stacki - 1] -= stack[stacki];
24009 stacki--;
24010 break;
24011
24012 case DW_OP_deref:
24013 /* If we're not the last op, then we definitely can't encode
24014 this using GDB's address_class enum. This is valid for partial
24015 global symbols, although the variable's address will be bogus
24016 in the psymtab. */
24017 if (i < size)
24018 dwarf2_complex_location_expr_complaint ();
24019 break;
24020
24021 case DW_OP_GNU_push_tls_address:
4aa4e28b 24022 case DW_OP_form_tls_address:
21ae7a4d
JK
24023 /* The top of the stack has the offset from the beginning
24024 of the thread control block at which the variable is located. */
24025 /* Nothing should follow this operator, so the top of stack would
24026 be returned. */
24027 /* This is valid for partial global symbols, but the variable's
585861ea
JK
24028 address will be bogus in the psymtab. Make it always at least
24029 non-zero to not look as a variable garbage collected by linker
24030 which have DW_OP_addr 0. */
21ae7a4d
JK
24031 if (i < size)
24032 dwarf2_complex_location_expr_complaint ();
585861ea 24033 stack[stacki]++;
21ae7a4d
JK
24034 break;
24035
24036 case DW_OP_GNU_uninit:
24037 break;
24038
336d760d 24039 case DW_OP_addrx:
3019eac3 24040 case DW_OP_GNU_addr_index:
49f6c839 24041 case DW_OP_GNU_const_index:
3019eac3
DE
24042 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24043 &bytes_read);
24044 i += bytes_read;
24045 break;
24046
21ae7a4d
JK
24047 default:
24048 {
f39c6ffd 24049 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
24050
24051 if (name)
b98664d3 24052 complaint (_("unsupported stack op: '%s'"),
21ae7a4d
JK
24053 name);
24054 else
b98664d3 24055 complaint (_("unsupported stack op: '%02x'"),
21ae7a4d
JK
24056 op);
24057 }
24058
24059 return (stack[stacki]);
d53d4ac5 24060 }
3c6e0cb3 24061
21ae7a4d
JK
24062 /* Enforce maximum stack depth of SIZE-1 to avoid writing
24063 outside of the allocated space. Also enforce minimum>0. */
24064 if (stacki >= ARRAY_SIZE (stack) - 1)
24065 {
b98664d3 24066 complaint (_("location description stack overflow"));
21ae7a4d
JK
24067 return 0;
24068 }
24069
24070 if (stacki <= 0)
24071 {
b98664d3 24072 complaint (_("location description stack underflow"));
21ae7a4d
JK
24073 return 0;
24074 }
24075 }
24076 return (stack[stacki]);
c906108c
SS
24077}
24078
24079/* memory allocation interface */
24080
c906108c 24081static struct dwarf_block *
7b5a2f43 24082dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 24083{
8d749320 24084 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
24085}
24086
c906108c 24087static struct die_info *
b60c80d6 24088dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
24089{
24090 struct die_info *die;
b60c80d6
DJ
24091 size_t size = sizeof (struct die_info);
24092
24093 if (num_attrs > 1)
24094 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 24095
b60c80d6 24096 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
24097 memset (die, 0, sizeof (struct die_info));
24098 return (die);
24099}
2e276125
JB
24100
24101\f
24102/* Macro support. */
24103
233d95b5
JK
24104/* Return file name relative to the compilation directory of file number I in
24105 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 24106 responsible for freeing it. */
233d95b5 24107
2e276125 24108static char *
233d95b5 24109file_file_name (int file, struct line_header *lh)
2e276125 24110{
6a83a1e6
EZ
24111 /* Is the file number a valid index into the line header's file name
24112 table? Remember that file numbers start with one, not zero. */
fff8551c 24113 if (1 <= file && file <= lh->file_names.size ())
6a83a1e6 24114 {
8c43009f 24115 const file_entry &fe = lh->file_names[file - 1];
6e70227d 24116
8c43009f
PA
24117 if (!IS_ABSOLUTE_PATH (fe.name))
24118 {
24119 const char *dir = fe.include_dir (lh);
24120 if (dir != NULL)
24121 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
24122 }
24123 return xstrdup (fe.name);
6a83a1e6 24124 }
2e276125
JB
24125 else
24126 {
6a83a1e6
EZ
24127 /* The compiler produced a bogus file number. We can at least
24128 record the macro definitions made in the file, even if we
24129 won't be able to find the file by name. */
24130 char fake_name[80];
9a619af0 24131
8c042590
PM
24132 xsnprintf (fake_name, sizeof (fake_name),
24133 "<bad macro file number %d>", file);
2e276125 24134
b98664d3 24135 complaint (_("bad file number in macro information (%d)"),
6a83a1e6 24136 file);
2e276125 24137
6a83a1e6 24138 return xstrdup (fake_name);
2e276125
JB
24139 }
24140}
24141
233d95b5
JK
24142/* Return the full name of file number I in *LH's file name table.
24143 Use COMP_DIR as the name of the current directory of the
24144 compilation. The result is allocated using xmalloc; the caller is
24145 responsible for freeing it. */
24146static char *
24147file_full_name (int file, struct line_header *lh, const char *comp_dir)
24148{
24149 /* Is the file number a valid index into the line header's file name
24150 table? Remember that file numbers start with one, not zero. */
fff8551c 24151 if (1 <= file && file <= lh->file_names.size ())
233d95b5
JK
24152 {
24153 char *relative = file_file_name (file, lh);
24154
24155 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
24156 return relative;
b36cec19
PA
24157 return reconcat (relative, comp_dir, SLASH_STRING,
24158 relative, (char *) NULL);
233d95b5
JK
24159 }
24160 else
24161 return file_file_name (file, lh);
24162}
24163
2e276125
JB
24164
24165static struct macro_source_file *
804d2729
TT
24166macro_start_file (struct dwarf2_cu *cu,
24167 int file, int line,
2e276125 24168 struct macro_source_file *current_file,
43f3e411 24169 struct line_header *lh)
2e276125 24170{
233d95b5
JK
24171 /* File name relative to the compilation directory of this source file. */
24172 char *file_name = file_file_name (file, lh);
2e276125 24173
2e276125 24174 if (! current_file)
abc9d0dc 24175 {
fc474241
DE
24176 /* Note: We don't create a macro table for this compilation unit
24177 at all until we actually get a filename. */
c24bdb02 24178 struct macro_table *macro_table = cu->get_builder ()->get_macro_table ();
fc474241 24179
abc9d0dc
TT
24180 /* If we have no current file, then this must be the start_file
24181 directive for the compilation unit's main source file. */
fc474241
DE
24182 current_file = macro_set_main (macro_table, file_name);
24183 macro_define_special (macro_table);
abc9d0dc 24184 }
2e276125 24185 else
233d95b5 24186 current_file = macro_include (current_file, line, file_name);
2e276125 24187
233d95b5 24188 xfree (file_name);
6e70227d 24189
2e276125
JB
24190 return current_file;
24191}
24192
2e276125
JB
24193static const char *
24194consume_improper_spaces (const char *p, const char *body)
24195{
24196 if (*p == ' ')
24197 {
b98664d3 24198 complaint (_("macro definition contains spaces "
3e43a32a 24199 "in formal argument list:\n`%s'"),
4d3c2250 24200 body);
2e276125
JB
24201
24202 while (*p == ' ')
24203 p++;
24204 }
24205
24206 return p;
24207}
24208
24209
24210static void
24211parse_macro_definition (struct macro_source_file *file, int line,
24212 const char *body)
24213{
24214 const char *p;
24215
24216 /* The body string takes one of two forms. For object-like macro
24217 definitions, it should be:
24218
24219 <macro name> " " <definition>
24220
24221 For function-like macro definitions, it should be:
24222
24223 <macro name> "() " <definition>
24224 or
24225 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
24226
24227 Spaces may appear only where explicitly indicated, and in the
24228 <definition>.
24229
24230 The Dwarf 2 spec says that an object-like macro's name is always
24231 followed by a space, but versions of GCC around March 2002 omit
6e70227d 24232 the space when the macro's definition is the empty string.
2e276125
JB
24233
24234 The Dwarf 2 spec says that there should be no spaces between the
24235 formal arguments in a function-like macro's formal argument list,
24236 but versions of GCC around March 2002 include spaces after the
24237 commas. */
24238
24239
24240 /* Find the extent of the macro name. The macro name is terminated
24241 by either a space or null character (for an object-like macro) or
24242 an opening paren (for a function-like macro). */
24243 for (p = body; *p; p++)
24244 if (*p == ' ' || *p == '(')
24245 break;
24246
24247 if (*p == ' ' || *p == '\0')
24248 {
24249 /* It's an object-like macro. */
24250 int name_len = p - body;
3f8a7804 24251 char *name = savestring (body, name_len);
2e276125
JB
24252 const char *replacement;
24253
24254 if (*p == ' ')
24255 replacement = body + name_len + 1;
24256 else
24257 {
4d3c2250 24258 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24259 replacement = body + name_len;
24260 }
6e70227d 24261
2e276125
JB
24262 macro_define_object (file, line, name, replacement);
24263
24264 xfree (name);
24265 }
24266 else if (*p == '(')
24267 {
24268 /* It's a function-like macro. */
3f8a7804 24269 char *name = savestring (body, p - body);
2e276125
JB
24270 int argc = 0;
24271 int argv_size = 1;
8d749320 24272 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
24273
24274 p++;
24275
24276 p = consume_improper_spaces (p, body);
24277
24278 /* Parse the formal argument list. */
24279 while (*p && *p != ')')
24280 {
24281 /* Find the extent of the current argument name. */
24282 const char *arg_start = p;
24283
24284 while (*p && *p != ',' && *p != ')' && *p != ' ')
24285 p++;
24286
24287 if (! *p || p == arg_start)
4d3c2250 24288 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24289 else
24290 {
24291 /* Make sure argv has room for the new argument. */
24292 if (argc >= argv_size)
24293 {
24294 argv_size *= 2;
224c3ddb 24295 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
24296 }
24297
3f8a7804 24298 argv[argc++] = savestring (arg_start, p - arg_start);
2e276125
JB
24299 }
24300
24301 p = consume_improper_spaces (p, body);
24302
24303 /* Consume the comma, if present. */
24304 if (*p == ',')
24305 {
24306 p++;
24307
24308 p = consume_improper_spaces (p, body);
24309 }
24310 }
24311
24312 if (*p == ')')
24313 {
24314 p++;
24315
24316 if (*p == ' ')
24317 /* Perfectly formed definition, no complaints. */
24318 macro_define_function (file, line, name,
6e70227d 24319 argc, (const char **) argv,
2e276125
JB
24320 p + 1);
24321 else if (*p == '\0')
24322 {
24323 /* Complain, but do define it. */
4d3c2250 24324 dwarf2_macro_malformed_definition_complaint (body);
2e276125 24325 macro_define_function (file, line, name,
6e70227d 24326 argc, (const char **) argv,
2e276125
JB
24327 p);
24328 }
24329 else
24330 /* Just complain. */
4d3c2250 24331 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24332 }
24333 else
24334 /* Just complain. */
4d3c2250 24335 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24336
24337 xfree (name);
24338 {
24339 int i;
24340
24341 for (i = 0; i < argc; i++)
24342 xfree (argv[i]);
24343 }
24344 xfree (argv);
24345 }
24346 else
4d3c2250 24347 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
24348}
24349
cf2c3c16
TT
24350/* Skip some bytes from BYTES according to the form given in FORM.
24351 Returns the new pointer. */
2e276125 24352
d521ce57
TT
24353static const gdb_byte *
24354skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
24355 enum dwarf_form form,
24356 unsigned int offset_size,
24357 struct dwarf2_section_info *section)
2e276125 24358{
cf2c3c16 24359 unsigned int bytes_read;
2e276125 24360
cf2c3c16 24361 switch (form)
2e276125 24362 {
cf2c3c16
TT
24363 case DW_FORM_data1:
24364 case DW_FORM_flag:
24365 ++bytes;
24366 break;
24367
24368 case DW_FORM_data2:
24369 bytes += 2;
24370 break;
24371
24372 case DW_FORM_data4:
24373 bytes += 4;
24374 break;
24375
24376 case DW_FORM_data8:
24377 bytes += 8;
24378 break;
24379
0224619f
JK
24380 case DW_FORM_data16:
24381 bytes += 16;
24382 break;
24383
cf2c3c16
TT
24384 case DW_FORM_string:
24385 read_direct_string (abfd, bytes, &bytes_read);
24386 bytes += bytes_read;
24387 break;
24388
24389 case DW_FORM_sec_offset:
24390 case DW_FORM_strp:
36586728 24391 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
24392 bytes += offset_size;
24393 break;
24394
24395 case DW_FORM_block:
24396 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24397 bytes += bytes_read;
24398 break;
24399
24400 case DW_FORM_block1:
24401 bytes += 1 + read_1_byte (abfd, bytes);
24402 break;
24403 case DW_FORM_block2:
24404 bytes += 2 + read_2_bytes (abfd, bytes);
24405 break;
24406 case DW_FORM_block4:
24407 bytes += 4 + read_4_bytes (abfd, bytes);
24408 break;
24409
336d760d 24410 case DW_FORM_addrx:
cf2c3c16 24411 case DW_FORM_sdata:
cf532bd1 24412 case DW_FORM_strx:
cf2c3c16 24413 case DW_FORM_udata:
3019eac3
DE
24414 case DW_FORM_GNU_addr_index:
24415 case DW_FORM_GNU_str_index:
d521ce57 24416 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
24417 if (bytes == NULL)
24418 {
24419 dwarf2_section_buffer_overflow_complaint (section);
24420 return NULL;
24421 }
cf2c3c16
TT
24422 break;
24423
663c44ac
JK
24424 case DW_FORM_implicit_const:
24425 break;
24426
cf2c3c16
TT
24427 default:
24428 {
b98664d3 24429 complaint (_("invalid form 0x%x in `%s'"),
a32a8923 24430 form, get_section_name (section));
cf2c3c16
TT
24431 return NULL;
24432 }
2e276125
JB
24433 }
24434
cf2c3c16
TT
24435 return bytes;
24436}
757a13d0 24437
cf2c3c16
TT
24438/* A helper for dwarf_decode_macros that handles skipping an unknown
24439 opcode. Returns an updated pointer to the macro data buffer; or,
24440 on error, issues a complaint and returns NULL. */
757a13d0 24441
d521ce57 24442static const gdb_byte *
cf2c3c16 24443skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
24444 const gdb_byte **opcode_definitions,
24445 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
24446 bfd *abfd,
24447 unsigned int offset_size,
24448 struct dwarf2_section_info *section)
24449{
24450 unsigned int bytes_read, i;
24451 unsigned long arg;
d521ce57 24452 const gdb_byte *defn;
2e276125 24453
cf2c3c16 24454 if (opcode_definitions[opcode] == NULL)
2e276125 24455 {
b98664d3 24456 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
cf2c3c16
TT
24457 opcode);
24458 return NULL;
24459 }
2e276125 24460
cf2c3c16
TT
24461 defn = opcode_definitions[opcode];
24462 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24463 defn += bytes_read;
2e276125 24464
cf2c3c16
TT
24465 for (i = 0; i < arg; ++i)
24466 {
aead7601
SM
24467 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24468 (enum dwarf_form) defn[i], offset_size,
f664829e 24469 section);
cf2c3c16
TT
24470 if (mac_ptr == NULL)
24471 {
24472 /* skip_form_bytes already issued the complaint. */
24473 return NULL;
24474 }
24475 }
757a13d0 24476
cf2c3c16
TT
24477 return mac_ptr;
24478}
757a13d0 24479
cf2c3c16
TT
24480/* A helper function which parses the header of a macro section.
24481 If the macro section is the extended (for now called "GNU") type,
24482 then this updates *OFFSET_SIZE. Returns a pointer to just after
24483 the header, or issues a complaint and returns NULL on error. */
757a13d0 24484
d521ce57
TT
24485static const gdb_byte *
24486dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 24487 bfd *abfd,
d521ce57 24488 const gdb_byte *mac_ptr,
cf2c3c16
TT
24489 unsigned int *offset_size,
24490 int section_is_gnu)
24491{
24492 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 24493
cf2c3c16
TT
24494 if (section_is_gnu)
24495 {
24496 unsigned int version, flags;
757a13d0 24497
cf2c3c16 24498 version = read_2_bytes (abfd, mac_ptr);
0af92d60 24499 if (version != 4 && version != 5)
cf2c3c16 24500 {
b98664d3 24501 complaint (_("unrecognized version `%d' in .debug_macro section"),
cf2c3c16
TT
24502 version);
24503 return NULL;
24504 }
24505 mac_ptr += 2;
757a13d0 24506
cf2c3c16
TT
24507 flags = read_1_byte (abfd, mac_ptr);
24508 ++mac_ptr;
24509 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 24510
cf2c3c16
TT
24511 if ((flags & 2) != 0)
24512 /* We don't need the line table offset. */
24513 mac_ptr += *offset_size;
757a13d0 24514
cf2c3c16
TT
24515 /* Vendor opcode descriptions. */
24516 if ((flags & 4) != 0)
24517 {
24518 unsigned int i, count;
757a13d0 24519
cf2c3c16
TT
24520 count = read_1_byte (abfd, mac_ptr);
24521 ++mac_ptr;
24522 for (i = 0; i < count; ++i)
24523 {
24524 unsigned int opcode, bytes_read;
24525 unsigned long arg;
24526
24527 opcode = read_1_byte (abfd, mac_ptr);
24528 ++mac_ptr;
24529 opcode_definitions[opcode] = mac_ptr;
24530 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24531 mac_ptr += bytes_read;
24532 mac_ptr += arg;
24533 }
757a13d0 24534 }
cf2c3c16 24535 }
757a13d0 24536
cf2c3c16
TT
24537 return mac_ptr;
24538}
757a13d0 24539
cf2c3c16 24540/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 24541 including DW_MACRO_import. */
cf2c3c16
TT
24542
24543static void
804d2729 24544dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
ed2dc618 24545 bfd *abfd,
d521ce57 24546 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 24547 struct macro_source_file *current_file,
43f3e411 24548 struct line_header *lh,
cf2c3c16 24549 struct dwarf2_section_info *section,
36586728 24550 int section_is_gnu, int section_is_dwz,
cf2c3c16 24551 unsigned int offset_size,
8fc3fc34 24552 htab_t include_hash)
cf2c3c16 24553{
804d2729
TT
24554 struct dwarf2_per_objfile *dwarf2_per_objfile
24555 = cu->per_cu->dwarf2_per_objfile;
4d663531 24556 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
24557 enum dwarf_macro_record_type macinfo_type;
24558 int at_commandline;
d521ce57 24559 const gdb_byte *opcode_definitions[256];
757a13d0 24560
cf2c3c16
TT
24561 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24562 &offset_size, section_is_gnu);
24563 if (mac_ptr == NULL)
24564 {
24565 /* We already issued a complaint. */
24566 return;
24567 }
757a13d0
JK
24568
24569 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24570 GDB is still reading the definitions from command line. First
24571 DW_MACINFO_start_file will need to be ignored as it was already executed
24572 to create CURRENT_FILE for the main source holding also the command line
24573 definitions. On first met DW_MACINFO_start_file this flag is reset to
24574 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24575
24576 at_commandline = 1;
24577
24578 do
24579 {
24580 /* Do we at least have room for a macinfo type byte? */
24581 if (mac_ptr >= mac_end)
24582 {
f664829e 24583 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
24584 break;
24585 }
24586
aead7601 24587 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
24588 mac_ptr++;
24589
cf2c3c16
TT
24590 /* Note that we rely on the fact that the corresponding GNU and
24591 DWARF constants are the same. */
132448f8
SM
24592 DIAGNOSTIC_PUSH
24593 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
757a13d0
JK
24594 switch (macinfo_type)
24595 {
24596 /* A zero macinfo type indicates the end of the macro
24597 information. */
24598 case 0:
24599 break;
2e276125 24600
0af92d60
JK
24601 case DW_MACRO_define:
24602 case DW_MACRO_undef:
24603 case DW_MACRO_define_strp:
24604 case DW_MACRO_undef_strp:
24605 case DW_MACRO_define_sup:
24606 case DW_MACRO_undef_sup:
2e276125 24607 {
891d2f0b 24608 unsigned int bytes_read;
2e276125 24609 int line;
d521ce57 24610 const char *body;
cf2c3c16 24611 int is_define;
2e276125 24612
cf2c3c16
TT
24613 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24614 mac_ptr += bytes_read;
24615
0af92d60
JK
24616 if (macinfo_type == DW_MACRO_define
24617 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
24618 {
24619 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24620 mac_ptr += bytes_read;
24621 }
24622 else
24623 {
24624 LONGEST str_offset;
24625
24626 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24627 mac_ptr += offset_size;
2e276125 24628
0af92d60
JK
24629 if (macinfo_type == DW_MACRO_define_sup
24630 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 24631 || section_is_dwz)
36586728 24632 {
ed2dc618
SM
24633 struct dwz_file *dwz
24634 = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728 24635
ed2dc618
SM
24636 body = read_indirect_string_from_dwz (objfile,
24637 dwz, str_offset);
36586728
TT
24638 }
24639 else
ed2dc618
SM
24640 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24641 abfd, str_offset);
cf2c3c16
TT
24642 }
24643
0af92d60
JK
24644 is_define = (macinfo_type == DW_MACRO_define
24645 || macinfo_type == DW_MACRO_define_strp
24646 || macinfo_type == DW_MACRO_define_sup);
2e276125 24647 if (! current_file)
757a13d0
JK
24648 {
24649 /* DWARF violation as no main source is present. */
b98664d3 24650 complaint (_("debug info with no main source gives macro %s "
757a13d0 24651 "on line %d: %s"),
cf2c3c16
TT
24652 is_define ? _("definition") : _("undefinition"),
24653 line, body);
757a13d0
JK
24654 break;
24655 }
3e43a32a
MS
24656 if ((line == 0 && !at_commandline)
24657 || (line != 0 && at_commandline))
b98664d3 24658 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
757a13d0 24659 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 24660 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
24661 line == 0 ? _("zero") : _("non-zero"), line, body);
24662
955b06fa 24663 if (body == NULL)
7bede828 24664 {
955b06fa
SDJ
24665 /* Fedora's rpm-build's "debugedit" binary
24666 corrupted .debug_macro sections.
24667
24668 For more info, see
24669 https://bugzilla.redhat.com/show_bug.cgi?id=1708786 */
24670 complaint (_("debug info gives %s invalid macro %s "
24671 "without body (corrupted?) at line %d "
24672 "on file %s"),
24673 at_commandline ? _("command-line") : _("in-file"),
24674 is_define ? _("definition") : _("undefinition"),
24675 line, current_file->filename);
7bede828 24676 }
955b06fa
SDJ
24677 else if (is_define)
24678 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
24679 else
24680 {
0af92d60
JK
24681 gdb_assert (macinfo_type == DW_MACRO_undef
24682 || macinfo_type == DW_MACRO_undef_strp
24683 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
24684 macro_undef (current_file, line, body);
24685 }
2e276125
JB
24686 }
24687 break;
24688
0af92d60 24689 case DW_MACRO_start_file:
2e276125 24690 {
891d2f0b 24691 unsigned int bytes_read;
2e276125
JB
24692 int line, file;
24693
24694 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24695 mac_ptr += bytes_read;
24696 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24697 mac_ptr += bytes_read;
24698
3e43a32a
MS
24699 if ((line == 0 && !at_commandline)
24700 || (line != 0 && at_commandline))
b98664d3 24701 complaint (_("debug info gives source %d included "
757a13d0
JK
24702 "from %s at %s line %d"),
24703 file, at_commandline ? _("command-line") : _("file"),
24704 line == 0 ? _("zero") : _("non-zero"), line);
24705
24706 if (at_commandline)
24707 {
0af92d60 24708 /* This DW_MACRO_start_file was executed in the
cf2c3c16 24709 pass one. */
757a13d0
JK
24710 at_commandline = 0;
24711 }
24712 else
804d2729
TT
24713 current_file = macro_start_file (cu, file, line, current_file,
24714 lh);
2e276125
JB
24715 }
24716 break;
24717
0af92d60 24718 case DW_MACRO_end_file:
2e276125 24719 if (! current_file)
b98664d3 24720 complaint (_("macro debug info has an unmatched "
3e43a32a 24721 "`close_file' directive"));
2e276125
JB
24722 else
24723 {
24724 current_file = current_file->included_by;
24725 if (! current_file)
24726 {
cf2c3c16 24727 enum dwarf_macro_record_type next_type;
2e276125
JB
24728
24729 /* GCC circa March 2002 doesn't produce the zero
24730 type byte marking the end of the compilation
24731 unit. Complain if it's not there, but exit no
24732 matter what. */
24733
24734 /* Do we at least have room for a macinfo type byte? */
24735 if (mac_ptr >= mac_end)
24736 {
f664829e 24737 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
24738 return;
24739 }
24740
24741 /* We don't increment mac_ptr here, so this is just
24742 a look-ahead. */
aead7601
SM
24743 next_type
24744 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24745 mac_ptr);
2e276125 24746 if (next_type != 0)
b98664d3 24747 complaint (_("no terminating 0-type entry for "
3e43a32a 24748 "macros in `.debug_macinfo' section"));
2e276125
JB
24749
24750 return;
24751 }
24752 }
24753 break;
24754
0af92d60
JK
24755 case DW_MACRO_import:
24756 case DW_MACRO_import_sup:
cf2c3c16
TT
24757 {
24758 LONGEST offset;
8fc3fc34 24759 void **slot;
a036ba48
TT
24760 bfd *include_bfd = abfd;
24761 struct dwarf2_section_info *include_section = section;
d521ce57 24762 const gdb_byte *include_mac_end = mac_end;
a036ba48 24763 int is_dwz = section_is_dwz;
d521ce57 24764 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
24765
24766 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24767 mac_ptr += offset_size;
24768
0af92d60 24769 if (macinfo_type == DW_MACRO_import_sup)
a036ba48 24770 {
ed2dc618 24771 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
a036ba48 24772
4d663531 24773 dwarf2_read_section (objfile, &dwz->macro);
a036ba48 24774
a036ba48 24775 include_section = &dwz->macro;
a32a8923 24776 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
24777 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24778 is_dwz = 1;
24779 }
24780
24781 new_mac_ptr = include_section->buffer + offset;
24782 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24783
8fc3fc34
TT
24784 if (*slot != NULL)
24785 {
24786 /* This has actually happened; see
24787 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
b98664d3 24788 complaint (_("recursive DW_MACRO_import in "
8fc3fc34
TT
24789 ".debug_macro section"));
24790 }
24791 else
24792 {
d521ce57 24793 *slot = (void *) new_mac_ptr;
36586728 24794
804d2729 24795 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
43f3e411 24796 include_mac_end, current_file, lh,
36586728 24797 section, section_is_gnu, is_dwz,
4d663531 24798 offset_size, include_hash);
8fc3fc34 24799
d521ce57 24800 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 24801 }
cf2c3c16
TT
24802 }
24803 break;
24804
2e276125 24805 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
24806 if (!section_is_gnu)
24807 {
24808 unsigned int bytes_read;
2e276125 24809
ac298888
TT
24810 /* This reads the constant, but since we don't recognize
24811 any vendor extensions, we ignore it. */
24812 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
24813 mac_ptr += bytes_read;
24814 read_direct_string (abfd, mac_ptr, &bytes_read);
24815 mac_ptr += bytes_read;
2e276125 24816
cf2c3c16
TT
24817 /* We don't recognize any vendor extensions. */
24818 break;
24819 }
24820 /* FALLTHROUGH */
24821
24822 default:
24823 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 24824 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
24825 section);
24826 if (mac_ptr == NULL)
24827 return;
24828 break;
2e276125 24829 }
132448f8 24830 DIAGNOSTIC_POP
757a13d0 24831 } while (macinfo_type != 0);
2e276125 24832}
8e19ed76 24833
cf2c3c16 24834static void
09262596 24835dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 24836 int section_is_gnu)
cf2c3c16 24837{
518817b3
SM
24838 struct dwarf2_per_objfile *dwarf2_per_objfile
24839 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 24840 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
24841 struct line_header *lh = cu->line_header;
24842 bfd *abfd;
d521ce57 24843 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
24844 struct macro_source_file *current_file = 0;
24845 enum dwarf_macro_record_type macinfo_type;
24846 unsigned int offset_size = cu->header.offset_size;
d521ce57 24847 const gdb_byte *opcode_definitions[256];
8fc3fc34 24848 void **slot;
09262596
DE
24849 struct dwarf2_section_info *section;
24850 const char *section_name;
24851
24852 if (cu->dwo_unit != NULL)
24853 {
24854 if (section_is_gnu)
24855 {
24856 section = &cu->dwo_unit->dwo_file->sections.macro;
24857 section_name = ".debug_macro.dwo";
24858 }
24859 else
24860 {
24861 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24862 section_name = ".debug_macinfo.dwo";
24863 }
24864 }
24865 else
24866 {
24867 if (section_is_gnu)
24868 {
24869 section = &dwarf2_per_objfile->macro;
24870 section_name = ".debug_macro";
24871 }
24872 else
24873 {
24874 section = &dwarf2_per_objfile->macinfo;
24875 section_name = ".debug_macinfo";
24876 }
24877 }
cf2c3c16 24878
bb5ed363 24879 dwarf2_read_section (objfile, section);
cf2c3c16
TT
24880 if (section->buffer == NULL)
24881 {
b98664d3 24882 complaint (_("missing %s section"), section_name);
cf2c3c16
TT
24883 return;
24884 }
a32a8923 24885 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
24886
24887 /* First pass: Find the name of the base filename.
24888 This filename is needed in order to process all macros whose definition
24889 (or undefinition) comes from the command line. These macros are defined
24890 before the first DW_MACINFO_start_file entry, and yet still need to be
24891 associated to the base file.
24892
24893 To determine the base file name, we scan the macro definitions until we
24894 reach the first DW_MACINFO_start_file entry. We then initialize
24895 CURRENT_FILE accordingly so that any macro definition found before the
24896 first DW_MACINFO_start_file can still be associated to the base file. */
24897
24898 mac_ptr = section->buffer + offset;
24899 mac_end = section->buffer + section->size;
24900
24901 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24902 &offset_size, section_is_gnu);
24903 if (mac_ptr == NULL)
24904 {
24905 /* We already issued a complaint. */
24906 return;
24907 }
24908
24909 do
24910 {
24911 /* Do we at least have room for a macinfo type byte? */
24912 if (mac_ptr >= mac_end)
24913 {
24914 /* Complaint is printed during the second pass as GDB will probably
24915 stop the first pass earlier upon finding
24916 DW_MACINFO_start_file. */
24917 break;
24918 }
24919
aead7601 24920 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
24921 mac_ptr++;
24922
24923 /* Note that we rely on the fact that the corresponding GNU and
24924 DWARF constants are the same. */
132448f8
SM
24925 DIAGNOSTIC_PUSH
24926 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
cf2c3c16
TT
24927 switch (macinfo_type)
24928 {
24929 /* A zero macinfo type indicates the end of the macro
24930 information. */
24931 case 0:
24932 break;
24933
0af92d60
JK
24934 case DW_MACRO_define:
24935 case DW_MACRO_undef:
cf2c3c16
TT
24936 /* Only skip the data by MAC_PTR. */
24937 {
24938 unsigned int bytes_read;
24939
24940 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24941 mac_ptr += bytes_read;
24942 read_direct_string (abfd, mac_ptr, &bytes_read);
24943 mac_ptr += bytes_read;
24944 }
24945 break;
24946
0af92d60 24947 case DW_MACRO_start_file:
cf2c3c16
TT
24948 {
24949 unsigned int bytes_read;
24950 int line, file;
24951
24952 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24953 mac_ptr += bytes_read;
24954 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24955 mac_ptr += bytes_read;
24956
804d2729 24957 current_file = macro_start_file (cu, file, line, current_file, lh);
cf2c3c16
TT
24958 }
24959 break;
24960
0af92d60 24961 case DW_MACRO_end_file:
cf2c3c16
TT
24962 /* No data to skip by MAC_PTR. */
24963 break;
24964
0af92d60
JK
24965 case DW_MACRO_define_strp:
24966 case DW_MACRO_undef_strp:
24967 case DW_MACRO_define_sup:
24968 case DW_MACRO_undef_sup:
cf2c3c16
TT
24969 {
24970 unsigned int bytes_read;
24971
24972 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24973 mac_ptr += bytes_read;
24974 mac_ptr += offset_size;
24975 }
24976 break;
24977
0af92d60
JK
24978 case DW_MACRO_import:
24979 case DW_MACRO_import_sup:
cf2c3c16 24980 /* Note that, according to the spec, a transparent include
0af92d60 24981 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
24982 skip this opcode. */
24983 mac_ptr += offset_size;
24984 break;
24985
24986 case DW_MACINFO_vendor_ext:
24987 /* Only skip the data by MAC_PTR. */
24988 if (!section_is_gnu)
24989 {
24990 unsigned int bytes_read;
24991
24992 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24993 mac_ptr += bytes_read;
24994 read_direct_string (abfd, mac_ptr, &bytes_read);
24995 mac_ptr += bytes_read;
24996 }
24997 /* FALLTHROUGH */
24998
24999 default:
25000 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 25001 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
25002 section);
25003 if (mac_ptr == NULL)
25004 return;
25005 break;
25006 }
132448f8 25007 DIAGNOSTIC_POP
cf2c3c16
TT
25008 } while (macinfo_type != 0 && current_file == NULL);
25009
25010 /* Second pass: Process all entries.
25011
25012 Use the AT_COMMAND_LINE flag to determine whether we are still processing
25013 command-line macro definitions/undefinitions. This flag is unset when we
25014 reach the first DW_MACINFO_start_file entry. */
25015
fc4007c9
TT
25016 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
25017 htab_eq_pointer,
25018 NULL, xcalloc, xfree));
8fc3fc34 25019 mac_ptr = section->buffer + offset;
fc4007c9 25020 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 25021 *slot = (void *) mac_ptr;
804d2729 25022 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
43f3e411 25023 current_file, lh, section,
fc4007c9
TT
25024 section_is_gnu, 0, offset_size,
25025 include_hash.get ());
cf2c3c16
TT
25026}
25027
8e19ed76 25028/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 25029 if so return true else false. */
380bca97 25030
8e19ed76 25031static int
6e5a29e1 25032attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
25033{
25034 return (attr == NULL ? 0 :
25035 attr->form == DW_FORM_block1
25036 || attr->form == DW_FORM_block2
25037 || attr->form == DW_FORM_block4
2dc7f7b3
TT
25038 || attr->form == DW_FORM_block
25039 || attr->form == DW_FORM_exprloc);
8e19ed76 25040}
4c2df51b 25041
c6a0999f
JB
25042/* Return non-zero if ATTR's value is a section offset --- classes
25043 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
25044 You may use DW_UNSND (attr) to retrieve such offsets.
25045
25046 Section 7.5.4, "Attribute Encodings", explains that no attribute
25047 may have a value that belongs to more than one of these classes; it
25048 would be ambiguous if we did, because we use the same forms for all
25049 of them. */
380bca97 25050
3690dd37 25051static int
6e5a29e1 25052attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
25053{
25054 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
25055 || attr->form == DW_FORM_data8
25056 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
25057}
25058
3690dd37
JB
25059/* Return non-zero if ATTR's value falls in the 'constant' class, or
25060 zero otherwise. When this function returns true, you can apply
25061 dwarf2_get_attr_constant_value to it.
25062
25063 However, note that for some attributes you must check
25064 attr_form_is_section_offset before using this test. DW_FORM_data4
25065 and DW_FORM_data8 are members of both the constant class, and of
25066 the classes that contain offsets into other debug sections
25067 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
25068 that, if an attribute's can be either a constant or one of the
25069 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
0224619f
JK
25070 taken as section offsets, not constants.
25071
25072 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
25073 cannot handle that. */
380bca97 25074
3690dd37 25075static int
6e5a29e1 25076attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
25077{
25078 switch (attr->form)
25079 {
25080 case DW_FORM_sdata:
25081 case DW_FORM_udata:
25082 case DW_FORM_data1:
25083 case DW_FORM_data2:
25084 case DW_FORM_data4:
25085 case DW_FORM_data8:
663c44ac 25086 case DW_FORM_implicit_const:
3690dd37
JB
25087 return 1;
25088 default:
25089 return 0;
25090 }
25091}
25092
7771576e
SA
25093
25094/* DW_ADDR is always stored already as sect_offset; despite for the forms
25095 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
25096
25097static int
6e5a29e1 25098attr_form_is_ref (const struct attribute *attr)
7771576e
SA
25099{
25100 switch (attr->form)
25101 {
25102 case DW_FORM_ref_addr:
25103 case DW_FORM_ref1:
25104 case DW_FORM_ref2:
25105 case DW_FORM_ref4:
25106 case DW_FORM_ref8:
25107 case DW_FORM_ref_udata:
25108 case DW_FORM_GNU_ref_alt:
25109 return 1;
25110 default:
25111 return 0;
25112 }
25113}
25114
3019eac3
DE
25115/* Return the .debug_loc section to use for CU.
25116 For DWO files use .debug_loc.dwo. */
25117
25118static struct dwarf2_section_info *
25119cu_debug_loc_section (struct dwarf2_cu *cu)
25120{
518817b3
SM
25121 struct dwarf2_per_objfile *dwarf2_per_objfile
25122 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 25123
3019eac3 25124 if (cu->dwo_unit)
43988095
JK
25125 {
25126 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
25127
25128 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
25129 }
25130 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
25131 : &dwarf2_per_objfile->loc);
3019eac3
DE
25132}
25133
8cf6f0b1
TT
25134/* A helper function that fills in a dwarf2_loclist_baton. */
25135
25136static void
25137fill_in_loclist_baton (struct dwarf2_cu *cu,
25138 struct dwarf2_loclist_baton *baton,
ff39bb5e 25139 const struct attribute *attr)
8cf6f0b1 25140{
518817b3
SM
25141 struct dwarf2_per_objfile *dwarf2_per_objfile
25142 = cu->per_cu->dwarf2_per_objfile;
3019eac3
DE
25143 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
25144
25145 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
25146
25147 baton->per_cu = cu->per_cu;
25148 gdb_assert (baton->per_cu);
25149 /* We don't know how long the location list is, but make sure we
25150 don't run off the edge of the section. */
3019eac3
DE
25151 baton->size = section->size - DW_UNSND (attr);
25152 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 25153 baton->base_address = cu->base_address;
f664829e 25154 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
25155}
25156
4c2df51b 25157static void
ff39bb5e 25158dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 25159 struct dwarf2_cu *cu, int is_block)
4c2df51b 25160{
518817b3
SM
25161 struct dwarf2_per_objfile *dwarf2_per_objfile
25162 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 25163 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 25164 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 25165
3690dd37 25166 if (attr_form_is_section_offset (attr)
3019eac3 25167 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
25168 the section. If so, fall through to the complaint in the
25169 other branch. */
3019eac3 25170 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 25171 {
0d53c4c4 25172 struct dwarf2_loclist_baton *baton;
4c2df51b 25173
8d749320 25174 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 25175
8cf6f0b1 25176 fill_in_loclist_baton (cu, baton, attr);
be391dca 25177
d00adf39 25178 if (cu->base_known == 0)
b98664d3 25179 complaint (_("Location list used without "
3e43a32a 25180 "specifying the CU base address."));
4c2df51b 25181
f1e6e072
TT
25182 SYMBOL_ACLASS_INDEX (sym) = (is_block
25183 ? dwarf2_loclist_block_index
25184 : dwarf2_loclist_index);
0d53c4c4
DJ
25185 SYMBOL_LOCATION_BATON (sym) = baton;
25186 }
25187 else
25188 {
25189 struct dwarf2_locexpr_baton *baton;
25190
8d749320 25191 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
25192 baton->per_cu = cu->per_cu;
25193 gdb_assert (baton->per_cu);
0d53c4c4
DJ
25194
25195 if (attr_form_is_block (attr))
25196 {
25197 /* Note that we're just copying the block's data pointer
25198 here, not the actual data. We're still pointing into the
6502dd73
DJ
25199 info_buffer for SYM's objfile; right now we never release
25200 that buffer, but when we do clean up properly this may
25201 need to change. */
0d53c4c4
DJ
25202 baton->size = DW_BLOCK (attr)->size;
25203 baton->data = DW_BLOCK (attr)->data;
25204 }
25205 else
25206 {
25207 dwarf2_invalid_attrib_class_complaint ("location description",
25208 SYMBOL_NATURAL_NAME (sym));
25209 baton->size = 0;
0d53c4c4 25210 }
6e70227d 25211
f1e6e072
TT
25212 SYMBOL_ACLASS_INDEX (sym) = (is_block
25213 ? dwarf2_locexpr_block_index
25214 : dwarf2_locexpr_index);
0d53c4c4
DJ
25215 SYMBOL_LOCATION_BATON (sym) = baton;
25216 }
4c2df51b 25217}
6502dd73 25218
9aa1f1e3
TT
25219/* Return the OBJFILE associated with the compilation unit CU. If CU
25220 came from a separate debuginfo file, then the master objfile is
25221 returned. */
ae0d2f24
UW
25222
25223struct objfile *
25224dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
25225{
e3b94546 25226 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
ae0d2f24
UW
25227
25228 /* Return the master objfile, so that we can report and look up the
25229 correct file containing this variable. */
25230 if (objfile->separate_debug_objfile_backlink)
25231 objfile = objfile->separate_debug_objfile_backlink;
25232
25233 return objfile;
25234}
25235
96408a79
SA
25236/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
25237 (CU_HEADERP is unused in such case) or prepare a temporary copy at
25238 CU_HEADERP first. */
25239
25240static const struct comp_unit_head *
25241per_cu_header_read_in (struct comp_unit_head *cu_headerp,
25242 struct dwarf2_per_cu_data *per_cu)
25243{
d521ce57 25244 const gdb_byte *info_ptr;
96408a79
SA
25245
25246 if (per_cu->cu)
25247 return &per_cu->cu->header;
25248
9c541725 25249 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
25250
25251 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
25252 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
25253 rcuh_kind::COMPILE);
96408a79
SA
25254
25255 return cu_headerp;
25256}
25257
ae0d2f24
UW
25258/* Return the address size given in the compilation unit header for CU. */
25259
98714339 25260int
ae0d2f24
UW
25261dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
25262{
96408a79
SA
25263 struct comp_unit_head cu_header_local;
25264 const struct comp_unit_head *cu_headerp;
c471e790 25265
96408a79
SA
25266 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25267
25268 return cu_headerp->addr_size;
ae0d2f24
UW
25269}
25270
9eae7c52
TT
25271/* Return the offset size given in the compilation unit header for CU. */
25272
25273int
25274dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
25275{
96408a79
SA
25276 struct comp_unit_head cu_header_local;
25277 const struct comp_unit_head *cu_headerp;
9c6c53f7 25278
96408a79
SA
25279 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25280
25281 return cu_headerp->offset_size;
25282}
25283
25284/* See its dwarf2loc.h declaration. */
25285
25286int
25287dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
25288{
25289 struct comp_unit_head cu_header_local;
25290 const struct comp_unit_head *cu_headerp;
25291
25292 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
25293
25294 if (cu_headerp->version == 2)
25295 return cu_headerp->addr_size;
25296 else
25297 return cu_headerp->offset_size;
181cebd4
JK
25298}
25299
9aa1f1e3
TT
25300/* Return the text offset of the CU. The returned offset comes from
25301 this CU's objfile. If this objfile came from a separate debuginfo
25302 file, then the offset may be different from the corresponding
25303 offset in the parent objfile. */
25304
25305CORE_ADDR
25306dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
25307{
e3b94546 25308 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
9aa1f1e3
TT
25309
25310 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
25311}
25312
9a49df9d
AB
25313/* Return a type that is a generic pointer type, the size of which matches
25314 the address size given in the compilation unit header for PER_CU. */
25315static struct type *
25316dwarf2_per_cu_addr_type (struct dwarf2_per_cu_data *per_cu)
25317{
25318 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
25319 struct type *void_type = objfile_type (objfile)->builtin_void;
25320 struct type *addr_type = lookup_pointer_type (void_type);
25321 int addr_size = dwarf2_per_cu_addr_size (per_cu);
25322
25323 if (TYPE_LENGTH (addr_type) == addr_size)
25324 return addr_type;
25325
25326 addr_type
25327 = dwarf2_per_cu_addr_sized_int_type (per_cu, TYPE_UNSIGNED (addr_type));
25328 return addr_type;
25329}
25330
43988095
JK
25331/* Return DWARF version number of PER_CU. */
25332
25333short
25334dwarf2_version (struct dwarf2_per_cu_data *per_cu)
25335{
25336 return per_cu->dwarf_version;
25337}
25338
348e048f
DE
25339/* Locate the .debug_info compilation unit from CU's objfile which contains
25340 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
25341
25342static struct dwarf2_per_cu_data *
9c541725 25343dwarf2_find_containing_comp_unit (sect_offset sect_off,
36586728 25344 unsigned int offset_in_dwz,
ed2dc618 25345 struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
25346{
25347 struct dwarf2_per_cu_data *this_cu;
25348 int low, high;
25349
ae038cb0 25350 low = 0;
b76e467d 25351 high = dwarf2_per_objfile->all_comp_units.size () - 1;
ae038cb0
DJ
25352 while (high > low)
25353 {
36586728 25354 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 25355 int mid = low + (high - low) / 2;
9a619af0 25356
36586728 25357 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
36586728 25358 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 25359 || (mid_cu->is_dwz == offset_in_dwz
45b8ae0c 25360 && mid_cu->sect_off + mid_cu->length >= sect_off))
ae038cb0
DJ
25361 high = mid;
25362 else
25363 low = mid + 1;
25364 }
25365 gdb_assert (low == high);
36586728 25366 this_cu = dwarf2_per_objfile->all_comp_units[low];
45b8ae0c 25367 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 25368 {
36586728 25369 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 25370 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
25371 "offset %s [in module %s]"),
25372 sect_offset_str (sect_off),
ed2dc618 25373 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 25374
9c541725
PA
25375 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
25376 <= sect_off);
ae038cb0
DJ
25377 return dwarf2_per_objfile->all_comp_units[low-1];
25378 }
25379 else
25380 {
b76e467d 25381 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
9c541725 25382 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 25383 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 25384 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
25385 return this_cu;
25386 }
25387}
25388
23745b47 25389/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 25390
fcd3b13d
SM
25391dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
25392 : per_cu (per_cu_),
9068261f
AB
25393 mark (false),
25394 has_loclist (false),
25395 checked_producer (false),
25396 producer_is_gxx_lt_4_6 (false),
25397 producer_is_gcc_lt_4_3 (false),
eb77c9df 25398 producer_is_icc (false),
9068261f 25399 producer_is_icc_lt_14 (false),
c258c396 25400 producer_is_codewarrior (false),
9068261f 25401 processing_has_namespace_info (false)
93311388 25402{
fcd3b13d
SM
25403 per_cu->cu = this;
25404}
25405
25406/* Destroy a dwarf2_cu. */
25407
25408dwarf2_cu::~dwarf2_cu ()
25409{
25410 per_cu->cu = NULL;
9816fde3
JK
25411}
25412
25413/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25414
25415static void
95554aad
TT
25416prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25417 enum language pretend_language)
9816fde3
JK
25418{
25419 struct attribute *attr;
25420
25421 /* Set the language we're debugging. */
25422 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25423 if (attr)
25424 set_cu_language (DW_UNSND (attr), cu);
25425 else
9cded63f 25426 {
95554aad 25427 cu->language = pretend_language;
9cded63f
TT
25428 cu->language_defn = language_def (cu->language);
25429 }
dee91e82 25430
7d45c7c3 25431 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
25432}
25433
ae038cb0
DJ
25434/* Increase the age counter on each cached compilation unit, and free
25435 any that are too old. */
25436
25437static void
ed2dc618 25438age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
25439{
25440 struct dwarf2_per_cu_data *per_cu, **last_chain;
25441
25442 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25443 per_cu = dwarf2_per_objfile->read_in_chain;
25444 while (per_cu != NULL)
25445 {
25446 per_cu->cu->last_used ++;
b4f54984 25447 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
25448 dwarf2_mark (per_cu->cu);
25449 per_cu = per_cu->cu->read_in_chain;
25450 }
25451
25452 per_cu = dwarf2_per_objfile->read_in_chain;
25453 last_chain = &dwarf2_per_objfile->read_in_chain;
25454 while (per_cu != NULL)
25455 {
25456 struct dwarf2_per_cu_data *next_cu;
25457
25458 next_cu = per_cu->cu->read_in_chain;
25459
25460 if (!per_cu->cu->mark)
25461 {
fcd3b13d 25462 delete per_cu->cu;
ae038cb0
DJ
25463 *last_chain = next_cu;
25464 }
25465 else
25466 last_chain = &per_cu->cu->read_in_chain;
25467
25468 per_cu = next_cu;
25469 }
25470}
25471
25472/* Remove a single compilation unit from the cache. */
25473
25474static void
dee91e82 25475free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
25476{
25477 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
25478 struct dwarf2_per_objfile *dwarf2_per_objfile
25479 = target_per_cu->dwarf2_per_objfile;
ae038cb0
DJ
25480
25481 per_cu = dwarf2_per_objfile->read_in_chain;
25482 last_chain = &dwarf2_per_objfile->read_in_chain;
25483 while (per_cu != NULL)
25484 {
25485 struct dwarf2_per_cu_data *next_cu;
25486
25487 next_cu = per_cu->cu->read_in_chain;
25488
dee91e82 25489 if (per_cu == target_per_cu)
ae038cb0 25490 {
fcd3b13d 25491 delete per_cu->cu;
dee91e82 25492 per_cu->cu = NULL;
ae038cb0
DJ
25493 *last_chain = next_cu;
25494 break;
25495 }
25496 else
25497 last_chain = &per_cu->cu->read_in_chain;
25498
25499 per_cu = next_cu;
25500 }
25501}
25502
dee91e82
DE
25503/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25504 We store these in a hash table separate from the DIEs, and preserve them
25505 when the DIEs are flushed out of cache.
25506
25507 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 25508 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
25509 or the type may come from a DWO file. Furthermore, while it's more logical
25510 to use per_cu->section+offset, with Fission the section with the data is in
25511 the DWO file but we don't know that section at the point we need it.
25512 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25513 because we can enter the lookup routine, get_die_type_at_offset, from
25514 outside this file, and thus won't necessarily have PER_CU->cu.
25515 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 25516
dee91e82 25517struct dwarf2_per_cu_offset_and_type
1c379e20 25518{
dee91e82 25519 const struct dwarf2_per_cu_data *per_cu;
9c541725 25520 sect_offset sect_off;
1c379e20
DJ
25521 struct type *type;
25522};
25523
dee91e82 25524/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25525
25526static hashval_t
dee91e82 25527per_cu_offset_and_type_hash (const void *item)
1c379e20 25528{
9a3c8263
SM
25529 const struct dwarf2_per_cu_offset_and_type *ofs
25530 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 25531
9c541725 25532 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
25533}
25534
dee91e82 25535/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25536
25537static int
dee91e82 25538per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 25539{
9a3c8263
SM
25540 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25541 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25542 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25543 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 25544
dee91e82 25545 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 25546 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
25547}
25548
25549/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
25550 table if necessary. For convenience, return TYPE.
25551
25552 The DIEs reading must have careful ordering to:
25553 * Not cause infite loops trying to read in DIEs as a prerequisite for
25554 reading current DIE.
25555 * Not trying to dereference contents of still incompletely read in types
25556 while reading in other DIEs.
25557 * Enable referencing still incompletely read in types just by a pointer to
25558 the type without accessing its fields.
25559
25560 Therefore caller should follow these rules:
25561 * Try to fetch any prerequisite types we may need to build this DIE type
25562 before building the type and calling set_die_type.
e71ec853 25563 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
25564 possible before fetching more types to complete the current type.
25565 * Make the type as complete as possible before fetching more types. */
1c379e20 25566
f792889a 25567static struct type *
1c379e20
DJ
25568set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25569{
518817b3
SM
25570 struct dwarf2_per_objfile *dwarf2_per_objfile
25571 = cu->per_cu->dwarf2_per_objfile;
dee91e82 25572 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 25573 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
25574 struct attribute *attr;
25575 struct dynamic_prop prop;
1c379e20 25576
b4ba55a1
JB
25577 /* For Ada types, make sure that the gnat-specific data is always
25578 initialized (if not already set). There are a few types where
25579 we should not be doing so, because the type-specific area is
25580 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25581 where the type-specific area is used to store the floatformat).
25582 But this is not a problem, because the gnat-specific information
25583 is actually not needed for these types. */
25584 if (need_gnat_info (cu)
25585 && TYPE_CODE (type) != TYPE_CODE_FUNC
25586 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
25587 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25588 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25589 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
25590 && !HAVE_GNAT_AUX_INFO (type))
25591 INIT_GNAT_SPECIFIC (type);
25592
3f2f83dd
KB
25593 /* Read DW_AT_allocated and set in type. */
25594 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25595 if (attr_form_is_block (attr))
25596 {
9a49df9d
AB
25597 struct type *prop_type
25598 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25599 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 25600 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
3f2f83dd
KB
25601 }
25602 else if (attr != NULL)
25603 {
b98664d3 25604 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 25605 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25606 sect_offset_str (die->sect_off));
3f2f83dd
KB
25607 }
25608
25609 /* Read DW_AT_associated and set in type. */
25610 attr = dwarf2_attr (die, DW_AT_associated, cu);
25611 if (attr_form_is_block (attr))
25612 {
9a49df9d
AB
25613 struct type *prop_type
25614 = dwarf2_per_cu_addr_sized_int_type (cu->per_cu, false);
25615 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 25616 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
3f2f83dd
KB
25617 }
25618 else if (attr != NULL)
25619 {
b98664d3 25620 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 25621 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25622 sect_offset_str (die->sect_off));
3f2f83dd
KB
25623 }
25624
3cdcd0ce
JB
25625 /* Read DW_AT_data_location and set in type. */
25626 attr = dwarf2_attr (die, DW_AT_data_location, cu);
9a49df9d
AB
25627 if (attr_to_dynamic_prop (attr, die, cu, &prop,
25628 dwarf2_per_cu_addr_type (cu->per_cu)))
50a82047 25629 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
3cdcd0ce 25630
dee91e82 25631 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25632 {
dee91e82
DE
25633 dwarf2_per_objfile->die_type_hash =
25634 htab_create_alloc_ex (127,
25635 per_cu_offset_and_type_hash,
25636 per_cu_offset_and_type_eq,
25637 NULL,
25638 &objfile->objfile_obstack,
25639 hashtab_obstack_allocate,
25640 dummy_obstack_deallocate);
f792889a 25641 }
1c379e20 25642
dee91e82 25643 ofs.per_cu = cu->per_cu;
9c541725 25644 ofs.sect_off = die->sect_off;
1c379e20 25645 ofs.type = type;
dee91e82
DE
25646 slot = (struct dwarf2_per_cu_offset_and_type **)
25647 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57 25648 if (*slot)
b98664d3 25649 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 25650 sect_offset_str (die->sect_off));
8d749320
SM
25651 *slot = XOBNEW (&objfile->objfile_obstack,
25652 struct dwarf2_per_cu_offset_and_type);
1c379e20 25653 **slot = ofs;
f792889a 25654 return type;
1c379e20
DJ
25655}
25656
9c541725 25657/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 25658 or return NULL if the die does not have a saved type. */
1c379e20
DJ
25659
25660static struct type *
9c541725 25661get_die_type_at_offset (sect_offset sect_off,
673bfd45 25662 struct dwarf2_per_cu_data *per_cu)
1c379e20 25663{
dee91e82 25664 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 25665 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 25666
dee91e82 25667 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25668 return NULL;
1c379e20 25669
dee91e82 25670 ofs.per_cu = per_cu;
9c541725 25671 ofs.sect_off = sect_off;
9a3c8263
SM
25672 slot = ((struct dwarf2_per_cu_offset_and_type *)
25673 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
25674 if (slot)
25675 return slot->type;
25676 else
25677 return NULL;
25678}
25679
02142a6c 25680/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
25681 or return NULL if DIE does not have a saved type. */
25682
25683static struct type *
25684get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25685{
9c541725 25686 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
25687}
25688
10b3939b
DJ
25689/* Add a dependence relationship from CU to REF_PER_CU. */
25690
25691static void
25692dwarf2_add_dependence (struct dwarf2_cu *cu,
25693 struct dwarf2_per_cu_data *ref_per_cu)
25694{
25695 void **slot;
25696
25697 if (cu->dependencies == NULL)
25698 cu->dependencies
25699 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25700 NULL, &cu->comp_unit_obstack,
25701 hashtab_obstack_allocate,
25702 dummy_obstack_deallocate);
25703
25704 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25705 if (*slot == NULL)
25706 *slot = ref_per_cu;
25707}
1c379e20 25708
f504f079
DE
25709/* Subroutine of dwarf2_mark to pass to htab_traverse.
25710 Set the mark field in every compilation unit in the
ae038cb0
DJ
25711 cache that we must keep because we are keeping CU. */
25712
10b3939b
DJ
25713static int
25714dwarf2_mark_helper (void **slot, void *data)
25715{
25716 struct dwarf2_per_cu_data *per_cu;
25717
25718 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
25719
25720 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25721 reading of the chain. As such dependencies remain valid it is not much
25722 useful to track and undo them during QUIT cleanups. */
25723 if (per_cu->cu == NULL)
25724 return 1;
25725
10b3939b
DJ
25726 if (per_cu->cu->mark)
25727 return 1;
9068261f 25728 per_cu->cu->mark = true;
10b3939b
DJ
25729
25730 if (per_cu->cu->dependencies != NULL)
25731 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25732
25733 return 1;
25734}
25735
f504f079
DE
25736/* Set the mark field in CU and in every other compilation unit in the
25737 cache that we must keep because we are keeping CU. */
25738
ae038cb0
DJ
25739static void
25740dwarf2_mark (struct dwarf2_cu *cu)
25741{
25742 if (cu->mark)
25743 return;
9068261f 25744 cu->mark = true;
10b3939b
DJ
25745 if (cu->dependencies != NULL)
25746 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
25747}
25748
25749static void
25750dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25751{
25752 while (per_cu)
25753 {
9068261f 25754 per_cu->cu->mark = false;
ae038cb0
DJ
25755 per_cu = per_cu->cu->read_in_chain;
25756 }
72bf9492
DJ
25757}
25758
72bf9492
DJ
25759/* Trivial hash function for partial_die_info: the hash value of a DIE
25760 is its offset in .debug_info for this objfile. */
25761
25762static hashval_t
25763partial_die_hash (const void *item)
25764{
9a3c8263
SM
25765 const struct partial_die_info *part_die
25766 = (const struct partial_die_info *) item;
9a619af0 25767
9c541725 25768 return to_underlying (part_die->sect_off);
72bf9492
DJ
25769}
25770
25771/* Trivial comparison function for partial_die_info structures: two DIEs
25772 are equal if they have the same offset. */
25773
25774static int
25775partial_die_eq (const void *item_lhs, const void *item_rhs)
25776{
9a3c8263
SM
25777 const struct partial_die_info *part_die_lhs
25778 = (const struct partial_die_info *) item_lhs;
25779 const struct partial_die_info *part_die_rhs
25780 = (const struct partial_die_info *) item_rhs;
9a619af0 25781
9c541725 25782 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
25783}
25784
3c3bb058
AB
25785struct cmd_list_element *set_dwarf_cmdlist;
25786struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
25787
25788static void
981a3fb3 25789set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 25790{
b4f54984 25791 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 25792 gdb_stdout);
ae038cb0
DJ
25793}
25794
25795static void
981a3fb3 25796show_dwarf_cmd (const char *args, int from_tty)
6e70227d 25797{
b4f54984 25798 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
25799}
25800
cd4fb1b2 25801int dwarf_always_disassemble;
437afbb8 25802
437afbb8 25803static void
cd4fb1b2
SM
25804show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
25805 struct cmd_list_element *c, const char *value)
9291a0cd 25806{
cd4fb1b2
SM
25807 fprintf_filtered (file,
25808 _("Whether to always disassemble "
25809 "DWARF expressions is %s.\n"),
25810 value);
9291a0cd
TT
25811}
25812
9291a0cd 25813static void
cd4fb1b2
SM
25814show_check_physname (struct ui_file *file, int from_tty,
25815 struct cmd_list_element *c, const char *value)
9291a0cd 25816{
cd4fb1b2
SM
25817 fprintf_filtered (file,
25818 _("Whether to check \"physname\" is %s.\n"),
25819 value);
9291a0cd
TT
25820}
25821
cd4fb1b2
SM
25822void
25823_initialize_dwarf2_read (void)
9291a0cd 25824{
cd4fb1b2
SM
25825 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
25826Set DWARF specific variables.\n\
590042fc 25827Configure DWARF variables such as the cache size."),
cd4fb1b2
SM
25828 &set_dwarf_cmdlist, "maintenance set dwarf ",
25829 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 25830
cd4fb1b2 25831 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
590042fc
PW
25832Show DWARF specific variables.\n\
25833Show DWARF variables such as the cache size."),
cd4fb1b2
SM
25834 &show_dwarf_cmdlist, "maintenance show dwarf ",
25835 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 25836
cd4fb1b2
SM
25837 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25838 &dwarf_max_cache_age, _("\
25839Set the upper bound on the age of cached DWARF compilation units."), _("\
25840Show the upper bound on the age of cached DWARF compilation units."), _("\
25841A higher limit means that cached compilation units will be stored\n\
25842in memory longer, and more total memory will be used. Zero disables\n\
25843caching, which can slow down startup."),
25844 NULL,
25845 show_dwarf_max_cache_age,
25846 &set_dwarf_cmdlist,
25847 &show_dwarf_cmdlist);
156942c7 25848
cd4fb1b2
SM
25849 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
25850 &dwarf_always_disassemble, _("\
25851Set whether `info address' always disassembles DWARF expressions."), _("\
25852Show whether `info address' always disassembles DWARF expressions."), _("\
25853When enabled, DWARF expressions are always printed in an assembly-like\n\
25854syntax. When disabled, expressions will be printed in a more\n\
25855conversational style, when possible."),
25856 NULL,
25857 show_dwarf_always_disassemble,
25858 &set_dwarf_cmdlist,
25859 &show_dwarf_cmdlist);
9291a0cd 25860
cd4fb1b2
SM
25861 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25862Set debugging of the DWARF reader."), _("\
25863Show debugging of the DWARF reader."), _("\
25864When enabled (non-zero), debugging messages are printed during DWARF\n\
25865reading and symtab expansion. A value of 1 (one) provides basic\n\
25866information. A value greater than 1 provides more verbose information."),
25867 NULL,
25868 NULL,
25869 &setdebuglist, &showdebuglist);
9291a0cd 25870
cd4fb1b2
SM
25871 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25872Set debugging of the DWARF DIE reader."), _("\
25873Show debugging of the DWARF DIE reader."), _("\
25874When enabled (non-zero), DIEs are dumped after they are read in.\n\
25875The value is the maximum depth to print."),
25876 NULL,
25877 NULL,
25878 &setdebuglist, &showdebuglist);
9291a0cd 25879
cd4fb1b2
SM
25880 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25881Set debugging of the dwarf line reader."), _("\
25882Show debugging of the dwarf line reader."), _("\
25883When enabled (non-zero), line number entries are dumped as they are read in.\n\
25884A value of 1 (one) provides basic information.\n\
25885A value greater than 1 provides more verbose information."),
25886 NULL,
25887 NULL,
25888 &setdebuglist, &showdebuglist);
437afbb8 25889
cd4fb1b2
SM
25890 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25891Set cross-checking of \"physname\" code against demangler."), _("\
25892Show cross-checking of \"physname\" code against demangler."), _("\
25893When enabled, GDB's internal \"physname\" code is checked against\n\
25894the demangler."),
25895 NULL, show_check_physname,
25896 &setdebuglist, &showdebuglist);
900e11f9 25897
e615022a
DE
25898 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25899 no_class, &use_deprecated_index_sections, _("\
25900Set whether to use deprecated gdb_index sections."), _("\
25901Show whether to use deprecated gdb_index sections."), _("\
25902When enabled, deprecated .gdb_index sections are used anyway.\n\
25903Normally they are ignored either because of a missing feature or\n\
25904performance issue.\n\
25905Warning: This option must be enabled before gdb reads the file."),
25906 NULL,
25907 NULL,
25908 &setlist, &showlist);
25909
f1e6e072
TT
25910 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25911 &dwarf2_locexpr_funcs);
25912 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25913 &dwarf2_loclist_funcs);
25914
25915 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25916 &dwarf2_block_frame_base_locexpr_funcs);
25917 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25918 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
25919
25920#if GDB_SELF_TEST
25921 selftests::register_test ("dw2_expand_symtabs_matching",
25922 selftests::dw2_expand_symtabs_matching::run_test);
25923#endif
6502dd73 25924}
This page took 5.097989 seconds and 4 git commands to generate.