[gdb/testsuite] Fix regexp in py-rbreak.exp
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
e2882c85 3 Copyright (C) 1994-2018 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"
cd4fb1b2
SM
32#include "dwarf2read.h"
33#include "dwarf-index-common.h"
c906108c 34#include "bfd.h"
80626a55 35#include "elf-bfd.h"
c906108c
SS
36#include "symtab.h"
37#include "gdbtypes.h"
c906108c 38#include "objfiles.h"
fa8f86ff 39#include "dwarf2.h"
804d2729 40#include "buildsym.h"
c906108c 41#include "demangle.h"
50f182aa 42#include "gdb-demangle.h"
c906108c 43#include "expression.h"
d5166ae1 44#include "filenames.h" /* for DOSish file names */
2e276125 45#include "macrotab.h"
c906108c
SS
46#include "language.h"
47#include "complaints.h"
357e46e7 48#include "bcache.h"
4c2df51b
DJ
49#include "dwarf2expr.h"
50#include "dwarf2loc.h"
9219021c 51#include "cp-support.h"
72bf9492 52#include "hashtab.h"
ae038cb0
DJ
53#include "command.h"
54#include "gdbcmd.h"
edb3359d 55#include "block.h"
ff013f42 56#include "addrmap.h"
94af9270 57#include "typeprint.h"
ccefe4c4 58#include "psympriv.h"
53ce3c39 59#include <sys/stat.h>
96d19272 60#include "completer.h"
34eaf542 61#include "vec.h"
98bfdba5 62#include "c-lang.h"
a766d390 63#include "go-lang.h"
98bfdba5 64#include "valprint.h"
3019eac3 65#include "gdbcore.h" /* for gnutarget */
156942c7 66#include "gdb/gdb-index.h"
60d5a603 67#include <ctype.h>
cbb099e8 68#include "gdb_bfd.h"
4357ac6c 69#include "f-lang.h"
05cba821 70#include "source.h"
614c279d 71#include "filestuff.h"
dc294be5 72#include "build-id.h"
22cee43f 73#include "namespace.h"
bef155c3 74#include "common/gdb_unlinker.h"
14bc53a8 75#include "common/function-view.h"
ecfb656c
PA
76#include "common/gdb_optional.h"
77#include "common/underlying.h"
d5722aa2 78#include "common/byte-vector.h"
927aa2e7 79#include "common/hash_enum.h"
bbf2f4df 80#include "filename-seen-cache.h"
b32b108a 81#include "producer.h"
c906108c 82#include <fcntl.h>
c906108c 83#include <sys/types.h>
325fac50 84#include <algorithm>
bc8f2430
JK
85#include <unordered_set>
86#include <unordered_map>
c62446b1 87#include "selftest.h"
437afbb8
JK
88#include <cmath>
89#include <set>
90#include <forward_list>
c9317f21 91#include "rust-lang.h"
b4987c95 92#include "common/pathstuff.h"
437afbb8 93
73be47f5
DE
94/* When == 1, print basic high level tracing messages.
95 When > 1, be more verbose.
b4f54984
DE
96 This is in contrast to the low level DIE reading of dwarf_die_debug. */
97static unsigned int dwarf_read_debug = 0;
45cfd468 98
d97bc12b 99/* When non-zero, dump DIEs after they are read in. */
b4f54984 100static unsigned int dwarf_die_debug = 0;
d97bc12b 101
27e0867f
DE
102/* When non-zero, dump line number entries as they are read in. */
103static unsigned int dwarf_line_debug = 0;
104
900e11f9
JK
105/* When non-zero, cross-check physname against demangler. */
106static int check_physname = 0;
107
481860b3 108/* When non-zero, do not reject deprecated .gdb_index sections. */
e615022a 109static int use_deprecated_index_sections = 0;
481860b3 110
6502dd73
DJ
111static const struct objfile_data *dwarf2_objfile_data_key;
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];
215 return bucket.name == 0 && bucket.vec;
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{
284 return ((struct dwarf2_per_objfile *)
285 objfile_data (objfile, dwarf2_objfile_data_key));
286}
287
288/* Set the dwarf2_per_objfile associated to OBJFILE. */
289
290void
291set_dwarf2_per_objfile (struct objfile *objfile,
292 struct dwarf2_per_objfile *dwarf2_per_objfile)
293{
294 gdb_assert (get_dwarf2_per_objfile (objfile) == NULL);
295 set_objfile_data (objfile, dwarf2_objfile_data_key, dwarf2_per_objfile);
296}
c906108c 297
251d32d9 298/* Default names of the debugging sections. */
c906108c 299
233a11ab
CS
300/* Note that if the debugging section has been compressed, it might
301 have a name like .zdebug_info. */
302
9cdd5dbd
DE
303static const struct dwarf2_debug_sections dwarf2_elf_names =
304{
251d32d9
TG
305 { ".debug_info", ".zdebug_info" },
306 { ".debug_abbrev", ".zdebug_abbrev" },
307 { ".debug_line", ".zdebug_line" },
308 { ".debug_loc", ".zdebug_loc" },
43988095 309 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 310 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 311 { ".debug_macro", ".zdebug_macro" },
251d32d9 312 { ".debug_str", ".zdebug_str" },
43988095 313 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 314 { ".debug_ranges", ".zdebug_ranges" },
43988095 315 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 316 { ".debug_types", ".zdebug_types" },
3019eac3 317 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
318 { ".debug_frame", ".zdebug_frame" },
319 { ".eh_frame", NULL },
24d3216f 320 { ".gdb_index", ".zgdb_index" },
927aa2e7
JK
321 { ".debug_names", ".zdebug_names" },
322 { ".debug_aranges", ".zdebug_aranges" },
24d3216f 323 23
251d32d9 324};
c906108c 325
80626a55 326/* List of DWO/DWP sections. */
3019eac3 327
80626a55 328static const struct dwop_section_names
3019eac3
DE
329{
330 struct dwarf2_section_names abbrev_dwo;
331 struct dwarf2_section_names info_dwo;
332 struct dwarf2_section_names line_dwo;
333 struct dwarf2_section_names loc_dwo;
43988095 334 struct dwarf2_section_names loclists_dwo;
09262596
DE
335 struct dwarf2_section_names macinfo_dwo;
336 struct dwarf2_section_names macro_dwo;
3019eac3
DE
337 struct dwarf2_section_names str_dwo;
338 struct dwarf2_section_names str_offsets_dwo;
339 struct dwarf2_section_names types_dwo;
80626a55
DE
340 struct dwarf2_section_names cu_index;
341 struct dwarf2_section_names tu_index;
3019eac3 342}
80626a55 343dwop_section_names =
3019eac3
DE
344{
345 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
346 { ".debug_info.dwo", ".zdebug_info.dwo" },
347 { ".debug_line.dwo", ".zdebug_line.dwo" },
348 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 349 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
350 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
351 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
352 { ".debug_str.dwo", ".zdebug_str.dwo" },
353 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
354 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
355 { ".debug_cu_index", ".zdebug_cu_index" },
356 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
357};
358
c906108c
SS
359/* local data types */
360
107d2387
AC
361/* The data in a compilation unit header, after target2host
362 translation, looks like this. */
c906108c 363struct comp_unit_head
a738430d 364{
c764a876 365 unsigned int length;
a738430d 366 short version;
a738430d
MK
367 unsigned char addr_size;
368 unsigned char signed_addr_p;
9c541725 369 sect_offset abbrev_sect_off;
57349743 370
a738430d
MK
371 /* Size of file offsets; either 4 or 8. */
372 unsigned int offset_size;
57349743 373
a738430d
MK
374 /* Size of the length field; either 4 or 12. */
375 unsigned int initial_length_size;
57349743 376
43988095
JK
377 enum dwarf_unit_type unit_type;
378
a738430d
MK
379 /* Offset to the first byte of this compilation unit header in the
380 .debug_info section, for resolving relative reference dies. */
9c541725 381 sect_offset sect_off;
57349743 382
d00adf39
DE
383 /* Offset to first die in this cu from the start of the cu.
384 This will be the first byte following the compilation unit header. */
9c541725 385 cu_offset first_die_cu_offset;
43988095
JK
386
387 /* 64-bit signature of this type unit - it is valid only for
388 UNIT_TYPE DW_UT_type. */
389 ULONGEST signature;
390
391 /* For types, offset in the type's DIE of the type defined by this TU. */
9c541725 392 cu_offset type_cu_offset_in_tu;
a738430d 393};
c906108c 394
3da10d80
KS
395/* Type used for delaying computation of method physnames.
396 See comments for compute_delayed_physnames. */
397struct delayed_method_info
398{
399 /* The type to which the method is attached, i.e., its parent class. */
400 struct type *type;
401
402 /* The index of the method in the type's function fieldlists. */
403 int fnfield_index;
404
405 /* The index of the method in the fieldlist. */
406 int index;
407
408 /* The name of the DIE. */
409 const char *name;
410
411 /* The DIE associated with this method. */
412 struct die_info *die;
413};
414
e7c27a73
DJ
415/* Internal state when decoding a particular compilation unit. */
416struct dwarf2_cu
417{
fcd3b13d
SM
418 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
419 ~dwarf2_cu ();
420
421 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
422
d00adf39 423 /* The header of the compilation unit. */
fcd3b13d 424 struct comp_unit_head header {};
e142c38c 425
d00adf39 426 /* Base address of this compilation unit. */
fcd3b13d 427 CORE_ADDR base_address = 0;
d00adf39
DE
428
429 /* Non-zero if base_address has been set. */
fcd3b13d 430 int base_known = 0;
d00adf39 431
e142c38c 432 /* The language we are debugging. */
fcd3b13d
SM
433 enum language language = language_unknown;
434 const struct language_defn *language_defn = nullptr;
e142c38c 435
fcd3b13d 436 const char *producer = nullptr;
b0f35d58 437
804d2729
TT
438 /* The symtab builder for this CU. This is only non-NULL when full
439 symbols are being read. */
440 std::unique_ptr<buildsym_compunit> builder;
441
e142c38c
DJ
442 /* The generic symbol table building routines have separate lists for
443 file scope symbols and all all other scopes (local scopes). So
444 we need to select the right one to pass to add_symbol_to_list().
445 We do it by keeping a pointer to the correct list in list_in_scope.
446
447 FIXME: The original dwarf code just treated the file scope as the
448 first local scope, and all other local scopes as nested local
449 scopes, and worked fine. Check to see if we really need to
450 distinguish these in buildsym.c. */
fcd3b13d 451 struct pending **list_in_scope = nullptr;
e142c38c 452
b64f50a1
JK
453 /* Hash table holding all the loaded partial DIEs
454 with partial_die->offset.SECT_OFF as hash. */
fcd3b13d 455 htab_t partial_dies = nullptr;
72bf9492
DJ
456
457 /* Storage for things with the same lifetime as this read-in compilation
458 unit, including partial DIEs. */
fcd3b13d 459 auto_obstack comp_unit_obstack;
72bf9492 460
ae038cb0
DJ
461 /* When multiple dwarf2_cu structures are living in memory, this field
462 chains them all together, so that they can be released efficiently.
463 We will probably also want a generation counter so that most-recently-used
464 compilation units are cached... */
fcd3b13d 465 struct dwarf2_per_cu_data *read_in_chain = nullptr;
ae038cb0 466
69d751e3 467 /* Backlink to our per_cu entry. */
ae038cb0
DJ
468 struct dwarf2_per_cu_data *per_cu;
469
470 /* How many compilation units ago was this CU last referenced? */
fcd3b13d 471 int last_used = 0;
ae038cb0 472
b64f50a1
JK
473 /* A hash table of DIE cu_offset for following references with
474 die_info->offset.sect_off as hash. */
fcd3b13d 475 htab_t die_hash = nullptr;
10b3939b
DJ
476
477 /* Full DIEs if read in. */
fcd3b13d 478 struct die_info *dies = nullptr;
10b3939b
DJ
479
480 /* A set of pointers to dwarf2_per_cu_data objects for compilation
481 units referenced by this one. Only set during full symbol processing;
482 partial symbol tables do not have dependencies. */
fcd3b13d 483 htab_t dependencies = nullptr;
10b3939b 484
cb1df416 485 /* Header data from the line table, during full symbol processing. */
fcd3b13d 486 struct line_header *line_header = nullptr;
4c8aa72d
PA
487 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
488 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
489 this is the DW_TAG_compile_unit die for this CU. We'll hold on
490 to the line header as long as this DIE is being processed. See
491 process_die_scope. */
fcd3b13d 492 die_info *line_header_die_owner = nullptr;
cb1df416 493
3da10d80
KS
494 /* A list of methods which need to have physnames computed
495 after all type information has been read. */
c89b44cd 496 std::vector<delayed_method_info> method_list;
3da10d80 497
96408a79 498 /* To be copied to symtab->call_site_htab. */
fcd3b13d 499 htab_t call_site_htab = nullptr;
96408a79 500
034e5797
DE
501 /* Non-NULL if this CU came from a DWO file.
502 There is an invariant here that is important to remember:
503 Except for attributes copied from the top level DIE in the "main"
504 (or "stub") file in preparation for reading the DWO file
505 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
506 Either there isn't a DWO file (in which case this is NULL and the point
507 is moot), or there is and either we're not going to read it (in which
508 case this is NULL) or there is and we are reading it (in which case this
509 is non-NULL). */
fcd3b13d 510 struct dwo_unit *dwo_unit = nullptr;
3019eac3
DE
511
512 /* The DW_AT_addr_base attribute if present, zero otherwise
513 (zero is a valid value though).
1dbab08b 514 Note this value comes from the Fission stub CU/TU's DIE. */
fcd3b13d 515 ULONGEST addr_base = 0;
3019eac3 516
2e3cf129
DE
517 /* The DW_AT_ranges_base attribute if present, zero otherwise
518 (zero is a valid value though).
1dbab08b 519 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 520 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
521 be used without needing to know whether DWO files are in use or not.
522 N.B. This does not apply to DW_AT_ranges appearing in
523 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
524 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
525 DW_AT_ranges_base *would* have to be applied, and we'd have to care
526 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
fcd3b13d 527 ULONGEST ranges_base = 0;
2e3cf129 528
c9317f21
TT
529 /* When reading debug info generated by older versions of rustc, we
530 have to rewrite some union types to be struct types with a
531 variant part. This rewriting must be done after the CU is fully
532 read in, because otherwise at the point of rewriting some struct
533 type might not have been fully processed. So, we keep a list of
534 all such types here and process them after expansion. */
535 std::vector<struct type *> rust_unions;
536
ae038cb0
DJ
537 /* Mark used when releasing cached dies. */
538 unsigned int mark : 1;
539
8be455d7
JK
540 /* This CU references .debug_loc. See the symtab->locations_valid field.
541 This test is imperfect as there may exist optimized debug code not using
542 any location list and still facing inlining issues if handled as
543 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 544 unsigned int has_loclist : 1;
ba919b58 545
1b80a9fa
JK
546 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
547 if all the producer_is_* fields are valid. This information is cached
548 because profiling CU expansion showed excessive time spent in
549 producer_is_gxx_lt_4_6. */
ba919b58
TT
550 unsigned int checked_producer : 1;
551 unsigned int producer_is_gxx_lt_4_6 : 1;
1b80a9fa 552 unsigned int producer_is_gcc_lt_4_3 : 1;
5230b05a 553 unsigned int producer_is_icc_lt_14 : 1;
4d4ec4e5
TT
554
555 /* When set, the file that we're processing is known to have
556 debugging info for C++ namespaces. GCC 3.3.x did not produce
557 this information, but later versions do. */
558
559 unsigned int processing_has_namespace_info : 1;
d590ff25
YQ
560
561 struct partial_die_info *find_partial_die (sect_offset sect_off);
e7c27a73
DJ
562};
563
094b34ac
DE
564/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
565 This includes type_unit_group and quick_file_names. */
566
567struct stmt_list_hash
568{
569 /* The DWO unit this table is from or NULL if there is none. */
570 struct dwo_unit *dwo_unit;
571
572 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 573 sect_offset line_sect_off;
094b34ac
DE
574};
575
f4dc4d17
DE
576/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
577 an object of this type. */
578
579struct type_unit_group
580{
0186c6a7 581 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
582 To simplify things we create an artificial CU that "includes" all the
583 type units using this stmt_list so that the rest of the code still has
584 a "per_cu" handle on the symtab.
585 This PER_CU is recognized by having no section. */
8a0459fd 586#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
587 struct dwarf2_per_cu_data per_cu;
588
0186c6a7
DE
589 /* The TUs that share this DW_AT_stmt_list entry.
590 This is added to while parsing type units to build partial symtabs,
591 and is deleted afterwards and not used again. */
592 VEC (sig_type_ptr) *tus;
f4dc4d17 593
43f3e411 594 /* The compunit symtab.
094b34ac 595 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
596 so we create an essentially anonymous symtab as the compunit symtab. */
597 struct compunit_symtab *compunit_symtab;
f4dc4d17 598
094b34ac
DE
599 /* The data used to construct the hash key. */
600 struct stmt_list_hash hash;
f4dc4d17
DE
601
602 /* The number of symtabs from the line header.
603 The value here must match line_header.num_file_names. */
604 unsigned int num_symtabs;
605
606 /* The symbol tables for this TU (obtained from the files listed in
607 DW_AT_stmt_list).
608 WARNING: The order of entries here must match the order of entries
609 in the line header. After the first TU using this type_unit_group, the
610 line header for the subsequent TUs is recreated from this. This is done
611 because we need to use the same symtabs for each TU using the same
612 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
613 there's no guarantee the line header doesn't have duplicate entries. */
614 struct symtab **symtabs;
615};
616
73869dc2 617/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
618
619struct dwo_sections
620{
621 struct dwarf2_section_info abbrev;
3019eac3
DE
622 struct dwarf2_section_info line;
623 struct dwarf2_section_info loc;
43988095 624 struct dwarf2_section_info loclists;
09262596
DE
625 struct dwarf2_section_info macinfo;
626 struct dwarf2_section_info macro;
3019eac3
DE
627 struct dwarf2_section_info str;
628 struct dwarf2_section_info str_offsets;
80626a55
DE
629 /* In the case of a virtual DWO file, these two are unused. */
630 struct dwarf2_section_info info;
3019eac3
DE
631 VEC (dwarf2_section_info_def) *types;
632};
633
c88ee1f0 634/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
635
636struct dwo_unit
637{
638 /* Backlink to the containing struct dwo_file. */
639 struct dwo_file *dwo_file;
640
641 /* The "id" that distinguishes this CU/TU.
642 .debug_info calls this "dwo_id", .debug_types calls this "signature".
643 Since signatures came first, we stick with it for consistency. */
644 ULONGEST signature;
645
646 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 647 struct dwarf2_section_info *section;
3019eac3 648
9c541725
PA
649 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
650 sect_offset sect_off;
3019eac3
DE
651 unsigned int length;
652
653 /* For types, offset in the type's DIE of the type defined by this TU. */
654 cu_offset type_offset_in_tu;
655};
656
73869dc2
DE
657/* include/dwarf2.h defines the DWP section codes.
658 It defines a max value but it doesn't define a min value, which we
659 use for error checking, so provide one. */
660
661enum dwp_v2_section_ids
662{
663 DW_SECT_MIN = 1
664};
665
80626a55 666/* Data for one DWO file.
57d63ce2
DE
667
668 This includes virtual DWO files (a virtual DWO file is a DWO file as it
669 appears in a DWP file). DWP files don't really have DWO files per se -
670 comdat folding of types "loses" the DWO file they came from, and from
671 a high level view DWP files appear to contain a mass of random types.
672 However, to maintain consistency with the non-DWP case we pretend DWP
673 files contain virtual DWO files, and we assign each TU with one virtual
674 DWO file (generally based on the line and abbrev section offsets -
675 a heuristic that seems to work in practice). */
3019eac3
DE
676
677struct dwo_file
678{
0ac5b59e 679 /* The DW_AT_GNU_dwo_name attribute.
80626a55
DE
680 For virtual DWO files the name is constructed from the section offsets
681 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
682 from related CU+TUs. */
0ac5b59e
DE
683 const char *dwo_name;
684
685 /* The DW_AT_comp_dir attribute. */
686 const char *comp_dir;
3019eac3 687
80626a55
DE
688 /* The bfd, when the file is open. Otherwise this is NULL.
689 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
690 bfd *dbfd;
3019eac3 691
73869dc2
DE
692 /* The sections that make up this DWO file.
693 Remember that for virtual DWO files in DWP V2, these are virtual
694 sections (for lack of a better name). */
3019eac3
DE
695 struct dwo_sections sections;
696
33c5cd75
DB
697 /* The CUs in the file.
698 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
699 an extension to handle LLVM's Link Time Optimization output (where
700 multiple source files may be compiled into a single object/dwo pair). */
701 htab_t cus;
3019eac3
DE
702
703 /* Table of TUs in the file.
704 Each element is a struct dwo_unit. */
705 htab_t tus;
706};
707
80626a55
DE
708/* These sections are what may appear in a DWP file. */
709
710struct dwp_sections
711{
73869dc2 712 /* These are used by both DWP version 1 and 2. */
80626a55
DE
713 struct dwarf2_section_info str;
714 struct dwarf2_section_info cu_index;
715 struct dwarf2_section_info tu_index;
73869dc2
DE
716
717 /* These are only used by DWP version 2 files.
718 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
719 sections are referenced by section number, and are not recorded here.
720 In DWP version 2 there is at most one copy of all these sections, each
721 section being (effectively) comprised of the concatenation of all of the
722 individual sections that exist in the version 1 format.
723 To keep the code simple we treat each of these concatenated pieces as a
724 section itself (a virtual section?). */
725 struct dwarf2_section_info abbrev;
726 struct dwarf2_section_info info;
727 struct dwarf2_section_info line;
728 struct dwarf2_section_info loc;
729 struct dwarf2_section_info macinfo;
730 struct dwarf2_section_info macro;
731 struct dwarf2_section_info str_offsets;
732 struct dwarf2_section_info types;
80626a55
DE
733};
734
73869dc2
DE
735/* These sections are what may appear in a virtual DWO file in DWP version 1.
736 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 737
73869dc2 738struct virtual_v1_dwo_sections
80626a55
DE
739{
740 struct dwarf2_section_info abbrev;
741 struct dwarf2_section_info line;
742 struct dwarf2_section_info loc;
743 struct dwarf2_section_info macinfo;
744 struct dwarf2_section_info macro;
745 struct dwarf2_section_info str_offsets;
746 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 747 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
748 struct dwarf2_section_info info_or_types;
749};
750
73869dc2
DE
751/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
752 In version 2, the sections of the DWO files are concatenated together
753 and stored in one section of that name. Thus each ELF section contains
754 several "virtual" sections. */
755
756struct virtual_v2_dwo_sections
757{
758 bfd_size_type abbrev_offset;
759 bfd_size_type abbrev_size;
760
761 bfd_size_type line_offset;
762 bfd_size_type line_size;
763
764 bfd_size_type loc_offset;
765 bfd_size_type loc_size;
766
767 bfd_size_type macinfo_offset;
768 bfd_size_type macinfo_size;
769
770 bfd_size_type macro_offset;
771 bfd_size_type macro_size;
772
773 bfd_size_type str_offsets_offset;
774 bfd_size_type str_offsets_size;
775
776 /* Each DWP hash table entry records one CU or one TU.
777 That is recorded here, and copied to dwo_unit.section. */
778 bfd_size_type info_or_types_offset;
779 bfd_size_type info_or_types_size;
780};
781
80626a55
DE
782/* Contents of DWP hash tables. */
783
784struct dwp_hash_table
785{
73869dc2 786 uint32_t version, nr_columns;
80626a55 787 uint32_t nr_units, nr_slots;
73869dc2
DE
788 const gdb_byte *hash_table, *unit_table;
789 union
790 {
791 struct
792 {
793 const gdb_byte *indices;
794 } v1;
795 struct
796 {
797 /* This is indexed by column number and gives the id of the section
798 in that column. */
799#define MAX_NR_V2_DWO_SECTIONS \
800 (1 /* .debug_info or .debug_types */ \
801 + 1 /* .debug_abbrev */ \
802 + 1 /* .debug_line */ \
803 + 1 /* .debug_loc */ \
804 + 1 /* .debug_str_offsets */ \
805 + 1 /* .debug_macro or .debug_macinfo */)
806 int section_ids[MAX_NR_V2_DWO_SECTIONS];
807 const gdb_byte *offsets;
808 const gdb_byte *sizes;
809 } v2;
810 } section_pool;
80626a55
DE
811};
812
813/* Data for one DWP file. */
814
815struct dwp_file
816{
400174b1
TT
817 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
818 : name (name_),
819 dbfd (std::move (abfd))
820 {
821 }
822
80626a55
DE
823 /* Name of the file. */
824 const char *name;
825
73869dc2 826 /* File format version. */
400174b1 827 int version = 0;
73869dc2 828
93417882 829 /* The bfd. */
400174b1 830 gdb_bfd_ref_ptr dbfd;
80626a55
DE
831
832 /* Section info for this file. */
400174b1 833 struct dwp_sections sections {};
80626a55 834
57d63ce2 835 /* Table of CUs in the file. */
400174b1 836 const struct dwp_hash_table *cus = nullptr;
80626a55
DE
837
838 /* Table of TUs in the file. */
400174b1 839 const struct dwp_hash_table *tus = nullptr;
80626a55 840
19ac8c2e 841 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
400174b1
TT
842 htab_t loaded_cus {};
843 htab_t loaded_tus {};
80626a55 844
73869dc2
DE
845 /* Table to map ELF section numbers to their sections.
846 This is only needed for the DWP V1 file format. */
400174b1
TT
847 unsigned int num_sections = 0;
848 asection **elf_sections = nullptr;
80626a55
DE
849};
850
36586728
TT
851/* This represents a '.dwz' file. */
852
853struct dwz_file
854{
7ff8cb8c
TT
855 dwz_file (gdb_bfd_ref_ptr &&bfd)
856 : dwz_bfd (std::move (bfd))
857 {
858 }
859
36586728 860 /* A dwz file can only contain a few sections. */
7ff8cb8c
TT
861 struct dwarf2_section_info abbrev {};
862 struct dwarf2_section_info info {};
863 struct dwarf2_section_info str {};
864 struct dwarf2_section_info line {};
865 struct dwarf2_section_info macro {};
866 struct dwarf2_section_info gdb_index {};
867 struct dwarf2_section_info debug_names {};
36586728
TT
868
869 /* The dwz's BFD. */
7ff8cb8c 870 gdb_bfd_ref_ptr dwz_bfd;
36586728
TT
871};
872
0963b4bd
MS
873/* Struct used to pass misc. parameters to read_die_and_children, et
874 al. which are used for both .debug_info and .debug_types dies.
875 All parameters here are unchanging for the life of the call. This
dee91e82 876 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
877
878struct die_reader_specs
879{
a32a8923 880 /* The bfd of die_section. */
93311388
DE
881 bfd* abfd;
882
883 /* The CU of the DIE we are parsing. */
884 struct dwarf2_cu *cu;
885
80626a55 886 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
887 struct dwo_file *dwo_file;
888
dee91e82 889 /* The section the die comes from.
3019eac3 890 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
891 struct dwarf2_section_info *die_section;
892
893 /* die_section->buffer. */
d521ce57 894 const gdb_byte *buffer;
f664829e
DE
895
896 /* The end of the buffer. */
897 const gdb_byte *buffer_end;
a2ce51a0
DE
898
899 /* The value of the DW_AT_comp_dir attribute. */
900 const char *comp_dir;
685af9cd
TT
901
902 /* The abbreviation table to use when reading the DIEs. */
903 struct abbrev_table *abbrev_table;
93311388
DE
904};
905
fd820528 906/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82 907typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
d521ce57 908 const gdb_byte *info_ptr,
dee91e82
DE
909 struct die_info *comp_unit_die,
910 int has_children,
911 void *data);
912
ecfb656c
PA
913/* A 1-based directory index. This is a strong typedef to prevent
914 accidentally using a directory index as a 0-based index into an
915 array/vector. */
916enum class dir_index : unsigned int {};
917
918/* Likewise, a 1-based file name index. */
919enum class file_name_index : unsigned int {};
920
52059ffd
TT
921struct file_entry
922{
fff8551c
PA
923 file_entry () = default;
924
ecfb656c 925 file_entry (const char *name_, dir_index d_index_,
fff8551c
PA
926 unsigned int mod_time_, unsigned int length_)
927 : name (name_),
ecfb656c 928 d_index (d_index_),
fff8551c
PA
929 mod_time (mod_time_),
930 length (length_)
931 {}
932
ecfb656c
PA
933 /* Return the include directory at D_INDEX stored in LH. Returns
934 NULL if D_INDEX is out of bounds. */
8c43009f
PA
935 const char *include_dir (const line_header *lh) const;
936
fff8551c
PA
937 /* The file name. Note this is an observing pointer. The memory is
938 owned by debug_line_buffer. */
939 const char *name {};
940
8c43009f 941 /* The directory index (1-based). */
ecfb656c 942 dir_index d_index {};
fff8551c
PA
943
944 unsigned int mod_time {};
945
946 unsigned int length {};
947
948 /* True if referenced by the Line Number Program. */
949 bool included_p {};
950
83769d0b 951 /* The associated symbol table, if any. */
fff8551c 952 struct symtab *symtab {};
52059ffd
TT
953};
954
debd256d
JB
955/* The line number information for a compilation unit (found in the
956 .debug_line section) begins with a "statement program header",
957 which contains the following information. */
958struct line_header
959{
fff8551c
PA
960 line_header ()
961 : offset_in_dwz {}
962 {}
963
964 /* Add an entry to the include directory table. */
965 void add_include_dir (const char *include_dir);
966
967 /* Add an entry to the file name table. */
ecfb656c 968 void add_file_name (const char *name, dir_index d_index,
fff8551c
PA
969 unsigned int mod_time, unsigned int length);
970
ecfb656c 971 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
8c43009f 972 is out of bounds. */
ecfb656c 973 const char *include_dir_at (dir_index index) const
8c43009f 974 {
ecfb656c
PA
975 /* Convert directory index number (1-based) to vector index
976 (0-based). */
977 size_t vec_index = to_underlying (index) - 1;
978
979 if (vec_index >= include_dirs.size ())
8c43009f 980 return NULL;
ecfb656c 981 return include_dirs[vec_index];
8c43009f
PA
982 }
983
ecfb656c 984 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
8c43009f 985 is out of bounds. */
ecfb656c 986 file_entry *file_name_at (file_name_index index)
8c43009f 987 {
ecfb656c
PA
988 /* Convert file name index number (1-based) to vector index
989 (0-based). */
990 size_t vec_index = to_underlying (index) - 1;
991
992 if (vec_index >= file_names.size ())
fff8551c 993 return NULL;
ecfb656c 994 return &file_names[vec_index];
fff8551c
PA
995 }
996
997 /* Const version of the above. */
998 const file_entry *file_name_at (unsigned int index) const
999 {
1000 if (index >= file_names.size ())
8c43009f
PA
1001 return NULL;
1002 return &file_names[index];
1003 }
1004
527f3840 1005 /* Offset of line number information in .debug_line section. */
9c541725 1006 sect_offset sect_off {};
527f3840
JK
1007
1008 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
fff8551c
PA
1009 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1010
1011 unsigned int total_length {};
1012 unsigned short version {};
1013 unsigned int header_length {};
1014 unsigned char minimum_instruction_length {};
1015 unsigned char maximum_ops_per_instruction {};
1016 unsigned char default_is_stmt {};
1017 int line_base {};
1018 unsigned char line_range {};
1019 unsigned char opcode_base {};
debd256d
JB
1020
1021 /* standard_opcode_lengths[i] is the number of operands for the
1022 standard opcode whose value is i. This means that
1023 standard_opcode_lengths[0] is unused, and the last meaningful
1024 element is standard_opcode_lengths[opcode_base - 1]. */
fff8551c 1025 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
debd256d 1026
fff8551c
PA
1027 /* The include_directories table. Note these are observing
1028 pointers. The memory is owned by debug_line_buffer. */
1029 std::vector<const char *> include_dirs;
debd256d 1030
fff8551c
PA
1031 /* The file_names table. */
1032 std::vector<file_entry> file_names;
debd256d
JB
1033
1034 /* The start and end of the statement program following this
6502dd73 1035 header. These point into dwarf2_per_objfile->line_buffer. */
fff8551c 1036 const gdb_byte *statement_program_start {}, *statement_program_end {};
debd256d 1037};
c906108c 1038
fff8551c
PA
1039typedef std::unique_ptr<line_header> line_header_up;
1040
8c43009f
PA
1041const char *
1042file_entry::include_dir (const line_header *lh) const
1043{
ecfb656c 1044 return lh->include_dir_at (d_index);
8c43009f
PA
1045}
1046
c906108c 1047/* When we construct a partial symbol table entry we only
0963b4bd 1048 need this much information. */
6f06d47b 1049struct partial_die_info : public allocate_on_obstack
c906108c 1050 {
6f06d47b
YQ
1051 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1052
1053 /* Disable assign but still keep copy ctor, which is needed
1054 load_partial_dies. */
1055 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1056
52356b79
YQ
1057 /* Adjust the partial die before generating a symbol for it. This
1058 function may set the is_external flag or change the DIE's
1059 name. */
1060 void fixup (struct dwarf2_cu *cu);
1061
48fbe735
YQ
1062 /* Read a minimal amount of information into the minimal die
1063 structure. */
1064 const gdb_byte *read (const struct die_reader_specs *reader,
1065 const struct abbrev_info &abbrev,
1066 const gdb_byte *info_ptr);
1067
72bf9492 1068 /* Offset of this DIE. */
6f06d47b 1069 const sect_offset sect_off;
72bf9492
DJ
1070
1071 /* DWARF-2 tag for this DIE. */
6f06d47b 1072 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 1073
72bf9492 1074 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
1075 const unsigned int has_children : 1;
1076
72bf9492
DJ
1077 unsigned int is_external : 1;
1078 unsigned int is_declaration : 1;
1079 unsigned int has_type : 1;
1080 unsigned int has_specification : 1;
1081 unsigned int has_pc_info : 1;
481860b3 1082 unsigned int may_be_inlined : 1;
72bf9492 1083
0c1b455e
TT
1084 /* This DIE has been marked DW_AT_main_subprogram. */
1085 unsigned int main_subprogram : 1;
1086
72bf9492
DJ
1087 /* Flag set if the SCOPE field of this structure has been
1088 computed. */
1089 unsigned int scope_set : 1;
1090
fa4028e9
JB
1091 /* Flag set if the DIE has a byte_size attribute. */
1092 unsigned int has_byte_size : 1;
1093
ff908ebf
AW
1094 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1095 unsigned int has_const_value : 1;
1096
98bfdba5
PA
1097 /* Flag set if any of the DIE's children are template arguments. */
1098 unsigned int has_template_arguments : 1;
1099
52356b79 1100 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
1101 unsigned int fixup_called : 1;
1102
36586728
TT
1103 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1104 unsigned int is_dwz : 1;
1105
1106 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1107 unsigned int spec_is_dwz : 1;
1108
72bf9492 1109 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1110 sometimes a default name for unnamed DIEs. */
6f06d47b 1111 const char *name = nullptr;
72bf9492 1112
abc72ce4 1113 /* The linkage name, if present. */
6f06d47b 1114 const char *linkage_name = nullptr;
abc72ce4 1115
72bf9492
DJ
1116 /* The scope to prepend to our children. This is generally
1117 allocated on the comp_unit_obstack, so will disappear
1118 when this compilation unit leaves the cache. */
6f06d47b 1119 const char *scope = nullptr;
72bf9492 1120
95554aad
TT
1121 /* Some data associated with the partial DIE. The tag determines
1122 which field is live. */
1123 union
1124 {
1125 /* The location description associated with this DIE, if any. */
1126 struct dwarf_block *locdesc;
1127 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1128 sect_offset sect_off;
6f06d47b 1129 } d {};
72bf9492
DJ
1130
1131 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
1132 CORE_ADDR lowpc = 0;
1133 CORE_ADDR highpc = 0;
72bf9492 1134
93311388 1135 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1136 DW_AT_sibling, if any. */
48fbe735
YQ
1137 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1138 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1139 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1140
1141 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1142 DW_AT_specification (or DW_AT_abstract_origin or
1143 DW_AT_extension). */
6f06d47b 1144 sect_offset spec_offset {};
72bf9492
DJ
1145
1146 /* Pointers to this DIE's parent, first child, and next sibling,
1147 if any. */
6f06d47b
YQ
1148 struct partial_die_info *die_parent = nullptr;
1149 struct partial_die_info *die_child = nullptr;
1150 struct partial_die_info *die_sibling = nullptr;
1151
1152 friend struct partial_die_info *
1153 dwarf2_cu::find_partial_die (sect_offset sect_off);
1154
1155 private:
1156 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1157 partial_die_info (sect_offset sect_off)
1158 : partial_die_info (sect_off, DW_TAG_padding, 0)
1159 {
1160 }
1161
1162 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1163 int has_children_)
1164 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1165 {
1166 is_external = 0;
1167 is_declaration = 0;
1168 has_type = 0;
1169 has_specification = 0;
1170 has_pc_info = 0;
1171 may_be_inlined = 0;
1172 main_subprogram = 0;
1173 scope_set = 0;
1174 has_byte_size = 0;
1175 has_const_value = 0;
1176 has_template_arguments = 0;
1177 fixup_called = 0;
1178 is_dwz = 0;
1179 spec_is_dwz = 0;
1180 }
c906108c
SS
1181 };
1182
0963b4bd 1183/* This data structure holds the information of an abbrev. */
c906108c
SS
1184struct abbrev_info
1185 {
1186 unsigned int number; /* number identifying abbrev */
1187 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
1188 unsigned short has_children; /* boolean */
1189 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1190 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1191 struct abbrev_info *next; /* next in chain */
1192 };
1193
1194struct attr_abbrev
1195 {
9d25dd43
DE
1196 ENUM_BITFIELD(dwarf_attribute) name : 16;
1197 ENUM_BITFIELD(dwarf_form) form : 16;
43988095
JK
1198
1199 /* It is valid only if FORM is DW_FORM_implicit_const. */
1200 LONGEST implicit_const;
c906108c
SS
1201 };
1202
433df2d4
DE
1203/* Size of abbrev_table.abbrev_hash_table. */
1204#define ABBREV_HASH_SIZE 121
1205
1206/* Top level data structure to contain an abbreviation table. */
1207
1208struct abbrev_table
1209{
685af9cd
TT
1210 explicit abbrev_table (sect_offset off)
1211 : sect_off (off)
1212 {
4a17f768 1213 m_abbrevs =
685af9cd 1214 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
4a17f768 1215 memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
685af9cd
TT
1216 }
1217
1218 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1219
1220 /* Allocate space for a struct abbrev_info object in
1221 ABBREV_TABLE. */
1222 struct abbrev_info *alloc_abbrev ();
1223
1224 /* Add an abbreviation to the table. */
1225 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1226
1227 /* Look up an abbrev in the table.
1228 Returns NULL if the abbrev is not found. */
1229
1230 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1231
1232
f4dc4d17
DE
1233 /* Where the abbrev table came from.
1234 This is used as a sanity check when the table is used. */
685af9cd 1235 const sect_offset sect_off;
433df2d4
DE
1236
1237 /* Storage for the abbrev table. */
685af9cd 1238 auto_obstack abbrev_obstack;
433df2d4 1239
4a17f768
YQ
1240private:
1241
433df2d4
DE
1242 /* Hash table of abbrevs.
1243 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1244 It could be statically allocated, but the previous code didn't so we
1245 don't either. */
4a17f768 1246 struct abbrev_info **m_abbrevs;
433df2d4
DE
1247};
1248
685af9cd
TT
1249typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
1250
0963b4bd 1251/* Attributes have a name and a value. */
b60c80d6
DJ
1252struct attribute
1253 {
9d25dd43 1254 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1255 ENUM_BITFIELD(dwarf_form) form : 15;
1256
1257 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1258 field should be in u.str (existing only for DW_STRING) but it is kept
1259 here for better struct attribute alignment. */
1260 unsigned int string_is_canonical : 1;
1261
b60c80d6
DJ
1262 union
1263 {
15d034d0 1264 const char *str;
b60c80d6 1265 struct dwarf_block *blk;
43bbcdc2
PH
1266 ULONGEST unsnd;
1267 LONGEST snd;
b60c80d6 1268 CORE_ADDR addr;
ac9ec31b 1269 ULONGEST signature;
b60c80d6
DJ
1270 }
1271 u;
1272 };
1273
0963b4bd 1274/* This data structure holds a complete die structure. */
c906108c
SS
1275struct die_info
1276 {
76815b17
DE
1277 /* DWARF-2 tag for this DIE. */
1278 ENUM_BITFIELD(dwarf_tag) tag : 16;
1279
1280 /* Number of attributes */
98bfdba5
PA
1281 unsigned char num_attrs;
1282
1283 /* True if we're presently building the full type name for the
1284 type derived from this DIE. */
1285 unsigned char building_fullname : 1;
76815b17 1286
adde2bff
DE
1287 /* True if this die is in process. PR 16581. */
1288 unsigned char in_process : 1;
1289
76815b17
DE
1290 /* Abbrev number */
1291 unsigned int abbrev;
1292
93311388 1293 /* Offset in .debug_info or .debug_types section. */
9c541725 1294 sect_offset sect_off;
78ba4af6
JB
1295
1296 /* The dies in a compilation unit form an n-ary tree. PARENT
1297 points to this die's parent; CHILD points to the first child of
1298 this node; and all the children of a given node are chained
4950bc1c 1299 together via their SIBLING fields. */
639d11d3
DC
1300 struct die_info *child; /* Its first child, if any. */
1301 struct die_info *sibling; /* Its next sibling, if any. */
1302 struct die_info *parent; /* Its parent, if any. */
c906108c 1303
b60c80d6
DJ
1304 /* An array of attributes, with NUM_ATTRS elements. There may be
1305 zero, but it's not common and zero-sized arrays are not
1306 sufficiently portable C. */
1307 struct attribute attrs[1];
c906108c
SS
1308 };
1309
0963b4bd 1310/* Get at parts of an attribute structure. */
c906108c
SS
1311
1312#define DW_STRING(attr) ((attr)->u.str)
8285870a 1313#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1314#define DW_UNSND(attr) ((attr)->u.unsnd)
1315#define DW_BLOCK(attr) ((attr)->u.blk)
1316#define DW_SND(attr) ((attr)->u.snd)
1317#define DW_ADDR(attr) ((attr)->u.addr)
ac9ec31b 1318#define DW_SIGNATURE(attr) ((attr)->u.signature)
c906108c 1319
0963b4bd 1320/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1321struct dwarf_block
1322 {
56eb65bd 1323 size_t size;
1d6edc3c
JK
1324
1325 /* Valid only if SIZE is not zero. */
d521ce57 1326 const gdb_byte *data;
c906108c
SS
1327 };
1328
c906108c
SS
1329#ifndef ATTR_ALLOC_CHUNK
1330#define ATTR_ALLOC_CHUNK 4
1331#endif
1332
c906108c
SS
1333/* Allocate fields for structs, unions and enums in this size. */
1334#ifndef DW_FIELD_ALLOC_CHUNK
1335#define DW_FIELD_ALLOC_CHUNK 4
1336#endif
1337
c906108c
SS
1338/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1339 but this would require a corresponding change in unpack_field_as_long
1340 and friends. */
1341static int bits_per_byte = 8;
1342
2ddeaf8a
TT
1343/* When reading a variant or variant part, we track a bit more
1344 information about the field, and store it in an object of this
1345 type. */
1346
1347struct variant_field
1348{
1349 /* If we see a DW_TAG_variant, then this will be the discriminant
1350 value. */
1351 ULONGEST discriminant_value;
1352 /* If we see a DW_TAG_variant, then this will be set if this is the
1353 default branch. */
1354 bool default_branch;
1355 /* While reading a DW_TAG_variant_part, this will be set if this
1356 field is the discriminant. */
1357 bool is_discriminant;
1358};
1359
52059ffd
TT
1360struct nextfield
1361{
be2daae6
TT
1362 int accessibility = 0;
1363 int virtuality = 0;
2ddeaf8a 1364 /* Extra information to describe a variant or variant part. */
be2daae6
TT
1365 struct variant_field variant {};
1366 struct field field {};
52059ffd
TT
1367};
1368
1369struct fnfieldlist
1370{
be2daae6
TT
1371 const char *name = nullptr;
1372 std::vector<struct fn_field> fnfields;
52059ffd
TT
1373};
1374
c906108c
SS
1375/* The routines that read and process dies for a C struct or C++ class
1376 pass lists of data member fields and lists of member function fields
1377 in an instance of a field_info structure, as defined below. */
1378struct field_info
c5aa993b 1379 {
0963b4bd 1380 /* List of data member and baseclasses fields. */
be2daae6
TT
1381 std::vector<struct nextfield> fields;
1382 std::vector<struct nextfield> baseclasses;
c906108c 1383
7d0ccb61 1384 /* Number of fields (including baseclasses). */
be2daae6 1385 int nfields = 0;
c906108c 1386
c5aa993b 1387 /* Set if the accesibility of one of the fields is not public. */
be2daae6 1388 int non_public_fields = 0;
c906108c 1389
c5aa993b
JM
1390 /* Member function fieldlist array, contains name of possibly overloaded
1391 member function, number of overloaded member functions and a pointer
1392 to the head of the member function field chain. */
be2daae6 1393 std::vector<struct fnfieldlist> fnfieldlists;
98751a41
JK
1394
1395 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1396 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
be2daae6 1397 std::vector<struct decl_field> typedef_field_list;
883fd55a
KS
1398
1399 /* Nested types defined by this class and the number of elements in this
1400 list. */
be2daae6 1401 std::vector<struct decl_field> nested_types_list;
c5aa993b 1402 };
c906108c 1403
10b3939b
DJ
1404/* One item on the queue of compilation units to read in full symbols
1405 for. */
1406struct dwarf2_queue_item
1407{
1408 struct dwarf2_per_cu_data *per_cu;
95554aad 1409 enum language pretend_language;
10b3939b
DJ
1410 struct dwarf2_queue_item *next;
1411};
1412
1413/* The current queue. */
1414static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1415
ae038cb0
DJ
1416/* Loaded secondary compilation units are kept in memory until they
1417 have not been referenced for the processing of this many
1418 compilation units. Set this to zero to disable caching. Cache
1419 sizes of up to at least twenty will improve startup time for
1420 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1421static int dwarf_max_cache_age = 5;
920d2a44 1422static void
b4f54984
DE
1423show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1424 struct cmd_list_element *c, const char *value)
920d2a44 1425{
3e43a32a 1426 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1427 "DWARF compilation units is %s.\n"),
920d2a44
AC
1428 value);
1429}
4390d890 1430\f
c906108c
SS
1431/* local function prototypes */
1432
a32a8923
DE
1433static const char *get_section_name (const struct dwarf2_section_info *);
1434
1435static const char *get_section_file_name (const struct dwarf2_section_info *);
1436
918dd910
JK
1437static void dwarf2_find_base_address (struct die_info *die,
1438 struct dwarf2_cu *cu);
1439
0018ea6f
DE
1440static struct partial_symtab *create_partial_symtab
1441 (struct dwarf2_per_cu_data *per_cu, const char *name);
1442
f1902523
JK
1443static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1444 const gdb_byte *info_ptr,
1445 struct die_info *type_unit_die,
1446 int has_children, void *data);
1447
ed2dc618
SM
1448static void dwarf2_build_psymtabs_hard
1449 (struct dwarf2_per_objfile *dwarf2_per_objfile);
c906108c 1450
72bf9492
DJ
1451static void scan_partial_symbols (struct partial_die_info *,
1452 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1453 int, struct dwarf2_cu *);
c906108c 1454
72bf9492
DJ
1455static void add_partial_symbol (struct partial_die_info *,
1456 struct dwarf2_cu *);
63d06c5c 1457
72bf9492
DJ
1458static void add_partial_namespace (struct partial_die_info *pdi,
1459 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1460 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1461
5d7cb8df 1462static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1463 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1464 struct dwarf2_cu *cu);
1465
72bf9492
DJ
1466static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1467 struct dwarf2_cu *cu);
91c24f0a 1468
bc30ff58
JB
1469static void add_partial_subprogram (struct partial_die_info *pdi,
1470 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1471 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1472
257e7a09
YQ
1473static void dwarf2_read_symtab (struct partial_symtab *,
1474 struct objfile *);
c906108c 1475
a14ed312 1476static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1477
685af9cd 1478static abbrev_table_up abbrev_table_read_table
ed2dc618
SM
1479 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1480 sect_offset);
433df2d4 1481
d521ce57 1482static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1483
dee91e82 1484static struct partial_die_info *load_partial_dies
d521ce57 1485 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1486
36586728 1487static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1488 struct dwarf2_cu *);
72bf9492 1489
d521ce57
TT
1490static const gdb_byte *read_attribute (const struct die_reader_specs *,
1491 struct attribute *, struct attr_abbrev *,
1492 const gdb_byte *);
a8329558 1493
a1855c1d 1494static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1495
a1855c1d 1496static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1497
a1855c1d 1498static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1499
a1855c1d 1500static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1501
a1855c1d 1502static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1503
d521ce57 1504static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1505 unsigned int *);
c906108c 1506
d521ce57 1507static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1508
1509static LONGEST read_checked_initial_length_and_offset
d521ce57 1510 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1511 unsigned int *, unsigned int *);
613e1657 1512
d521ce57
TT
1513static LONGEST read_offset (bfd *, const gdb_byte *,
1514 const struct comp_unit_head *,
c764a876
DE
1515 unsigned int *);
1516
d521ce57 1517static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1518
ed2dc618
SM
1519static sect_offset read_abbrev_offset
1520 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1521 struct dwarf2_section_info *, sect_offset);
f4dc4d17 1522
d521ce57 1523static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1524
d521ce57 1525static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1526
ed2dc618
SM
1527static const char *read_indirect_string
1528 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1529 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1530
ed2dc618
SM
1531static const char *read_indirect_line_string
1532 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1533 const struct comp_unit_head *, unsigned int *);
36586728 1534
ed2dc618
SM
1535static const char *read_indirect_string_at_offset
1536 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1537 LONGEST str_offset);
927aa2e7 1538
ed2dc618
SM
1539static const char *read_indirect_string_from_dwz
1540 (struct objfile *objfile, struct dwz_file *, LONGEST);
c906108c 1541
d521ce57 1542static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1543
d521ce57
TT
1544static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1545 const gdb_byte *,
3019eac3
DE
1546 unsigned int *);
1547
d521ce57 1548static const char *read_str_index (const struct die_reader_specs *reader,
342587c4 1549 ULONGEST str_index);
3019eac3 1550
e142c38c 1551static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1552
e142c38c
DJ
1553static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1554 struct dwarf2_cu *);
c906108c 1555
348e048f 1556static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1557 unsigned int);
348e048f 1558
7d45c7c3
KB
1559static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1560 struct dwarf2_cu *cu);
1561
05cf31d1
JB
1562static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1563 struct dwarf2_cu *cu);
1564
e142c38c 1565static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1566
e142c38c 1567static struct die_info *die_specification (struct die_info *die,
f2f0e013 1568 struct dwarf2_cu **);
63d06c5c 1569
9c541725 1570static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1571 struct dwarf2_cu *cu);
debd256d 1572
f3f5162e 1573static void dwarf_decode_lines (struct line_header *, const char *,
c3b7b696 1574 struct dwarf2_cu *, struct partial_symtab *,
527f3840 1575 CORE_ADDR, int decode_mapping);
c906108c 1576
804d2729
TT
1577static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1578 const char *);
c906108c 1579
43f3e411
DE
1580static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1581 const char *, const char *,
1582 CORE_ADDR);
f4dc4d17 1583
a14ed312 1584static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1585 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1586
ff39bb5e 1587static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1588 struct dwarf2_cu *);
c906108c 1589
ff39bb5e 1590static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1591 struct type *type,
1592 const char *name,
1593 struct obstack *obstack,
12df843f 1594 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1595 const gdb_byte **bytes,
98bfdba5 1596 struct dwarf2_locexpr_baton **baton);
2df3850c 1597
e7c27a73 1598static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1599
b4ba55a1
JB
1600static int need_gnat_info (struct dwarf2_cu *);
1601
3e43a32a
MS
1602static struct type *die_descriptive_type (struct die_info *,
1603 struct dwarf2_cu *);
b4ba55a1
JB
1604
1605static void set_descriptive_type (struct type *, struct die_info *,
1606 struct dwarf2_cu *);
1607
e7c27a73
DJ
1608static struct type *die_containing_type (struct die_info *,
1609 struct dwarf2_cu *);
c906108c 1610
ff39bb5e 1611static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1612 struct dwarf2_cu *);
c906108c 1613
f792889a 1614static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1615
673bfd45
DE
1616static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1617
0d5cff50 1618static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1619
6e70227d 1620static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1621 const char *suffix, int physname,
1622 struct dwarf2_cu *cu);
63d06c5c 1623
e7c27a73 1624static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1625
348e048f
DE
1626static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1627
e7c27a73 1628static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1629
e7c27a73 1630static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1631
96408a79
SA
1632static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1633
71a3c369
TT
1634static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1635
ff013f42
JK
1636static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1637 struct dwarf2_cu *, struct partial_symtab *);
1638
3a2b436a 1639/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1640 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1641enum pc_bounds_kind
1642{
e385593e 1643 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1644 PC_BOUNDS_NOT_PRESENT,
1645
e385593e
JK
1646 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1647 were present but they do not form a valid range of PC addresses. */
1648 PC_BOUNDS_INVALID,
1649
3a2b436a
JK
1650 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1651 PC_BOUNDS_RANGES,
1652
1653 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1654 PC_BOUNDS_HIGH_LOW,
1655};
1656
1657static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1658 CORE_ADDR *, CORE_ADDR *,
1659 struct dwarf2_cu *,
1660 struct partial_symtab *);
c906108c 1661
fae299cd
DC
1662static void get_scope_pc_bounds (struct die_info *,
1663 CORE_ADDR *, CORE_ADDR *,
1664 struct dwarf2_cu *);
1665
801e3a5b
JB
1666static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1667 CORE_ADDR, struct dwarf2_cu *);
1668
a14ed312 1669static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1670 struct dwarf2_cu *);
c906108c 1671
a14ed312 1672static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1673 struct type *, struct dwarf2_cu *);
c906108c 1674
a14ed312 1675static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1676 struct die_info *, struct type *,
e7c27a73 1677 struct dwarf2_cu *);
c906108c 1678
a14ed312 1679static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1680 struct type *,
1681 struct dwarf2_cu *);
c906108c 1682
134d01f1 1683static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1684
e7c27a73 1685static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1686
e7c27a73 1687static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1688
5d7cb8df
JK
1689static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1690
804d2729 1691static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1692
27aa8d6a
SW
1693static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1694
74921315
KS
1695static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1696
f55ee35c
JK
1697static struct type *read_module_type (struct die_info *die,
1698 struct dwarf2_cu *cu);
1699
38d518c9 1700static const char *namespace_name (struct die_info *die,
e142c38c 1701 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1702
134d01f1 1703static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1704
e7c27a73 1705static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1706
6e70227d 1707static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1708 struct dwarf2_cu *);
1709
bf6af496 1710static struct die_info *read_die_and_siblings_1
d521ce57 1711 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1712 struct die_info *);
639d11d3 1713
dee91e82 1714static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1715 const gdb_byte *info_ptr,
1716 const gdb_byte **new_info_ptr,
639d11d3
DC
1717 struct die_info *parent);
1718
d521ce57
TT
1719static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1720 struct die_info **, const gdb_byte *,
1721 int *, int);
3019eac3 1722
d521ce57
TT
1723static const gdb_byte *read_full_die (const struct die_reader_specs *,
1724 struct die_info **, const gdb_byte *,
1725 int *);
93311388 1726
e7c27a73 1727static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1728
15d034d0
TT
1729static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1730 struct obstack *);
71c25dea 1731
15d034d0 1732static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1733
15d034d0 1734static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1735 struct die_info *die,
1736 struct dwarf2_cu *cu);
1737
ca69b9e6
DE
1738static const char *dwarf2_physname (const char *name, struct die_info *die,
1739 struct dwarf2_cu *cu);
1740
e142c38c 1741static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1742 struct dwarf2_cu **);
9219021c 1743
f39c6ffd 1744static const char *dwarf_tag_name (unsigned int);
c906108c 1745
f39c6ffd 1746static const char *dwarf_attr_name (unsigned int);
c906108c 1747
f39c6ffd 1748static const char *dwarf_form_name (unsigned int);
c906108c 1749
a121b7c1 1750static const char *dwarf_bool_name (unsigned int);
c906108c 1751
f39c6ffd 1752static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1753
f9aca02d 1754static struct die_info *sibling_die (struct die_info *);
c906108c 1755
d97bc12b
DE
1756static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1757
1758static void dump_die_for_error (struct die_info *);
1759
1760static void dump_die_1 (struct ui_file *, int level, int max_level,
1761 struct die_info *);
c906108c 1762
d97bc12b 1763/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1764
51545339 1765static void store_in_ref_table (struct die_info *,
10b3939b 1766 struct dwarf2_cu *);
c906108c 1767
ff39bb5e 1768static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1769
ff39bb5e 1770static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1771
348e048f 1772static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1773 const struct attribute *,
348e048f
DE
1774 struct dwarf2_cu **);
1775
10b3939b 1776static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1777 const struct attribute *,
f2f0e013 1778 struct dwarf2_cu **);
c906108c 1779
348e048f 1780static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1781 const struct attribute *,
348e048f
DE
1782 struct dwarf2_cu **);
1783
ac9ec31b
DE
1784static struct type *get_signatured_type (struct die_info *, ULONGEST,
1785 struct dwarf2_cu *);
1786
1787static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1788 const struct attribute *,
ac9ec31b
DE
1789 struct dwarf2_cu *);
1790
e5fe5e75 1791static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1792
52dc124a 1793static void read_signatured_type (struct signatured_type *);
348e048f 1794
63e43d3a
PMR
1795static int attr_to_dynamic_prop (const struct attribute *attr,
1796 struct die_info *die, struct dwarf2_cu *cu,
1797 struct dynamic_prop *prop);
1798
c906108c
SS
1799/* memory allocation interface */
1800
7b5a2f43 1801static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1802
b60c80d6 1803static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1804
43f3e411 1805static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1806
6e5a29e1 1807static int attr_form_is_block (const struct attribute *);
8e19ed76 1808
6e5a29e1 1809static int attr_form_is_section_offset (const struct attribute *);
3690dd37 1810
6e5a29e1 1811static int attr_form_is_constant (const struct attribute *);
3690dd37 1812
6e5a29e1 1813static int attr_form_is_ref (const struct attribute *);
7771576e 1814
8cf6f0b1
TT
1815static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1816 struct dwarf2_loclist_baton *baton,
ff39bb5e 1817 const struct attribute *attr);
8cf6f0b1 1818
ff39bb5e 1819static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1820 struct symbol *sym,
f1e6e072
TT
1821 struct dwarf2_cu *cu,
1822 int is_block);
4c2df51b 1823
d521ce57
TT
1824static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1825 const gdb_byte *info_ptr,
1826 struct abbrev_info *abbrev);
4bb7a0a7 1827
72bf9492
DJ
1828static hashval_t partial_die_hash (const void *item);
1829
1830static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1831
ae038cb0 1832static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618
SM
1833 (sect_offset sect_off, unsigned int offset_in_dwz,
1834 struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1835
9816fde3 1836static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1837 struct die_info *comp_unit_die,
1838 enum language pretend_language);
93311388 1839
ed2dc618 1840static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1841
dee91e82 1842static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1843
f792889a
DJ
1844static struct type *set_die_type (struct die_info *, struct type *,
1845 struct dwarf2_cu *);
1c379e20 1846
ed2dc618 1847static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1848
ed2dc618 1849static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 1850
58f0c718 1851static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
95554aad 1852 enum language);
10b3939b 1853
95554aad
TT
1854static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1855 enum language);
10b3939b 1856
f4dc4d17
DE
1857static void process_full_type_unit (struct dwarf2_per_cu_data *,
1858 enum language);
1859
10b3939b
DJ
1860static void dwarf2_add_dependence (struct dwarf2_cu *,
1861 struct dwarf2_per_cu_data *);
1862
ae038cb0
DJ
1863static void dwarf2_mark (struct dwarf2_cu *);
1864
1865static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1866
b64f50a1 1867static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1868 struct dwarf2_per_cu_data *);
673bfd45 1869
f792889a 1870static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1871
95554aad
TT
1872static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1873 enum language pretend_language);
1874
ed2dc618 1875static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 1876
b303c6f6
AB
1877/* Class, the destructor of which frees all allocated queue entries. This
1878 will only have work to do if an error was thrown while processing the
1879 dwarf. If no error was thrown then the queue entries should have all
1880 been processed, and freed, as we went along. */
1881
1882class dwarf2_queue_guard
1883{
1884public:
1885 dwarf2_queue_guard () = default;
1886
1887 /* Free any entries remaining on the queue. There should only be
1888 entries left if we hit an error while processing the dwarf. */
1889 ~dwarf2_queue_guard ()
1890 {
1891 struct dwarf2_queue_item *item, *last;
1892
1893 item = dwarf2_queue;
1894 while (item)
1895 {
1896 /* Anything still marked queued is likely to be in an
1897 inconsistent state, so discard it. */
1898 if (item->per_cu->queued)
1899 {
1900 if (item->per_cu->cu != NULL)
1901 free_one_cached_comp_unit (item->per_cu);
1902 item->per_cu->queued = 0;
1903 }
1904
1905 last = item;
1906 item = item->next;
1907 xfree (last);
1908 }
1909
1910 dwarf2_queue = dwarf2_queue_tail = NULL;
1911 }
1912};
1913
d721ba37
PA
1914/* The return type of find_file_and_directory. Note, the enclosed
1915 string pointers are only valid while this object is valid. */
1916
1917struct file_and_directory
1918{
1919 /* The filename. This is never NULL. */
1920 const char *name;
1921
1922 /* The compilation directory. NULL if not known. If we needed to
1923 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1924 points directly to the DW_AT_comp_dir string attribute owned by
1925 the obstack that owns the DIE. */
1926 const char *comp_dir;
1927
1928 /* If we needed to build a new string for comp_dir, this is what
1929 owns the storage. */
1930 std::string comp_dir_storage;
1931};
1932
1933static file_and_directory find_file_and_directory (struct die_info *die,
1934 struct dwarf2_cu *cu);
9291a0cd
TT
1935
1936static char *file_full_name (int file, struct line_header *lh,
1937 const char *comp_dir);
1938
43988095
JK
1939/* Expected enum dwarf_unit_type for read_comp_unit_head. */
1940enum class rcuh_kind { COMPILE, TYPE };
1941
d521ce57 1942static const gdb_byte *read_and_check_comp_unit_head
ed2dc618
SM
1943 (struct dwarf2_per_objfile* dwarf2_per_objfile,
1944 struct comp_unit_head *header,
36586728 1945 struct dwarf2_section_info *section,
d521ce57 1946 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
43988095 1947 rcuh_kind section_kind);
36586728 1948
fd820528 1949static void init_cutu_and_read_dies
f4dc4d17 1950 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
58f0c718 1951 int use_existing_cu, int keep, bool skip_partial,
3019eac3
DE
1952 die_reader_func_ftype *die_reader_func, void *data);
1953
dee91e82
DE
1954static void init_cutu_and_read_dies_simple
1955 (struct dwarf2_per_cu_data *this_cu,
1956 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1957
673bfd45 1958static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1959
3019eac3
DE
1960static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1961
57d63ce2 1962static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
1963 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1964 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 1965 ULONGEST signature, int is_debug_types);
a2ce51a0 1966
ed2dc618
SM
1967static struct dwp_file *get_dwp_file
1968 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 1969
3019eac3 1970static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1971 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1972
1973static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1974 (struct signatured_type *, const char *, const char *);
3019eac3 1975
89e63ee4
DE
1976static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1977
263db9a1 1978static void free_dwo_file (struct dwo_file *);
3019eac3 1979
263db9a1
TT
1980/* A unique_ptr helper to free a dwo_file. */
1981
1982struct dwo_file_deleter
ed2dc618 1983{
263db9a1
TT
1984 void operator() (struct dwo_file *df) const
1985 {
1986 free_dwo_file (df);
1987 }
ed2dc618
SM
1988};
1989
263db9a1
TT
1990/* A unique pointer to a dwo_file. */
1991
1992typedef std::unique_ptr<struct dwo_file, dwo_file_deleter> dwo_file_up;
1993
ed2dc618 1994static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 1995
1b80a9fa 1996static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1997
1998static void free_line_header_voidp (void *arg);
4390d890
DE
1999\f
2000/* Various complaints about symbol reading that don't abort the process. */
2001
2002static void
2003dwarf2_statement_list_fits_in_line_number_section_complaint (void)
2004{
b98664d3 2005 complaint (_("statement list doesn't fit in .debug_line section"));
4390d890
DE
2006}
2007
2008static void
2009dwarf2_debug_line_missing_file_complaint (void)
2010{
b98664d3 2011 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
2012}
2013
2014static void
2015dwarf2_debug_line_missing_end_sequence_complaint (void)
2016{
b98664d3 2017 complaint (_(".debug_line section has line "
4390d890
DE
2018 "program sequence without an end"));
2019}
2020
2021static void
2022dwarf2_complex_location_expr_complaint (void)
2023{
b98664d3 2024 complaint (_("location expression too complex"));
4390d890
DE
2025}
2026
2027static void
2028dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2029 int arg3)
2030{
b98664d3 2031 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
2032 arg1, arg2, arg3);
2033}
2034
2035static void
2036dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2037{
b98664d3 2038 complaint (_("debug info runs off end of %s section"
4390d890 2039 " [in module %s]"),
a32a8923
DE
2040 get_section_name (section),
2041 get_section_file_name (section));
4390d890 2042}
1b80a9fa 2043
4390d890
DE
2044static void
2045dwarf2_macro_malformed_definition_complaint (const char *arg1)
2046{
b98664d3 2047 complaint (_("macro debug info contains a "
4390d890
DE
2048 "malformed macro definition:\n`%s'"),
2049 arg1);
2050}
2051
2052static void
2053dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2054{
b98664d3 2055 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
2056 arg1, arg2);
2057}
527f3840
JK
2058
2059/* Hash function for line_header_hash. */
2060
2061static hashval_t
2062line_header_hash (const struct line_header *ofs)
2063{
9c541725 2064 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
2065}
2066
2067/* Hash function for htab_create_alloc_ex for line_header_hash. */
2068
2069static hashval_t
2070line_header_hash_voidp (const void *item)
2071{
9a3c8263 2072 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
2073
2074 return line_header_hash (ofs);
2075}
2076
2077/* Equality function for line_header_hash. */
2078
2079static int
2080line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2081{
9a3c8263
SM
2082 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2083 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 2084
9c541725 2085 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
2086 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2087}
2088
4390d890 2089\f
9291a0cd 2090
31aa7e4e
JB
2091/* Read the given attribute value as an address, taking the attribute's
2092 form into account. */
2093
2094static CORE_ADDR
2095attr_value_as_address (struct attribute *attr)
2096{
2097 CORE_ADDR addr;
2098
2099 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2100 {
2101 /* Aside from a few clearly defined exceptions, attributes that
2102 contain an address must always be in DW_FORM_addr form.
2103 Unfortunately, some compilers happen to be violating this
2104 requirement by encoding addresses using other forms, such
2105 as DW_FORM_data4 for example. For those broken compilers,
2106 we try to do our best, without any guarantee of success,
2107 to interpret the address correctly. It would also be nice
2108 to generate a complaint, but that would require us to maintain
2109 a list of legitimate cases where a non-address form is allowed,
2110 as well as update callers to pass in at least the CU's DWARF
2111 version. This is more overhead than what we're willing to
2112 expand for a pretty rare case. */
2113 addr = DW_UNSND (attr);
2114 }
2115 else
2116 addr = DW_ADDR (attr);
2117
2118 return addr;
2119}
2120
330cdd98
PA
2121/* See declaration. */
2122
2123dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2124 const dwarf2_debug_sections *names)
2125 : objfile (objfile_)
2126{
2127 if (names == NULL)
2128 names = &dwarf2_elf_names;
2129
2130 bfd *obfd = objfile->obfd;
2131
2132 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2133 locate_sections (obfd, sec, *names);
2134}
2135
fc8e7e75
SM
2136static void free_dwo_files (htab_t dwo_files, struct objfile *objfile);
2137
330cdd98
PA
2138dwarf2_per_objfile::~dwarf2_per_objfile ()
2139{
2140 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2141 free_cached_comp_units ();
2142
2143 if (quick_file_names_table)
2144 htab_delete (quick_file_names_table);
2145
2146 if (line_header_hash)
2147 htab_delete (line_header_hash);
2148
b76e467d
SM
2149 for (dwarf2_per_cu_data *per_cu : all_comp_units)
2150 VEC_free (dwarf2_per_cu_ptr, per_cu->imported_symtabs);
fc8e7e75 2151
b2bdb8cf
SM
2152 for (signatured_type *sig_type : all_type_units)
2153 VEC_free (dwarf2_per_cu_ptr, sig_type->per_cu.imported_symtabs);
fc8e7e75
SM
2154
2155 VEC_free (dwarf2_section_info_def, types);
2156
2157 if (dwo_files != NULL)
2158 free_dwo_files (dwo_files, objfile);
fc8e7e75 2159
330cdd98
PA
2160 /* Everything else should be on the objfile obstack. */
2161}
2162
2163/* See declaration. */
2164
2165void
2166dwarf2_per_objfile::free_cached_comp_units ()
2167{
2168 dwarf2_per_cu_data *per_cu = read_in_chain;
2169 dwarf2_per_cu_data **last_chain = &read_in_chain;
2170 while (per_cu != NULL)
2171 {
2172 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2173
fcd3b13d 2174 delete per_cu->cu;
330cdd98
PA
2175 *last_chain = next_cu;
2176 per_cu = next_cu;
2177 }
2178}
2179
11ed8cad
TT
2180/* A helper class that calls free_cached_comp_units on
2181 destruction. */
2182
2183class free_cached_comp_units
2184{
2185public:
2186
2187 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2188 : m_per_objfile (per_objfile)
2189 {
2190 }
2191
2192 ~free_cached_comp_units ()
2193 {
2194 m_per_objfile->free_cached_comp_units ();
2195 }
2196
2197 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2198
2199private:
2200
2201 dwarf2_per_objfile *m_per_objfile;
2202};
2203
c906108c 2204/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
2205 information and return true if we have enough to do something.
2206 NAMES points to the dwarf2 section names, or is NULL if the standard
2207 ELF names are used. */
c906108c
SS
2208
2209int
251d32d9
TG
2210dwarf2_has_info (struct objfile *objfile,
2211 const struct dwarf2_debug_sections *names)
c906108c 2212{
97cbe998
SDJ
2213 if (objfile->flags & OBJF_READNEVER)
2214 return 0;
2215
ed2dc618
SM
2216 struct dwarf2_per_objfile *dwarf2_per_objfile
2217 = get_dwarf2_per_objfile (objfile);
2218
2219 if (dwarf2_per_objfile == NULL)
be391dca
TT
2220 {
2221 /* Initialize per-objfile state. */
fd90ace4
YQ
2222 dwarf2_per_objfile
2223 = new (&objfile->objfile_obstack) struct dwarf2_per_objfile (objfile,
2224 names);
ed2dc618 2225 set_dwarf2_per_objfile (objfile, dwarf2_per_objfile);
be391dca 2226 }
73869dc2 2227 return (!dwarf2_per_objfile->info.is_virtual
049412e3 2228 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 2229 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 2230 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
2231}
2232
2233/* Return the containing section of virtual section SECTION. */
2234
2235static struct dwarf2_section_info *
2236get_containing_section (const struct dwarf2_section_info *section)
2237{
2238 gdb_assert (section->is_virtual);
2239 return section->s.containing_section;
c906108c
SS
2240}
2241
a32a8923
DE
2242/* Return the bfd owner of SECTION. */
2243
2244static struct bfd *
2245get_section_bfd_owner (const struct dwarf2_section_info *section)
2246{
73869dc2
DE
2247 if (section->is_virtual)
2248 {
2249 section = get_containing_section (section);
2250 gdb_assert (!section->is_virtual);
2251 }
049412e3 2252 return section->s.section->owner;
a32a8923
DE
2253}
2254
2255/* Return the bfd section of SECTION.
2256 Returns NULL if the section is not present. */
2257
2258static asection *
2259get_section_bfd_section (const struct dwarf2_section_info *section)
2260{
73869dc2
DE
2261 if (section->is_virtual)
2262 {
2263 section = get_containing_section (section);
2264 gdb_assert (!section->is_virtual);
2265 }
049412e3 2266 return section->s.section;
a32a8923
DE
2267}
2268
2269/* Return the name of SECTION. */
2270
2271static const char *
2272get_section_name (const struct dwarf2_section_info *section)
2273{
2274 asection *sectp = get_section_bfd_section (section);
2275
2276 gdb_assert (sectp != NULL);
2277 return bfd_section_name (get_section_bfd_owner (section), sectp);
2278}
2279
2280/* Return the name of the file SECTION is in. */
2281
2282static const char *
2283get_section_file_name (const struct dwarf2_section_info *section)
2284{
2285 bfd *abfd = get_section_bfd_owner (section);
2286
2287 return bfd_get_filename (abfd);
2288}
2289
2290/* Return the id of SECTION.
2291 Returns 0 if SECTION doesn't exist. */
2292
2293static int
2294get_section_id (const struct dwarf2_section_info *section)
2295{
2296 asection *sectp = get_section_bfd_section (section);
2297
2298 if (sectp == NULL)
2299 return 0;
2300 return sectp->id;
2301}
2302
2303/* Return the flags of SECTION.
73869dc2 2304 SECTION (or containing section if this is a virtual section) must exist. */
a32a8923
DE
2305
2306static int
2307get_section_flags (const struct dwarf2_section_info *section)
2308{
2309 asection *sectp = get_section_bfd_section (section);
2310
2311 gdb_assert (sectp != NULL);
2312 return bfd_get_section_flags (sectp->owner, sectp);
2313}
2314
251d32d9
TG
2315/* When loading sections, we look either for uncompressed section or for
2316 compressed section names. */
233a11ab
CS
2317
2318static int
251d32d9
TG
2319section_is_p (const char *section_name,
2320 const struct dwarf2_section_names *names)
233a11ab 2321{
251d32d9
TG
2322 if (names->normal != NULL
2323 && strcmp (section_name, names->normal) == 0)
2324 return 1;
2325 if (names->compressed != NULL
2326 && strcmp (section_name, names->compressed) == 0)
2327 return 1;
2328 return 0;
233a11ab
CS
2329}
2330
330cdd98 2331/* See declaration. */
c906108c 2332
330cdd98
PA
2333void
2334dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2335 const dwarf2_debug_sections &names)
c906108c 2336{
dc7650b8 2337 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9 2338
dc7650b8
JK
2339 if ((aflag & SEC_HAS_CONTENTS) == 0)
2340 {
2341 }
330cdd98 2342 else if (section_is_p (sectp->name, &names.info))
c906108c 2343 {
330cdd98
PA
2344 this->info.s.section = sectp;
2345 this->info.size = bfd_get_section_size (sectp);
c906108c 2346 }
330cdd98 2347 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 2348 {
330cdd98
PA
2349 this->abbrev.s.section = sectp;
2350 this->abbrev.size = bfd_get_section_size (sectp);
c906108c 2351 }
330cdd98 2352 else if (section_is_p (sectp->name, &names.line))
c906108c 2353 {
330cdd98
PA
2354 this->line.s.section = sectp;
2355 this->line.size = bfd_get_section_size (sectp);
c906108c 2356 }
330cdd98 2357 else if (section_is_p (sectp->name, &names.loc))
c906108c 2358 {
330cdd98
PA
2359 this->loc.s.section = sectp;
2360 this->loc.size = bfd_get_section_size (sectp);
c906108c 2361 }
330cdd98 2362 else if (section_is_p (sectp->name, &names.loclists))
43988095 2363 {
330cdd98
PA
2364 this->loclists.s.section = sectp;
2365 this->loclists.size = bfd_get_section_size (sectp);
43988095 2366 }
330cdd98 2367 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 2368 {
330cdd98
PA
2369 this->macinfo.s.section = sectp;
2370 this->macinfo.size = bfd_get_section_size (sectp);
c906108c 2371 }
330cdd98 2372 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 2373 {
330cdd98
PA
2374 this->macro.s.section = sectp;
2375 this->macro.size = bfd_get_section_size (sectp);
cf2c3c16 2376 }
330cdd98 2377 else if (section_is_p (sectp->name, &names.str))
c906108c 2378 {
330cdd98
PA
2379 this->str.s.section = sectp;
2380 this->str.size = bfd_get_section_size (sectp);
c906108c 2381 }
330cdd98 2382 else if (section_is_p (sectp->name, &names.line_str))
43988095 2383 {
330cdd98
PA
2384 this->line_str.s.section = sectp;
2385 this->line_str.size = bfd_get_section_size (sectp);
43988095 2386 }
330cdd98 2387 else if (section_is_p (sectp->name, &names.addr))
3019eac3 2388 {
330cdd98
PA
2389 this->addr.s.section = sectp;
2390 this->addr.size = bfd_get_section_size (sectp);
3019eac3 2391 }
330cdd98 2392 else if (section_is_p (sectp->name, &names.frame))
b6af0555 2393 {
330cdd98
PA
2394 this->frame.s.section = sectp;
2395 this->frame.size = bfd_get_section_size (sectp);
b6af0555 2396 }
330cdd98 2397 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2398 {
330cdd98
PA
2399 this->eh_frame.s.section = sectp;
2400 this->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 2401 }
330cdd98 2402 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2403 {
330cdd98
PA
2404 this->ranges.s.section = sectp;
2405 this->ranges.size = bfd_get_section_size (sectp);
af34e669 2406 }
330cdd98 2407 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2408 {
330cdd98
PA
2409 this->rnglists.s.section = sectp;
2410 this->rnglists.size = bfd_get_section_size (sectp);
43988095 2411 }
330cdd98 2412 else if (section_is_p (sectp->name, &names.types))
348e048f 2413 {
8b70b953
TT
2414 struct dwarf2_section_info type_section;
2415
2416 memset (&type_section, 0, sizeof (type_section));
049412e3 2417 type_section.s.section = sectp;
8b70b953
TT
2418 type_section.size = bfd_get_section_size (sectp);
2419
330cdd98 2420 VEC_safe_push (dwarf2_section_info_def, this->types,
8b70b953 2421 &type_section);
348e048f 2422 }
330cdd98 2423 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2424 {
330cdd98
PA
2425 this->gdb_index.s.section = sectp;
2426 this->gdb_index.size = bfd_get_section_size (sectp);
9291a0cd 2427 }
927aa2e7
JK
2428 else if (section_is_p (sectp->name, &names.debug_names))
2429 {
2430 this->debug_names.s.section = sectp;
2431 this->debug_names.size = bfd_get_section_size (sectp);
2432 }
2433 else if (section_is_p (sectp->name, &names.debug_aranges))
2434 {
2435 this->debug_aranges.s.section = sectp;
2436 this->debug_aranges.size = bfd_get_section_size (sectp);
2437 }
dce234bc 2438
b4e1fd61 2439 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
72dca2f5 2440 && bfd_section_vma (abfd, sectp) == 0)
330cdd98 2441 this->has_section_at_zero = true;
c906108c
SS
2442}
2443
fceca515
DE
2444/* A helper function that decides whether a section is empty,
2445 or not present. */
9e0ac564
TT
2446
2447static int
19ac8c2e 2448dwarf2_section_empty_p (const struct dwarf2_section_info *section)
9e0ac564 2449{
73869dc2
DE
2450 if (section->is_virtual)
2451 return section->size == 0;
049412e3 2452 return section->s.section == NULL || section->size == 0;
9e0ac564
TT
2453}
2454
cd4fb1b2 2455/* See dwarf2read.h. */
c906108c 2456
cd4fb1b2
SM
2457void
2458dwarf2_read_section (struct objfile *objfile, dwarf2_section_info *info)
c906108c 2459{
a32a8923 2460 asection *sectp;
3019eac3 2461 bfd *abfd;
dce234bc 2462 gdb_byte *buf, *retbuf;
c906108c 2463
be391dca
TT
2464 if (info->readin)
2465 return;
dce234bc 2466 info->buffer = NULL;
be391dca 2467 info->readin = 1;
188dd5d6 2468
9e0ac564 2469 if (dwarf2_section_empty_p (info))
dce234bc 2470 return;
c906108c 2471
a32a8923 2472 sectp = get_section_bfd_section (info);
3019eac3 2473
73869dc2
DE
2474 /* If this is a virtual section we need to read in the real one first. */
2475 if (info->is_virtual)
2476 {
2477 struct dwarf2_section_info *containing_section =
2478 get_containing_section (info);
2479
2480 gdb_assert (sectp != NULL);
2481 if ((sectp->flags & SEC_RELOC) != 0)
2482 {
2483 error (_("Dwarf Error: DWP format V2 with relocations is not"
2484 " supported in section %s [in module %s]"),
2485 get_section_name (info), get_section_file_name (info));
2486 }
2487 dwarf2_read_section (objfile, containing_section);
2488 /* Other code should have already caught virtual sections that don't
2489 fit. */
2490 gdb_assert (info->virtual_offset + info->size
2491 <= containing_section->size);
2492 /* If the real section is empty or there was a problem reading the
2493 section we shouldn't get here. */
2494 gdb_assert (containing_section->buffer != NULL);
2495 info->buffer = containing_section->buffer + info->virtual_offset;
2496 return;
2497 }
2498
4bf44c1c
TT
2499 /* If the section has relocations, we must read it ourselves.
2500 Otherwise we attach it to the BFD. */
2501 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 2502 {
d521ce57 2503 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 2504 return;
dce234bc 2505 }
dce234bc 2506
224c3ddb 2507 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
4bf44c1c 2508 info->buffer = buf;
dce234bc
PP
2509
2510 /* When debugging .o files, we may need to apply relocations; see
2511 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2512 We never compress sections in .o files, so we only need to
2513 try this when the section is not compressed. */
ac8035ab 2514 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2515 if (retbuf != NULL)
2516 {
2517 info->buffer = retbuf;
2518 return;
2519 }
2520
a32a8923
DE
2521 abfd = get_section_bfd_owner (info);
2522 gdb_assert (abfd != NULL);
2523
dce234bc
PP
2524 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2525 || bfd_bread (buf, info->size, abfd) != info->size)
19ac8c2e
DE
2526 {
2527 error (_("Dwarf Error: Can't read DWARF data"
2528 " in section %s [in module %s]"),
2529 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2530 }
dce234bc
PP
2531}
2532
9e0ac564
TT
2533/* A helper function that returns the size of a section in a safe way.
2534 If you are positive that the section has been read before using the
2535 size, then it is safe to refer to the dwarf2_section_info object's
2536 "size" field directly. In other cases, you must call this
2537 function, because for compressed sections the size field is not set
2538 correctly until the section has been read. */
2539
2540static bfd_size_type
2541dwarf2_section_size (struct objfile *objfile,
2542 struct dwarf2_section_info *info)
2543{
2544 if (!info->readin)
2545 dwarf2_read_section (objfile, info);
2546 return info->size;
2547}
2548
dce234bc 2549/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2550 SECTION_NAME. */
af34e669 2551
dce234bc 2552void
3017a003
TG
2553dwarf2_get_section_info (struct objfile *objfile,
2554 enum dwarf2_section_enum sect,
d521ce57 2555 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2556 bfd_size_type *sizep)
2557{
2558 struct dwarf2_per_objfile *data
9a3c8263
SM
2559 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2560 dwarf2_objfile_data_key);
dce234bc 2561 struct dwarf2_section_info *info;
a3b2a86b
TT
2562
2563 /* We may see an objfile without any DWARF, in which case we just
2564 return nothing. */
2565 if (data == NULL)
2566 {
2567 *sectp = NULL;
2568 *bufp = NULL;
2569 *sizep = 0;
2570 return;
2571 }
3017a003
TG
2572 switch (sect)
2573 {
2574 case DWARF2_DEBUG_FRAME:
2575 info = &data->frame;
2576 break;
2577 case DWARF2_EH_FRAME:
2578 info = &data->eh_frame;
2579 break;
2580 default:
2581 gdb_assert_not_reached ("unexpected section");
2582 }
dce234bc 2583
9e0ac564 2584 dwarf2_read_section (objfile, info);
dce234bc 2585
a32a8923 2586 *sectp = get_section_bfd_section (info);
dce234bc
PP
2587 *bufp = info->buffer;
2588 *sizep = info->size;
2589}
2590
36586728
TT
2591/* A helper function to find the sections for a .dwz file. */
2592
2593static void
2594locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2595{
9a3c8263 2596 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2597
2598 /* Note that we only support the standard ELF names, because .dwz
2599 is ELF-only (at the time of writing). */
2600 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2601 {
049412e3 2602 dwz_file->abbrev.s.section = sectp;
36586728
TT
2603 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2604 }
2605 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2606 {
049412e3 2607 dwz_file->info.s.section = sectp;
36586728
TT
2608 dwz_file->info.size = bfd_get_section_size (sectp);
2609 }
2610 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2611 {
049412e3 2612 dwz_file->str.s.section = sectp;
36586728
TT
2613 dwz_file->str.size = bfd_get_section_size (sectp);
2614 }
2615 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2616 {
049412e3 2617 dwz_file->line.s.section = sectp;
36586728
TT
2618 dwz_file->line.size = bfd_get_section_size (sectp);
2619 }
2620 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2621 {
049412e3 2622 dwz_file->macro.s.section = sectp;
36586728
TT
2623 dwz_file->macro.size = bfd_get_section_size (sectp);
2624 }
2ec9a5e0
TT
2625 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2626 {
049412e3 2627 dwz_file->gdb_index.s.section = sectp;
2ec9a5e0
TT
2628 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2629 }
927aa2e7
JK
2630 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2631 {
2632 dwz_file->debug_names.s.section = sectp;
2633 dwz_file->debug_names.size = bfd_get_section_size (sectp);
2634 }
36586728
TT
2635}
2636
4db1a1dc
TT
2637/* Open the separate '.dwz' debug file, if needed. Return NULL if
2638 there is no .gnu_debugaltlink section in the file. Error if there
2639 is such a section but the file cannot be found. */
36586728
TT
2640
2641static struct 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
TT
2685 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2686 dwz_bfd.release ();
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
3186 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3187 &objfile->objfile_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 {
3243 warning (_("Section .debug_aranges in %s entry at offset %zu "
3244 "length %s exceeds section length %s, "
3245 "ignoring .debug_aranges."),
3246 objfile_name (objfile), entry_addr - section->buffer,
3247 plongest (bytes_read + entry_length),
3248 pulongest (section->size));
3249 return;
3250 }
3251
3252 /* The version number. */
3253 const uint16_t version = read_2_bytes (abfd, addr);
3254 addr += 2;
3255 if (version != 2)
3256 {
3257 warning (_("Section .debug_aranges in %s entry at offset %zu "
3258 "has unsupported version %d, ignoring .debug_aranges."),
3259 objfile_name (objfile), entry_addr - section->buffer,
3260 version);
3261 return;
3262 }
3263
3264 const uint64_t debug_info_offset
3265 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3266 addr += offset_size;
3267 const auto per_cu_it
3268 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3269 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3270 {
3271 warning (_("Section .debug_aranges in %s entry at offset %zu "
3272 "debug_info_offset %s does not exists, "
3273 "ignoring .debug_aranges."),
3274 objfile_name (objfile), entry_addr - section->buffer,
3275 pulongest (debug_info_offset));
3276 return;
3277 }
3278 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3279
3280 const uint8_t address_size = *addr++;
3281 if (address_size < 1 || address_size > 8)
3282 {
3283 warning (_("Section .debug_aranges in %s entry at offset %zu "
3284 "address_size %u is invalid, ignoring .debug_aranges."),
3285 objfile_name (objfile), entry_addr - section->buffer,
3286 address_size);
3287 return;
3288 }
3289
3290 const uint8_t segment_selector_size = *addr++;
3291 if (segment_selector_size != 0)
3292 {
3293 warning (_("Section .debug_aranges in %s entry at offset %zu "
3294 "segment_selector_size %u is not supported, "
3295 "ignoring .debug_aranges."),
3296 objfile_name (objfile), entry_addr - section->buffer,
3297 segment_selector_size);
3298 return;
3299 }
3300
3301 /* Must pad to an alignment boundary that is twice the address
3302 size. It is undocumented by the DWARF standard but GCC does
3303 use it. */
3304 for (size_t padding = ((-(addr - section->buffer))
3305 & (2 * address_size - 1));
3306 padding > 0; padding--)
3307 if (*addr++ != 0)
3308 {
3309 warning (_("Section .debug_aranges in %s entry at offset %zu "
3310 "padding is not zero, ignoring .debug_aranges."),
3311 objfile_name (objfile), entry_addr - section->buffer);
3312 return;
3313 }
3314
3315 for (;;)
3316 {
3317 if (addr + 2 * address_size > entry_end)
3318 {
3319 warning (_("Section .debug_aranges in %s entry at offset %zu "
3320 "address list is not properly terminated, "
3321 "ignoring .debug_aranges."),
3322 objfile_name (objfile), entry_addr - section->buffer);
3323 return;
3324 }
3325 ULONGEST start = extract_unsigned_integer (addr, address_size,
3326 dwarf5_byte_order);
3327 addr += address_size;
3328 ULONGEST length = extract_unsigned_integer (addr, address_size,
3329 dwarf5_byte_order);
3330 addr += address_size;
3331 if (start == 0 && length == 0)
3332 break;
3333 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3334 {
3335 /* Symbol was eliminated due to a COMDAT group. */
3336 continue;
3337 }
3338 ULONGEST end = start + length;
79748972
TT
3339 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3340 - baseaddr);
3341 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3342 - baseaddr);
927aa2e7
JK
3343 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3344 }
3345 }
3346
3347 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3348 &objfile->objfile_obstack);
3349}
3350
9291a0cd
TT
3351/* Find a slot in the mapped index INDEX for the object named NAME.
3352 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
3353 constant pool and return true. If NAME cannot be found, return
3354 false. */
2fdf6df6 3355
109483d9 3356static bool
9291a0cd
TT
3357find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3358 offset_type **vec_out)
3359{
0cf03b49 3360 offset_type hash;
9291a0cd 3361 offset_type slot, step;
559a7a62 3362 int (*cmp) (const char *, const char *);
9291a0cd 3363
791afaa2 3364 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 3365 if (current_language->la_language == language_cplus
45280282
IB
3366 || current_language->la_language == language_fortran
3367 || current_language->la_language == language_d)
0cf03b49
JK
3368 {
3369 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3370 not contain any. */
a8719064 3371
72998fb3 3372 if (strchr (name, '(') != NULL)
0cf03b49 3373 {
109483d9 3374 without_params = cp_remove_params (name);
0cf03b49 3375
72998fb3 3376 if (without_params != NULL)
791afaa2 3377 name = without_params.get ();
0cf03b49
JK
3378 }
3379 }
3380
559a7a62 3381 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3382 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3383 simulate our NAME being searched is also lowercased. */
3384 hash = mapped_index_string_hash ((index->version == 4
3385 && case_sensitivity == case_sensitive_off
3386 ? 5 : index->version),
3387 name);
3388
f00a2de2
PA
3389 slot = hash & (index->symbol_table.size () - 1);
3390 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 3391 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3392
3393 for (;;)
3394 {
9291a0cd 3395 const char *str;
f00a2de2
PA
3396
3397 const auto &bucket = index->symbol_table[slot];
3398 if (bucket.name == 0 && bucket.vec == 0)
109483d9 3399 return false;
9291a0cd 3400
f00a2de2 3401 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 3402 if (!cmp (name, str))
9291a0cd
TT
3403 {
3404 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 3405 + MAYBE_SWAP (bucket.vec));
109483d9 3406 return true;
9291a0cd
TT
3407 }
3408
f00a2de2 3409 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
3410 }
3411}
3412
2ec9a5e0
TT
3413/* A helper function that reads the .gdb_index from SECTION and fills
3414 in MAP. FILENAME is the name of the file containing the section;
d33bc52e 3415 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
3416 ok to use deprecated sections.
3417
3418 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3419 out parameters that are filled in with information about the CU and
3420 TU lists in the section.
3421
3422 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 3423
d33bc52e 3424static bool
7b23e087
SM
3425read_gdb_index_from_section (struct objfile *objfile,
3426 const char *filename,
3427 bool deprecated_ok,
3428 struct dwarf2_section_info *section,
3429 struct mapped_index *map,
3430 const gdb_byte **cu_list,
3431 offset_type *cu_list_elements,
3432 const gdb_byte **types_list,
3433 offset_type *types_list_elements)
9291a0cd 3434{
948f8e3d 3435 const gdb_byte *addr;
2ec9a5e0 3436 offset_type version;
b3b272e1 3437 offset_type *metadata;
1fd400ff 3438 int i;
9291a0cd 3439
2ec9a5e0 3440 if (dwarf2_section_empty_p (section))
9291a0cd 3441 return 0;
82430852
JK
3442
3443 /* Older elfutils strip versions could keep the section in the main
3444 executable while splitting it for the separate debug info file. */
a32a8923 3445 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
82430852
JK
3446 return 0;
3447
2ec9a5e0 3448 dwarf2_read_section (objfile, section);
9291a0cd 3449
2ec9a5e0 3450 addr = section->buffer;
9291a0cd 3451 /* Version check. */
1fd400ff 3452 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3453 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3454 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3455 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3456 indices. */
831adc1f 3457 if (version < 4)
481860b3
GB
3458 {
3459 static int warning_printed = 0;
3460 if (!warning_printed)
3461 {
3462 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3463 filename);
481860b3
GB
3464 warning_printed = 1;
3465 }
3466 return 0;
3467 }
3468 /* Index version 4 uses a different hash function than index version
3469 5 and later.
3470
3471 Versions earlier than 6 did not emit psymbols for inlined
3472 functions. Using these files will cause GDB not to be able to
3473 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3474 indices unless the user has done
3475 "set use-deprecated-index-sections on". */
2ec9a5e0 3476 if (version < 6 && !deprecated_ok)
481860b3
GB
3477 {
3478 static int warning_printed = 0;
3479 if (!warning_printed)
3480 {
e615022a
DE
3481 warning (_("\
3482Skipping deprecated .gdb_index section in %s.\n\
3483Do \"set use-deprecated-index-sections on\" before the file is read\n\
3484to use the section anyway."),
2ec9a5e0 3485 filename);
481860b3
GB
3486 warning_printed = 1;
3487 }
3488 return 0;
3489 }
796a7ff8 3490 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3491 of the TU (for symbols coming from TUs),
3492 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3493 Plus gold-generated indices can have duplicate entries for global symbols,
3494 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3495 These are just performance bugs, and we can't distinguish gdb-generated
3496 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3497
481860b3 3498 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3499 longer backward compatible. */
796a7ff8 3500 if (version > 8)
594e8718 3501 return 0;
9291a0cd 3502
559a7a62 3503 map->version = version;
9291a0cd
TT
3504
3505 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
3506
3507 i = 0;
2ec9a5e0
TT
3508 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3509 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3510 / 8);
1fd400ff
TT
3511 ++i;
3512
2ec9a5e0
TT
3513 *types_list = addr + MAYBE_SWAP (metadata[i]);
3514 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3515 - MAYBE_SWAP (metadata[i]))
3516 / 8);
987d643c 3517 ++i;
1fd400ff 3518
f00a2de2
PA
3519 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3520 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3521 map->address_table
3522 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3523 ++i;
3524
f00a2de2
PA
3525 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3526 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3527 map->symbol_table
3528 = gdb::array_view<mapped_index::symbol_table_slot>
3529 ((mapped_index::symbol_table_slot *) symbol_table,
3530 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3531
f00a2de2 3532 ++i;
f9d83a0b 3533 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3534
2ec9a5e0
TT
3535 return 1;
3536}
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
7b23e087 3542dwarf2_read_gdb_index (struct dwarf2_per_objfile *dwarf2_per_objfile)
2ec9a5e0 3543{
2ec9a5e0
TT
3544 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3545 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3546 struct dwz_file *dwz;
12359b5e 3547 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ec9a5e0 3548
3063847f 3549 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
7b23e087
SM
3550 if (!read_gdb_index_from_section (objfile, objfile_name (objfile),
3551 use_deprecated_index_sections,
3552 &dwarf2_per_objfile->gdb_index, map.get (),
3553 &cu_list, &cu_list_elements,
3554 &types_list, &types_list_elements))
2ec9a5e0
TT
3555 return 0;
3556
0fefef59 3557 /* Don't use the index if it's empty. */
3063847f 3558 if (map->symbol_table.empty ())
0fefef59
DE
3559 return 0;
3560
2ec9a5e0
TT
3561 /* If there is a .dwz file, read it so we can get its CU list as
3562 well. */
ed2dc618 3563 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 3564 if (dwz != NULL)
2ec9a5e0 3565 {
2ec9a5e0
TT
3566 struct mapped_index dwz_map;
3567 const gdb_byte *dwz_types_ignore;
3568 offset_type dwz_types_elements_ignore;
3569
7b23e087
SM
3570 if (!read_gdb_index_from_section (objfile,
3571 bfd_get_filename (dwz->dwz_bfd), 1,
3572 &dwz->gdb_index, &dwz_map,
3573 &dwz_list, &dwz_list_elements,
3574 &dwz_types_ignore,
3575 &dwz_types_elements_ignore))
2ec9a5e0
TT
3576 {
3577 warning (_("could not read '.gdb_index' section from %s; skipping"),
3578 bfd_get_filename (dwz->dwz_bfd));
3579 return 0;
3580 }
3581 }
3582
12359b5e
SM
3583 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3584 dwz_list, dwz_list_elements);
1fd400ff 3585
8b70b953
TT
3586 if (types_list_elements)
3587 {
3588 struct dwarf2_section_info *section;
3589
3590 /* We can only handle a single .debug_types when we have an
3591 index. */
3592 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3593 return 0;
3594
3595 section = VEC_index (dwarf2_section_info_def,
3596 dwarf2_per_objfile->types, 0);
3597
12359b5e
SM
3598 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3599 types_list, types_list_elements);
8b70b953 3600 }
9291a0cd 3601
3063847f 3602 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
9291a0cd 3603
3063847f 3604 dwarf2_per_objfile->index_table = std::move (map);
9291a0cd 3605 dwarf2_per_objfile->using_index = 1;
7b9f3c50 3606 dwarf2_per_objfile->quick_file_names_table =
b76e467d 3607 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd
TT
3608
3609 return 1;
3610}
3611
dee91e82 3612/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3613
dee91e82
DE
3614static void
3615dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3616 const gdb_byte *info_ptr,
dee91e82
DE
3617 struct die_info *comp_unit_die,
3618 int has_children,
3619 void *data)
9291a0cd 3620{
dee91e82 3621 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3622 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
518817b3
SM
3623 struct dwarf2_per_objfile *dwarf2_per_objfile
3624 = cu->per_cu->dwarf2_per_objfile;
dee91e82 3625 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3626 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3627 struct attribute *attr;
dee91e82 3628 int i;
7b9f3c50
DE
3629 void **slot;
3630 struct quick_file_names *qfn;
9291a0cd 3631
0186c6a7
DE
3632 gdb_assert (! this_cu->is_debug_types);
3633
07261596
TT
3634 /* Our callers never want to match partial units -- instead they
3635 will match the enclosing full CU. */
3636 if (comp_unit_die->tag == DW_TAG_partial_unit)
3637 {
3638 this_cu->v.quick->no_file_data = 1;
3639 return;
3640 }
3641
0186c6a7 3642 lh_cu = this_cu;
7b9f3c50 3643 slot = NULL;
dee91e82 3644
fff8551c 3645 line_header_up lh;
9c541725 3646 sect_offset line_offset {};
fff8551c 3647
dee91e82 3648 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
3649 if (attr)
3650 {
7b9f3c50
DE
3651 struct quick_file_names find_entry;
3652
9c541725 3653 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3654
3655 /* We may have already read in this line header (TU line header sharing).
3656 If we have we're done. */
094b34ac 3657 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3658 find_entry.hash.line_sect_off = line_offset;
7b9f3c50
DE
3659 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3660 &find_entry, INSERT);
3661 if (*slot != NULL)
3662 {
9a3c8263 3663 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3664 return;
7b9f3c50
DE
3665 }
3666
3019eac3 3667 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3668 }
3669 if (lh == NULL)
3670 {
094b34ac 3671 lh_cu->v.quick->no_file_data = 1;
dee91e82 3672 return;
9291a0cd
TT
3673 }
3674
8d749320 3675 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3676 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3677 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3678 gdb_assert (slot != NULL);
3679 *slot = qfn;
9291a0cd 3680
d721ba37 3681 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3682
fff8551c 3683 qfn->num_file_names = lh->file_names.size ();
8d749320 3684 qfn->file_names =
fff8551c
PA
3685 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3686 for (i = 0; i < lh->file_names.size (); ++i)
3687 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
7b9f3c50 3688 qfn->real_names = NULL;
9291a0cd 3689
094b34ac 3690 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3691}
3692
3693/* A helper for the "quick" functions which attempts to read the line
3694 table for THIS_CU. */
3695
3696static struct quick_file_names *
e4a48d9d 3697dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3698{
0186c6a7
DE
3699 /* This should never be called for TUs. */
3700 gdb_assert (! this_cu->is_debug_types);
3701 /* Nor type unit groups. */
3702 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 3703
dee91e82
DE
3704 if (this_cu->v.quick->file_names != NULL)
3705 return this_cu->v.quick->file_names;
3706 /* If we know there is no line data, no point in looking again. */
3707 if (this_cu->v.quick->no_file_data)
3708 return NULL;
3709
0186c6a7 3710 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
3711
3712 if (this_cu->v.quick->no_file_data)
3713 return NULL;
3714 return this_cu->v.quick->file_names;
9291a0cd
TT
3715}
3716
3717/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3718 real path for a given file name from the line table. */
2fdf6df6 3719
9291a0cd 3720static const char *
7b9f3c50
DE
3721dw2_get_real_path (struct objfile *objfile,
3722 struct quick_file_names *qfn, int index)
9291a0cd 3723{
7b9f3c50
DE
3724 if (qfn->real_names == NULL)
3725 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3726 qfn->num_file_names, const char *);
9291a0cd 3727
7b9f3c50 3728 if (qfn->real_names[index] == NULL)
14278e1f 3729 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3730
7b9f3c50 3731 return qfn->real_names[index];
9291a0cd
TT
3732}
3733
3734static struct symtab *
3735dw2_find_last_source_symtab (struct objfile *objfile)
3736{
ed2dc618
SM
3737 struct dwarf2_per_objfile *dwarf2_per_objfile
3738 = get_dwarf2_per_objfile (objfile);
b76e467d 3739 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
58f0c718 3740 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
ae2de4f8 3741
43f3e411
DE
3742 if (cust == NULL)
3743 return NULL;
ed2dc618 3744
43f3e411 3745 return compunit_primary_filetab (cust);
9291a0cd
TT
3746}
3747
7b9f3c50
DE
3748/* Traversal function for dw2_forget_cached_source_info. */
3749
3750static int
3751dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3752{
7b9f3c50 3753 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3754
7b9f3c50 3755 if (file_data->real_names)
9291a0cd 3756 {
7b9f3c50 3757 int i;
9291a0cd 3758
7b9f3c50 3759 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3760 {
7b9f3c50
DE
3761 xfree ((void*) file_data->real_names[i]);
3762 file_data->real_names[i] = NULL;
9291a0cd
TT
3763 }
3764 }
7b9f3c50
DE
3765
3766 return 1;
3767}
3768
3769static void
3770dw2_forget_cached_source_info (struct objfile *objfile)
3771{
ed2dc618
SM
3772 struct dwarf2_per_objfile *dwarf2_per_objfile
3773 = get_dwarf2_per_objfile (objfile);
7b9f3c50
DE
3774
3775 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3776 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3777}
3778
f8eba3c6
TT
3779/* Helper function for dw2_map_symtabs_matching_filename that expands
3780 the symtabs and calls the iterator. */
3781
3782static int
3783dw2_map_expand_apply (struct objfile *objfile,
3784 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3785 const char *name, const char *real_path,
14bc53a8 3786 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3787{
43f3e411 3788 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3789
3790 /* Don't visit already-expanded CUs. */
43f3e411 3791 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3792 return 0;
3793
3794 /* This may expand more than one symtab, and we want to iterate over
3795 all of them. */
58f0c718 3796 dw2_instantiate_symtab (per_cu, false);
f8eba3c6 3797
14bc53a8
PA
3798 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3799 last_made, callback);
f8eba3c6
TT
3800}
3801
3802/* Implementation of the map_symtabs_matching_filename method. */
3803
14bc53a8
PA
3804static bool
3805dw2_map_symtabs_matching_filename
3806 (struct objfile *objfile, const char *name, const char *real_path,
3807 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3808{
c011a4f4 3809 const char *name_basename = lbasename (name);
ed2dc618
SM
3810 struct dwarf2_per_objfile *dwarf2_per_objfile
3811 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3812
848e3e78
DE
3813 /* The rule is CUs specify all the files, including those used by
3814 any TU, so there's no need to scan TUs here. */
f4dc4d17 3815
b76e467d 3816 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3817 {
3d7bb9d9 3818 /* We only need to look at symtabs not already expanded. */
43f3e411 3819 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3820 continue;
3821
b76e467d 3822 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3823 if (file_data == NULL)
9291a0cd
TT
3824 continue;
3825
b76e467d 3826 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3827 {
7b9f3c50 3828 const char *this_name = file_data->file_names[j];
da235a7c 3829 const char *this_real_name;
9291a0cd 3830
af529f8f 3831 if (compare_filenames_for_search (this_name, name))
9291a0cd 3832 {
f5b95b50 3833 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3834 callback))
3835 return true;
288e77a7 3836 continue;
4aac40c8 3837 }
9291a0cd 3838
c011a4f4
DE
3839 /* Before we invoke realpath, which can get expensive when many
3840 files are involved, do a quick comparison of the basenames. */
3841 if (! basenames_may_differ
3842 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3843 continue;
3844
da235a7c
JK
3845 this_real_name = dw2_get_real_path (objfile, file_data, j);
3846 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3847 {
da235a7c 3848 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3849 callback))
3850 return true;
288e77a7 3851 continue;
da235a7c 3852 }
9291a0cd 3853
da235a7c
JK
3854 if (real_path != NULL)
3855 {
af529f8f
JK
3856 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3857 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3858 if (this_real_name != NULL
af529f8f 3859 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3860 {
f5b95b50 3861 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3862 callback))
3863 return true;
288e77a7 3864 continue;
9291a0cd
TT
3865 }
3866 }
3867 }
3868 }
3869
14bc53a8 3870 return false;
9291a0cd
TT
3871}
3872
da51c347
DE
3873/* Struct used to manage iterating over all CUs looking for a symbol. */
3874
3875struct dw2_symtab_iterator
9291a0cd 3876{
ed2dc618
SM
3877 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3878 struct dwarf2_per_objfile *dwarf2_per_objfile;
da51c347
DE
3879 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3880 int want_specific_block;
3881 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3882 Unused if !WANT_SPECIFIC_BLOCK. */
3883 int block_index;
3884 /* The kind of symbol we're looking for. */
3885 domain_enum domain;
3886 /* The list of CUs from the index entry of the symbol,
3887 or NULL if not found. */
3888 offset_type *vec;
3889 /* The next element in VEC to look at. */
3890 int next;
3891 /* The number of elements in VEC, or zero if there is no match. */
3892 int length;
8943b874
DE
3893 /* Have we seen a global version of the symbol?
3894 If so we can ignore all further global instances.
3895 This is to work around gold/15646, inefficient gold-generated
3896 indices. */
3897 int global_seen;
da51c347 3898};
9291a0cd 3899
da51c347
DE
3900/* Initialize the index symtab iterator ITER.
3901 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3902 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 3903
9291a0cd 3904static void
da51c347 3905dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3906 struct dwarf2_per_objfile *dwarf2_per_objfile,
da51c347
DE
3907 int want_specific_block,
3908 int block_index,
3909 domain_enum domain,
3910 const char *name)
3911{
ed2dc618 3912 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3913 iter->want_specific_block = want_specific_block;
3914 iter->block_index = block_index;
3915 iter->domain = domain;
3916 iter->next = 0;
8943b874 3917 iter->global_seen = 0;
da51c347 3918
3063847f 3919 mapped_index *index = dwarf2_per_objfile->index_table.get ();
ed2dc618
SM
3920
3921 /* index is NULL if OBJF_READNOW. */
3922 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3923 iter->length = MAYBE_SWAP (*iter->vec);
3924 else
3925 {
3926 iter->vec = NULL;
3927 iter->length = 0;
3928 }
3929}
3930
3931/* Return the next matching CU or NULL if there are no more. */
3932
3933static struct dwarf2_per_cu_data *
3934dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3935{
ed2dc618
SM
3936 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3937
da51c347
DE
3938 for ( ; iter->next < iter->length; ++iter->next)
3939 {
3940 offset_type cu_index_and_attrs =
3941 MAYBE_SWAP (iter->vec[iter->next + 1]);
3942 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3943 int want_static = iter->block_index != GLOBAL_BLOCK;
3944 /* This value is only valid for index versions >= 7. */
3945 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3946 gdb_index_symbol_kind symbol_kind =
3947 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3948 /* Only check the symbol attributes if they're present.
3949 Indices prior to version 7 don't record them,
3950 and indices >= 7 may elide them for certain symbols
3951 (gold does this). */
3952 int attrs_valid =
ed2dc618 3953 (dwarf2_per_objfile->index_table->version >= 7
da51c347
DE
3954 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3955
3190f0c6 3956 /* Don't crash on bad data. */
b76e467d 3957 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3958 + dwarf2_per_objfile->all_type_units.size ()))
3190f0c6 3959 {
b98664d3 3960 complaint (_(".gdb_index entry has bad CU index"
4262abfb
JK
3961 " [in module %s]"),
3962 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3963 continue;
3964 }
3965
ff4c9fec 3966 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3190f0c6 3967
da51c347 3968 /* Skip if already read in. */
43f3e411 3969 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
3970 continue;
3971
8943b874
DE
3972 /* Check static vs global. */
3973 if (attrs_valid)
3974 {
3975 if (iter->want_specific_block
3976 && want_static != is_static)
3977 continue;
3978 /* Work around gold/15646. */
3979 if (!is_static && iter->global_seen)
3980 continue;
3981 if (!is_static)
3982 iter->global_seen = 1;
3983 }
da51c347
DE
3984
3985 /* Only check the symbol's kind if it has one. */
3986 if (attrs_valid)
3987 {
3988 switch (iter->domain)
3989 {
3990 case VAR_DOMAIN:
3991 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3992 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3993 /* Some types are also in VAR_DOMAIN. */
3994 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3995 continue;
3996 break;
3997 case STRUCT_DOMAIN:
3998 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3999 continue;
4000 break;
4001 case LABEL_DOMAIN:
4002 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4003 continue;
4004 break;
4005 default:
4006 break;
4007 }
4008 }
4009
4010 ++iter->next;
4011 return per_cu;
4012 }
4013
4014 return NULL;
4015}
4016
43f3e411 4017static struct compunit_symtab *
da51c347
DE
4018dw2_lookup_symbol (struct objfile *objfile, int block_index,
4019 const char *name, domain_enum domain)
9291a0cd 4020{
43f3e411 4021 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
4022 struct dwarf2_per_objfile *dwarf2_per_objfile
4023 = get_dwarf2_per_objfile (objfile);
9291a0cd 4024
b5ec771e
PA
4025 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4026
ed2dc618
SM
4027 struct dw2_symtab_iterator iter;
4028 struct dwarf2_per_cu_data *per_cu;
da51c347 4029
ed2dc618 4030 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 1, block_index, domain, name);
9291a0cd 4031
ed2dc618
SM
4032 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4033 {
4034 struct symbol *sym, *with_opaque = NULL;
58f0c718 4035 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
ed2dc618
SM
4036 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4037 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 4038
ed2dc618
SM
4039 sym = block_find_symbol (block, name, domain,
4040 block_find_non_opaque_type_preferred,
4041 &with_opaque);
b2e2f908 4042
ed2dc618
SM
4043 /* Some caution must be observed with overloaded functions
4044 and methods, since the index will not contain any overload
4045 information (but NAME might contain it). */
da51c347 4046
ed2dc618
SM
4047 if (sym != NULL
4048 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4049 return stab;
4050 if (with_opaque != NULL
4051 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4052 stab_best = stab;
da51c347 4053
ed2dc618 4054 /* Keep looking through other CUs. */
9291a0cd 4055 }
9291a0cd 4056
da51c347 4057 return stab_best;
9291a0cd
TT
4058}
4059
4060static void
4061dw2_print_stats (struct objfile *objfile)
4062{
ed2dc618
SM
4063 struct dwarf2_per_objfile *dwarf2_per_objfile
4064 = get_dwarf2_per_objfile (objfile);
b76e467d 4065 int total = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4066 + dwarf2_per_objfile->all_type_units.size ());
ed2dc618 4067 int count = 0;
9291a0cd 4068
ed2dc618 4069 for (int i = 0; i < total; ++i)
9291a0cd 4070 {
ff4c9fec 4071 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 4072
43f3e411 4073 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4074 ++count;
4075 }
e4a48d9d 4076 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
4077 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4078}
4079
779bd270
DE
4080/* This dumps minimal information about the index.
4081 It is called via "mt print objfiles".
4082 One use is to verify .gdb_index has been loaded by the
4083 gdb.dwarf2/gdb-index.exp testcase. */
4084
9291a0cd
TT
4085static void
4086dw2_dump (struct objfile *objfile)
4087{
ed2dc618
SM
4088 struct dwarf2_per_objfile *dwarf2_per_objfile
4089 = get_dwarf2_per_objfile (objfile);
4090
779bd270
DE
4091 gdb_assert (dwarf2_per_objfile->using_index);
4092 printf_filtered (".gdb_index:");
4093 if (dwarf2_per_objfile->index_table != NULL)
4094 {
4095 printf_filtered (" version %d\n",
4096 dwarf2_per_objfile->index_table->version);
4097 }
4098 else
4099 printf_filtered (" faked for \"readnow\"\n");
4100 printf_filtered ("\n");
9291a0cd
TT
4101}
4102
9291a0cd
TT
4103static void
4104dw2_expand_symtabs_for_function (struct objfile *objfile,
4105 const char *func_name)
4106{
ed2dc618
SM
4107 struct dwarf2_per_objfile *dwarf2_per_objfile
4108 = get_dwarf2_per_objfile (objfile);
da51c347 4109
ed2dc618
SM
4110 struct dw2_symtab_iterator iter;
4111 struct dwarf2_per_cu_data *per_cu;
da51c347 4112
ed2dc618
SM
4113 /* Note: It doesn't matter what we pass for block_index here. */
4114 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 0, GLOBAL_BLOCK, VAR_DOMAIN,
4115 func_name);
da51c347 4116
ed2dc618 4117 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
58f0c718 4118 dw2_instantiate_symtab (per_cu, false);
da51c347 4119
9291a0cd
TT
4120}
4121
4122static void
4123dw2_expand_all_symtabs (struct objfile *objfile)
4124{
ed2dc618
SM
4125 struct dwarf2_per_objfile *dwarf2_per_objfile
4126 = get_dwarf2_per_objfile (objfile);
b76e467d 4127 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4128 + dwarf2_per_objfile->all_type_units.size ());
9291a0cd 4129
ed2dc618 4130 for (int i = 0; i < total_units; ++i)
9291a0cd 4131 {
ff4c9fec 4132 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 4133
58f0c718
TT
4134 /* We don't want to directly expand a partial CU, because if we
4135 read it with the wrong language, then assertion failures can
4136 be triggered later on. See PR symtab/23010. So, tell
4137 dw2_instantiate_symtab to skip partial CUs -- any important
4138 partial CU will be read via DW_TAG_imported_unit anyway. */
4139 dw2_instantiate_symtab (per_cu, true);
9291a0cd
TT
4140 }
4141}
4142
4143static void
652a8996
JK
4144dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4145 const char *fullname)
9291a0cd 4146{
ed2dc618
SM
4147 struct dwarf2_per_objfile *dwarf2_per_objfile
4148 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
4149
4150 /* We don't need to consider type units here.
4151 This is only called for examining code, e.g. expand_line_sal.
4152 There can be an order of magnitude (or more) more type units
4153 than comp units, and we avoid them if we can. */
4154
b76e467d 4155 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 4156 {
3d7bb9d9 4157 /* We only need to look at symtabs not already expanded. */
43f3e411 4158 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4159 continue;
4160
b76e467d 4161 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 4162 if (file_data == NULL)
9291a0cd
TT
4163 continue;
4164
b76e467d 4165 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4166 {
652a8996
JK
4167 const char *this_fullname = file_data->file_names[j];
4168
4169 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 4170 {
58f0c718 4171 dw2_instantiate_symtab (per_cu, false);
9291a0cd
TT
4172 break;
4173 }
4174 }
4175 }
4176}
4177
9291a0cd 4178static void
ade7ed9e 4179dw2_map_matching_symbols (struct objfile *objfile,
fe978cb0 4180 const char * name, domain_enum domain,
ade7ed9e 4181 int global,
40658b94
PH
4182 int (*callback) (struct block *,
4183 struct symbol *, void *),
b5ec771e 4184 void *data, symbol_name_match_type match,
2edb89d3 4185 symbol_compare_ftype *ordered_compare)
9291a0cd 4186{
40658b94 4187 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
4188 current language is Ada for a non-Ada objfile using GNU index. As Ada
4189 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
4190}
4191
b5ec771e
PA
4192/* Symbol name matcher for .gdb_index names.
4193
4194 Symbol names in .gdb_index have a few particularities:
4195
4196 - There's no indication of which is the language of each symbol.
4197
4198 Since each language has its own symbol name matching algorithm,
4199 and we don't know which language is the right one, we must match
3f563c84
PA
4200 each symbol against all languages. This would be a potential
4201 performance problem if it were not mitigated by the
4202 mapped_index::name_components lookup table, which significantly
4203 reduces the number of times we need to call into this matcher,
4204 making it a non-issue.
b5ec771e
PA
4205
4206 - Symbol names in the index have no overload (parameter)
4207 information. I.e., in C++, "foo(int)" and "foo(long)" both
4208 appear as "foo" in the index, for example.
4209
4210 This means that the lookup names passed to the symbol name
4211 matcher functions must have no parameter information either
4212 because (e.g.) symbol search name "foo" does not match
4213 lookup-name "foo(int)" [while swapping search name for lookup
4214 name would match].
4215*/
4216class gdb_index_symbol_name_matcher
4217{
4218public:
4219 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4220 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4221
4222 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4223 Returns true if any matcher matches. */
4224 bool matches (const char *symbol_name);
4225
4226private:
4227 /* A reference to the lookup name we're matching against. */
4228 const lookup_name_info &m_lookup_name;
4229
4230 /* A vector holding all the different symbol name matchers, for all
4231 languages. */
4232 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4233};
4234
4235gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4236 (const lookup_name_info &lookup_name)
4237 : m_lookup_name (lookup_name)
4238{
4239 /* Prepare the vector of comparison functions upfront, to avoid
4240 doing the same work for each symbol. Care is taken to avoid
4241 matching with the same matcher more than once if/when multiple
4242 languages use the same matcher function. */
4243 auto &matchers = m_symbol_name_matcher_funcs;
4244 matchers.reserve (nr_languages);
4245
4246 matchers.push_back (default_symbol_name_matcher);
4247
4248 for (int i = 0; i < nr_languages; i++)
4249 {
4250 const language_defn *lang = language_def ((enum language) i);
c63d3e8d 4251 symbol_name_matcher_ftype *name_matcher
618daa93 4252 = get_symbol_name_matcher (lang, m_lookup_name);
c63d3e8d
PA
4253
4254 /* Don't insert the same comparison routine more than once.
4255 Note that we do this linear walk instead of a seemingly
4256 cheaper sorted insert, or use a std::set or something like
4257 that, because relative order of function addresses is not
4258 stable. This is not a problem in practice because the number
4259 of supported languages is low, and the cost here is tiny
4260 compared to the number of searches we'll do afterwards using
4261 this object. */
4262 if (name_matcher != default_symbol_name_matcher
4263 && (std::find (matchers.begin (), matchers.end (), name_matcher)
4264 == matchers.end ()))
4265 matchers.push_back (name_matcher);
b5ec771e
PA
4266 }
4267}
4268
4269bool
4270gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4271{
4272 for (auto matches_name : m_symbol_name_matcher_funcs)
4273 if (matches_name (symbol_name, m_lookup_name, NULL))
4274 return true;
4275
4276 return false;
4277}
4278
e1ef7d7a
PA
4279/* Starting from a search name, return the string that finds the upper
4280 bound of all strings that start with SEARCH_NAME in a sorted name
4281 list. Returns the empty string to indicate that the upper bound is
4282 the end of the list. */
4283
4284static std::string
4285make_sort_after_prefix_name (const char *search_name)
4286{
4287 /* When looking to complete "func", we find the upper bound of all
4288 symbols that start with "func" by looking for where we'd insert
4289 the closest string that would follow "func" in lexicographical
4290 order. Usually, that's "func"-with-last-character-incremented,
4291 i.e. "fund". Mind non-ASCII characters, though. Usually those
4292 will be UTF-8 multi-byte sequences, but we can't be certain.
4293 Especially mind the 0xff character, which is a valid character in
4294 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4295 rule out compilers allowing it in identifiers. Note that
4296 conveniently, strcmp/strcasecmp are specified to compare
4297 characters interpreted as unsigned char. So what we do is treat
4298 the whole string as a base 256 number composed of a sequence of
4299 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4300 to 0, and carries 1 to the following more-significant position.
4301 If the very first character in SEARCH_NAME ends up incremented
4302 and carries/overflows, then the upper bound is the end of the
4303 list. The string after the empty string is also the empty
4304 string.
4305
4306 Some examples of this operation:
4307
4308 SEARCH_NAME => "+1" RESULT
4309
4310 "abc" => "abd"
4311 "ab\xff" => "ac"
4312 "\xff" "a" "\xff" => "\xff" "b"
4313 "\xff" => ""
4314 "\xff\xff" => ""
4315 "" => ""
4316
4317 Then, with these symbols for example:
4318
4319 func
4320 func1
4321 fund
4322
4323 completing "func" looks for symbols between "func" and
4324 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4325 which finds "func" and "func1", but not "fund".
4326
4327 And with:
4328
4329 funcÿ (Latin1 'ÿ' [0xff])
4330 funcÿ1
4331 fund
4332
4333 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4334 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4335
4336 And with:
4337
4338 ÿÿ (Latin1 'ÿ' [0xff])
4339 ÿÿ1
4340
4341 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4342 the end of the list.
4343 */
4344 std::string after = search_name;
4345 while (!after.empty () && (unsigned char) after.back () == 0xff)
4346 after.pop_back ();
4347 if (!after.empty ())
4348 after.back () = (unsigned char) after.back () + 1;
4349 return after;
4350}
4351
5c58de74 4352/* See declaration. */
61d96d7e 4353
5c58de74
PA
4354std::pair<std::vector<name_component>::const_iterator,
4355 std::vector<name_component>::const_iterator>
44ed8f3e 4356mapped_index_base::find_name_components_bounds
5c58de74 4357 (const lookup_name_info &lookup_name_without_params) const
3f563c84 4358{
5c58de74
PA
4359 auto *name_cmp
4360 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84
PA
4361
4362 const char *cplus
c62446b1 4363 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
9291a0cd 4364
3f563c84
PA
4365 /* Comparison function object for lower_bound that matches against a
4366 given symbol name. */
4367 auto lookup_compare_lower = [&] (const name_component &elem,
4368 const char *name)
4369 {
5c58de74 4370 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4371 const char *elem_name = elem_qualified + elem.name_offset;
4372 return name_cmp (elem_name, name) < 0;
4373 };
4374
4375 /* Comparison function object for upper_bound that matches against a
4376 given symbol name. */
4377 auto lookup_compare_upper = [&] (const char *name,
4378 const name_component &elem)
4379 {
5c58de74 4380 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4381 const char *elem_name = elem_qualified + elem.name_offset;
4382 return name_cmp (name, elem_name) < 0;
4383 };
4384
5c58de74
PA
4385 auto begin = this->name_components.begin ();
4386 auto end = this->name_components.end ();
3f563c84
PA
4387
4388 /* Find the lower bound. */
4389 auto lower = [&] ()
4390 {
5c58de74 4391 if (lookup_name_without_params.completion_mode () && cplus[0] == '\0')
3f563c84
PA
4392 return begin;
4393 else
4394 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4395 } ();
4396
4397 /* Find the upper bound. */
4398 auto upper = [&] ()
4399 {
5c58de74 4400 if (lookup_name_without_params.completion_mode ())
3f563c84 4401 {
e1ef7d7a
PA
4402 /* In completion mode, we want UPPER to point past all
4403 symbols names that have the same prefix. I.e., with
4404 these symbols, and completing "func":
4405
4406 function << lower bound
4407 function1
4408 other_function << upper bound
4409
4410 We find the upper bound by looking for the insertion
4411 point of "func"-with-last-character-incremented,
4412 i.e. "fund". */
4413 std::string after = make_sort_after_prefix_name (cplus);
4414 if (after.empty ())
3f563c84 4415 return end;
e6b2f5ef
PA
4416 return std::lower_bound (lower, end, after.c_str (),
4417 lookup_compare_lower);
3f563c84
PA
4418 }
4419 else
4420 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4421 } ();
4422
5c58de74
PA
4423 return {lower, upper};
4424}
4425
4426/* See declaration. */
4427
4428void
44ed8f3e 4429mapped_index_base::build_name_components ()
5c58de74
PA
4430{
4431 if (!this->name_components.empty ())
4432 return;
4433
4434 this->name_components_casing = case_sensitivity;
4435 auto *name_cmp
4436 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4437
4438 /* The code below only knows how to break apart components of C++
4439 symbol names (and other languages that use '::' as
4440 namespace/module separator). If we add support for wild matching
4441 to some language that uses some other operator (E.g., Ada, Go and
4442 D use '.'), then we'll need to try splitting the symbol name
4443 according to that language too. Note that Ada does support wild
4444 matching, but doesn't currently support .gdb_index. */
44ed8f3e
PA
4445 auto count = this->symbol_name_count ();
4446 for (offset_type idx = 0; idx < count; idx++)
5c58de74 4447 {
44ed8f3e 4448 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
4449 continue;
4450
4451 const char *name = this->symbol_name_at (idx);
4452
4453 /* Add each name component to the name component table. */
4454 unsigned int previous_len = 0;
4455 for (unsigned int current_len = cp_find_first_component (name);
4456 name[current_len] != '\0';
4457 current_len += cp_find_first_component (name + current_len))
4458 {
4459 gdb_assert (name[current_len] == ':');
4460 this->name_components.push_back ({previous_len, idx});
4461 /* Skip the '::'. */
4462 current_len += 2;
4463 previous_len = current_len;
4464 }
4465 this->name_components.push_back ({previous_len, idx});
4466 }
4467
4468 /* Sort name_components elements by name. */
4469 auto name_comp_compare = [&] (const name_component &left,
4470 const name_component &right)
4471 {
4472 const char *left_qualified = this->symbol_name_at (left.idx);
4473 const char *right_qualified = this->symbol_name_at (right.idx);
4474
4475 const char *left_name = left_qualified + left.name_offset;
4476 const char *right_name = right_qualified + right.name_offset;
4477
4478 return name_cmp (left_name, right_name) < 0;
4479 };
4480
4481 std::sort (this->name_components.begin (),
4482 this->name_components.end (),
4483 name_comp_compare);
4484}
4485
4486/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4487 mapped_index_base instead of the containing objfile. This is split
4488 to a separate function in order to be able to unit test the
4489 name_components matching using a mock mapped_index_base. For each
5c58de74 4490 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4491 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4492
4493static void
4494dw2_expand_symtabs_matching_symbol
44ed8f3e 4495 (mapped_index_base &index,
5c58de74
PA
4496 const lookup_name_info &lookup_name_in,
4497 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4498 enum search_domain kind,
4499 gdb::function_view<void (offset_type)> match_callback)
4500{
4501 lookup_name_info lookup_name_without_params
4502 = lookup_name_in.make_ignore_params ();
4503 gdb_index_symbol_name_matcher lookup_name_matcher
4504 (lookup_name_without_params);
4505
4506 /* Build the symbol name component sorted vector, if we haven't
4507 yet. */
4508 index.build_name_components ();
4509
4510 auto bounds = index.find_name_components_bounds (lookup_name_without_params);
4511
3f563c84
PA
4512 /* Now for each symbol name in range, check to see if we have a name
4513 match, and if so, call the MATCH_CALLBACK callback. */
4514
4515 /* The same symbol may appear more than once in the range though.
4516 E.g., if we're looking for symbols that complete "w", and we have
4517 a symbol named "w1::w2", we'll find the two name components for
4518 that same symbol in the range. To be sure we only call the
4519 callback once per symbol, we first collect the symbol name
4520 indexes that matched in a temporary vector and ignore
4521 duplicates. */
4522 std::vector<offset_type> matches;
5c58de74 4523 matches.reserve (std::distance (bounds.first, bounds.second));
3f563c84 4524
5c58de74 4525 for (; bounds.first != bounds.second; ++bounds.first)
3f563c84 4526 {
5c58de74 4527 const char *qualified = index.symbol_name_at (bounds.first->idx);
3f563c84
PA
4528
4529 if (!lookup_name_matcher.matches (qualified)
4530 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
9291a0cd
TT
4531 continue;
4532
5c58de74 4533 matches.push_back (bounds.first->idx);
3f563c84
PA
4534 }
4535
4536 std::sort (matches.begin (), matches.end ());
4537
4538 /* Finally call the callback, once per match. */
4539 ULONGEST prev = -1;
4540 for (offset_type idx : matches)
4541 {
4542 if (prev != idx)
4543 {
4544 match_callback (idx);
4545 prev = idx;
4546 }
4547 }
4548
4549 /* Above we use a type wider than idx's for 'prev', since 0 and
4550 (offset_type)-1 are both possible values. */
4551 static_assert (sizeof (prev) > sizeof (offset_type), "");
4552}
4553
c62446b1
PA
4554#if GDB_SELF_TEST
4555
4556namespace selftests { namespace dw2_expand_symtabs_matching {
4557
a3c5fafd
PA
4558/* A mock .gdb_index/.debug_names-like name index table, enough to
4559 exercise dw2_expand_symtabs_matching_symbol, which works with the
4560 mapped_index_base interface. Builds an index from the symbol list
4561 passed as parameter to the constructor. */
4562class mock_mapped_index : public mapped_index_base
c62446b1
PA
4563{
4564public:
a3c5fafd
PA
4565 mock_mapped_index (gdb::array_view<const char *> symbols)
4566 : m_symbol_table (symbols)
c62446b1
PA
4567 {}
4568
a3c5fafd 4569 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4570
a3c5fafd 4571 /* Return the number of names in the symbol table. */
632e107b 4572 size_t symbol_name_count () const override
c62446b1 4573 {
a3c5fafd 4574 return m_symbol_table.size ();
c62446b1
PA
4575 }
4576
a3c5fafd 4577 /* Get the name of the symbol at IDX in the symbol table. */
632e107b 4578 const char *symbol_name_at (offset_type idx) const override
a3c5fafd
PA
4579 {
4580 return m_symbol_table[idx];
4581 }
c62446b1 4582
a3c5fafd
PA
4583private:
4584 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4585};
4586
4587/* Convenience function that converts a NULL pointer to a "<null>"
4588 string, to pass to print routines. */
4589
4590static const char *
4591string_or_null (const char *str)
4592{
4593 return str != NULL ? str : "<null>";
4594}
4595
4596/* Check if a lookup_name_info built from
4597 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4598 index. EXPECTED_LIST is the list of expected matches, in expected
4599 matching order. If no match expected, then an empty list is
4600 specified. Returns true on success. On failure prints a warning
4601 indicating the file:line that failed, and returns false. */
4602
4603static bool
4604check_match (const char *file, int line,
4605 mock_mapped_index &mock_index,
4606 const char *name, symbol_name_match_type match_type,
4607 bool completion_mode,
4608 std::initializer_list<const char *> expected_list)
4609{
4610 lookup_name_info lookup_name (name, match_type, completion_mode);
4611
4612 bool matched = true;
4613
4614 auto mismatch = [&] (const char *expected_str,
4615 const char *got)
4616 {
4617 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4618 "expected=\"%s\", got=\"%s\"\n"),
4619 file, line,
4620 (match_type == symbol_name_match_type::FULL
4621 ? "FULL" : "WILD"),
4622 name, string_or_null (expected_str), string_or_null (got));
4623 matched = false;
4624 };
4625
4626 auto expected_it = expected_list.begin ();
4627 auto expected_end = expected_list.end ();
4628
a3c5fafd 4629 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4630 NULL, ALL_DOMAIN,
4631 [&] (offset_type idx)
4632 {
a3c5fafd 4633 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4634 const char *expected_str
4635 = expected_it == expected_end ? NULL : *expected_it++;
4636
4637 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4638 mismatch (expected_str, matched_name);
4639 });
4640
4641 const char *expected_str
4642 = expected_it == expected_end ? NULL : *expected_it++;
4643 if (expected_str != NULL)
4644 mismatch (expected_str, NULL);
4645
4646 return matched;
4647}
4648
4649/* The symbols added to the mock mapped_index for testing (in
4650 canonical form). */
4651static const char *test_symbols[] = {
4652 "function",
4653 "std::bar",
4654 "std::zfunction",
4655 "std::zfunction2",
4656 "w1::w2",
4657 "ns::foo<char*>",
4658 "ns::foo<int>",
4659 "ns::foo<long>",
a20714ff
PA
4660 "ns2::tmpl<int>::foo2",
4661 "(anonymous namespace)::A::B::C",
c62446b1 4662
e1ef7d7a
PA
4663 /* These are used to check that the increment-last-char in the
4664 matching algorithm for completion doesn't match "t1_fund" when
4665 completing "t1_func". */
4666 "t1_func",
4667 "t1_func1",
4668 "t1_fund",
4669 "t1_fund1",
4670
4671 /* A UTF-8 name with multi-byte sequences to make sure that
4672 cp-name-parser understands this as a single identifier ("função"
4673 is "function" in PT). */
4674 u8"u8função",
4675
4676 /* \377 (0xff) is Latin1 'ÿ'. */
4677 "yfunc\377",
4678
4679 /* \377 (0xff) is Latin1 'ÿ'. */
4680 "\377",
4681 "\377\377123",
4682
c62446b1
PA
4683 /* A name with all sorts of complications. Starts with "z" to make
4684 it easier for the completion tests below. */
4685#define Z_SYM_NAME \
4686 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4687 "::tuple<(anonymous namespace)::ui*, " \
4688 "std::default_delete<(anonymous namespace)::ui>, void>"
4689
4690 Z_SYM_NAME
4691};
4692
a3c5fafd
PA
4693/* Returns true if the mapped_index_base::find_name_component_bounds
4694 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4695 in completion mode. */
5c58de74
PA
4696
4697static bool
a3c5fafd 4698check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4699 const char *search_name,
4700 gdb::array_view<const char *> expected_syms)
4701{
4702 lookup_name_info lookup_name (search_name,
4703 symbol_name_match_type::FULL, true);
4704
4705 auto bounds = index.find_name_components_bounds (lookup_name);
4706
4707 size_t distance = std::distance (bounds.first, bounds.second);
4708 if (distance != expected_syms.size ())
4709 return false;
4710
4711 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4712 {
4713 auto nc_elem = bounds.first + exp_elem;
4714 const char *qualified = index.symbol_name_at (nc_elem->idx);
4715 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4716 return false;
4717 }
4718
4719 return true;
4720}
4721
4722/* Test the lower-level mapped_index::find_name_component_bounds
4723 method. */
4724
c62446b1 4725static void
5c58de74
PA
4726test_mapped_index_find_name_component_bounds ()
4727{
4728 mock_mapped_index mock_index (test_symbols);
4729
a3c5fafd 4730 mock_index.build_name_components ();
5c58de74
PA
4731
4732 /* Test the lower-level mapped_index::find_name_component_bounds
4733 method in completion mode. */
4734 {
4735 static const char *expected_syms[] = {
4736 "t1_func",
4737 "t1_func1",
5c58de74
PA
4738 };
4739
a3c5fafd 4740 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4741 "t1_func", expected_syms));
4742 }
4743
4744 /* Check that the increment-last-char in the name matching algorithm
4745 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4746 {
4747 static const char *expected_syms1[] = {
4748 "\377",
4749 "\377\377123",
4750 };
a3c5fafd 4751 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4752 "\377", expected_syms1));
4753
4754 static const char *expected_syms2[] = {
4755 "\377\377123",
4756 };
a3c5fafd 4757 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4758 "\377\377", expected_syms2));
4759 }
4760}
4761
4762/* Test dw2_expand_symtabs_matching_symbol. */
4763
4764static void
4765test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4766{
4767 mock_mapped_index mock_index (test_symbols);
4768
4769 /* We let all tests run until the end even if some fails, for debug
4770 convenience. */
4771 bool any_mismatch = false;
4772
4773 /* Create the expected symbols list (an initializer_list). Needed
4774 because lists have commas, and we need to pass them to CHECK,
4775 which is a macro. */
4776#define EXPECT(...) { __VA_ARGS__ }
4777
4778 /* Wrapper for check_match that passes down the current
4779 __FILE__/__LINE__. */
4780#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4781 any_mismatch |= !check_match (__FILE__, __LINE__, \
4782 mock_index, \
4783 NAME, MATCH_TYPE, COMPLETION_MODE, \
4784 EXPECTED_LIST)
4785
4786 /* Identity checks. */
4787 for (const char *sym : test_symbols)
4788 {
4789 /* Should be able to match all existing symbols. */
4790 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4791 EXPECT (sym));
4792
4793 /* Should be able to match all existing symbols with
4794 parameters. */
4795 std::string with_params = std::string (sym) + "(int)";
4796 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4797 EXPECT (sym));
4798
4799 /* Should be able to match all existing symbols with
4800 parameters and qualifiers. */
4801 with_params = std::string (sym) + " ( int ) const";
4802 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4803 EXPECT (sym));
4804
4805 /* This should really find sym, but cp-name-parser.y doesn't
4806 know about lvalue/rvalue qualifiers yet. */
4807 with_params = std::string (sym) + " ( int ) &&";
4808 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4809 {});
4810 }
4811
e1ef7d7a
PA
4812 /* Check that the name matching algorithm for completion doesn't get
4813 confused with Latin1 'ÿ' / 0xff. */
4814 {
4815 static const char str[] = "\377";
4816 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4817 EXPECT ("\377", "\377\377123"));
4818 }
4819
4820 /* Check that the increment-last-char in the matching algorithm for
4821 completion doesn't match "t1_fund" when completing "t1_func". */
4822 {
4823 static const char str[] = "t1_func";
4824 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4825 EXPECT ("t1_func", "t1_func1"));
4826 }
4827
c62446b1
PA
4828 /* Check that completion mode works at each prefix of the expected
4829 symbol name. */
4830 {
4831 static const char str[] = "function(int)";
4832 size_t len = strlen (str);
4833 std::string lookup;
4834
4835 for (size_t i = 1; i < len; i++)
4836 {
4837 lookup.assign (str, i);
4838 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4839 EXPECT ("function"));
4840 }
4841 }
4842
4843 /* While "w" is a prefix of both components, the match function
4844 should still only be called once. */
4845 {
4846 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4847 EXPECT ("w1::w2"));
a20714ff
PA
4848 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4849 EXPECT ("w1::w2"));
c62446b1
PA
4850 }
4851
4852 /* Same, with a "complicated" symbol. */
4853 {
4854 static const char str[] = Z_SYM_NAME;
4855 size_t len = strlen (str);
4856 std::string lookup;
4857
4858 for (size_t i = 1; i < len; i++)
4859 {
4860 lookup.assign (str, i);
4861 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4862 EXPECT (Z_SYM_NAME));
4863 }
4864 }
4865
4866 /* In FULL mode, an incomplete symbol doesn't match. */
4867 {
4868 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4869 {});
4870 }
4871
4872 /* A complete symbol with parameters matches any overload, since the
4873 index has no overload info. */
4874 {
4875 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4876 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4877 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4878 EXPECT ("std::zfunction", "std::zfunction2"));
4879 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4880 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4881 }
4882
4883 /* Check that whitespace is ignored appropriately. A symbol with a
4884 template argument list. */
4885 {
4886 static const char expected[] = "ns::foo<int>";
4887 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4888 EXPECT (expected));
a20714ff
PA
4889 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4890 EXPECT (expected));
c62446b1
PA
4891 }
4892
4893 /* Check that whitespace is ignored appropriately. A symbol with a
4894 template argument list that includes a pointer. */
4895 {
4896 static const char expected[] = "ns::foo<char*>";
4897 /* Try both completion and non-completion modes. */
4898 static const bool completion_mode[2] = {false, true};
4899 for (size_t i = 0; i < 2; i++)
4900 {
4901 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4902 completion_mode[i], EXPECT (expected));
a20714ff
PA
4903 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4904 completion_mode[i], EXPECT (expected));
c62446b1
PA
4905
4906 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4907 completion_mode[i], EXPECT (expected));
a20714ff
PA
4908 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4909 completion_mode[i], EXPECT (expected));
c62446b1
PA
4910 }
4911 }
4912
4913 {
4914 /* Check method qualifiers are ignored. */
4915 static const char expected[] = "ns::foo<char*>";
4916 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4917 symbol_name_match_type::FULL, true, EXPECT (expected));
4918 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4919 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4920 CHECK_MATCH ("foo < char * > ( int ) const",
4921 symbol_name_match_type::WILD, true, EXPECT (expected));
4922 CHECK_MATCH ("foo < char * > ( int ) &&",
4923 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4924 }
4925
4926 /* Test lookup names that don't match anything. */
4927 {
a20714ff
PA
4928 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4929 {});
4930
c62446b1
PA
4931 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4932 {});
4933 }
4934
a20714ff
PA
4935 /* Some wild matching tests, exercising "(anonymous namespace)",
4936 which should not be confused with a parameter list. */
4937 {
4938 static const char *syms[] = {
4939 "A::B::C",
4940 "B::C",
4941 "C",
4942 "A :: B :: C ( int )",
4943 "B :: C ( int )",
4944 "C ( int )",
4945 };
4946
4947 for (const char *s : syms)
4948 {
4949 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4950 EXPECT ("(anonymous namespace)::A::B::C"));
4951 }
4952 }
4953
4954 {
4955 static const char expected[] = "ns2::tmpl<int>::foo2";
4956 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4957 EXPECT (expected));
4958 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4959 EXPECT (expected));
4960 }
4961
c62446b1
PA
4962 SELF_CHECK (!any_mismatch);
4963
4964#undef EXPECT
4965#undef CHECK_MATCH
4966}
4967
5c58de74
PA
4968static void
4969run_test ()
4970{
4971 test_mapped_index_find_name_component_bounds ();
4972 test_dw2_expand_symtabs_matching_symbol ();
4973}
4974
c62446b1
PA
4975}} // namespace selftests::dw2_expand_symtabs_matching
4976
4977#endif /* GDB_SELF_TEST */
4978
4b514bc8
JK
4979/* If FILE_MATCHER is NULL or if PER_CU has
4980 dwarf2_per_cu_quick_data::MARK set (see
4981 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4982 EXPANSION_NOTIFY on it. */
4983
4984static void
4985dw2_expand_symtabs_matching_one
4986 (struct dwarf2_per_cu_data *per_cu,
4987 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4988 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4989{
4990 if (file_matcher == NULL || per_cu->v.quick->mark)
4991 {
4992 bool symtab_was_null
4993 = (per_cu->v.quick->compunit_symtab == NULL);
4994
58f0c718 4995 dw2_instantiate_symtab (per_cu, false);
4b514bc8
JK
4996
4997 if (expansion_notify != NULL
4998 && symtab_was_null
4999 && per_cu->v.quick->compunit_symtab != NULL)
5000 expansion_notify (per_cu->v.quick->compunit_symtab);
5001 }
5002}
5003
3f563c84
PA
5004/* Helper for dw2_expand_matching symtabs. Called on each symbol
5005 matched, to expand corresponding CUs that were marked. IDX is the
5006 index of the symbol name that matched. */
5007
5008static void
5009dw2_expand_marked_cus
ed2dc618 5010 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
5011 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5012 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5013 search_domain kind)
5014{
3f563c84
PA
5015 offset_type *vec, vec_len, vec_idx;
5016 bool global_seen = false;
ed2dc618 5017 mapped_index &index = *dwarf2_per_objfile->index_table;
3f563c84 5018
61920122 5019 vec = (offset_type *) (index.constant_pool
f00a2de2 5020 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
5021 vec_len = MAYBE_SWAP (vec[0]);
5022 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5023 {
61920122
PA
5024 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5025 /* This value is only valid for index versions >= 7. */
5026 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5027 gdb_index_symbol_kind symbol_kind =
5028 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5029 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5030 /* Only check the symbol attributes if they're present.
5031 Indices prior to version 7 don't record them,
5032 and indices >= 7 may elide them for certain symbols
5033 (gold does this). */
5034 int attrs_valid =
5035 (index.version >= 7
5036 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5037
5038 /* Work around gold/15646. */
5039 if (attrs_valid)
9291a0cd 5040 {
61920122
PA
5041 if (!is_static && global_seen)
5042 continue;
5043 if (!is_static)
5044 global_seen = true;
5045 }
3190f0c6 5046
61920122
PA
5047 /* Only check the symbol's kind if it has one. */
5048 if (attrs_valid)
5049 {
5050 switch (kind)
8943b874 5051 {
61920122
PA
5052 case VARIABLES_DOMAIN:
5053 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5054 continue;
5055 break;
5056 case FUNCTIONS_DOMAIN:
5057 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 5058 continue;
61920122
PA
5059 break;
5060 case TYPES_DOMAIN:
5061 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5062 continue;
5063 break;
5064 default:
5065 break;
8943b874 5066 }
61920122 5067 }
8943b874 5068
61920122 5069 /* Don't crash on bad data. */
b76e467d 5070 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 5071 + dwarf2_per_objfile->all_type_units.size ()))
61920122 5072 {
b98664d3 5073 complaint (_(".gdb_index entry has bad CU index"
ed2dc618
SM
5074 " [in module %s]"),
5075 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
5076 continue;
5077 }
5078
ff4c9fec 5079 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4b514bc8
JK
5080 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5081 expansion_notify);
61920122
PA
5082 }
5083}
5084
4b514bc8
JK
5085/* If FILE_MATCHER is non-NULL, set all the
5086 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5087 that match FILE_MATCHER. */
5088
61920122 5089static void
4b514bc8 5090dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
5091 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5092 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 5093{
4b514bc8 5094 if (file_matcher == NULL)
61920122
PA
5095 return;
5096
4b514bc8
JK
5097 objfile *const objfile = dwarf2_per_objfile->objfile;
5098
5099 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5100 htab_eq_pointer,
5101 NULL, xcalloc, xfree));
5102 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
5103 htab_eq_pointer,
5104 NULL, xcalloc, xfree));
61920122 5105
4b514bc8
JK
5106 /* The rule is CUs specify all the files, including those used by
5107 any TU, so there's no need to scan TUs here. */
61920122 5108
b76e467d 5109 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5110 {
927aa2e7
JK
5111 QUIT;
5112
5113 per_cu->v.quick->mark = 0;
5114
5115 /* We only need to look at symtabs not already expanded. */
5116 if (per_cu->v.quick->compunit_symtab)
5117 continue;
5118
b76e467d 5119 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
5120 if (file_data == NULL)
5121 continue;
5122
5123 if (htab_find (visited_not_found.get (), file_data) != NULL)
5124 continue;
5125 else if (htab_find (visited_found.get (), file_data) != NULL)
5126 {
5127 per_cu->v.quick->mark = 1;
5128 continue;
5129 }
5130
b76e467d 5131 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
5132 {
5133 const char *this_real_name;
5134
5135 if (file_matcher (file_data->file_names[j], false))
5136 {
5137 per_cu->v.quick->mark = 1;
5138 break;
5139 }
5140
5141 /* Before we invoke realpath, which can get expensive when many
5142 files are involved, do a quick comparison of the basenames. */
5143 if (!basenames_may_differ
5144 && !file_matcher (lbasename (file_data->file_names[j]),
5145 true))
5146 continue;
5147
5148 this_real_name = dw2_get_real_path (objfile, file_data, j);
5149 if (file_matcher (this_real_name, false))
5150 {
5151 per_cu->v.quick->mark = 1;
5152 break;
5153 }
5154 }
5155
b76e467d
SM
5156 void **slot = htab_find_slot (per_cu->v.quick->mark
5157 ? visited_found.get ()
5158 : visited_not_found.get (),
5159 file_data, INSERT);
927aa2e7
JK
5160 *slot = file_data;
5161 }
5162}
5163
5164static void
5165dw2_expand_symtabs_matching
5166 (struct objfile *objfile,
5167 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5168 const lookup_name_info &lookup_name,
5169 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5170 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5171 enum search_domain kind)
5172{
ed2dc618
SM
5173 struct dwarf2_per_objfile *dwarf2_per_objfile
5174 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5175
5176 /* index_table is NULL if OBJF_READNOW. */
5177 if (!dwarf2_per_objfile->index_table)
5178 return;
5179
ed2dc618 5180 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7
JK
5181
5182 mapped_index &index = *dwarf2_per_objfile->index_table;
5183
5184 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5185 symbol_matcher,
5186 kind, [&] (offset_type idx)
5187 {
ed2dc618 5188 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7
JK
5189 expansion_notify, kind);
5190 });
5191}
5192
5193/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5194 symtab. */
5195
5196static struct compunit_symtab *
5197recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5198 CORE_ADDR pc)
5199{
5200 int i;
5201
5202 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5203 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5204 return cust;
5205
5206 if (cust->includes == NULL)
5207 return NULL;
5208
5209 for (i = 0; cust->includes[i]; ++i)
5210 {
5211 struct compunit_symtab *s = cust->includes[i];
5212
5213 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5214 if (s != NULL)
5215 return s;
5216 }
5217
5218 return NULL;
5219}
5220
5221static struct compunit_symtab *
5222dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5223 struct bound_minimal_symbol msymbol,
5224 CORE_ADDR pc,
5225 struct obj_section *section,
5226 int warn_if_readin)
5227{
5228 struct dwarf2_per_cu_data *data;
5229 struct compunit_symtab *result;
5230
927aa2e7
JK
5231 if (!objfile->psymtabs_addrmap)
5232 return NULL;
5233
79748972
TT
5234 CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
5235 SECT_OFF_TEXT (objfile));
927aa2e7 5236 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
79748972 5237 pc - baseaddr);
927aa2e7
JK
5238 if (!data)
5239 return NULL;
5240
5241 if (warn_if_readin && data->v.quick->compunit_symtab)
5242 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5243 paddress (get_objfile_arch (objfile), pc));
5244
5245 result
58f0c718
TT
5246 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
5247 false),
927aa2e7
JK
5248 pc);
5249 gdb_assert (result != NULL);
5250 return result;
5251}
5252
5253static void
5254dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5255 void *data, int need_fullname)
5256{
ed2dc618
SM
5257 struct dwarf2_per_objfile *dwarf2_per_objfile
5258 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5259
5260 if (!dwarf2_per_objfile->filenames_cache)
5261 {
5262 dwarf2_per_objfile->filenames_cache.emplace ();
5263
5264 htab_up visited (htab_create_alloc (10,
5265 htab_hash_pointer, htab_eq_pointer,
5266 NULL, xcalloc, xfree));
5267
5268 /* The rule is CUs specify all the files, including those used
5269 by any TU, so there's no need to scan TUs here. We can
5270 ignore file names coming from already-expanded CUs. */
5271
b76e467d 5272 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5273 {
927aa2e7
JK
5274 if (per_cu->v.quick->compunit_symtab)
5275 {
5276 void **slot = htab_find_slot (visited.get (),
5277 per_cu->v.quick->file_names,
5278 INSERT);
5279
5280 *slot = per_cu->v.quick->file_names;
5281 }
5282 }
5283
b76e467d 5284 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5285 {
927aa2e7
JK
5286 /* We only need to look at symtabs not already expanded. */
5287 if (per_cu->v.quick->compunit_symtab)
5288 continue;
5289
b76e467d 5290 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
5291 if (file_data == NULL)
5292 continue;
5293
b76e467d 5294 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
5295 if (*slot)
5296 {
5297 /* Already visited. */
5298 continue;
5299 }
5300 *slot = file_data;
5301
5302 for (int j = 0; j < file_data->num_file_names; ++j)
5303 {
5304 const char *filename = file_data->file_names[j];
5305 dwarf2_per_objfile->filenames_cache->seen (filename);
5306 }
5307 }
5308 }
5309
5310 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5311 {
5312 gdb::unique_xmalloc_ptr<char> this_real_name;
5313
5314 if (need_fullname)
5315 this_real_name = gdb_realpath (filename);
5316 (*fun) (filename, this_real_name.get (), data);
5317 });
5318}
5319
5320static int
5321dw2_has_symbols (struct objfile *objfile)
5322{
5323 return 1;
5324}
5325
5326const struct quick_symbol_functions dwarf2_gdb_index_functions =
5327{
5328 dw2_has_symbols,
5329 dw2_find_last_source_symtab,
5330 dw2_forget_cached_source_info,
5331 dw2_map_symtabs_matching_filename,
5332 dw2_lookup_symbol,
5333 dw2_print_stats,
5334 dw2_dump,
927aa2e7
JK
5335 dw2_expand_symtabs_for_function,
5336 dw2_expand_all_symtabs,
5337 dw2_expand_symtabs_with_fullname,
5338 dw2_map_matching_symbols,
5339 dw2_expand_symtabs_matching,
5340 dw2_find_pc_sect_compunit_symtab,
5341 NULL,
5342 dw2_map_symbol_filenames
5343};
5344
5345/* DWARF-5 debug_names reader. */
5346
5347/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5348static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5349
5350/* A helper function that reads the .debug_names section in SECTION
5351 and fills in MAP. FILENAME is the name of the file containing the
5352 section; it is used for error reporting.
5353
5354 Returns true if all went well, false otherwise. */
5355
5356static bool
5357read_debug_names_from_section (struct objfile *objfile,
5358 const char *filename,
5359 struct dwarf2_section_info *section,
5360 mapped_debug_names &map)
5361{
5362 if (dwarf2_section_empty_p (section))
5363 return false;
5364
5365 /* Older elfutils strip versions could keep the section in the main
5366 executable while splitting it for the separate debug info file. */
5367 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
5368 return false;
5369
5370 dwarf2_read_section (objfile, section);
5371
5372 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
5373
5374 const gdb_byte *addr = section->buffer;
5375
5376 bfd *const abfd = get_section_bfd_owner (section);
5377
5378 unsigned int bytes_read;
5379 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5380 addr += bytes_read;
5381
5382 map.dwarf5_is_dwarf64 = bytes_read != 4;
5383 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5384 if (bytes_read + length != section->size)
5385 {
5386 /* There may be multiple per-CU indices. */
5387 warning (_("Section .debug_names in %s length %s does not match "
5388 "section length %s, ignoring .debug_names."),
5389 filename, plongest (bytes_read + length),
5390 pulongest (section->size));
5391 return false;
5392 }
5393
5394 /* The version number. */
5395 uint16_t version = read_2_bytes (abfd, addr);
5396 addr += 2;
5397 if (version != 5)
5398 {
5399 warning (_("Section .debug_names in %s has unsupported version %d, "
5400 "ignoring .debug_names."),
5401 filename, version);
5402 return false;
5403 }
5404
5405 /* Padding. */
5406 uint16_t padding = read_2_bytes (abfd, addr);
5407 addr += 2;
5408 if (padding != 0)
5409 {
5410 warning (_("Section .debug_names in %s has unsupported padding %d, "
5411 "ignoring .debug_names."),
5412 filename, padding);
5413 return false;
5414 }
5415
5416 /* comp_unit_count - The number of CUs in the CU list. */
5417 map.cu_count = read_4_bytes (abfd, addr);
5418 addr += 4;
5419
5420 /* local_type_unit_count - The number of TUs in the local TU
5421 list. */
5422 map.tu_count = read_4_bytes (abfd, addr);
5423 addr += 4;
5424
5425 /* foreign_type_unit_count - The number of TUs in the foreign TU
5426 list. */
5427 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5428 addr += 4;
5429 if (foreign_tu_count != 0)
5430 {
5431 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5432 "ignoring .debug_names."),
5433 filename, static_cast<unsigned long> (foreign_tu_count));
5434 return false;
5435 }
5436
5437 /* bucket_count - The number of hash buckets in the hash lookup
5438 table. */
5439 map.bucket_count = read_4_bytes (abfd, addr);
5440 addr += 4;
5441
5442 /* name_count - The number of unique names in the index. */
5443 map.name_count = read_4_bytes (abfd, addr);
5444 addr += 4;
5445
5446 /* abbrev_table_size - The size in bytes of the abbreviations
5447 table. */
5448 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5449 addr += 4;
5450
5451 /* augmentation_string_size - The size in bytes of the augmentation
5452 string. This value is rounded up to a multiple of 4. */
5453 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5454 addr += 4;
5455 map.augmentation_is_gdb = ((augmentation_string_size
5456 == sizeof (dwarf5_augmentation))
5457 && memcmp (addr, dwarf5_augmentation,
5458 sizeof (dwarf5_augmentation)) == 0);
5459 augmentation_string_size += (-augmentation_string_size) & 3;
5460 addr += augmentation_string_size;
5461
5462 /* List of CUs */
5463 map.cu_table_reordered = addr;
5464 addr += map.cu_count * map.offset_size;
5465
5466 /* List of Local TUs */
5467 map.tu_table_reordered = addr;
5468 addr += map.tu_count * map.offset_size;
5469
5470 /* Hash Lookup Table */
5471 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5472 addr += map.bucket_count * 4;
5473 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5474 addr += map.name_count * 4;
5475
5476 /* Name Table */
5477 map.name_table_string_offs_reordered = addr;
5478 addr += map.name_count * map.offset_size;
5479 map.name_table_entry_offs_reordered = addr;
5480 addr += map.name_count * map.offset_size;
5481
5482 const gdb_byte *abbrev_table_start = addr;
5483 for (;;)
5484 {
5485 unsigned int bytes_read;
5486 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5487 addr += bytes_read;
5488 if (index_num == 0)
5489 break;
5490
5491 const auto insertpair
5492 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5493 if (!insertpair.second)
5494 {
5495 warning (_("Section .debug_names in %s has duplicate index %s, "
5496 "ignoring .debug_names."),
5497 filename, pulongest (index_num));
5498 return false;
5499 }
5500 mapped_debug_names::index_val &indexval = insertpair.first->second;
5501 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5502 addr += bytes_read;
5503
5504 for (;;)
5505 {
5506 mapped_debug_names::index_val::attr attr;
5507 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5508 addr += bytes_read;
5509 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5510 addr += bytes_read;
5511 if (attr.form == DW_FORM_implicit_const)
5512 {
5513 attr.implicit_const = read_signed_leb128 (abfd, addr,
5514 &bytes_read);
5515 addr += bytes_read;
5516 }
5517 if (attr.dw_idx == 0 && attr.form == 0)
5518 break;
5519 indexval.attr_vec.push_back (std::move (attr));
5520 }
5521 }
5522 if (addr != abbrev_table_start + abbrev_table_size)
5523 {
5524 warning (_("Section .debug_names in %s has abbreviation_table "
5525 "of size %zu vs. written as %u, ignoring .debug_names."),
5526 filename, addr - abbrev_table_start, abbrev_table_size);
5527 return false;
5528 }
5529 map.entry_pool = addr;
5530
5531 return true;
5532}
5533
5534/* A helper for create_cus_from_debug_names that handles the MAP's CU
5535 list. */
5536
5537static void
ed2dc618 5538create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5539 const mapped_debug_names &map,
5540 dwarf2_section_info &section,
b76e467d 5541 bool is_dwz)
927aa2e7
JK
5542{
5543 sect_offset sect_off_prev;
5544 for (uint32_t i = 0; i <= map.cu_count; ++i)
5545 {
5546 sect_offset sect_off_next;
5547 if (i < map.cu_count)
5548 {
5549 sect_off_next
5550 = (sect_offset) (extract_unsigned_integer
5551 (map.cu_table_reordered + i * map.offset_size,
5552 map.offset_size,
5553 map.dwarf5_byte_order));
5554 }
5555 else
5556 sect_off_next = (sect_offset) section.size;
5557 if (i >= 1)
5558 {
5559 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5560 dwarf2_per_cu_data *per_cu
ed2dc618 5561 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7 5562 sect_off_prev, length);
b76e467d 5563 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
927aa2e7
JK
5564 }
5565 sect_off_prev = sect_off_next;
5566 }
5567}
5568
5569/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5570 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5571
5572static void
ed2dc618 5573create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5574 const mapped_debug_names &map,
5575 const mapped_debug_names &dwz_map)
5576{
b76e467d
SM
5577 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5578 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5579
ed2dc618
SM
5580 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5581 dwarf2_per_objfile->info,
b76e467d 5582 false /* is_dwz */);
927aa2e7
JK
5583
5584 if (dwz_map.cu_count == 0)
5585 return;
5586
ed2dc618
SM
5587 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5588 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
b76e467d 5589 true /* is_dwz */);
927aa2e7
JK
5590}
5591
5592/* Read .debug_names. If everything went ok, initialize the "quick"
5593 elements of all the CUs and return true. Otherwise, return false. */
5594
5595static bool
ed2dc618 5596dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5597{
22ca247e
TT
5598 std::unique_ptr<mapped_debug_names> map
5599 (new mapped_debug_names (dwarf2_per_objfile));
ed2dc618
SM
5600 mapped_debug_names dwz_map (dwarf2_per_objfile);
5601 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5602
5603 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5604 &dwarf2_per_objfile->debug_names,
22ca247e 5605 *map))
927aa2e7
JK
5606 return false;
5607
5608 /* Don't use the index if it's empty. */
22ca247e 5609 if (map->name_count == 0)
927aa2e7
JK
5610 return false;
5611
5612 /* If there is a .dwz file, read it so we can get its CU list as
5613 well. */
ed2dc618 5614 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
927aa2e7
JK
5615 if (dwz != NULL)
5616 {
5617 if (!read_debug_names_from_section (objfile,
5618 bfd_get_filename (dwz->dwz_bfd),
5619 &dwz->debug_names, dwz_map))
5620 {
5621 warning (_("could not read '.debug_names' section from %s; skipping"),
5622 bfd_get_filename (dwz->dwz_bfd));
5623 return false;
5624 }
5625 }
5626
22ca247e 5627 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
927aa2e7 5628
22ca247e 5629 if (map->tu_count != 0)
927aa2e7
JK
5630 {
5631 /* We can only handle a single .debug_types when we have an
5632 index. */
5633 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
5634 return false;
5635
5636 dwarf2_section_info *section = VEC_index (dwarf2_section_info_def,
5637 dwarf2_per_objfile->types, 0);
5638
5639 create_signatured_type_table_from_debug_names
22ca247e 5640 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
927aa2e7
JK
5641 }
5642
ed2dc618
SM
5643 create_addrmap_from_aranges (dwarf2_per_objfile,
5644 &dwarf2_per_objfile->debug_aranges);
927aa2e7 5645
22ca247e 5646 dwarf2_per_objfile->debug_names_table = std::move (map);
927aa2e7
JK
5647 dwarf2_per_objfile->using_index = 1;
5648 dwarf2_per_objfile->quick_file_names_table =
b76e467d 5649 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
927aa2e7
JK
5650
5651 return true;
5652}
5653
927aa2e7
JK
5654/* Type used to manage iterating over all CUs looking for a symbol for
5655 .debug_names. */
5656
5657class dw2_debug_names_iterator
5658{
5659public:
5660 /* If WANT_SPECIFIC_BLOCK is true, only look for symbols in block
5661 BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
5662 dw2_debug_names_iterator (const mapped_debug_names &map,
5663 bool want_specific_block,
5664 block_enum block_index, domain_enum domain,
5665 const char *name)
5666 : m_map (map), m_want_specific_block (want_specific_block),
5667 m_block_index (block_index), m_domain (domain),
5668 m_addr (find_vec_in_debug_names (map, name))
5669 {}
5670
5671 dw2_debug_names_iterator (const mapped_debug_names &map,
5672 search_domain search, uint32_t namei)
5673 : m_map (map),
5674 m_search (search),
5675 m_addr (find_vec_in_debug_names (map, namei))
5676 {}
5677
5678 /* Return the next matching CU or NULL if there are no more. */
5679 dwarf2_per_cu_data *next ();
5680
5681private:
5682 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5683 const char *name);
5684 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5685 uint32_t namei);
5686
5687 /* The internalized form of .debug_names. */
5688 const mapped_debug_names &m_map;
5689
5690 /* If true, only look for symbols that match BLOCK_INDEX. */
5691 const bool m_want_specific_block = false;
5692
5693 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
5694 Unused if !WANT_SPECIFIC_BLOCK - FIRST_LOCAL_BLOCK is an invalid
5695 value. */
5696 const block_enum m_block_index = FIRST_LOCAL_BLOCK;
5697
5698 /* The kind of symbol we're looking for. */
5699 const domain_enum m_domain = UNDEF_DOMAIN;
5700 const search_domain m_search = ALL_DOMAIN;
5701
5702 /* The list of CUs from the index entry of the symbol, or NULL if
5703 not found. */
5704 const gdb_byte *m_addr;
5705};
5706
5707const char *
5708mapped_debug_names::namei_to_name (uint32_t namei) const
5709{
5710 const ULONGEST namei_string_offs
5711 = extract_unsigned_integer ((name_table_string_offs_reordered
5712 + namei * offset_size),
5713 offset_size,
5714 dwarf5_byte_order);
5715 return read_indirect_string_at_offset
ed2dc618 5716 (dwarf2_per_objfile, dwarf2_per_objfile->objfile->obfd, namei_string_offs);
927aa2e7
JK
5717}
5718
5719/* Find a slot in .debug_names for the object named NAME. If NAME is
5720 found, return pointer to its pool data. If NAME cannot be found,
5721 return NULL. */
5722
5723const gdb_byte *
5724dw2_debug_names_iterator::find_vec_in_debug_names
5725 (const mapped_debug_names &map, const char *name)
5726{
5727 int (*cmp) (const char *, const char *);
5728
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 {
5738 gdb::unique_xmalloc_ptr<char> without_params
5739 = cp_remove_params (name);
5740
5741 if (without_params != NULL)
5742 {
5743 name = without_params.get();
5744 }
5745 }
5746 }
5747
5748 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5749
5750 const uint32_t full_hash = dwarf5_djb_hash (name);
5751 uint32_t namei
5752 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5753 (map.bucket_table_reordered
5754 + (full_hash % map.bucket_count)), 4,
5755 map.dwarf5_byte_order);
5756 if (namei == 0)
5757 return NULL;
5758 --namei;
5759 if (namei >= map.name_count)
5760 {
b98664d3 5761 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5762 "[in module %s]"),
5763 namei, map.name_count,
ed2dc618 5764 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5765 return NULL;
5766 }
5767
5768 for (;;)
5769 {
5770 const uint32_t namei_full_hash
5771 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5772 (map.hash_table_reordered + namei), 4,
5773 map.dwarf5_byte_order);
5774 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5775 return NULL;
5776
5777 if (full_hash == namei_full_hash)
5778 {
5779 const char *const namei_string = map.namei_to_name (namei);
5780
5781#if 0 /* An expensive sanity check. */
5782 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5783 {
b98664d3 5784 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5785 "[in module %s]"),
5786 namei, objfile_name (dwarf2_per_objfile->objfile));
5787 return NULL;
5788 }
5789#endif
5790
5791 if (cmp (namei_string, name) == 0)
5792 {
5793 const ULONGEST namei_entry_offs
5794 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5795 + namei * map.offset_size),
5796 map.offset_size, map.dwarf5_byte_order);
5797 return map.entry_pool + namei_entry_offs;
5798 }
5799 }
5800
5801 ++namei;
5802 if (namei >= map.name_count)
5803 return NULL;
5804 }
5805}
5806
5807const gdb_byte *
5808dw2_debug_names_iterator::find_vec_in_debug_names
5809 (const mapped_debug_names &map, uint32_t namei)
5810{
5811 if (namei >= map.name_count)
5812 {
b98664d3 5813 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5814 "[in module %s]"),
5815 namei, map.name_count,
ed2dc618 5816 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5817 return NULL;
5818 }
5819
5820 const ULONGEST namei_entry_offs
5821 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5822 + namei * map.offset_size),
5823 map.offset_size, map.dwarf5_byte_order);
5824 return map.entry_pool + namei_entry_offs;
5825}
5826
5827/* See dw2_debug_names_iterator. */
5828
5829dwarf2_per_cu_data *
5830dw2_debug_names_iterator::next ()
5831{
5832 if (m_addr == NULL)
5833 return NULL;
5834
ed2dc618
SM
5835 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5836 struct objfile *objfile = dwarf2_per_objfile->objfile;
5837 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5838
5839 again:
5840
5841 unsigned int bytes_read;
5842 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5843 m_addr += bytes_read;
5844 if (abbrev == 0)
5845 return NULL;
5846
5847 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5848 if (indexval_it == m_map.abbrev_map.cend ())
5849 {
b98664d3 5850 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5851 "[in module %s]"),
ed2dc618 5852 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5853 return NULL;
5854 }
5855 const mapped_debug_names::index_val &indexval = indexval_it->second;
5856 bool have_is_static = false;
5857 bool is_static;
5858 dwarf2_per_cu_data *per_cu = NULL;
5859 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5860 {
5861 ULONGEST ull;
5862 switch (attr.form)
5863 {
5864 case DW_FORM_implicit_const:
5865 ull = attr.implicit_const;
5866 break;
5867 case DW_FORM_flag_present:
5868 ull = 1;
5869 break;
5870 case DW_FORM_udata:
5871 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5872 m_addr += bytes_read;
5873 break;
5874 default:
b98664d3 5875 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5876 dwarf_form_name (attr.form),
ed2dc618 5877 objfile_name (objfile));
927aa2e7
JK
5878 return NULL;
5879 }
5880 switch (attr.dw_idx)
5881 {
5882 case DW_IDX_compile_unit:
5883 /* Don't crash on bad data. */
b76e467d 5884 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
927aa2e7 5885 {
b98664d3 5886 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5887 " [in module %s]"),
5888 pulongest (ull),
5889 objfile_name (dwarf2_per_objfile->objfile));
5890 continue;
5891 }
ff4c9fec 5892 per_cu = dwarf2_per_objfile->get_cutu (ull);
927aa2e7 5893 break;
8af5c486
JK
5894 case DW_IDX_type_unit:
5895 /* Don't crash on bad data. */
b2bdb8cf 5896 if (ull >= dwarf2_per_objfile->all_type_units.size ())
8af5c486 5897 {
b98664d3 5898 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5899 " [in module %s]"),
5900 pulongest (ull),
5901 objfile_name (dwarf2_per_objfile->objfile));
5902 continue;
5903 }
ff4c9fec 5904 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
8af5c486 5905 break;
927aa2e7
JK
5906 case DW_IDX_GNU_internal:
5907 if (!m_map.augmentation_is_gdb)
5908 break;
5909 have_is_static = true;
5910 is_static = true;
5911 break;
5912 case DW_IDX_GNU_external:
5913 if (!m_map.augmentation_is_gdb)
5914 break;
5915 have_is_static = true;
5916 is_static = false;
5917 break;
5918 }
5919 }
5920
5921 /* Skip if already read in. */
5922 if (per_cu->v.quick->compunit_symtab)
5923 goto again;
5924
5925 /* Check static vs global. */
5926 if (have_is_static)
5927 {
5928 const bool want_static = m_block_index != GLOBAL_BLOCK;
5929 if (m_want_specific_block && want_static != is_static)
5930 goto again;
5931 }
5932
5933 /* Match dw2_symtab_iter_next, symbol_kind
5934 and debug_names::psymbol_tag. */
5935 switch (m_domain)
5936 {
5937 case VAR_DOMAIN:
5938 switch (indexval.dwarf_tag)
5939 {
5940 case DW_TAG_variable:
5941 case DW_TAG_subprogram:
5942 /* Some types are also in VAR_DOMAIN. */
5943 case DW_TAG_typedef:
5944 case DW_TAG_structure_type:
5945 break;
5946 default:
5947 goto again;
5948 }
5949 break;
5950 case STRUCT_DOMAIN:
5951 switch (indexval.dwarf_tag)
5952 {
5953 case DW_TAG_typedef:
5954 case DW_TAG_structure_type:
5955 break;
5956 default:
5957 goto again;
5958 }
5959 break;
5960 case LABEL_DOMAIN:
5961 switch (indexval.dwarf_tag)
5962 {
5963 case 0:
5964 case DW_TAG_variable:
5965 break;
5966 default:
5967 goto again;
5968 }
5969 break;
5970 default:
5971 break;
5972 }
5973
5974 /* Match dw2_expand_symtabs_matching, symbol_kind and
5975 debug_names::psymbol_tag. */
5976 switch (m_search)
4b514bc8 5977 {
927aa2e7
JK
5978 case VARIABLES_DOMAIN:
5979 switch (indexval.dwarf_tag)
4b514bc8 5980 {
927aa2e7
JK
5981 case DW_TAG_variable:
5982 break;
5983 default:
5984 goto again;
4b514bc8 5985 }
927aa2e7
JK
5986 break;
5987 case FUNCTIONS_DOMAIN:
5988 switch (indexval.dwarf_tag)
4b514bc8 5989 {
927aa2e7
JK
5990 case DW_TAG_subprogram:
5991 break;
5992 default:
5993 goto again;
4b514bc8 5994 }
927aa2e7
JK
5995 break;
5996 case TYPES_DOMAIN:
5997 switch (indexval.dwarf_tag)
5998 {
5999 case DW_TAG_typedef:
6000 case DW_TAG_structure_type:
6001 break;
6002 default:
6003 goto again;
6004 }
6005 break;
6006 default:
6007 break;
4b514bc8 6008 }
927aa2e7
JK
6009
6010 return per_cu;
4b514bc8 6011}
61920122 6012
927aa2e7
JK
6013static struct compunit_symtab *
6014dw2_debug_names_lookup_symbol (struct objfile *objfile, int block_index_int,
6015 const char *name, domain_enum domain)
4b514bc8 6016{
927aa2e7 6017 const block_enum block_index = static_cast<block_enum> (block_index_int);
ed2dc618
SM
6018 struct dwarf2_per_objfile *dwarf2_per_objfile
6019 = get_dwarf2_per_objfile (objfile);
61920122 6020
927aa2e7
JK
6021 const auto &mapp = dwarf2_per_objfile->debug_names_table;
6022 if (!mapp)
61920122 6023 {
927aa2e7
JK
6024 /* index is NULL if OBJF_READNOW. */
6025 return NULL;
6026 }
6027 const auto &map = *mapp;
9291a0cd 6028
927aa2e7
JK
6029 dw2_debug_names_iterator iter (map, true /* want_specific_block */,
6030 block_index, domain, name);
9703b513 6031
927aa2e7
JK
6032 struct compunit_symtab *stab_best = NULL;
6033 struct dwarf2_per_cu_data *per_cu;
6034 while ((per_cu = iter.next ()) != NULL)
6035 {
6036 struct symbol *sym, *with_opaque = NULL;
58f0c718 6037 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
927aa2e7
JK
6038 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
6039 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 6040
927aa2e7
JK
6041 sym = block_find_symbol (block, name, domain,
6042 block_find_non_opaque_type_preferred,
6043 &with_opaque);
9703b513 6044
927aa2e7
JK
6045 /* Some caution must be observed with overloaded functions and
6046 methods, since the index will not contain any overload
6047 information (but NAME might contain it). */
a3ec0bb1 6048
927aa2e7
JK
6049 if (sym != NULL
6050 && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
6051 return stab;
6052 if (with_opaque != NULL
6053 && strcmp_iw (SYMBOL_SEARCH_NAME (with_opaque), name) == 0)
6054 stab_best = stab;
9703b513 6055
927aa2e7 6056 /* Keep looking through other CUs. */
9703b513
TT
6057 }
6058
927aa2e7 6059 return stab_best;
9703b513
TT
6060}
6061
927aa2e7
JK
6062/* This dumps minimal information about .debug_names. It is called
6063 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
6064 uses this to verify that .debug_names has been loaded. */
9291a0cd 6065
927aa2e7
JK
6066static void
6067dw2_debug_names_dump (struct objfile *objfile)
6068{
ed2dc618
SM
6069 struct dwarf2_per_objfile *dwarf2_per_objfile
6070 = get_dwarf2_per_objfile (objfile);
6071
927aa2e7
JK
6072 gdb_assert (dwarf2_per_objfile->using_index);
6073 printf_filtered (".debug_names:");
6074 if (dwarf2_per_objfile->debug_names_table)
6075 printf_filtered (" exists\n");
6076 else
6077 printf_filtered (" faked for \"readnow\"\n");
6078 printf_filtered ("\n");
9291a0cd
TT
6079}
6080
9291a0cd 6081static void
927aa2e7
JK
6082dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
6083 const char *func_name)
9291a0cd 6084{
ed2dc618
SM
6085 struct dwarf2_per_objfile *dwarf2_per_objfile
6086 = get_dwarf2_per_objfile (objfile);
ae2de4f8 6087
927aa2e7
JK
6088 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
6089 if (dwarf2_per_objfile->debug_names_table)
24c79950 6090 {
927aa2e7 6091 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
24c79950 6092
927aa2e7
JK
6093 /* Note: It doesn't matter what we pass for block_index here. */
6094 dw2_debug_names_iterator iter (map, false /* want_specific_block */,
6095 GLOBAL_BLOCK, VAR_DOMAIN, func_name);
24c79950 6096
927aa2e7
JK
6097 struct dwarf2_per_cu_data *per_cu;
6098 while ((per_cu = iter.next ()) != NULL)
58f0c718 6099 dw2_instantiate_symtab (per_cu, false);
927aa2e7
JK
6100 }
6101}
24c79950 6102
927aa2e7
JK
6103static void
6104dw2_debug_names_expand_symtabs_matching
6105 (struct objfile *objfile,
6106 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
6107 const lookup_name_info &lookup_name,
6108 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
6109 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6110 enum search_domain kind)
6111{
ed2dc618
SM
6112 struct dwarf2_per_objfile *dwarf2_per_objfile
6113 = get_dwarf2_per_objfile (objfile);
9291a0cd 6114
927aa2e7
JK
6115 /* debug_names_table is NULL if OBJF_READNOW. */
6116 if (!dwarf2_per_objfile->debug_names_table)
6117 return;
9291a0cd 6118
ed2dc618 6119 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 6120
44ed8f3e 6121 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
bbf2f4df 6122
44ed8f3e
PA
6123 dw2_expand_symtabs_matching_symbol (map, lookup_name,
6124 symbol_matcher,
6125 kind, [&] (offset_type namei)
927aa2e7 6126 {
927aa2e7
JK
6127 /* The name was matched, now expand corresponding CUs that were
6128 marked. */
6129 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 6130
927aa2e7
JK
6131 struct dwarf2_per_cu_data *per_cu;
6132 while ((per_cu = iter.next ()) != NULL)
6133 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
6134 expansion_notify);
44ed8f3e 6135 });
9291a0cd
TT
6136}
6137
927aa2e7 6138const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
6139{
6140 dw2_has_symbols,
6141 dw2_find_last_source_symtab,
6142 dw2_forget_cached_source_info,
f8eba3c6 6143 dw2_map_symtabs_matching_filename,
927aa2e7 6144 dw2_debug_names_lookup_symbol,
9291a0cd 6145 dw2_print_stats,
927aa2e7 6146 dw2_debug_names_dump,
927aa2e7 6147 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 6148 dw2_expand_all_symtabs,
652a8996 6149 dw2_expand_symtabs_with_fullname,
40658b94 6150 dw2_map_matching_symbols,
927aa2e7 6151 dw2_debug_names_expand_symtabs_matching,
43f3e411 6152 dw2_find_pc_sect_compunit_symtab,
71a3c369 6153 NULL,
9291a0cd
TT
6154 dw2_map_symbol_filenames
6155};
6156
3c0aa29a 6157/* See symfile.h. */
9291a0cd 6158
3c0aa29a
PA
6159bool
6160dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 6161{
ed2dc618
SM
6162 struct dwarf2_per_objfile *dwarf2_per_objfile
6163 = get_dwarf2_per_objfile (objfile);
6164
9291a0cd
TT
6165 /* If we're about to read full symbols, don't bother with the
6166 indices. In this case we also don't care if some other debug
6167 format is making psymtabs, because they are all about to be
6168 expanded anyway. */
6169 if ((objfile->flags & OBJF_READNOW))
6170 {
9291a0cd 6171 dwarf2_per_objfile->using_index = 1;
ed2dc618
SM
6172 create_all_comp_units (dwarf2_per_objfile);
6173 create_all_type_units (dwarf2_per_objfile);
b76e467d
SM
6174 dwarf2_per_objfile->quick_file_names_table
6175 = create_quick_file_names_table
6176 (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd 6177
b76e467d 6178 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 6179 + dwarf2_per_objfile->all_type_units.size ()); ++i)
9291a0cd 6180 {
ff4c9fec 6181 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 6182
e254ef6a
DE
6183 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6184 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
6185 }
6186
6187 /* Return 1 so that gdb sees the "quick" functions. However,
6188 these functions will be no-ops because we will have expanded
6189 all symtabs. */
3c0aa29a
PA
6190 *index_kind = dw_index_kind::GDB_INDEX;
6191 return true;
9291a0cd
TT
6192 }
6193
ed2dc618 6194 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
6195 {
6196 *index_kind = dw_index_kind::DEBUG_NAMES;
6197 return true;
6198 }
927aa2e7 6199
7b23e087 6200 if (dwarf2_read_gdb_index (dwarf2_per_objfile))
3c0aa29a
PA
6201 {
6202 *index_kind = dw_index_kind::GDB_INDEX;
6203 return true;
6204 }
9291a0cd 6205
3c0aa29a 6206 return false;
9291a0cd
TT
6207}
6208
6209\f
6210
dce234bc
PP
6211/* Build a partial symbol table. */
6212
6213void
f29dff0a 6214dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 6215{
ed2dc618
SM
6216 struct dwarf2_per_objfile *dwarf2_per_objfile
6217 = get_dwarf2_per_objfile (objfile);
c9bf0622 6218
af5bf4ad
SM
6219 if (objfile->global_psymbols.capacity () == 0
6220 && objfile->static_psymbols.capacity () == 0)
6221 init_psymbol_list (objfile, 1024);
c906108c 6222
492d29ea 6223 TRY
c9bf0622
TT
6224 {
6225 /* This isn't really ideal: all the data we allocate on the
6226 objfile's obstack is still uselessly kept around. However,
6227 freeing it seems unsafe. */
906768f9 6228 psymtab_discarder psymtabs (objfile);
ed2dc618 6229 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 6230 psymtabs.keep ();
c9bf0622 6231 }
492d29ea
PA
6232 CATCH (except, RETURN_MASK_ERROR)
6233 {
6234 exception_print (gdb_stderr, except);
6235 }
6236 END_CATCH
c906108c 6237}
c906108c 6238
1ce1cefd
DE
6239/* Return the total length of the CU described by HEADER. */
6240
6241static unsigned int
6242get_cu_length (const struct comp_unit_head *header)
6243{
6244 return header->initial_length_size + header->length;
6245}
6246
9c541725 6247/* Return TRUE if SECT_OFF is within CU_HEADER. */
45452591 6248
9c541725
PA
6249static inline bool
6250offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
45452591 6251{
9c541725
PA
6252 sect_offset bottom = cu_header->sect_off;
6253 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
9a619af0 6254
9c541725 6255 return sect_off >= bottom && sect_off < top;
45452591
DE
6256}
6257
3b80fe9b
DE
6258/* Find the base address of the compilation unit for range lists and
6259 location lists. It will normally be specified by DW_AT_low_pc.
6260 In DWARF-3 draft 4, the base address could be overridden by
6261 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6262 compilation units with discontinuous ranges. */
6263
6264static void
6265dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6266{
6267 struct attribute *attr;
6268
6269 cu->base_known = 0;
6270 cu->base_address = 0;
6271
6272 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6273 if (attr)
6274 {
31aa7e4e 6275 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6276 cu->base_known = 1;
6277 }
6278 else
6279 {
6280 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6281 if (attr)
6282 {
31aa7e4e 6283 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6284 cu->base_known = 1;
6285 }
6286 }
6287}
6288
93311388 6289/* Read in the comp unit header information from the debug_info at info_ptr.
43988095 6290 Use rcuh_kind::COMPILE as the default type if not known by the caller.
93311388
DE
6291 NOTE: This leaves members offset, first_die_offset to be filled in
6292 by the caller. */
107d2387 6293
d521ce57 6294static const gdb_byte *
107d2387 6295read_comp_unit_head (struct comp_unit_head *cu_header,
43988095
JK
6296 const gdb_byte *info_ptr,
6297 struct dwarf2_section_info *section,
6298 rcuh_kind section_kind)
107d2387
AC
6299{
6300 int signed_addr;
891d2f0b 6301 unsigned int bytes_read;
43988095
JK
6302 const char *filename = get_section_file_name (section);
6303 bfd *abfd = get_section_bfd_owner (section);
c764a876
DE
6304
6305 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6306 cu_header->initial_length_size = bytes_read;
6307 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 6308 info_ptr += bytes_read;
107d2387 6309 cu_header->version = read_2_bytes (abfd, info_ptr);
1ea5da02
TV
6310 if (cu_header->version < 2 || cu_header->version > 5)
6311 error (_("Dwarf Error: wrong version in compilation unit header "
6312 "(is %d, should be 2, 3, 4 or 5) [in module %s]"),
6313 cu_header->version, filename);
107d2387 6314 info_ptr += 2;
43988095
JK
6315 if (cu_header->version < 5)
6316 switch (section_kind)
6317 {
6318 case rcuh_kind::COMPILE:
6319 cu_header->unit_type = DW_UT_compile;
6320 break;
6321 case rcuh_kind::TYPE:
6322 cu_header->unit_type = DW_UT_type;
6323 break;
6324 default:
6325 internal_error (__FILE__, __LINE__,
6326 _("read_comp_unit_head: invalid section_kind"));
6327 }
6328 else
6329 {
6330 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6331 (read_1_byte (abfd, info_ptr));
6332 info_ptr += 1;
6333 switch (cu_header->unit_type)
6334 {
6335 case DW_UT_compile:
6336 if (section_kind != rcuh_kind::COMPILE)
6337 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6338 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
6339 filename);
6340 break;
6341 case DW_UT_type:
6342 section_kind = rcuh_kind::TYPE;
6343 break;
6344 default:
6345 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6346 "(is %d, should be %d or %d) [in module %s]"),
6347 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
6348 }
6349
6350 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6351 info_ptr += 1;
6352 }
9c541725
PA
6353 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6354 cu_header,
6355 &bytes_read);
613e1657 6356 info_ptr += bytes_read;
43988095
JK
6357 if (cu_header->version < 5)
6358 {
6359 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6360 info_ptr += 1;
6361 }
107d2387
AC
6362 signed_addr = bfd_get_sign_extend_vma (abfd);
6363 if (signed_addr < 0)
8e65ff28 6364 internal_error (__FILE__, __LINE__,
e2e0b3e5 6365 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 6366 cu_header->signed_addr_p = signed_addr;
c764a876 6367
43988095
JK
6368 if (section_kind == rcuh_kind::TYPE)
6369 {
6370 LONGEST type_offset;
6371
6372 cu_header->signature = read_8_bytes (abfd, info_ptr);
6373 info_ptr += 8;
6374
6375 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6376 info_ptr += bytes_read;
9c541725
PA
6377 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6378 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
43988095
JK
6379 error (_("Dwarf Error: Too big type_offset in compilation unit "
6380 "header (is %s) [in module %s]"), plongest (type_offset),
6381 filename);
6382 }
6383
107d2387
AC
6384 return info_ptr;
6385}
6386
36586728
TT
6387/* Helper function that returns the proper abbrev section for
6388 THIS_CU. */
6389
6390static struct dwarf2_section_info *
6391get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6392{
6393 struct dwarf2_section_info *abbrev;
ed2dc618 6394 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
36586728
TT
6395
6396 if (this_cu->is_dwz)
ed2dc618 6397 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
36586728
TT
6398 else
6399 abbrev = &dwarf2_per_objfile->abbrev;
6400
6401 return abbrev;
6402}
6403
9ff913ba
DE
6404/* Subroutine of read_and_check_comp_unit_head and
6405 read_and_check_type_unit_head to simplify them.
6406 Perform various error checking on the header. */
6407
6408static void
ed2dc618
SM
6409error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6410 struct comp_unit_head *header,
4bdcc0c1
DE
6411 struct dwarf2_section_info *section,
6412 struct dwarf2_section_info *abbrev_section)
9ff913ba 6413{
a32a8923 6414 const char *filename = get_section_file_name (section);
9ff913ba 6415
9c541725 6416 if (to_underlying (header->abbrev_sect_off)
36586728 6417 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9d8780f0
SM
6418 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6419 "(offset %s + 6) [in module %s]"),
6420 sect_offset_str (header->abbrev_sect_off),
6421 sect_offset_str (header->sect_off),
9ff913ba
DE
6422 filename);
6423
9c541725 6424 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
9ff913ba 6425 avoid potential 32-bit overflow. */
9c541725 6426 if (((ULONGEST) header->sect_off + get_cu_length (header))
9ff913ba 6427 > section->size)
9c541725 6428 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
9d8780f0
SM
6429 "(offset %s + 0) [in module %s]"),
6430 header->length, sect_offset_str (header->sect_off),
9ff913ba
DE
6431 filename);
6432}
6433
6434/* Read in a CU/TU header and perform some basic error checking.
6435 The contents of the header are stored in HEADER.
6436 The result is a pointer to the start of the first DIE. */
adabb602 6437
d521ce57 6438static const gdb_byte *
ed2dc618
SM
6439read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6440 struct comp_unit_head *header,
9ff913ba 6441 struct dwarf2_section_info *section,
4bdcc0c1 6442 struct dwarf2_section_info *abbrev_section,
d521ce57 6443 const gdb_byte *info_ptr,
43988095 6444 rcuh_kind section_kind)
72bf9492 6445{
d521ce57 6446 const gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492 6447
9c541725 6448 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
adabb602 6449
43988095 6450 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
9ff913ba 6451
9c541725 6452 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
348e048f 6453
ed2dc618
SM
6454 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6455 abbrev_section);
9ff913ba
DE
6456
6457 return info_ptr;
348e048f
DE
6458}
6459
f4dc4d17
DE
6460/* Fetch the abbreviation table offset from a comp or type unit header. */
6461
6462static sect_offset
ed2dc618
SM
6463read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6464 struct dwarf2_section_info *section,
9c541725 6465 sect_offset sect_off)
f4dc4d17 6466{
a32a8923 6467 bfd *abfd = get_section_bfd_owner (section);
d521ce57 6468 const gdb_byte *info_ptr;
ac298888 6469 unsigned int initial_length_size, offset_size;
43988095 6470 uint16_t version;
f4dc4d17
DE
6471
6472 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
9c541725 6473 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6474 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6475 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6476 info_ptr += initial_length_size;
6477
6478 version = read_2_bytes (abfd, info_ptr);
6479 info_ptr += 2;
6480 if (version >= 5)
6481 {
6482 /* Skip unit type and address size. */
6483 info_ptr += 2;
6484 }
6485
9c541725 6486 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
f4dc4d17
DE
6487}
6488
aaa75496
JB
6489/* Allocate a new partial symtab for file named NAME and mark this new
6490 partial symtab as being an include of PST. */
6491
6492static void
d521ce57 6493dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
6494 struct objfile *objfile)
6495{
6496 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6497
fbd9ab74
JK
6498 if (!IS_ABSOLUTE_PATH (subpst->filename))
6499 {
6500 /* It shares objfile->objfile_obstack. */
6501 subpst->dirname = pst->dirname;
6502 }
6503
8d749320
SM
6504 subpst->dependencies
6505 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
aaa75496
JB
6506 subpst->dependencies[0] = pst;
6507 subpst->number_of_dependencies = 1;
6508
6509 subpst->globals_offset = 0;
6510 subpst->n_global_syms = 0;
6511 subpst->statics_offset = 0;
6512 subpst->n_static_syms = 0;
43f3e411 6513 subpst->compunit_symtab = NULL;
aaa75496
JB
6514 subpst->read_symtab = pst->read_symtab;
6515 subpst->readin = 0;
6516
6517 /* No private part is necessary for include psymtabs. This property
6518 can be used to differentiate between such include psymtabs and
10b3939b 6519 the regular ones. */
58a9656e 6520 subpst->read_symtab_private = NULL;
aaa75496
JB
6521}
6522
6523/* Read the Line Number Program data and extract the list of files
6524 included by the source file represented by PST. Build an include
d85a05f0 6525 partial symtab for each of these included files. */
aaa75496
JB
6526
6527static void
6528dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
6529 struct die_info *die,
6530 struct partial_symtab *pst)
aaa75496 6531{
fff8551c 6532 line_header_up lh;
d85a05f0 6533 struct attribute *attr;
aaa75496 6534
d85a05f0
DJ
6535 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6536 if (attr)
9c541725 6537 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6538 if (lh == NULL)
6539 return; /* No linetable, so no includes. */
6540
79748972
TT
6541 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6542 that we pass in the raw text_low here; that is ok because we're
6543 only decoding the line table to make include partial symtabs, and
6544 so the addresses aren't really used. */
4ae976d1 6545 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6546 pst->raw_text_low (), 1);
aaa75496
JB
6547}
6548
348e048f 6549static hashval_t
52dc124a 6550hash_signatured_type (const void *item)
348e048f 6551{
9a3c8263
SM
6552 const struct signatured_type *sig_type
6553 = (const struct signatured_type *) item;
9a619af0 6554
348e048f 6555 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6556 return sig_type->signature;
348e048f
DE
6557}
6558
6559static int
52dc124a 6560eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6561{
9a3c8263
SM
6562 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6563 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6564
348e048f
DE
6565 return lhs->signature == rhs->signature;
6566}
6567
1fd400ff
TT
6568/* Allocate a hash table for signatured types. */
6569
6570static htab_t
673bfd45 6571allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
6572{
6573 return htab_create_alloc_ex (41,
52dc124a
DE
6574 hash_signatured_type,
6575 eq_signatured_type,
1fd400ff
TT
6576 NULL,
6577 &objfile->objfile_obstack,
6578 hashtab_obstack_allocate,
6579 dummy_obstack_deallocate);
6580}
6581
d467dd73 6582/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6583
6584static int
d467dd73 6585add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6586{
9a3c8263 6587 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6588 std::vector<signatured_type *> *all_type_units
6589 = (std::vector<signatured_type *> *) datum;
1fd400ff 6590
b2bdb8cf 6591 all_type_units->push_back (sigt);
1fd400ff
TT
6592
6593 return 1;
6594}
6595
78d4d2c5 6596/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6597 and fill them into TYPES_HTAB. It will process only type units,
6598 therefore DW_UT_type. */
c88ee1f0 6599
78d4d2c5 6600static void
ed2dc618
SM
6601create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6602 struct dwo_file *dwo_file,
43988095
JK
6603 dwarf2_section_info *section, htab_t &types_htab,
6604 rcuh_kind section_kind)
348e048f 6605{
3019eac3 6606 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6607 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6608 bfd *abfd;
6609 const gdb_byte *info_ptr, *end_ptr;
348e048f 6610
4bdcc0c1
DE
6611 abbrev_section = (dwo_file != NULL
6612 ? &dwo_file->sections.abbrev
6613 : &dwarf2_per_objfile->abbrev);
6614
b4f54984 6615 if (dwarf_read_debug)
43988095
JK
6616 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6617 get_section_name (section),
a32a8923 6618 get_section_file_name (abbrev_section));
09406207 6619
78d4d2c5
JK
6620 dwarf2_read_section (objfile, section);
6621 info_ptr = section->buffer;
348e048f 6622
78d4d2c5
JK
6623 if (info_ptr == NULL)
6624 return;
348e048f 6625
78d4d2c5
JK
6626 /* We can't set abfd until now because the section may be empty or
6627 not present, in which case the bfd is unknown. */
6628 abfd = get_section_bfd_owner (section);
348e048f 6629
78d4d2c5
JK
6630 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6631 because we don't need to read any dies: the signature is in the
6632 header. */
3019eac3 6633
78d4d2c5
JK
6634 end_ptr = info_ptr + section->size;
6635 while (info_ptr < end_ptr)
6636 {
78d4d2c5
JK
6637 struct signatured_type *sig_type;
6638 struct dwo_unit *dwo_tu;
6639 void **slot;
6640 const gdb_byte *ptr = info_ptr;
6641 struct comp_unit_head header;
6642 unsigned int length;
8b70b953 6643
9c541725 6644 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6645
a49dd8dd
JK
6646 /* Initialize it due to a false compiler warning. */
6647 header.signature = -1;
9c541725 6648 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6649
78d4d2c5
JK
6650 /* We need to read the type's signature in order to build the hash
6651 table, but we don't need anything else just yet. */
348e048f 6652
ed2dc618 6653 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6654 abbrev_section, ptr, section_kind);
348e048f 6655
78d4d2c5 6656 length = get_cu_length (&header);
6caca83c 6657
78d4d2c5
JK
6658 /* Skip dummy type units. */
6659 if (ptr >= info_ptr + length
43988095
JK
6660 || peek_abbrev_code (abfd, ptr) == 0
6661 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6662 {
6663 info_ptr += length;
6664 continue;
6665 }
dee91e82 6666
78d4d2c5
JK
6667 if (types_htab == NULL)
6668 {
6669 if (dwo_file)
6670 types_htab = allocate_dwo_unit_table (objfile);
6671 else
6672 types_htab = allocate_signatured_type_table (objfile);
6673 }
8b70b953 6674
78d4d2c5
JK
6675 if (dwo_file)
6676 {
6677 sig_type = NULL;
6678 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6679 struct dwo_unit);
6680 dwo_tu->dwo_file = dwo_file;
43988095 6681 dwo_tu->signature = header.signature;
9c541725 6682 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6683 dwo_tu->section = section;
9c541725 6684 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6685 dwo_tu->length = length;
6686 }
6687 else
6688 {
6689 /* N.B.: type_offset is not usable if this type uses a DWO file.
6690 The real type_offset is in the DWO file. */
6691 dwo_tu = NULL;
6692 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6693 struct signatured_type);
43988095 6694 sig_type->signature = header.signature;
9c541725 6695 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 6696 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
6697 sig_type->per_cu.is_debug_types = 1;
6698 sig_type->per_cu.section = section;
9c541725 6699 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6700 sig_type->per_cu.length = length;
6701 }
6702
6703 slot = htab_find_slot (types_htab,
6704 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6705 INSERT);
6706 gdb_assert (slot != NULL);
6707 if (*slot != NULL)
6708 {
9c541725 6709 sect_offset dup_sect_off;
0349ea22 6710
3019eac3
DE
6711 if (dwo_file)
6712 {
78d4d2c5
JK
6713 const struct dwo_unit *dup_tu
6714 = (const struct dwo_unit *) *slot;
6715
9c541725 6716 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6717 }
6718 else
6719 {
78d4d2c5
JK
6720 const struct signatured_type *dup_tu
6721 = (const struct signatured_type *) *slot;
6722
9c541725 6723 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6724 }
8b70b953 6725
b98664d3 6726 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6727 " the entry at offset %s, signature %s"),
6728 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6729 hex_string (header.signature));
78d4d2c5
JK
6730 }
6731 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6732
78d4d2c5 6733 if (dwarf_read_debug > 1)
9d8780f0
SM
6734 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6735 sect_offset_str (sect_off),
43988095 6736 hex_string (header.signature));
3019eac3 6737
78d4d2c5
JK
6738 info_ptr += length;
6739 }
6740}
3019eac3 6741
78d4d2c5
JK
6742/* Create the hash table of all entries in the .debug_types
6743 (or .debug_types.dwo) section(s).
6744 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6745 otherwise it is NULL.
b3c8eb43 6746
78d4d2c5 6747 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6748
78d4d2c5 6749 Note: This function processes DWO files only, not DWP files. */
348e048f 6750
78d4d2c5 6751static void
ed2dc618
SM
6752create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6753 struct dwo_file *dwo_file,
78d4d2c5
JK
6754 VEC (dwarf2_section_info_def) *types,
6755 htab_t &types_htab)
6756{
6757 int ix;
6758 struct dwarf2_section_info *section;
6759
6760 if (VEC_empty (dwarf2_section_info_def, types))
6761 return;
348e048f 6762
78d4d2c5
JK
6763 for (ix = 0;
6764 VEC_iterate (dwarf2_section_info_def, types, ix, section);
6765 ++ix)
ed2dc618
SM
6766 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, section,
6767 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6768}
6769
6770/* Create the hash table of all entries in the .debug_types section,
6771 and initialize all_type_units.
6772 The result is zero if there is an error (e.g. missing .debug_types section),
6773 otherwise non-zero. */
6774
6775static int
ed2dc618 6776create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6777{
78d4d2c5 6778 htab_t types_htab = NULL;
3019eac3 6779
ed2dc618
SM
6780 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6781 &dwarf2_per_objfile->info, types_htab,
43988095 6782 rcuh_kind::COMPILE);
ed2dc618
SM
6783 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6784 dwarf2_per_objfile->types, types_htab);
3019eac3
DE
6785 if (types_htab == NULL)
6786 {
6787 dwarf2_per_objfile->signatured_types = NULL;
6788 return 0;
6789 }
6790
348e048f
DE
6791 dwarf2_per_objfile->signatured_types = types_htab;
6792
b2bdb8cf
SM
6793 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6794 dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
6795
6796 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
6797 &dwarf2_per_objfile->all_type_units);
1fd400ff 6798
348e048f
DE
6799 return 1;
6800}
6801
6aa5f3a6
DE
6802/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6803 If SLOT is non-NULL, it is the entry to use in the hash table.
6804 Otherwise we find one. */
6805
6806static struct signatured_type *
ed2dc618
SM
6807add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6808 void **slot)
6aa5f3a6
DE
6809{
6810 struct objfile *objfile = dwarf2_per_objfile->objfile;
6aa5f3a6 6811
b2bdb8cf
SM
6812 if (dwarf2_per_objfile->all_type_units.size ()
6813 == dwarf2_per_objfile->all_type_units.capacity ())
6814 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6815
b2bdb8cf
SM
6816 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6817 struct signatured_type);
6818
6819 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6820 sig_type->signature = sig;
6821 sig_type->per_cu.is_debug_types = 1;
6822 if (dwarf2_per_objfile->using_index)
6823 {
6824 sig_type->per_cu.v.quick =
6825 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6826 struct dwarf2_per_cu_quick_data);
6827 }
6828
6829 if (slot == NULL)
6830 {
6831 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6832 sig_type, INSERT);
6833 }
6834 gdb_assert (*slot == NULL);
6835 *slot = sig_type;
6836 /* The rest of sig_type must be filled in by the caller. */
6837 return sig_type;
6838}
6839
a2ce51a0
DE
6840/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6841 Fill in SIG_ENTRY with DWO_ENTRY. */
6842
6843static void
ed2dc618 6844fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
6845 struct signatured_type *sig_entry,
6846 struct dwo_unit *dwo_entry)
6847{
7ee85ab1 6848 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
6849 gdb_assert (! sig_entry->per_cu.queued);
6850 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
6851 if (dwarf2_per_objfile->using_index)
6852 {
6853 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 6854 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
6855 }
6856 else
6857 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6858 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6859 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6860 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6861 gdb_assert (sig_entry->dwo_unit == NULL);
6862
6863 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6864 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6865 sig_entry->per_cu.length = dwo_entry->length;
6866 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 6867 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
a2ce51a0
DE
6868 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6869 sig_entry->dwo_unit = dwo_entry;
6870}
6871
6872/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6873 If we haven't read the TU yet, create the signatured_type data structure
6874 for a TU to be read in directly from a DWO file, bypassing the stub.
6875 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6876 using .gdb_index, then when reading a CU we want to stay in the DWO file
6877 containing that CU. Otherwise we could end up reading several other DWO
6878 files (due to comdat folding) to process the transitive closure of all the
6879 mentioned TUs, and that can be slow. The current DWO file will have every
6880 type signature that it needs.
a2ce51a0
DE
6881 We only do this for .gdb_index because in the psymtab case we already have
6882 to read all the DWOs to build the type unit groups. */
6883
6884static struct signatured_type *
6885lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6886{
518817b3
SM
6887 struct dwarf2_per_objfile *dwarf2_per_objfile
6888 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0
DE
6889 struct objfile *objfile = dwarf2_per_objfile->objfile;
6890 struct dwo_file *dwo_file;
6891 struct dwo_unit find_dwo_entry, *dwo_entry;
6892 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6893 void **slot;
a2ce51a0
DE
6894
6895 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6896
6aa5f3a6
DE
6897 /* If TU skeletons have been removed then we may not have read in any
6898 TUs yet. */
6899 if (dwarf2_per_objfile->signatured_types == NULL)
6900 {
6901 dwarf2_per_objfile->signatured_types
6902 = allocate_signatured_type_table (objfile);
6903 }
a2ce51a0
DE
6904
6905 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6906 Use the global signatured_types array to do our own comdat-folding
6907 of types. If this is the first time we're reading this TU, and
6908 the TU has an entry in .gdb_index, replace the recorded data from
6909 .gdb_index with this TU. */
a2ce51a0 6910
a2ce51a0 6911 find_sig_entry.signature = sig;
6aa5f3a6
DE
6912 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6913 &find_sig_entry, INSERT);
9a3c8263 6914 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6915
6916 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6917 read. Don't reassign the global entry to point to this DWO if that's
6918 the case. Also note that if the TU is already being read, it may not
6919 have come from a DWO, the program may be a mix of Fission-compiled
6920 code and non-Fission-compiled code. */
6921
6922 /* Have we already tried to read this TU?
6923 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6924 needn't exist in the global table yet). */
6925 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6926 return sig_entry;
6927
6aa5f3a6
DE
6928 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6929 dwo_unit of the TU itself. */
6930 dwo_file = cu->dwo_unit->dwo_file;
6931
a2ce51a0
DE
6932 /* Ok, this is the first time we're reading this TU. */
6933 if (dwo_file->tus == NULL)
6934 return NULL;
6935 find_dwo_entry.signature = sig;
9a3c8263 6936 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
a2ce51a0
DE
6937 if (dwo_entry == NULL)
6938 return NULL;
6939
6aa5f3a6
DE
6940 /* If the global table doesn't have an entry for this TU, add one. */
6941 if (sig_entry == NULL)
ed2dc618 6942 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 6943
ed2dc618 6944 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 6945 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6946 return sig_entry;
6947}
6948
a2ce51a0
DE
6949/* Subroutine of lookup_signatured_type.
6950 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6951 then try the DWP file. If the TU stub (skeleton) has been removed then
6952 it won't be in .gdb_index. */
a2ce51a0
DE
6953
6954static struct signatured_type *
6955lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6956{
518817b3
SM
6957 struct dwarf2_per_objfile *dwarf2_per_objfile
6958 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0 6959 struct objfile *objfile = dwarf2_per_objfile->objfile;
ed2dc618 6960 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
6961 struct dwo_unit *dwo_entry;
6962 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6963 void **slot;
a2ce51a0
DE
6964
6965 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6966 gdb_assert (dwp_file != NULL);
6967
6aa5f3a6
DE
6968 /* If TU skeletons have been removed then we may not have read in any
6969 TUs yet. */
6970 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 6971 {
6aa5f3a6
DE
6972 dwarf2_per_objfile->signatured_types
6973 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
6974 }
6975
6aa5f3a6
DE
6976 find_sig_entry.signature = sig;
6977 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6978 &find_sig_entry, INSERT);
9a3c8263 6979 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6980
6981 /* Have we already tried to read this TU?
6982 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6983 needn't exist in the global table yet). */
6984 if (sig_entry != NULL)
6985 return sig_entry;
6986
a2ce51a0
DE
6987 if (dwp_file->tus == NULL)
6988 return NULL;
ed2dc618 6989 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 6990 sig, 1 /* is_debug_types */);
a2ce51a0
DE
6991 if (dwo_entry == NULL)
6992 return NULL;
6993
ed2dc618
SM
6994 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6995 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 6996
a2ce51a0
DE
6997 return sig_entry;
6998}
6999
380bca97 7000/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
7001 Returns NULL if signature SIG is not present in the table.
7002 It is up to the caller to complain about this. */
348e048f
DE
7003
7004static struct signatured_type *
a2ce51a0 7005lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 7006{
518817b3
SM
7007 struct dwarf2_per_objfile *dwarf2_per_objfile
7008 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 7009
a2ce51a0
DE
7010 if (cu->dwo_unit
7011 && dwarf2_per_objfile->using_index)
7012 {
7013 /* We're in a DWO/DWP file, and we're using .gdb_index.
7014 These cases require special processing. */
ed2dc618 7015 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
7016 return lookup_dwo_signatured_type (cu, sig);
7017 else
7018 return lookup_dwp_signatured_type (cu, sig);
7019 }
7020 else
7021 {
7022 struct signatured_type find_entry, *entry;
348e048f 7023
a2ce51a0
DE
7024 if (dwarf2_per_objfile->signatured_types == NULL)
7025 return NULL;
7026 find_entry.signature = sig;
9a3c8263
SM
7027 entry = ((struct signatured_type *)
7028 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
a2ce51a0
DE
7029 return entry;
7030 }
348e048f 7031}
42e7ad6c
DE
7032\f
7033/* Low level DIE reading support. */
348e048f 7034
d85a05f0
DJ
7035/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7036
7037static void
7038init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 7039 struct dwarf2_cu *cu,
3019eac3 7040 struct dwarf2_section_info *section,
685af9cd
TT
7041 struct dwo_file *dwo_file,
7042 struct abbrev_table *abbrev_table)
d85a05f0 7043{
fceca515 7044 gdb_assert (section->readin && section->buffer != NULL);
a32a8923 7045 reader->abfd = get_section_bfd_owner (section);
d85a05f0 7046 reader->cu = cu;
3019eac3 7047 reader->dwo_file = dwo_file;
dee91e82
DE
7048 reader->die_section = section;
7049 reader->buffer = section->buffer;
f664829e 7050 reader->buffer_end = section->buffer + section->size;
a2ce51a0 7051 reader->comp_dir = NULL;
685af9cd 7052 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
7053}
7054
b0c7bfa9
DE
7055/* Subroutine of init_cutu_and_read_dies to simplify it.
7056 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7057 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7058 already.
7059
7060 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7061 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
7062 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7063 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
7064 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7065 STUB_COMP_DIR may be non-NULL.
b0c7bfa9
DE
7066 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7067 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
7068 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7069 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7070 kept around for at least as long as *RESULT_READER.
7071
b0c7bfa9
DE
7072 The result is non-zero if a valid (non-dummy) DIE was found. */
7073
7074static int
7075read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7076 struct dwo_unit *dwo_unit,
b0c7bfa9 7077 struct die_info *stub_comp_unit_die,
a2ce51a0 7078 const char *stub_comp_dir,
b0c7bfa9 7079 struct die_reader_specs *result_reader,
d521ce57 7080 const gdb_byte **result_info_ptr,
b0c7bfa9 7081 struct die_info **result_comp_unit_die,
685af9cd
TT
7082 int *result_has_children,
7083 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 7084{
ed2dc618 7085 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
b0c7bfa9
DE
7086 struct objfile *objfile = dwarf2_per_objfile->objfile;
7087 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 7088 bfd *abfd;
d521ce57 7089 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
7090 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7091 int i,num_extra_attrs;
7092 struct dwarf2_section_info *dwo_abbrev_section;
7093 struct attribute *attr;
7094 struct die_info *comp_unit_die;
7095
b0aeadb3
DE
7096 /* At most one of these may be provided. */
7097 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 7098
b0c7bfa9
DE
7099 /* These attributes aren't processed until later:
7100 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
7101 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7102 referenced later. However, these attributes are found in the stub
7103 which we won't have later. In order to not impose this complication
7104 on the rest of the code, we read them here and copy them to the
7105 DWO CU/TU die. */
b0c7bfa9
DE
7106
7107 stmt_list = NULL;
7108 low_pc = NULL;
7109 high_pc = NULL;
7110 ranges = NULL;
7111 comp_dir = NULL;
7112
7113 if (stub_comp_unit_die != NULL)
7114 {
7115 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7116 DWO file. */
7117 if (! this_cu->is_debug_types)
7118 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7119 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7120 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7121 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7122 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7123
7124 /* There should be a DW_AT_addr_base attribute here (if needed).
7125 We need the value before we can process DW_FORM_GNU_addr_index. */
7126 cu->addr_base = 0;
7127 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7128 if (attr)
7129 cu->addr_base = DW_UNSND (attr);
7130
7131 /* There should be a DW_AT_ranges_base attribute here (if needed).
7132 We need the value before we can process DW_AT_ranges. */
7133 cu->ranges_base = 0;
7134 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7135 if (attr)
7136 cu->ranges_base = DW_UNSND (attr);
7137 }
a2ce51a0
DE
7138 else if (stub_comp_dir != NULL)
7139 {
7140 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 7141 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
7142 comp_dir->name = DW_AT_comp_dir;
7143 comp_dir->form = DW_FORM_string;
7144 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7145 DW_STRING (comp_dir) = stub_comp_dir;
7146 }
b0c7bfa9
DE
7147
7148 /* Set up for reading the DWO CU/TU. */
7149 cu->dwo_unit = dwo_unit;
685af9cd 7150 dwarf2_section_info *section = dwo_unit->section;
b0c7bfa9 7151 dwarf2_read_section (objfile, section);
a32a8923 7152 abfd = get_section_bfd_owner (section);
9c541725
PA
7153 begin_info_ptr = info_ptr = (section->buffer
7154 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 7155 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9
DE
7156
7157 if (this_cu->is_debug_types)
7158 {
b0c7bfa9
DE
7159 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7160
ed2dc618
SM
7161 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7162 &cu->header, section,
b0c7bfa9 7163 dwo_abbrev_section,
43988095 7164 info_ptr, rcuh_kind::TYPE);
a2ce51a0 7165 /* This is not an assert because it can be caused by bad debug info. */
43988095 7166 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
7167 {
7168 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 7169 " TU at offset %s [in module %s]"),
a2ce51a0 7170 hex_string (sig_type->signature),
43988095 7171 hex_string (cu->header.signature),
9d8780f0 7172 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
7173 bfd_get_filename (abfd));
7174 }
9c541725 7175 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7176 /* For DWOs coming from DWP files, we don't know the CU length
7177 nor the type's offset in the TU until now. */
7178 dwo_unit->length = get_cu_length (&cu->header);
9c541725 7179 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
7180
7181 /* Establish the type offset that can be used to lookup the type.
7182 For DWO files, we don't know it until now. */
9c541725
PA
7183 sig_type->type_offset_in_section
7184 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
7185 }
7186 else
7187 {
ed2dc618
SM
7188 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7189 &cu->header, section,
b0c7bfa9 7190 dwo_abbrev_section,
43988095 7191 info_ptr, rcuh_kind::COMPILE);
9c541725 7192 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7193 /* For DWOs coming from DWP files, we don't know the CU length
7194 until now. */
7195 dwo_unit->length = get_cu_length (&cu->header);
7196 }
7197
685af9cd
TT
7198 *result_dwo_abbrev_table
7199 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7200 cu->header.abbrev_sect_off);
7201 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7202 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
7203
7204 /* Read in the die, but leave space to copy over the attributes
7205 from the stub. This has the benefit of simplifying the rest of
7206 the code - all the work to maintain the illusion of a single
7207 DW_TAG_{compile,type}_unit DIE is done here. */
7208 num_extra_attrs = ((stmt_list != NULL)
7209 + (low_pc != NULL)
7210 + (high_pc != NULL)
7211 + (ranges != NULL)
7212 + (comp_dir != NULL));
7213 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7214 result_has_children, num_extra_attrs);
7215
7216 /* Copy over the attributes from the stub to the DIE we just read in. */
7217 comp_unit_die = *result_comp_unit_die;
7218 i = comp_unit_die->num_attrs;
7219 if (stmt_list != NULL)
7220 comp_unit_die->attrs[i++] = *stmt_list;
7221 if (low_pc != NULL)
7222 comp_unit_die->attrs[i++] = *low_pc;
7223 if (high_pc != NULL)
7224 comp_unit_die->attrs[i++] = *high_pc;
7225 if (ranges != NULL)
7226 comp_unit_die->attrs[i++] = *ranges;
7227 if (comp_dir != NULL)
7228 comp_unit_die->attrs[i++] = *comp_dir;
7229 comp_unit_die->num_attrs += num_extra_attrs;
7230
b4f54984 7231 if (dwarf_die_debug)
bf6af496
DE
7232 {
7233 fprintf_unfiltered (gdb_stdlog,
7234 "Read die from %s@0x%x of %s:\n",
a32a8923 7235 get_section_name (section),
bf6af496
DE
7236 (unsigned) (begin_info_ptr - section->buffer),
7237 bfd_get_filename (abfd));
b4f54984 7238 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
7239 }
7240
a2ce51a0
DE
7241 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7242 TUs by skipping the stub and going directly to the entry in the DWO file.
7243 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7244 to get it via circuitous means. Blech. */
7245 if (comp_dir != NULL)
7246 result_reader->comp_dir = DW_STRING (comp_dir);
7247
b0c7bfa9
DE
7248 /* Skip dummy compilation units. */
7249 if (info_ptr >= begin_info_ptr + dwo_unit->length
7250 || peek_abbrev_code (abfd, info_ptr) == 0)
7251 return 0;
7252
7253 *result_info_ptr = info_ptr;
7254 return 1;
7255}
7256
7257/* Subroutine of init_cutu_and_read_dies to simplify it.
7258 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 7259 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
7260
7261static struct dwo_unit *
7262lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7263 struct die_info *comp_unit_die)
7264{
7265 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9
DE
7266 ULONGEST signature;
7267 struct dwo_unit *dwo_unit;
7268 const char *comp_dir, *dwo_name;
7269
a2ce51a0
DE
7270 gdb_assert (cu != NULL);
7271
b0c7bfa9 7272 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7d45c7c3
KB
7273 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7274 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
7275
7276 if (this_cu->is_debug_types)
7277 {
7278 struct signatured_type *sig_type;
7279
7280 /* Since this_cu is the first member of struct signatured_type,
7281 we can go from a pointer to one to a pointer to the other. */
7282 sig_type = (struct signatured_type *) this_cu;
7283 signature = sig_type->signature;
7284 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7285 }
7286 else
7287 {
7288 struct attribute *attr;
7289
7290 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7291 if (! attr)
7292 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7293 " [in module %s]"),
e3b94546 7294 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
b0c7bfa9
DE
7295 signature = DW_UNSND (attr);
7296 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7297 signature);
7298 }
7299
b0c7bfa9
DE
7300 return dwo_unit;
7301}
7302
a2ce51a0 7303/* Subroutine of init_cutu_and_read_dies to simplify it.
6aa5f3a6 7304 See it for a description of the parameters.
fcd3b13d 7305 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0
DE
7306
7307static void
6aa5f3a6
DE
7308init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7309 int use_existing_cu, int keep,
a2ce51a0
DE
7310 die_reader_func_ftype *die_reader_func,
7311 void *data)
7312{
fcd3b13d 7313 std::unique_ptr<dwarf2_cu> new_cu;
a2ce51a0 7314 struct signatured_type *sig_type;
a2ce51a0
DE
7315 struct die_reader_specs reader;
7316 const gdb_byte *info_ptr;
7317 struct die_info *comp_unit_die;
7318 int has_children;
ed2dc618 7319 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
a2ce51a0
DE
7320
7321 /* Verify we can do the following downcast, and that we have the
7322 data we need. */
7323 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7324 sig_type = (struct signatured_type *) this_cu;
7325 gdb_assert (sig_type->dwo_unit != NULL);
7326
6aa5f3a6
DE
7327 if (use_existing_cu && this_cu->cu != NULL)
7328 {
7329 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6
DE
7330 /* There's no need to do the rereading_dwo_cu handling that
7331 init_cutu_and_read_dies does since we don't read the stub. */
7332 }
7333 else
7334 {
7335 /* If !use_existing_cu, this_cu->cu must be NULL. */
7336 gdb_assert (this_cu->cu == NULL);
fcd3b13d 7337 new_cu.reset (new dwarf2_cu (this_cu));
6aa5f3a6
DE
7338 }
7339
7340 /* A future optimization, if needed, would be to use an existing
7341 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7342 could share abbrev tables. */
a2ce51a0 7343
685af9cd
TT
7344 /* The abbreviation table used by READER, this must live at least as long as
7345 READER. */
7346 abbrev_table_up dwo_abbrev_table;
7347
a2ce51a0 7348 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
a2ce51a0
DE
7349 NULL /* stub_comp_unit_die */,
7350 sig_type->dwo_unit->dwo_file->comp_dir,
7351 &reader, &info_ptr,
685af9cd
TT
7352 &comp_unit_die, &has_children,
7353 &dwo_abbrev_table) == 0)
a2ce51a0
DE
7354 {
7355 /* Dummy die. */
a2ce51a0
DE
7356 return;
7357 }
7358
7359 /* All the "real" work is done here. */
7360 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7361
6aa5f3a6 7362 /* This duplicates the code in init_cutu_and_read_dies,
a2ce51a0
DE
7363 but the alternative is making the latter more complex.
7364 This function is only for the special case of using DWO files directly:
7365 no point in overly complicating the general case just to handle this. */
fcd3b13d 7366 if (new_cu != NULL && keep)
a2ce51a0 7367 {
fcd3b13d
SM
7368 /* Link this CU into read_in_chain. */
7369 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7370 dwarf2_per_objfile->read_in_chain = this_cu;
7371 /* The chain owns it now. */
7372 new_cu.release ();
a2ce51a0 7373 }
a2ce51a0
DE
7374}
7375
fd820528 7376/* Initialize a CU (or TU) and read its DIEs.
3019eac3 7377 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 7378
f4dc4d17
DE
7379 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7380 Otherwise the table specified in the comp unit header is read in and used.
7381 This is an optimization for when we already have the abbrev table.
7382
dee91e82
DE
7383 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7384 Otherwise, a new CU is allocated with xmalloc.
7385
7386 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7387 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7388
7389 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 7390 linker) then DIE_READER_FUNC will not get called. */
aaa75496 7391
70221824 7392static void
fd820528 7393init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 7394 struct abbrev_table *abbrev_table,
fd820528 7395 int use_existing_cu, int keep,
58f0c718 7396 bool skip_partial,
fd820528
DE
7397 die_reader_func_ftype *die_reader_func,
7398 void *data)
c906108c 7399{
ed2dc618 7400 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7401 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7402 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7403 bfd *abfd = get_section_bfd_owner (section);
dee91e82 7404 struct dwarf2_cu *cu;
d521ce57 7405 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7406 struct die_reader_specs reader;
d85a05f0 7407 struct die_info *comp_unit_die;
dee91e82 7408 int has_children;
d85a05f0 7409 struct attribute *attr;
dee91e82 7410 struct signatured_type *sig_type = NULL;
4bdcc0c1 7411 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7412 /* Non-zero if CU currently points to a DWO file and we need to
7413 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7414 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7415 int rereading_dwo_cu = 0;
c906108c 7416
b4f54984 7417 if (dwarf_die_debug)
9d8780f0 7418 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7419 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7420 sect_offset_str (this_cu->sect_off));
09406207 7421
dee91e82
DE
7422 if (use_existing_cu)
7423 gdb_assert (keep);
23745b47 7424
a2ce51a0
DE
7425 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7426 file (instead of going through the stub), short-circuit all of this. */
7427 if (this_cu->reading_dwo_directly)
7428 {
7429 /* Narrow down the scope of possibilities to have to understand. */
7430 gdb_assert (this_cu->is_debug_types);
7431 gdb_assert (abbrev_table == NULL);
6aa5f3a6
DE
7432 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7433 die_reader_func, data);
a2ce51a0
DE
7434 return;
7435 }
7436
dee91e82
DE
7437 /* This is cheap if the section is already read in. */
7438 dwarf2_read_section (objfile, section);
7439
9c541725 7440 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7441
7442 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 7443
fcd3b13d 7444 std::unique_ptr<dwarf2_cu> new_cu;
dee91e82
DE
7445 if (use_existing_cu && this_cu->cu != NULL)
7446 {
7447 cu = this_cu->cu;
42e7ad6c
DE
7448 /* If this CU is from a DWO file we need to start over, we need to
7449 refetch the attributes from the skeleton CU.
7450 This could be optimized by retrieving those attributes from when we
7451 were here the first time: the previous comp_unit_die was stored in
7452 comp_unit_obstack. But there's no data yet that we need this
7453 optimization. */
7454 if (cu->dwo_unit != NULL)
7455 rereading_dwo_cu = 1;
dee91e82
DE
7456 }
7457 else
7458 {
7459 /* If !use_existing_cu, this_cu->cu must be NULL. */
7460 gdb_assert (this_cu->cu == NULL);
fcd3b13d
SM
7461 new_cu.reset (new dwarf2_cu (this_cu));
7462 cu = new_cu.get ();
42e7ad6c 7463 }
dee91e82 7464
b0c7bfa9 7465 /* Get the header. */
9c541725 7466 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7467 {
7468 /* We already have the header, there's no need to read it in again. */
9c541725 7469 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7470 }
7471 else
7472 {
3019eac3 7473 if (this_cu->is_debug_types)
dee91e82 7474 {
ed2dc618
SM
7475 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7476 &cu->header, section,
4bdcc0c1 7477 abbrev_section, info_ptr,
43988095 7478 rcuh_kind::TYPE);
dee91e82 7479
42e7ad6c
DE
7480 /* Since per_cu is the first member of struct signatured_type,
7481 we can go from a pointer to one to a pointer to the other. */
7482 sig_type = (struct signatured_type *) this_cu;
43988095 7483 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7484 gdb_assert (sig_type->type_offset_in_tu
7485 == cu->header.type_cu_offset_in_tu);
7486 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7487
42e7ad6c
DE
7488 /* LENGTH has not been set yet for type units if we're
7489 using .gdb_index. */
1ce1cefd 7490 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
7491
7492 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7493 sig_type->type_offset_in_section =
7494 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7495
7496 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7497 }
7498 else
7499 {
ed2dc618
SM
7500 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7501 &cu->header, section,
4bdcc0c1 7502 abbrev_section,
43988095
JK
7503 info_ptr,
7504 rcuh_kind::COMPILE);
dee91e82 7505
9c541725 7506 gdb_assert (this_cu->sect_off == cu->header.sect_off);
1ce1cefd 7507 gdb_assert (this_cu->length == get_cu_length (&cu->header));
43988095 7508 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7509 }
7510 }
10b3939b 7511
6caca83c 7512 /* Skip dummy compilation units. */
dee91e82 7513 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7514 || peek_abbrev_code (abfd, info_ptr) == 0)
fcd3b13d 7515 return;
6caca83c 7516
433df2d4
DE
7517 /* If we don't have them yet, read the abbrevs for this compilation unit.
7518 And if we need to read them now, make sure they're freed when we're
685af9cd
TT
7519 done (own the table through ABBREV_TABLE_HOLDER). */
7520 abbrev_table_up abbrev_table_holder;
f4dc4d17 7521 if (abbrev_table != NULL)
685af9cd
TT
7522 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7523 else
f4dc4d17 7524 {
685af9cd
TT
7525 abbrev_table_holder
7526 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7527 cu->header.abbrev_sect_off);
7528 abbrev_table = abbrev_table_holder.get ();
42e7ad6c 7529 }
af703f96 7530
dee91e82 7531 /* Read the top level CU/TU die. */
685af9cd 7532 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
dee91e82 7533 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 7534
58f0c718
TT
7535 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7536 return;
7537
b0c7bfa9 7538 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7539 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7540 table from the DWO file and pass the ownership over to us. It will be
7541 referenced from READER, so we must make sure to free it after we're done
7542 with READER.
7543
b0c7bfa9
DE
7544 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7545 DWO CU, that this test will fail (the attribute will not be present). */
3019eac3 7546 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
685af9cd 7547 abbrev_table_up dwo_abbrev_table;
3019eac3
DE
7548 if (attr)
7549 {
3019eac3 7550 struct dwo_unit *dwo_unit;
b0c7bfa9 7551 struct die_info *dwo_comp_unit_die;
3019eac3
DE
7552
7553 if (has_children)
6a506a2d 7554 {
b98664d3 7555 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7556 " has children (offset %s) [in module %s]"),
7557 sect_offset_str (this_cu->sect_off),
7558 bfd_get_filename (abfd));
6a506a2d 7559 }
b0c7bfa9 7560 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 7561 if (dwo_unit != NULL)
3019eac3 7562 {
6a506a2d 7563 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
a2ce51a0 7564 comp_unit_die, NULL,
6a506a2d 7565 &reader, &info_ptr,
685af9cd
TT
7566 &dwo_comp_unit_die, &has_children,
7567 &dwo_abbrev_table) == 0)
6a506a2d
DE
7568 {
7569 /* Dummy die. */
6a506a2d
DE
7570 return;
7571 }
7572 comp_unit_die = dwo_comp_unit_die;
7573 }
7574 else
7575 {
7576 /* Yikes, we couldn't find the rest of the DIE, we only have
7577 the stub. A complaint has already been logged. There's
7578 not much more we can do except pass on the stub DIE to
7579 die_reader_func. We don't want to throw an error on bad
7580 debug info. */
3019eac3
DE
7581 }
7582 }
7583
b0c7bfa9 7584 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
7585 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7586
b0c7bfa9 7587 /* Done, clean up. */
fcd3b13d 7588 if (new_cu != NULL && keep)
348e048f 7589 {
fcd3b13d
SM
7590 /* Link this CU into read_in_chain. */
7591 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7592 dwarf2_per_objfile->read_in_chain = this_cu;
7593 /* The chain owns it now. */
7594 new_cu.release ();
348e048f 7595 }
dee91e82
DE
7596}
7597
33e80786
DE
7598/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7599 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7600 to have already done the lookup to find the DWO file).
dee91e82
DE
7601
7602 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7603 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7604
7605 We fill in THIS_CU->length.
7606
7607 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7608 linker) then DIE_READER_FUNC will not get called.
7609
7610 THIS_CU->cu is always freed when done.
3019eac3
DE
7611 This is done in order to not leave THIS_CU->cu in a state where we have
7612 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
7613
7614static void
7615init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3019eac3 7616 struct dwo_file *dwo_file,
dee91e82
DE
7617 die_reader_func_ftype *die_reader_func,
7618 void *data)
7619{
ed2dc618 7620 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7621 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7622 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7623 bfd *abfd = get_section_bfd_owner (section);
33e80786 7624 struct dwarf2_section_info *abbrev_section;
d521ce57 7625 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7626 struct die_reader_specs reader;
dee91e82
DE
7627 struct die_info *comp_unit_die;
7628 int has_children;
7629
b4f54984 7630 if (dwarf_die_debug)
9d8780f0 7631 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7632 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7633 sect_offset_str (this_cu->sect_off));
09406207 7634
dee91e82
DE
7635 gdb_assert (this_cu->cu == NULL);
7636
33e80786
DE
7637 abbrev_section = (dwo_file != NULL
7638 ? &dwo_file->sections.abbrev
7639 : get_abbrev_section_for_cu (this_cu));
7640
dee91e82
DE
7641 /* This is cheap if the section is already read in. */
7642 dwarf2_read_section (objfile, section);
7643
fcd3b13d 7644 struct dwarf2_cu cu (this_cu);
dee91e82 7645
9c541725 7646 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618
SM
7647 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7648 &cu.header, section,
4bdcc0c1 7649 abbrev_section, info_ptr,
43988095
JK
7650 (this_cu->is_debug_types
7651 ? rcuh_kind::TYPE
7652 : rcuh_kind::COMPILE));
dee91e82 7653
1ce1cefd 7654 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
7655
7656 /* Skip dummy compilation units. */
7657 if (info_ptr >= begin_info_ptr + this_cu->length
7658 || peek_abbrev_code (abfd, info_ptr) == 0)
fcd3b13d 7659 return;
72bf9492 7660
685af9cd
TT
7661 abbrev_table_up abbrev_table
7662 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7663 cu.header.abbrev_sect_off);
dee91e82 7664
685af9cd 7665 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
dee91e82
DE
7666 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7667
7668 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
dee91e82
DE
7669}
7670
3019eac3
DE
7671/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
7672 does not lookup the specified DWO file.
7673 This cannot be used to read DWO files.
dee91e82
DE
7674
7675 THIS_CU->cu is always freed when done.
3019eac3
DE
7676 This is done in order to not leave THIS_CU->cu in a state where we have
7677 to care whether it refers to the "main" CU or the DWO CU.
7678 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
7679
7680static void
7681init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
7682 die_reader_func_ftype *die_reader_func,
7683 void *data)
7684{
33e80786 7685 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
dee91e82 7686}
0018ea6f
DE
7687\f
7688/* Type Unit Groups.
dee91e82 7689
0018ea6f
DE
7690 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7691 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7692 so that all types coming from the same compilation (.o file) are grouped
7693 together. A future step could be to put the types in the same symtab as
7694 the CU the types ultimately came from. */
ff013f42 7695
f4dc4d17
DE
7696static hashval_t
7697hash_type_unit_group (const void *item)
7698{
9a3c8263
SM
7699 const struct type_unit_group *tu_group
7700 = (const struct type_unit_group *) item;
f4dc4d17 7701
094b34ac 7702 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7703}
348e048f
DE
7704
7705static int
f4dc4d17 7706eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7707{
9a3c8263
SM
7708 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7709 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7710
094b34ac 7711 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7712}
348e048f 7713
f4dc4d17
DE
7714/* Allocate a hash table for type unit groups. */
7715
7716static htab_t
ed2dc618 7717allocate_type_unit_groups_table (struct objfile *objfile)
f4dc4d17
DE
7718{
7719 return htab_create_alloc_ex (3,
7720 hash_type_unit_group,
7721 eq_type_unit_group,
7722 NULL,
ed2dc618 7723 &objfile->objfile_obstack,
f4dc4d17
DE
7724 hashtab_obstack_allocate,
7725 dummy_obstack_deallocate);
7726}
dee91e82 7727
f4dc4d17
DE
7728/* Type units that don't have DW_AT_stmt_list are grouped into their own
7729 partial symtabs. We combine several TUs per psymtab to not let the size
7730 of any one psymtab grow too big. */
7731#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7732#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7733
094b34ac 7734/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7735 Create the type_unit_group object used to hold one or more TUs. */
7736
7737static struct type_unit_group *
094b34ac 7738create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7739{
518817b3
SM
7740 struct dwarf2_per_objfile *dwarf2_per_objfile
7741 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17 7742 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 7743 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7744 struct type_unit_group *tu_group;
f4dc4d17
DE
7745
7746 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7747 struct type_unit_group);
094b34ac 7748 per_cu = &tu_group->per_cu;
518817b3 7749 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
f4dc4d17 7750
094b34ac
DE
7751 if (dwarf2_per_objfile->using_index)
7752 {
7753 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7754 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7755 }
7756 else
7757 {
9c541725 7758 unsigned int line_offset = to_underlying (line_offset_struct);
094b34ac
DE
7759 struct partial_symtab *pst;
7760 char *name;
7761
7762 /* Give the symtab a useful name for debug purposes. */
7763 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7764 name = xstrprintf ("<type_units_%d>",
7765 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7766 else
7767 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
7768
7769 pst = create_partial_symtab (per_cu, name);
7770 pst->anonymous = 1;
f4dc4d17 7771
094b34ac
DE
7772 xfree (name);
7773 }
f4dc4d17 7774
094b34ac 7775 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7776 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7777
7778 return tu_group;
7779}
7780
094b34ac
DE
7781/* Look up the type_unit_group for type unit CU, and create it if necessary.
7782 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7783
7784static struct type_unit_group *
ff39bb5e 7785get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7786{
518817b3
SM
7787 struct dwarf2_per_objfile *dwarf2_per_objfile
7788 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7789 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7790 struct type_unit_group *tu_group;
7791 void **slot;
7792 unsigned int line_offset;
7793 struct type_unit_group type_unit_group_for_lookup;
7794
7795 if (dwarf2_per_objfile->type_unit_groups == NULL)
7796 {
7797 dwarf2_per_objfile->type_unit_groups =
ed2dc618 7798 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
f4dc4d17
DE
7799 }
7800
7801 /* Do we need to create a new group, or can we use an existing one? */
7802
7803 if (stmt_list)
7804 {
7805 line_offset = DW_UNSND (stmt_list);
7806 ++tu_stats->nr_symtab_sharers;
7807 }
7808 else
7809 {
7810 /* Ugh, no stmt_list. Rare, but we have to handle it.
7811 We can do various things here like create one group per TU or
7812 spread them over multiple groups to split up the expansion work.
7813 To avoid worst case scenarios (too many groups or too large groups)
7814 we, umm, group them in bunches. */
7815 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7816 | (tu_stats->nr_stmt_less_type_units
7817 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7818 ++tu_stats->nr_stmt_less_type_units;
7819 }
7820
094b34ac 7821 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7822 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
f4dc4d17
DE
7823 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7824 &type_unit_group_for_lookup, INSERT);
7825 if (*slot != NULL)
7826 {
9a3c8263 7827 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7828 gdb_assert (tu_group != NULL);
7829 }
7830 else
7831 {
9c541725 7832 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7833 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7834 *slot = tu_group;
7835 ++tu_stats->nr_symtabs;
7836 }
7837
7838 return tu_group;
7839}
0018ea6f
DE
7840\f
7841/* Partial symbol tables. */
7842
7843/* Create a psymtab named NAME and assign it to PER_CU.
7844
7845 The caller must fill in the following details:
7846 dirname, textlow, texthigh. */
7847
7848static struct partial_symtab *
7849create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7850{
e3b94546 7851 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
0018ea6f
DE
7852 struct partial_symtab *pst;
7853
18a94d75 7854 pst = start_psymtab_common (objfile, name, 0,
af5bf4ad
SM
7855 objfile->global_psymbols,
7856 objfile->static_psymbols);
0018ea6f
DE
7857
7858 pst->psymtabs_addrmap_supported = 1;
7859
7860 /* This is the glue that links PST into GDB's symbol API. */
7861 pst->read_symtab_private = per_cu;
7862 pst->read_symtab = dwarf2_read_symtab;
7863 per_cu->v.psymtab = pst;
7864
7865 return pst;
7866}
7867
b93601f3
TT
7868/* The DATA object passed to process_psymtab_comp_unit_reader has this
7869 type. */
7870
7871struct process_psymtab_comp_unit_data
7872{
7873 /* True if we are reading a DW_TAG_partial_unit. */
7874
7875 int want_partial_unit;
7876
7877 /* The "pretend" language that is used if the CU doesn't declare a
7878 language. */
7879
7880 enum language pretend_language;
7881};
7882
0018ea6f
DE
7883/* die_reader_func for process_psymtab_comp_unit. */
7884
7885static void
7886process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7887 const gdb_byte *info_ptr,
0018ea6f
DE
7888 struct die_info *comp_unit_die,
7889 int has_children,
7890 void *data)
7891{
7892 struct dwarf2_cu *cu = reader->cu;
518817b3 7893 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 7894 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 7895 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7896 CORE_ADDR baseaddr;
7897 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7898 struct partial_symtab *pst;
3a2b436a 7899 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7900 const char *filename;
9a3c8263
SM
7901 struct process_psymtab_comp_unit_data *info
7902 = (struct process_psymtab_comp_unit_data *) data;
0018ea6f 7903
b93601f3 7904 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
0018ea6f
DE
7905 return;
7906
7907 gdb_assert (! per_cu->is_debug_types);
7908
b93601f3 7909 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
0018ea6f 7910
0018ea6f 7911 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
7912 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7913 if (filename == NULL)
0018ea6f 7914 filename = "";
0018ea6f
DE
7915
7916 pst = create_partial_symtab (per_cu, filename);
7917
7918 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7919 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f
DE
7920
7921 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7922
7923 dwarf2_find_base_address (comp_unit_die, cu);
7924
7925 /* Possibly set the default values of LOWPC and HIGHPC from
7926 `DW_AT_ranges'. */
3a2b436a
JK
7927 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7928 &best_highpc, cu, pst);
7929 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
7930 {
7931 CORE_ADDR low
7932 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7933 - baseaddr);
7934 CORE_ADDR high
7935 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7936 - baseaddr - 1);
7937 /* Store the contiguous range if it is not empty; it can be
7938 empty for CUs with no code. */
7939 addrmap_set_empty (objfile->psymtabs_addrmap, low, high, pst);
7940 }
0018ea6f
DE
7941
7942 /* Check if comp unit has_children.
7943 If so, read the rest of the partial symbols from this comp unit.
7944 If not, there's no more debug_info for this comp unit. */
7945 if (has_children)
7946 {
7947 struct partial_die_info *first_die;
7948 CORE_ADDR lowpc, highpc;
7949
7950 lowpc = ((CORE_ADDR) -1);
7951 highpc = ((CORE_ADDR) 0);
7952
7953 first_die = load_partial_dies (reader, info_ptr, 1);
7954
7955 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7956 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7957
7958 /* If we didn't find a lowpc, set it to highpc to avoid
7959 complaints from `maint check'. */
7960 if (lowpc == ((CORE_ADDR) -1))
7961 lowpc = highpc;
7962
7963 /* If the compilation unit didn't have an explicit address range,
7964 then use the information extracted from its child dies. */
e385593e 7965 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7966 {
7967 best_lowpc = lowpc;
7968 best_highpc = highpc;
7969 }
7970 }
4ae976d1 7971 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7972 best_lowpc + baseaddr)
7973 - baseaddr);
4ae976d1 7974 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7975 best_highpc + baseaddr)
7976 - baseaddr);
0018ea6f 7977
8763cede 7978 end_psymtab_common (objfile, pst);
0018ea6f
DE
7979
7980 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
7981 {
7982 int i;
7983 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
7984 struct dwarf2_per_cu_data *iter;
7985
7986 /* Fill in 'dependencies' here; we fill in 'users' in a
7987 post-pass. */
7988 pst->number_of_dependencies = len;
8d749320
SM
7989 pst->dependencies =
7990 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
0018ea6f
DE
7991 for (i = 0;
7992 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
7993 i, iter);
7994 ++i)
7995 pst->dependencies[i] = iter->v.psymtab;
7996
7997 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
7998 }
7999
8000 /* Get the list of files included in the current compilation unit,
8001 and build a psymtab for each of them. */
8002 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
8003
b4f54984 8004 if (dwarf_read_debug)
0018ea6f
DE
8005 {
8006 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8007
8008 fprintf_unfiltered (gdb_stdlog,
9d8780f0 8009 "Psymtab for %s unit @%s: %s - %s"
0018ea6f
DE
8010 ", %d global, %d static syms\n",
8011 per_cu->is_debug_types ? "type" : "comp",
9d8780f0 8012 sect_offset_str (per_cu->sect_off),
79748972
TT
8013 paddress (gdbarch, pst->text_low (objfile)),
8014 paddress (gdbarch, pst->text_high (objfile)),
0018ea6f
DE
8015 pst->n_global_syms, pst->n_static_syms);
8016 }
8017}
8018
8019/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8020 Process compilation unit THIS_CU for a psymtab. */
8021
8022static void
8023process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
8024 int want_partial_unit,
8025 enum language pretend_language)
0018ea6f
DE
8026{
8027 /* If this compilation unit was already read in, free the
8028 cached copy in order to read it in again. This is
8029 necessary because we skipped some symbols when we first
8030 read in the compilation unit (see load_partial_dies).
8031 This problem could be avoided, but the benefit is unclear. */
8032 if (this_cu->cu != NULL)
8033 free_one_cached_comp_unit (this_cu);
8034
f1902523 8035 if (this_cu->is_debug_types)
58f0c718
TT
8036 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8037 build_type_psymtabs_reader, NULL);
f1902523
JK
8038 else
8039 {
8040 process_psymtab_comp_unit_data info;
8041 info.want_partial_unit = want_partial_unit;
8042 info.pretend_language = pretend_language;
58f0c718 8043 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
f1902523
JK
8044 process_psymtab_comp_unit_reader, &info);
8045 }
0018ea6f
DE
8046
8047 /* Age out any secondary CUs. */
ed2dc618 8048 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 8049}
f4dc4d17
DE
8050
8051/* Reader function for build_type_psymtabs. */
8052
8053static void
8054build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 8055 const gdb_byte *info_ptr,
f4dc4d17
DE
8056 struct die_info *type_unit_die,
8057 int has_children,
8058 void *data)
8059{
ed2dc618 8060 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 8061 = reader->cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
8062 struct objfile *objfile = dwarf2_per_objfile->objfile;
8063 struct dwarf2_cu *cu = reader->cu;
8064 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 8065 struct signatured_type *sig_type;
f4dc4d17
DE
8066 struct type_unit_group *tu_group;
8067 struct attribute *attr;
8068 struct partial_die_info *first_die;
8069 CORE_ADDR lowpc, highpc;
8070 struct partial_symtab *pst;
8071
8072 gdb_assert (data == NULL);
0186c6a7
DE
8073 gdb_assert (per_cu->is_debug_types);
8074 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
8075
8076 if (! has_children)
8077 return;
8078
8079 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 8080 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 8081
0186c6a7 8082 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
f4dc4d17
DE
8083
8084 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
f4dc4d17
DE
8085 pst = create_partial_symtab (per_cu, "");
8086 pst->anonymous = 1;
8087
8088 first_die = load_partial_dies (reader, info_ptr, 1);
8089
8090 lowpc = (CORE_ADDR) -1;
8091 highpc = (CORE_ADDR) 0;
8092 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8093
8763cede 8094 end_psymtab_common (objfile, pst);
f4dc4d17
DE
8095}
8096
73051182
DE
8097/* Struct used to sort TUs by their abbreviation table offset. */
8098
8099struct tu_abbrev_offset
8100{
b2bdb8cf
SM
8101 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
8102 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
8103 {}
8104
8105 signatured_type *sig_type;
73051182
DE
8106 sect_offset abbrev_offset;
8107};
8108
484cf504 8109/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 8110
484cf504
TT
8111static bool
8112sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8113 const struct tu_abbrev_offset &b)
73051182 8114{
484cf504 8115 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
8116}
8117
8118/* Efficiently read all the type units.
8119 This does the bulk of the work for build_type_psymtabs.
8120
8121 The efficiency is because we sort TUs by the abbrev table they use and
8122 only read each abbrev table once. In one program there are 200K TUs
8123 sharing 8K abbrev tables.
8124
8125 The main purpose of this function is to support building the
8126 dwarf2_per_objfile->type_unit_groups table.
8127 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8128 can collapse the search space by grouping them by stmt_list.
8129 The savings can be significant, in the same program from above the 200K TUs
8130 share 8K stmt_list tables.
8131
8132 FUNC is expected to call get_type_unit_group, which will create the
8133 struct type_unit_group if necessary and add it to
8134 dwarf2_per_objfile->type_unit_groups. */
8135
8136static void
ed2dc618 8137build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 8138{
73051182 8139 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
685af9cd 8140 abbrev_table_up abbrev_table;
73051182 8141 sect_offset abbrev_offset;
73051182
DE
8142
8143 /* It's up to the caller to not call us multiple times. */
8144 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8145
b2bdb8cf 8146 if (dwarf2_per_objfile->all_type_units.empty ())
73051182
DE
8147 return;
8148
8149 /* TUs typically share abbrev tables, and there can be way more TUs than
8150 abbrev tables. Sort by abbrev table to reduce the number of times we
8151 read each abbrev table in.
8152 Alternatives are to punt or to maintain a cache of abbrev tables.
8153 This is simpler and efficient enough for now.
8154
8155 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8156 symtab to use). Typically TUs with the same abbrev offset have the same
8157 stmt_list value too so in practice this should work well.
8158
8159 The basic algorithm here is:
8160
8161 sort TUs by abbrev table
8162 for each TU with same abbrev table:
8163 read abbrev table if first user
8164 read TU top level DIE
8165 [IWBN if DWO skeletons had DW_AT_stmt_list]
8166 call FUNC */
8167
b4f54984 8168 if (dwarf_read_debug)
73051182
DE
8169 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8170
8171 /* Sort in a separate table to maintain the order of all_type_units
8172 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf
SM
8173 std::vector<tu_abbrev_offset> sorted_by_abbrev;
8174 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
8175
8176 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
8177 sorted_by_abbrev.emplace_back
8178 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
8179 sig_type->per_cu.section,
8180 sig_type->per_cu.sect_off));
73051182 8181
484cf504
TT
8182 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8183 sort_tu_by_abbrev_offset);
73051182 8184
9c541725 8185 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 8186
b2bdb8cf 8187 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 8188 {
73051182
DE
8189 /* Switch to the next abbrev table if necessary. */
8190 if (abbrev_table == NULL
b2bdb8cf 8191 || tu.abbrev_offset != abbrev_offset)
73051182 8192 {
b2bdb8cf 8193 abbrev_offset = tu.abbrev_offset;
73051182 8194 abbrev_table =
ed2dc618
SM
8195 abbrev_table_read_table (dwarf2_per_objfile,
8196 &dwarf2_per_objfile->abbrev,
73051182
DE
8197 abbrev_offset);
8198 ++tu_stats->nr_uniq_abbrev_tables;
8199 }
8200
b2bdb8cf 8201 init_cutu_and_read_dies (&tu.sig_type->per_cu, abbrev_table.get (),
58f0c718 8202 0, 0, false, build_type_psymtabs_reader, NULL);
73051182 8203 }
6aa5f3a6 8204}
73051182 8205
6aa5f3a6
DE
8206/* Print collected type unit statistics. */
8207
8208static void
ed2dc618 8209print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8210{
8211 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8212
8213 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf
SM
8214 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8215 dwarf2_per_objfile->all_type_units.size ());
6aa5f3a6
DE
8216 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8217 tu_stats->nr_uniq_abbrev_tables);
8218 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8219 tu_stats->nr_symtabs);
8220 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8221 tu_stats->nr_symtab_sharers);
8222 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8223 tu_stats->nr_stmt_less_type_units);
8224 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8225 tu_stats->nr_all_type_units_reallocs);
73051182
DE
8226}
8227
f4dc4d17
DE
8228/* Traversal function for build_type_psymtabs. */
8229
8230static int
8231build_type_psymtab_dependencies (void **slot, void *info)
8232{
ed2dc618
SM
8233 struct dwarf2_per_objfile *dwarf2_per_objfile
8234 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
8235 struct objfile *objfile = dwarf2_per_objfile->objfile;
8236 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 8237 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 8238 struct partial_symtab *pst = per_cu->v.psymtab;
0186c6a7
DE
8239 int len = VEC_length (sig_type_ptr, tu_group->tus);
8240 struct signatured_type *iter;
f4dc4d17
DE
8241 int i;
8242
8243 gdb_assert (len > 0);
0186c6a7 8244 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
8245
8246 pst->number_of_dependencies = len;
8d749320
SM
8247 pst->dependencies =
8248 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
f4dc4d17 8249 for (i = 0;
0186c6a7 8250 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
f4dc4d17
DE
8251 ++i)
8252 {
0186c6a7
DE
8253 gdb_assert (iter->per_cu.is_debug_types);
8254 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 8255 iter->type_unit_group = tu_group;
f4dc4d17
DE
8256 }
8257
0186c6a7 8258 VEC_free (sig_type_ptr, tu_group->tus);
348e048f
DE
8259
8260 return 1;
8261}
8262
8263/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8264 Build partial symbol tables for the .debug_types comp-units. */
8265
8266static void
ed2dc618 8267build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 8268{
ed2dc618 8269 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
8270 return;
8271
ed2dc618 8272 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 8273}
f4dc4d17 8274
6aa5f3a6
DE
8275/* Traversal function for process_skeletonless_type_unit.
8276 Read a TU in a DWO file and build partial symbols for it. */
8277
8278static int
8279process_skeletonless_type_unit (void **slot, void *info)
8280{
8281 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
8282 struct dwarf2_per_objfile *dwarf2_per_objfile
8283 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
8284 struct signatured_type find_entry, *entry;
8285
8286 /* If this TU doesn't exist in the global table, add it and read it in. */
8287
8288 if (dwarf2_per_objfile->signatured_types == NULL)
8289 {
8290 dwarf2_per_objfile->signatured_types
ed2dc618 8291 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
6aa5f3a6
DE
8292 }
8293
8294 find_entry.signature = dwo_unit->signature;
8295 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8296 INSERT);
8297 /* If we've already seen this type there's nothing to do. What's happening
8298 is we're doing our own version of comdat-folding here. */
8299 if (*slot != NULL)
8300 return 1;
8301
8302 /* This does the job that create_all_type_units would have done for
8303 this TU. */
ed2dc618
SM
8304 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8305 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
8306 *slot = entry;
8307
8308 /* This does the job that build_type_psymtabs_1 would have done. */
58f0c718 8309 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0, false,
6aa5f3a6
DE
8310 build_type_psymtabs_reader, NULL);
8311
8312 return 1;
8313}
8314
8315/* Traversal function for process_skeletonless_type_units. */
8316
8317static int
8318process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8319{
8320 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8321
8322 if (dwo_file->tus != NULL)
8323 {
8324 htab_traverse_noresize (dwo_file->tus,
8325 process_skeletonless_type_unit, info);
8326 }
8327
8328 return 1;
8329}
8330
8331/* Scan all TUs of DWO files, verifying we've processed them.
8332 This is needed in case a TU was emitted without its skeleton.
8333 Note: This can't be done until we know what all the DWO files are. */
8334
8335static void
ed2dc618 8336process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8337{
8338 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 8339 if (get_dwp_file (dwarf2_per_objfile) == NULL
6aa5f3a6
DE
8340 && dwarf2_per_objfile->dwo_files != NULL)
8341 {
8342 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
8343 process_dwo_file_for_skeletonless_type_units,
ed2dc618 8344 dwarf2_per_objfile);
6aa5f3a6 8345 }
348e048f
DE
8346}
8347
ed2dc618 8348/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
8349
8350static void
ed2dc618 8351set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 8352{
b76e467d 8353 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
95554aad 8354 {
95554aad 8355 struct partial_symtab *pst = per_cu->v.psymtab;
95554aad 8356
36586728
TT
8357 if (pst == NULL)
8358 continue;
8359
b76e467d 8360 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
8361 {
8362 /* Set the 'user' field only if it is not already set. */
8363 if (pst->dependencies[j]->user == NULL)
8364 pst->dependencies[j]->user = pst;
8365 }
8366 }
8367}
8368
93311388
DE
8369/* Build the partial symbol table by doing a quick pass through the
8370 .debug_info and .debug_abbrev sections. */
72bf9492 8371
93311388 8372static void
ed2dc618 8373dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 8374{
ed2dc618 8375 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 8376
b4f54984 8377 if (dwarf_read_debug)
45cfd468
DE
8378 {
8379 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 8380 objfile_name (objfile));
45cfd468
DE
8381 }
8382
98bfdba5
PA
8383 dwarf2_per_objfile->reading_partial_symbols = 1;
8384
be391dca 8385 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 8386
93311388
DE
8387 /* Any cached compilation units will be linked by the per-objfile
8388 read_in_chain. Make sure to free them when we're done. */
11ed8cad 8389 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 8390
ed2dc618 8391 build_type_psymtabs (dwarf2_per_objfile);
348e048f 8392
ed2dc618 8393 create_all_comp_units (dwarf2_per_objfile);
c906108c 8394
60606b2c
TT
8395 /* Create a temporary address map on a temporary obstack. We later
8396 copy this to the final obstack. */
8268c778 8397 auto_obstack temp_obstack;
791afaa2
TT
8398
8399 scoped_restore save_psymtabs_addrmap
8400 = make_scoped_restore (&objfile->psymtabs_addrmap,
8401 addrmap_create_mutable (&temp_obstack));
72bf9492 8402
b76e467d
SM
8403 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8404 process_psymtab_comp_unit (per_cu, 0, language_minimal);
ff013f42 8405
6aa5f3a6 8406 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 8407 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
8408
8409 /* Now that all TUs have been processed we can fill in the dependencies. */
8410 if (dwarf2_per_objfile->type_unit_groups != NULL)
8411 {
8412 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
ed2dc618 8413 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
8414 }
8415
b4f54984 8416 if (dwarf_read_debug)
ed2dc618 8417 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 8418
ed2dc618 8419 set_partial_user (dwarf2_per_objfile);
95554aad 8420
ff013f42
JK
8421 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
8422 &objfile->objfile_obstack);
791afaa2
TT
8423 /* At this point we want to keep the address map. */
8424 save_psymtabs_addrmap.release ();
ff013f42 8425
b4f54984 8426 if (dwarf_read_debug)
45cfd468 8427 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 8428 objfile_name (objfile));
ae038cb0
DJ
8429}
8430
3019eac3 8431/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
8432
8433static void
dee91e82 8434load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8435 const gdb_byte *info_ptr,
dee91e82
DE
8436 struct die_info *comp_unit_die,
8437 int has_children,
8438 void *data)
ae038cb0 8439{
dee91e82 8440 struct dwarf2_cu *cu = reader->cu;
ae038cb0 8441
95554aad 8442 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 8443
ae038cb0
DJ
8444 /* Check if comp unit has_children.
8445 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 8446 If not, there's no more debug_info for this comp unit. */
d85a05f0 8447 if (has_children)
dee91e82
DE
8448 load_partial_dies (reader, info_ptr, 0);
8449}
98bfdba5 8450
dee91e82
DE
8451/* Load the partial DIEs for a secondary CU into memory.
8452 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8453
dee91e82
DE
8454static void
8455load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8456{
58f0c718 8457 init_cutu_and_read_dies (this_cu, NULL, 1, 1, false,
f4dc4d17 8458 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
8459}
8460
ae038cb0 8461static void
ed2dc618 8462read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 8463 struct dwarf2_section_info *section,
f1902523 8464 struct dwarf2_section_info *abbrev_section,
b76e467d 8465 unsigned int is_dwz)
ae038cb0 8466{
d521ce57 8467 const gdb_byte *info_ptr;
ed2dc618 8468 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 8469
b4f54984 8470 if (dwarf_read_debug)
bf6af496 8471 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
a32a8923
DE
8472 get_section_name (section),
8473 get_section_file_name (section));
bf6af496 8474
36586728 8475 dwarf2_read_section (objfile, section);
ae038cb0 8476
36586728 8477 info_ptr = section->buffer;
6e70227d 8478
36586728 8479 while (info_ptr < section->buffer + section->size)
ae038cb0 8480 {
ae038cb0 8481 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8482
9c541725 8483 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8484
f1902523 8485 comp_unit_head cu_header;
ed2dc618
SM
8486 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8487 abbrev_section, info_ptr,
8488 rcuh_kind::COMPILE);
ae038cb0
DJ
8489
8490 /* Save the compilation unit for later lookup. */
f1902523
JK
8491 if (cu_header.unit_type != DW_UT_type)
8492 {
8493 this_cu = XOBNEW (&objfile->objfile_obstack,
8494 struct dwarf2_per_cu_data);
8495 memset (this_cu, 0, sizeof (*this_cu));
8496 }
8497 else
8498 {
8499 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8500 struct signatured_type);
8501 memset (sig_type, 0, sizeof (*sig_type));
8502 sig_type->signature = cu_header.signature;
8503 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8504 this_cu = &sig_type->per_cu;
8505 }
8506 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8507 this_cu->sect_off = sect_off;
f1902523 8508 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8509 this_cu->is_dwz = is_dwz;
e3b94546 8510 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 8511 this_cu->section = section;
ae038cb0 8512
b76e467d 8513 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8514
8515 info_ptr = info_ptr + this_cu->length;
8516 }
36586728
TT
8517}
8518
8519/* Create a list of all compilation units in OBJFILE.
8520 This is only done for -readnow and building partial symtabs. */
8521
8522static void
ed2dc618 8523create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 8524{
b76e467d 8525 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
ed2dc618 8526 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
b76e467d 8527 &dwarf2_per_objfile->abbrev, 0);
36586728 8528
b76e467d 8529 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 8530 if (dwz != NULL)
ed2dc618 8531 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
b76e467d 8532 1);
c906108c
SS
8533}
8534
5734ee8b 8535/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8536 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8537 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8538 DW_AT_ranges). See the comments of add_partial_subprogram on how
8539 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8540
72bf9492
DJ
8541static void
8542scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8543 CORE_ADDR *highpc, int set_addrmap,
8544 struct dwarf2_cu *cu)
c906108c 8545{
72bf9492 8546 struct partial_die_info *pdi;
c906108c 8547
91c24f0a
DC
8548 /* Now, march along the PDI's, descending into ones which have
8549 interesting children but skipping the children of the other ones,
8550 until we reach the end of the compilation unit. */
c906108c 8551
72bf9492 8552 pdi = first_die;
91c24f0a 8553
72bf9492
DJ
8554 while (pdi != NULL)
8555 {
52356b79 8556 pdi->fixup (cu);
c906108c 8557
f55ee35c 8558 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8559 children, so we need to look at them. Ditto for anonymous
8560 enums. */
933c6fe4 8561
72bf9492 8562 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8563 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8564 || pdi->tag == DW_TAG_imported_unit
8565 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8566 {
72bf9492 8567 switch (pdi->tag)
c906108c
SS
8568 {
8569 case DW_TAG_subprogram:
b1dc1806 8570 case DW_TAG_inlined_subroutine:
cdc07690 8571 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 8572 break;
72929c62 8573 case DW_TAG_constant:
c906108c
SS
8574 case DW_TAG_variable:
8575 case DW_TAG_typedef:
91c24f0a 8576 case DW_TAG_union_type:
72bf9492 8577 if (!pdi->is_declaration)
63d06c5c 8578 {
72bf9492 8579 add_partial_symbol (pdi, cu);
63d06c5c
DC
8580 }
8581 break;
c906108c 8582 case DW_TAG_class_type:
680b30c7 8583 case DW_TAG_interface_type:
c906108c 8584 case DW_TAG_structure_type:
72bf9492 8585 if (!pdi->is_declaration)
c906108c 8586 {
72bf9492 8587 add_partial_symbol (pdi, cu);
c906108c 8588 }
b7fee5a3
KS
8589 if ((cu->language == language_rust
8590 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8591 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8592 set_addrmap, cu);
c906108c 8593 break;
91c24f0a 8594 case DW_TAG_enumeration_type:
72bf9492
DJ
8595 if (!pdi->is_declaration)
8596 add_partial_enumeration (pdi, cu);
c906108c
SS
8597 break;
8598 case DW_TAG_base_type:
a02abb62 8599 case DW_TAG_subrange_type:
c906108c 8600 /* File scope base type definitions are added to the partial
c5aa993b 8601 symbol table. */
72bf9492 8602 add_partial_symbol (pdi, cu);
c906108c 8603 break;
d9fa45fe 8604 case DW_TAG_namespace:
cdc07690 8605 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8606 break;
5d7cb8df 8607 case DW_TAG_module:
cdc07690 8608 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8609 break;
95554aad
TT
8610 case DW_TAG_imported_unit:
8611 {
8612 struct dwarf2_per_cu_data *per_cu;
8613
f4dc4d17
DE
8614 /* For now we don't handle imported units in type units. */
8615 if (cu->per_cu->is_debug_types)
8616 {
8617 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8618 " supported in type units [in module %s]"),
518817b3 8619 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
8620 }
8621
e3b94546
SM
8622 per_cu = dwarf2_find_containing_comp_unit
8623 (pdi->d.sect_off, pdi->is_dwz,
518817b3 8624 cu->per_cu->dwarf2_per_objfile);
95554aad
TT
8625
8626 /* Go read the partial unit, if needed. */
8627 if (per_cu->v.psymtab == NULL)
b93601f3 8628 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 8629
f4dc4d17 8630 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 8631 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
8632 }
8633 break;
74921315
KS
8634 case DW_TAG_imported_declaration:
8635 add_partial_symbol (pdi, cu);
8636 break;
c906108c
SS
8637 default:
8638 break;
8639 }
8640 }
8641
72bf9492
DJ
8642 /* If the die has a sibling, skip to the sibling. */
8643
8644 pdi = pdi->die_sibling;
8645 }
8646}
8647
8648/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8649
72bf9492 8650 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8651 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8652 Enumerators are an exception; they use the scope of their parent
8653 enumeration type, i.e. the name of the enumeration type is not
8654 prepended to the enumerator.
91c24f0a 8655
72bf9492
DJ
8656 There are two complexities. One is DW_AT_specification; in this
8657 case "parent" means the parent of the target of the specification,
8658 instead of the direct parent of the DIE. The other is compilers
8659 which do not emit DW_TAG_namespace; in this case we try to guess
8660 the fully qualified name of structure types from their members'
8661 linkage names. This must be done using the DIE's children rather
8662 than the children of any DW_AT_specification target. We only need
8663 to do this for structures at the top level, i.e. if the target of
8664 any DW_AT_specification (if any; otherwise the DIE itself) does not
8665 have a parent. */
8666
8667/* Compute the scope prefix associated with PDI's parent, in
8668 compilation unit CU. The result will be allocated on CU's
8669 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8670 field. NULL is returned if no prefix is necessary. */
15d034d0 8671static const char *
72bf9492
DJ
8672partial_die_parent_scope (struct partial_die_info *pdi,
8673 struct dwarf2_cu *cu)
8674{
15d034d0 8675 const char *grandparent_scope;
72bf9492 8676 struct partial_die_info *parent, *real_pdi;
91c24f0a 8677
72bf9492
DJ
8678 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8679 then this means the parent of the specification DIE. */
8680
8681 real_pdi = pdi;
72bf9492 8682 while (real_pdi->has_specification)
36586728
TT
8683 real_pdi = find_partial_die (real_pdi->spec_offset,
8684 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
8685
8686 parent = real_pdi->die_parent;
8687 if (parent == NULL)
8688 return NULL;
8689
8690 if (parent->scope_set)
8691 return parent->scope;
8692
52356b79 8693 parent->fixup (cu);
72bf9492 8694
10b3939b 8695 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8696
acebe513
UW
8697 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8698 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8699 Work around this problem here. */
8700 if (cu->language == language_cplus
6e70227d 8701 && parent->tag == DW_TAG_namespace
acebe513
UW
8702 && strcmp (parent->name, "::") == 0
8703 && grandparent_scope == NULL)
8704 {
8705 parent->scope = NULL;
8706 parent->scope_set = 1;
8707 return NULL;
8708 }
8709
9c6c53f7
SA
8710 if (pdi->tag == DW_TAG_enumerator)
8711 /* Enumerators should not get the name of the enumeration as a prefix. */
8712 parent->scope = grandparent_scope;
8713 else if (parent->tag == DW_TAG_namespace
f55ee35c 8714 || parent->tag == DW_TAG_module
72bf9492
DJ
8715 || parent->tag == DW_TAG_structure_type
8716 || parent->tag == DW_TAG_class_type
680b30c7 8717 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
8718 || parent->tag == DW_TAG_union_type
8719 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
8720 {
8721 if (grandparent_scope == NULL)
8722 parent->scope = parent->name;
8723 else
3e43a32a
MS
8724 parent->scope = typename_concat (&cu->comp_unit_obstack,
8725 grandparent_scope,
f55ee35c 8726 parent->name, 0, cu);
72bf9492 8727 }
72bf9492
DJ
8728 else
8729 {
8730 /* FIXME drow/2004-04-01: What should we be doing with
8731 function-local names? For partial symbols, we should probably be
8732 ignoring them. */
b98664d3 8733 complaint (_("unhandled containing DIE tag %d for DIE at %s"),
9d8780f0 8734 parent->tag, sect_offset_str (pdi->sect_off));
72bf9492 8735 parent->scope = grandparent_scope;
c906108c
SS
8736 }
8737
72bf9492
DJ
8738 parent->scope_set = 1;
8739 return parent->scope;
8740}
8741
8742/* Return the fully scoped name associated with PDI, from compilation unit
8743 CU. The result will be allocated with malloc. */
4568ecf9 8744
72bf9492
DJ
8745static char *
8746partial_die_full_name (struct partial_die_info *pdi,
8747 struct dwarf2_cu *cu)
8748{
15d034d0 8749 const char *parent_scope;
72bf9492 8750
98bfdba5
PA
8751 /* If this is a template instantiation, we can not work out the
8752 template arguments from partial DIEs. So, unfortunately, we have
8753 to go through the full DIEs. At least any work we do building
8754 types here will be reused if full symbols are loaded later. */
8755 if (pdi->has_template_arguments)
8756 {
52356b79 8757 pdi->fixup (cu);
98bfdba5
PA
8758
8759 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8760 {
8761 struct die_info *die;
8762 struct attribute attr;
8763 struct dwarf2_cu *ref_cu = cu;
8764
b64f50a1 8765 /* DW_FORM_ref_addr is using section offset. */
b4069958 8766 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8767 attr.form = DW_FORM_ref_addr;
9c541725 8768 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8769 die = follow_die_ref (NULL, &attr, &ref_cu);
8770
8771 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8772 }
8773 }
8774
72bf9492
DJ
8775 parent_scope = partial_die_parent_scope (pdi, cu);
8776 if (parent_scope == NULL)
8777 return NULL;
8778 else
f55ee35c 8779 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
8780}
8781
8782static void
72bf9492 8783add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8784{
518817b3
SM
8785 struct dwarf2_per_objfile *dwarf2_per_objfile
8786 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 8787 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 8788 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 8789 CORE_ADDR addr = 0;
15d034d0 8790 const char *actual_name = NULL;
e142c38c 8791 CORE_ADDR baseaddr;
15d034d0 8792 char *built_actual_name;
e142c38c
DJ
8793
8794 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 8795
15d034d0
TT
8796 built_actual_name = partial_die_full_name (pdi, cu);
8797 if (built_actual_name != NULL)
8798 actual_name = built_actual_name;
63d06c5c 8799
72bf9492
DJ
8800 if (actual_name == NULL)
8801 actual_name = pdi->name;
8802
c906108c
SS
8803 switch (pdi->tag)
8804 {
b1dc1806 8805 case DW_TAG_inlined_subroutine:
c906108c 8806 case DW_TAG_subprogram:
79748972
TT
8807 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8808 - baseaddr);
2cfa0c8d 8809 if (pdi->is_external || cu->language == language_ada)
c906108c 8810 {
2cfa0c8d
JB
8811 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
8812 of the global scope. But in Ada, we want to be able to access
8813 nested procedures globally. So all Ada subprograms are stored
8814 in the global scope. */
f47fb265 8815 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8816 built_actual_name != NULL,
f47fb265 8817 VAR_DOMAIN, LOC_BLOCK,
79748972 8818 SECT_OFF_TEXT (objfile),
f47fb265 8819 &objfile->global_psymbols,
79748972
TT
8820 addr,
8821 cu->language, objfile);
c906108c
SS
8822 }
8823 else
8824 {
f47fb265 8825 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8826 built_actual_name != NULL,
f47fb265 8827 VAR_DOMAIN, LOC_BLOCK,
79748972 8828 SECT_OFF_TEXT (objfile),
f47fb265 8829 &objfile->static_psymbols,
1762568f 8830 addr, cu->language, objfile);
c906108c 8831 }
0c1b455e
TT
8832
8833 if (pdi->main_subprogram && actual_name != NULL)
8834 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8835 break;
72929c62
JB
8836 case DW_TAG_constant:
8837 {
af5bf4ad 8838 std::vector<partial_symbol *> *list;
72929c62
JB
8839
8840 if (pdi->is_external)
8841 list = &objfile->global_psymbols;
8842 else
8843 list = &objfile->static_psymbols;
f47fb265 8844 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8845 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
79748972 8846 -1, list, 0, cu->language, objfile);
72929c62
JB
8847 }
8848 break;
c906108c 8849 case DW_TAG_variable:
95554aad
TT
8850 if (pdi->d.locdesc)
8851 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8852
95554aad 8853 if (pdi->d.locdesc
caac4577
JG
8854 && addr == 0
8855 && !dwarf2_per_objfile->has_section_at_zero)
8856 {
8857 /* A global or static variable may also have been stripped
8858 out by the linker if unused, in which case its address
8859 will be nullified; do not add such variables into partial
8860 symbol table then. */
8861 }
8862 else if (pdi->is_external)
c906108c
SS
8863 {
8864 /* Global Variable.
8865 Don't enter into the minimal symbol tables as there is
8866 a minimal symbol table entry from the ELF symbols already.
8867 Enter into partial symbol table if it has a location
8868 descriptor or a type.
8869 If the location descriptor is missing, new_symbol will create
8870 a LOC_UNRESOLVED symbol, the address of the variable will then
8871 be determined from the minimal symbol table whenever the variable
8872 is referenced.
8873 The address for the partial symbol table entry is not
8874 used by GDB, but it comes in handy for debugging partial symbol
8875 table building. */
8876
95554aad 8877 if (pdi->d.locdesc || pdi->has_type)
f47fb265 8878 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8879 built_actual_name != NULL,
f47fb265 8880 VAR_DOMAIN, LOC_STATIC,
79748972 8881 SECT_OFF_TEXT (objfile),
f47fb265 8882 &objfile->global_psymbols,
79748972 8883 addr, cu->language, objfile);
c906108c
SS
8884 }
8885 else
8886 {
ff908ebf
AW
8887 int has_loc = pdi->d.locdesc != NULL;
8888
8889 /* Static Variable. Skip symbols whose value we cannot know (those
8890 without location descriptors or constant values). */
8891 if (!has_loc && !pdi->has_const_value)
decbce07 8892 {
15d034d0 8893 xfree (built_actual_name);
decbce07
MS
8894 return;
8895 }
ff908ebf 8896
f47fb265 8897 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8898 built_actual_name != NULL,
f47fb265 8899 VAR_DOMAIN, LOC_STATIC,
79748972 8900 SECT_OFF_TEXT (objfile),
f47fb265 8901 &objfile->static_psymbols,
79748972 8902 has_loc ? addr : 0,
f47fb265 8903 cu->language, objfile);
c906108c
SS
8904 }
8905 break;
8906 case DW_TAG_typedef:
8907 case DW_TAG_base_type:
a02abb62 8908 case DW_TAG_subrange_type:
38d518c9 8909 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8910 built_actual_name != NULL,
79748972 8911 VAR_DOMAIN, LOC_TYPEDEF, -1,
c906108c 8912 &objfile->static_psymbols,
1762568f 8913 0, cu->language, objfile);
c906108c 8914 break;
74921315 8915 case DW_TAG_imported_declaration:
72bf9492
DJ
8916 case DW_TAG_namespace:
8917 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8918 built_actual_name != NULL,
79748972 8919 VAR_DOMAIN, LOC_TYPEDEF, -1,
72bf9492 8920 &objfile->global_psymbols,
1762568f 8921 0, cu->language, objfile);
72bf9492 8922 break;
530e8392
KB
8923 case DW_TAG_module:
8924 add_psymbol_to_list (actual_name, strlen (actual_name),
8925 built_actual_name != NULL,
79748972 8926 MODULE_DOMAIN, LOC_TYPEDEF, -1,
530e8392 8927 &objfile->global_psymbols,
1762568f 8928 0, cu->language, objfile);
530e8392 8929 break;
c906108c 8930 case DW_TAG_class_type:
680b30c7 8931 case DW_TAG_interface_type:
c906108c
SS
8932 case DW_TAG_structure_type:
8933 case DW_TAG_union_type:
8934 case DW_TAG_enumeration_type:
fa4028e9
JB
8935 /* Skip external references. The DWARF standard says in the section
8936 about "Structure, Union, and Class Type Entries": "An incomplete
8937 structure, union or class type is represented by a structure,
8938 union or class entry that does not have a byte size attribute
8939 and that has a DW_AT_declaration attribute." */
8940 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 8941 {
15d034d0 8942 xfree (built_actual_name);
decbce07
MS
8943 return;
8944 }
fa4028e9 8945
63d06c5c
DC
8946 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8947 static vs. global. */
38d518c9 8948 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8949 built_actual_name != NULL,
79748972 8950 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
9c37b5ae 8951 cu->language == language_cplus
63d06c5c
DC
8952 ? &objfile->global_psymbols
8953 : &objfile->static_psymbols,
1762568f 8954 0, cu->language, objfile);
c906108c 8955
c906108c
SS
8956 break;
8957 case DW_TAG_enumerator:
38d518c9 8958 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8959 built_actual_name != NULL,
79748972 8960 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 8961 cu->language == language_cplus
f6fe98ef
DJ
8962 ? &objfile->global_psymbols
8963 : &objfile->static_psymbols,
1762568f 8964 0, cu->language, objfile);
c906108c
SS
8965 break;
8966 default:
8967 break;
8968 }
5c4e30ca 8969
15d034d0 8970 xfree (built_actual_name);
c906108c
SS
8971}
8972
5c4e30ca
DC
8973/* Read a partial die corresponding to a namespace; also, add a symbol
8974 corresponding to that namespace to the symbol table. NAMESPACE is
8975 the name of the enclosing namespace. */
91c24f0a 8976
72bf9492
DJ
8977static void
8978add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8979 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8980 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8981{
72bf9492 8982 /* Add a symbol for the namespace. */
e7c27a73 8983
72bf9492 8984 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8985
8986 /* Now scan partial symbols in that namespace. */
8987
91c24f0a 8988 if (pdi->has_children)
cdc07690 8989 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8990}
8991
5d7cb8df
JK
8992/* Read a partial die corresponding to a Fortran module. */
8993
8994static void
8995add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8996 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8997{
530e8392
KB
8998 /* Add a symbol for the namespace. */
8999
9000 add_partial_symbol (pdi, cu);
9001
f55ee35c 9002 /* Now scan partial symbols in that module. */
5d7cb8df
JK
9003
9004 if (pdi->has_children)
cdc07690 9005 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
9006}
9007
b1dc1806
XR
9008/* Read a partial die corresponding to a subprogram or an inlined
9009 subprogram and create a partial symbol for that subprogram.
9010 When the CU language allows it, this routine also defines a partial
9011 symbol for each nested subprogram that this subprogram contains.
9012 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9013 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 9014
cdc07690
YQ
9015 PDI may also be a lexical block, in which case we simply search
9016 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
9017 Again, this is only performed when the CU language allows this
9018 type of definitions. */
9019
9020static void
9021add_partial_subprogram (struct partial_die_info *pdi,
9022 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9023 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 9024{
b1dc1806 9025 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
9026 {
9027 if (pdi->has_pc_info)
9028 {
9029 if (pdi->lowpc < *lowpc)
9030 *lowpc = pdi->lowpc;
9031 if (pdi->highpc > *highpc)
9032 *highpc = pdi->highpc;
cdc07690 9033 if (set_addrmap)
5734ee8b 9034 {
518817b3 9035 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a
MR
9036 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9037 CORE_ADDR baseaddr;
9038 CORE_ADDR highpc;
9039 CORE_ADDR lowpc;
5734ee8b
DJ
9040
9041 baseaddr = ANOFFSET (objfile->section_offsets,
9042 SECT_OFF_TEXT (objfile));
79748972
TT
9043 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
9044 pdi->lowpc + baseaddr)
9045 - baseaddr);
9046 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
9047 pdi->highpc + baseaddr)
9048 - baseaddr);
3e29f34a 9049 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9291a0cd 9050 cu->per_cu->v.psymtab);
5734ee8b 9051 }
481860b3
GB
9052 }
9053
9054 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9055 {
bc30ff58 9056 if (!pdi->is_declaration)
e8d05480
JB
9057 /* Ignore subprogram DIEs that do not have a name, they are
9058 illegal. Do not emit a complaint at this point, we will
9059 do so when we convert this psymtab into a symtab. */
9060 if (pdi->name)
9061 add_partial_symbol (pdi, cu);
bc30ff58
JB
9062 }
9063 }
6e70227d 9064
bc30ff58
JB
9065 if (! pdi->has_children)
9066 return;
9067
9068 if (cu->language == language_ada)
9069 {
9070 pdi = pdi->die_child;
9071 while (pdi != NULL)
9072 {
52356b79 9073 pdi->fixup (cu);
bc30ff58 9074 if (pdi->tag == DW_TAG_subprogram
b1dc1806 9075 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 9076 || pdi->tag == DW_TAG_lexical_block)
cdc07690 9077 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
9078 pdi = pdi->die_sibling;
9079 }
9080 }
9081}
9082
91c24f0a
DC
9083/* Read a partial die corresponding to an enumeration type. */
9084
72bf9492
DJ
9085static void
9086add_partial_enumeration (struct partial_die_info *enum_pdi,
9087 struct dwarf2_cu *cu)
91c24f0a 9088{
72bf9492 9089 struct partial_die_info *pdi;
91c24f0a
DC
9090
9091 if (enum_pdi->name != NULL)
72bf9492
DJ
9092 add_partial_symbol (enum_pdi, cu);
9093
9094 pdi = enum_pdi->die_child;
9095 while (pdi)
91c24f0a 9096 {
72bf9492 9097 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
b98664d3 9098 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 9099 else
72bf9492
DJ
9100 add_partial_symbol (pdi, cu);
9101 pdi = pdi->die_sibling;
91c24f0a 9102 }
91c24f0a
DC
9103}
9104
6caca83c
CC
9105/* Return the initial uleb128 in the die at INFO_PTR. */
9106
9107static unsigned int
d521ce57 9108peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
9109{
9110 unsigned int bytes_read;
9111
9112 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9113}
9114
685af9cd
TT
9115/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9116 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9117
4bb7a0a7
DJ
9118 Return the corresponding abbrev, or NULL if the number is zero (indicating
9119 an empty DIE). In either case *BYTES_READ will be set to the length of
9120 the initial number. */
9121
9122static struct abbrev_info *
685af9cd
TT
9123peek_die_abbrev (const die_reader_specs &reader,
9124 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 9125{
685af9cd 9126 dwarf2_cu *cu = reader.cu;
518817b3 9127 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
685af9cd
TT
9128 unsigned int abbrev_number
9129 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
9130
9131 if (abbrev_number == 0)
9132 return NULL;
9133
685af9cd 9134 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
9135 if (!abbrev)
9136 {
422b9917 9137 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 9138 " at offset %s [in module %s]"),
422b9917 9139 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 9140 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
9141 }
9142
9143 return abbrev;
9144}
9145
93311388
DE
9146/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9147 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
9148 DIE. Any children of the skipped DIEs will also be skipped. */
9149
d521ce57
TT
9150static const gdb_byte *
9151skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 9152{
4bb7a0a7
DJ
9153 while (1)
9154 {
685af9cd
TT
9155 unsigned int bytes_read;
9156 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9157
4bb7a0a7
DJ
9158 if (abbrev == NULL)
9159 return info_ptr + bytes_read;
9160 else
dee91e82 9161 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
9162 }
9163}
9164
93311388
DE
9165/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9166 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
9167 abbrev corresponding to that skipped uleb128 should be passed in
9168 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9169 children. */
9170
d521ce57
TT
9171static const gdb_byte *
9172skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 9173 struct abbrev_info *abbrev)
4bb7a0a7
DJ
9174{
9175 unsigned int bytes_read;
9176 struct attribute attr;
dee91e82
DE
9177 bfd *abfd = reader->abfd;
9178 struct dwarf2_cu *cu = reader->cu;
d521ce57 9179 const gdb_byte *buffer = reader->buffer;
f664829e 9180 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
9181 unsigned int form, i;
9182
9183 for (i = 0; i < abbrev->num_attrs; i++)
9184 {
9185 /* The only abbrev we care about is DW_AT_sibling. */
9186 if (abbrev->attrs[i].name == DW_AT_sibling)
9187 {
dee91e82 9188 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 9189 if (attr.form == DW_FORM_ref_addr)
b98664d3 9190 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 9191 else
b9502d3f 9192 {
9c541725
PA
9193 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9194 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
9195
9196 if (sibling_ptr < info_ptr)
b98664d3 9197 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
9198 else if (sibling_ptr > reader->buffer_end)
9199 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
9200 else
9201 return sibling_ptr;
9202 }
4bb7a0a7
DJ
9203 }
9204
9205 /* If it isn't DW_AT_sibling, skip this attribute. */
9206 form = abbrev->attrs[i].form;
9207 skip_attribute:
9208 switch (form)
9209 {
4bb7a0a7 9210 case DW_FORM_ref_addr:
ae411497
TT
9211 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9212 and later it is offset sized. */
9213 if (cu->header.version == 2)
9214 info_ptr += cu->header.addr_size;
9215 else
9216 info_ptr += cu->header.offset_size;
9217 break;
36586728
TT
9218 case DW_FORM_GNU_ref_alt:
9219 info_ptr += cu->header.offset_size;
9220 break;
ae411497 9221 case DW_FORM_addr:
4bb7a0a7
DJ
9222 info_ptr += cu->header.addr_size;
9223 break;
9224 case DW_FORM_data1:
9225 case DW_FORM_ref1:
9226 case DW_FORM_flag:
9227 info_ptr += 1;
9228 break;
2dc7f7b3 9229 case DW_FORM_flag_present:
43988095 9230 case DW_FORM_implicit_const:
2dc7f7b3 9231 break;
4bb7a0a7
DJ
9232 case DW_FORM_data2:
9233 case DW_FORM_ref2:
9234 info_ptr += 2;
9235 break;
9236 case DW_FORM_data4:
9237 case DW_FORM_ref4:
9238 info_ptr += 4;
9239 break;
9240 case DW_FORM_data8:
9241 case DW_FORM_ref8:
55f1336d 9242 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
9243 info_ptr += 8;
9244 break;
0224619f
JK
9245 case DW_FORM_data16:
9246 info_ptr += 16;
9247 break;
4bb7a0a7 9248 case DW_FORM_string:
9b1c24c8 9249 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
9250 info_ptr += bytes_read;
9251 break;
2dc7f7b3 9252 case DW_FORM_sec_offset:
4bb7a0a7 9253 case DW_FORM_strp:
36586728 9254 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
9255 info_ptr += cu->header.offset_size;
9256 break;
2dc7f7b3 9257 case DW_FORM_exprloc:
4bb7a0a7
DJ
9258 case DW_FORM_block:
9259 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9260 info_ptr += bytes_read;
9261 break;
9262 case DW_FORM_block1:
9263 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9264 break;
9265 case DW_FORM_block2:
9266 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9267 break;
9268 case DW_FORM_block4:
9269 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9270 break;
9271 case DW_FORM_sdata:
9272 case DW_FORM_udata:
9273 case DW_FORM_ref_udata:
3019eac3
DE
9274 case DW_FORM_GNU_addr_index:
9275 case DW_FORM_GNU_str_index:
d521ce57 9276 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
9277 break;
9278 case DW_FORM_indirect:
9279 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9280 info_ptr += bytes_read;
9281 /* We need to continue parsing from here, so just go back to
9282 the top. */
9283 goto skip_attribute;
9284
9285 default:
3e43a32a
MS
9286 error (_("Dwarf Error: Cannot handle %s "
9287 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
9288 dwarf_form_name (form),
9289 bfd_get_filename (abfd));
9290 }
9291 }
9292
9293 if (abbrev->has_children)
dee91e82 9294 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
9295 else
9296 return info_ptr;
9297}
9298
93311388 9299/* Locate ORIG_PDI's sibling.
dee91e82 9300 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 9301
d521ce57 9302static const gdb_byte *
dee91e82
DE
9303locate_pdi_sibling (const struct die_reader_specs *reader,
9304 struct partial_die_info *orig_pdi,
d521ce57 9305 const gdb_byte *info_ptr)
91c24f0a
DC
9306{
9307 /* Do we know the sibling already? */
72bf9492 9308
91c24f0a
DC
9309 if (orig_pdi->sibling)
9310 return orig_pdi->sibling;
9311
9312 /* Are there any children to deal with? */
9313
9314 if (!orig_pdi->has_children)
9315 return info_ptr;
9316
4bb7a0a7 9317 /* Skip the children the long way. */
91c24f0a 9318
dee91e82 9319 return skip_children (reader, info_ptr);
91c24f0a
DC
9320}
9321
257e7a09 9322/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 9323 not NULL. */
c906108c
SS
9324
9325static void
257e7a09
YQ
9326dwarf2_read_symtab (struct partial_symtab *self,
9327 struct objfile *objfile)
c906108c 9328{
ed2dc618
SM
9329 struct dwarf2_per_objfile *dwarf2_per_objfile
9330 = get_dwarf2_per_objfile (objfile);
9331
257e7a09 9332 if (self->readin)
c906108c 9333 {
442e4d9c 9334 warning (_("bug: psymtab for %s is already read in."),
257e7a09 9335 self->filename);
442e4d9c
YQ
9336 }
9337 else
9338 {
9339 if (info_verbose)
c906108c 9340 {
442e4d9c 9341 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 9342 self->filename);
442e4d9c 9343 gdb_flush (gdb_stdout);
c906108c 9344 }
c906108c 9345
442e4d9c
YQ
9346 /* If this psymtab is constructed from a debug-only objfile, the
9347 has_section_at_zero flag will not necessarily be correct. We
9348 can get the correct value for this flag by looking at the data
9349 associated with the (presumably stripped) associated objfile. */
9350 if (objfile->separate_debug_objfile_backlink)
9351 {
9352 struct dwarf2_per_objfile *dpo_backlink
ed2dc618 9353 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9a619af0 9354
442e4d9c
YQ
9355 dwarf2_per_objfile->has_section_at_zero
9356 = dpo_backlink->has_section_at_zero;
9357 }
b2ab525c 9358
442e4d9c 9359 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 9360
257e7a09 9361 psymtab_to_symtab_1 (self);
c906108c 9362
442e4d9c
YQ
9363 /* Finish up the debug error message. */
9364 if (info_verbose)
9365 printf_filtered (_("done.\n"));
c906108c 9366 }
95554aad 9367
ed2dc618 9368 process_cu_includes (dwarf2_per_objfile);
c906108c 9369}
9cdd5dbd
DE
9370\f
9371/* Reading in full CUs. */
c906108c 9372
10b3939b
DJ
9373/* Add PER_CU to the queue. */
9374
9375static void
95554aad
TT
9376queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9377 enum language pretend_language)
10b3939b
DJ
9378{
9379 struct dwarf2_queue_item *item;
9380
9381 per_cu->queued = 1;
8d749320 9382 item = XNEW (struct dwarf2_queue_item);
10b3939b 9383 item->per_cu = per_cu;
95554aad 9384 item->pretend_language = pretend_language;
10b3939b
DJ
9385 item->next = NULL;
9386
9387 if (dwarf2_queue == NULL)
9388 dwarf2_queue = item;
9389 else
9390 dwarf2_queue_tail->next = item;
9391
9392 dwarf2_queue_tail = item;
9393}
9394
89e63ee4
DE
9395/* If PER_CU is not yet queued, add it to the queue.
9396 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9397 dependency.
0907af0c 9398 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
9399 meaning either PER_CU is already queued or it is already loaded.
9400
9401 N.B. There is an invariant here that if a CU is queued then it is loaded.
9402 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
9403
9404static int
89e63ee4 9405maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
9406 struct dwarf2_per_cu_data *per_cu,
9407 enum language pretend_language)
9408{
9409 /* We may arrive here during partial symbol reading, if we need full
9410 DIEs to process an unusual case (e.g. template arguments). Do
9411 not queue PER_CU, just tell our caller to load its DIEs. */
ed2dc618 9412 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
0907af0c
DE
9413 {
9414 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9415 return 1;
9416 return 0;
9417 }
9418
9419 /* Mark the dependence relation so that we don't flush PER_CU
9420 too early. */
89e63ee4
DE
9421 if (dependent_cu != NULL)
9422 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
9423
9424 /* If it's already on the queue, we have nothing to do. */
9425 if (per_cu->queued)
9426 return 0;
9427
9428 /* If the compilation unit is already loaded, just mark it as
9429 used. */
9430 if (per_cu->cu != NULL)
9431 {
9432 per_cu->cu->last_used = 0;
9433 return 0;
9434 }
9435
9436 /* Add it to the queue. */
9437 queue_comp_unit (per_cu, pretend_language);
9438
9439 return 1;
9440}
9441
10b3939b
DJ
9442/* Process the queue. */
9443
9444static void
ed2dc618 9445process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b
DJ
9446{
9447 struct dwarf2_queue_item *item, *next_item;
9448
b4f54984 9449 if (dwarf_read_debug)
45cfd468
DE
9450 {
9451 fprintf_unfiltered (gdb_stdlog,
9452 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 9453 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
9454 }
9455
03dd20cc
DJ
9456 /* The queue starts out with one item, but following a DIE reference
9457 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
9458 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9459 {
cc12ce38
DE
9460 if ((dwarf2_per_objfile->using_index
9461 ? !item->per_cu->v.quick->compunit_symtab
9462 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9463 /* Skip dummy CUs. */
9464 && item->per_cu->cu != NULL)
f4dc4d17
DE
9465 {
9466 struct dwarf2_per_cu_data *per_cu = item->per_cu;
73be47f5 9467 unsigned int debug_print_threshold;
247f5c4f 9468 char buf[100];
f4dc4d17 9469
247f5c4f 9470 if (per_cu->is_debug_types)
f4dc4d17 9471 {
247f5c4f
DE
9472 struct signatured_type *sig_type =
9473 (struct signatured_type *) per_cu;
9474
9d8780f0 9475 sprintf (buf, "TU %s at offset %s",
73be47f5 9476 hex_string (sig_type->signature),
9d8780f0 9477 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9478 /* There can be 100s of TUs.
9479 Only print them in verbose mode. */
9480 debug_print_threshold = 2;
f4dc4d17 9481 }
247f5c4f 9482 else
73be47f5 9483 {
9d8780f0
SM
9484 sprintf (buf, "CU at offset %s",
9485 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9486 debug_print_threshold = 1;
9487 }
247f5c4f 9488
b4f54984 9489 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9490 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
9491
9492 if (per_cu->is_debug_types)
9493 process_full_type_unit (per_cu, item->pretend_language);
9494 else
9495 process_full_comp_unit (per_cu, item->pretend_language);
9496
b4f54984 9497 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9498 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 9499 }
10b3939b
DJ
9500
9501 item->per_cu->queued = 0;
9502 next_item = item->next;
9503 xfree (item);
9504 }
9505
9506 dwarf2_queue_tail = NULL;
45cfd468 9507
b4f54984 9508 if (dwarf_read_debug)
45cfd468
DE
9509 {
9510 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 9511 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 9512 }
10b3939b
DJ
9513}
9514
10b3939b
DJ
9515/* Read in full symbols for PST, and anything it depends on. */
9516
c906108c 9517static void
fba45db2 9518psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 9519{
10b3939b 9520 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
9521 int i;
9522
95554aad
TT
9523 if (pst->readin)
9524 return;
9525
aaa75496 9526 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
9527 if (!pst->dependencies[i]->readin
9528 && pst->dependencies[i]->user == NULL)
aaa75496
JB
9529 {
9530 /* Inform about additional files that need to be read in. */
9531 if (info_verbose)
9532 {
a3f17187 9533 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
9534 fputs_filtered (" ", gdb_stdout);
9535 wrap_here ("");
9536 fputs_filtered ("and ", gdb_stdout);
9537 wrap_here ("");
9538 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 9539 wrap_here (""); /* Flush output. */
aaa75496
JB
9540 gdb_flush (gdb_stdout);
9541 }
9542 psymtab_to_symtab_1 (pst->dependencies[i]);
9543 }
9544
9a3c8263 9545 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10b3939b
DJ
9546
9547 if (per_cu == NULL)
aaa75496
JB
9548 {
9549 /* It's an include file, no symbols to read for it.
9550 Everything is in the parent symtab. */
9551 pst->readin = 1;
9552 return;
9553 }
c906108c 9554
58f0c718 9555 dw2_do_instantiate_symtab (per_cu, false);
10b3939b
DJ
9556}
9557
dee91e82
DE
9558/* Trivial hash function for die_info: the hash value of a DIE
9559 is its offset in .debug_info for this objfile. */
10b3939b 9560
dee91e82
DE
9561static hashval_t
9562die_hash (const void *item)
10b3939b 9563{
9a3c8263 9564 const struct die_info *die = (const struct die_info *) item;
6502dd73 9565
9c541725 9566 return to_underlying (die->sect_off);
dee91e82 9567}
63d06c5c 9568
dee91e82
DE
9569/* Trivial comparison function for die_info structures: two DIEs
9570 are equal if they have the same offset. */
98bfdba5 9571
dee91e82
DE
9572static int
9573die_eq (const void *item_lhs, const void *item_rhs)
9574{
9a3c8263
SM
9575 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9576 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9577
9c541725 9578 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9579}
c906108c 9580
dee91e82
DE
9581/* die_reader_func for load_full_comp_unit.
9582 This is identical to read_signatured_type_reader,
9583 but is kept separate for now. */
c906108c 9584
dee91e82
DE
9585static void
9586load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 9587 const gdb_byte *info_ptr,
dee91e82
DE
9588 struct die_info *comp_unit_die,
9589 int has_children,
9590 void *data)
9591{
9592 struct dwarf2_cu *cu = reader->cu;
9a3c8263 9593 enum language *language_ptr = (enum language *) data;
6caca83c 9594
dee91e82
DE
9595 gdb_assert (cu->die_hash == NULL);
9596 cu->die_hash =
9597 htab_create_alloc_ex (cu->header.length / 12,
9598 die_hash,
9599 die_eq,
9600 NULL,
9601 &cu->comp_unit_obstack,
9602 hashtab_obstack_allocate,
9603 dummy_obstack_deallocate);
e142c38c 9604
dee91e82
DE
9605 if (has_children)
9606 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
9607 &info_ptr, comp_unit_die);
9608 cu->dies = comp_unit_die;
9609 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9610
9611 /* We try not to read any attributes in this function, because not
9cdd5dbd 9612 all CUs needed for references have been loaded yet, and symbol
10b3939b 9613 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9614 or we won't be able to build types correctly.
9615 Similarly, if we do not read the producer, we can not apply
9616 producer-specific interpretation. */
95554aad 9617 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 9618}
10b3939b 9619
dee91e82 9620/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 9621
dee91e82 9622static void
95554aad 9623load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
58f0c718 9624 bool skip_partial,
95554aad 9625 enum language pretend_language)
dee91e82 9626{
3019eac3 9627 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 9628
58f0c718 9629 init_cutu_and_read_dies (this_cu, NULL, 1, 1, skip_partial,
f4dc4d17 9630 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
9631}
9632
3da10d80
KS
9633/* Add a DIE to the delayed physname list. */
9634
9635static void
9636add_to_method_list (struct type *type, int fnfield_index, int index,
9637 const char *name, struct die_info *die,
9638 struct dwarf2_cu *cu)
9639{
9640 struct delayed_method_info mi;
9641 mi.type = type;
9642 mi.fnfield_index = fnfield_index;
9643 mi.index = index;
9644 mi.name = name;
9645 mi.die = die;
c89b44cd 9646 cu->method_list.push_back (mi);
3da10d80
KS
9647}
9648
3693fdb3
PA
9649/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9650 "const" / "volatile". If so, decrements LEN by the length of the
9651 modifier and return true. Otherwise return false. */
9652
9653template<size_t N>
9654static bool
9655check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9656{
9657 size_t mod_len = sizeof (mod) - 1;
9658 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9659 {
9660 len -= mod_len;
9661 return true;
9662 }
9663 return false;
9664}
9665
3da10d80
KS
9666/* Compute the physnames of any methods on the CU's method list.
9667
9668 The computation of method physnames is delayed in order to avoid the
9669 (bad) condition that one of the method's formal parameters is of an as yet
9670 incomplete type. */
9671
9672static void
9673compute_delayed_physnames (struct dwarf2_cu *cu)
9674{
3693fdb3 9675 /* Only C++ delays computing physnames. */
c89b44cd 9676 if (cu->method_list.empty ())
3693fdb3
PA
9677 return;
9678 gdb_assert (cu->language == language_cplus);
9679
52941706 9680 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9681 {
1d06ead6 9682 const char *physname;
3da10d80 9683 struct fn_fieldlist *fn_flp
c89b44cd
TT
9684 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9685 physname = dwarf2_physname (mi.name, mi.die, cu);
9686 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9687 = physname ? physname : "";
3693fdb3
PA
9688
9689 /* Since there's no tag to indicate whether a method is a
9690 const/volatile overload, extract that information out of the
9691 demangled name. */
9692 if (physname != NULL)
9693 {
9694 size_t len = strlen (physname);
9695
9696 while (1)
9697 {
9698 if (physname[len] == ')') /* shortcut */
9699 break;
9700 else if (check_modifier (physname, len, " const"))
c89b44cd 9701 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9702 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9703 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9704 else
9705 break;
9706 }
9707 }
3da10d80 9708 }
c89b44cd
TT
9709
9710 /* The list is no longer needed. */
9711 cu->method_list.clear ();
3da10d80
KS
9712}
9713
380618d6
KS
9714/* A wrapper for add_symbol_to_list to ensure that SYMBOL's language is
9715 the same as all other symbols in LISTHEAD. If a new symbol is added
9716 with a different language, this function asserts. */
9717
9718static inline void
9719dw2_add_symbol_to_list (struct symbol *symbol, struct pending **listhead)
9720{
9721 /* Only assert if LISTHEAD already contains symbols of a different
9722 language (dict_create_hashed/insert_symbol_hashed requires that all
9723 symbols in this list are of the same language). */
9724 gdb_assert ((*listhead) == NULL
9725 || (SYMBOL_LANGUAGE ((*listhead)->symbol[0])
9726 == SYMBOL_LANGUAGE (symbol)));
9727
9728 add_symbol_to_list (symbol, listhead);
9729}
9730
a766d390
DE
9731/* Go objects should be embedded in a DW_TAG_module DIE,
9732 and it's not clear if/how imported objects will appear.
9733 To keep Go support simple until that's worked out,
9734 go back through what we've read and create something usable.
9735 We could do this while processing each DIE, and feels kinda cleaner,
9736 but that way is more invasive.
9737 This is to, for example, allow the user to type "p var" or "b main"
9738 without having to specify the package name, and allow lookups
9739 of module.object to work in contexts that use the expression
9740 parser. */
9741
9742static void
9743fixup_go_packaging (struct dwarf2_cu *cu)
9744{
9745 char *package_name = NULL;
9746 struct pending *list;
9747 int i;
9748
804d2729
TT
9749 for (list = *cu->builder->get_global_symbols ();
9750 list != NULL;
9751 list = list->next)
a766d390
DE
9752 {
9753 for (i = 0; i < list->nsyms; ++i)
9754 {
9755 struct symbol *sym = list->symbol[i];
9756
9757 if (SYMBOL_LANGUAGE (sym) == language_go
9758 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9759 {
9760 char *this_package_name = go_symbol_package_name (sym);
9761
9762 if (this_package_name == NULL)
9763 continue;
9764 if (package_name == NULL)
9765 package_name = this_package_name;
9766 else
9767 {
518817b3
SM
9768 struct objfile *objfile
9769 = cu->per_cu->dwarf2_per_objfile->objfile;
a766d390 9770 if (strcmp (package_name, this_package_name) != 0)
b98664d3 9771 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9772 (symbol_symtab (sym) != NULL
9773 ? symtab_to_filename_for_display
9774 (symbol_symtab (sym))
e3b94546 9775 : objfile_name (objfile)),
a766d390
DE
9776 this_package_name, package_name);
9777 xfree (this_package_name);
9778 }
9779 }
9780 }
9781 }
9782
9783 if (package_name != NULL)
9784 {
518817b3 9785 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
34a68019 9786 const char *saved_package_name
224c3ddb
SM
9787 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
9788 package_name,
9789 strlen (package_name));
19f392bc
UW
9790 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9791 saved_package_name);
a766d390
DE
9792 struct symbol *sym;
9793
e623cf5d 9794 sym = allocate_symbol (objfile);
f85f34ed 9795 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
9796 SYMBOL_SET_NAMES (sym, saved_package_name,
9797 strlen (saved_package_name), 0, objfile);
a766d390
DE
9798 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9799 e.g., "main" finds the "main" module and not C's main(). */
9800 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9801 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9802 SYMBOL_TYPE (sym) = type;
9803
380618d6 9804 dw2_add_symbol_to_list (sym, cu->builder->get_global_symbols ());
a766d390
DE
9805
9806 xfree (package_name);
9807 }
9808}
9809
c9317f21
TT
9810/* Allocate a fully-qualified name consisting of the two parts on the
9811 obstack. */
9812
9813static const char *
9814rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9815{
9816 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9817}
9818
9819/* A helper that allocates a struct discriminant_info to attach to a
9820 union type. */
9821
9822static struct discriminant_info *
9823alloc_discriminant_info (struct type *type, int discriminant_index,
9824 int default_index)
9825{
9826 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
c7b15a66
TT
9827 gdb_assert (discriminant_index == -1
9828 || (discriminant_index >= 0
9829 && discriminant_index < TYPE_NFIELDS (type)));
c9317f21 9830 gdb_assert (default_index == -1
c7b15a66 9831 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
c9317f21
TT
9832
9833 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9834
9835 struct discriminant_info *disc
9836 = ((struct discriminant_info *)
9837 TYPE_ZALLOC (type,
9838 offsetof (struct discriminant_info, discriminants)
9839 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9840 disc->default_index = default_index;
9841 disc->discriminant_index = discriminant_index;
9842
9843 struct dynamic_prop prop;
9844 prop.kind = PROP_UNDEFINED;
9845 prop.data.baton = disc;
9846
9847 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9848
9849 return disc;
9850}
9851
9852/* Some versions of rustc emitted enums in an unusual way.
9853
9854 Ordinary enums were emitted as unions. The first element of each
9855 structure in the union was named "RUST$ENUM$DISR". This element
9856 held the discriminant.
9857
9858 These versions of Rust also implemented the "non-zero"
9859 optimization. When the enum had two values, and one is empty and
9860 the other holds a pointer that cannot be zero, the pointer is used
9861 as the discriminant, with a zero value meaning the empty variant.
9862 Here, the union's first member is of the form
9863 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9864 where the fieldnos are the indices of the fields that should be
9865 traversed in order to find the field (which may be several fields deep)
9866 and the variantname is the name of the variant of the case when the
9867 field is zero.
9868
9869 This function recognizes whether TYPE is of one of these forms,
9870 and, if so, smashes it to be a variant type. */
9871
9872static void
9873quirk_rust_enum (struct type *type, struct objfile *objfile)
9874{
9875 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9876
9877 /* We don't need to deal with empty enums. */
9878 if (TYPE_NFIELDS (type) == 0)
9879 return;
9880
9881#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9882 if (TYPE_NFIELDS (type) == 1
9883 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9884 {
9885 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9886
9887 /* Decode the field name to find the offset of the
9888 discriminant. */
9889 ULONGEST bit_offset = 0;
9890 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9891 while (name[0] >= '0' && name[0] <= '9')
9892 {
9893 char *tail;
9894 unsigned long index = strtoul (name, &tail, 10);
9895 name = tail;
9896 if (*name != '$'
9897 || index >= TYPE_NFIELDS (field_type)
9898 || (TYPE_FIELD_LOC_KIND (field_type, index)
9899 != FIELD_LOC_KIND_BITPOS))
9900 {
b98664d3 9901 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9902 "[in module %s]"),
9903 TYPE_FIELD_NAME (type, 0),
9904 objfile_name (objfile));
9905 return;
9906 }
9907 ++name;
9908
9909 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9910 field_type = TYPE_FIELD_TYPE (field_type, index);
9911 }
9912
9913 /* Make a union to hold the variants. */
9914 struct type *union_type = alloc_type (objfile);
9915 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9916 TYPE_NFIELDS (union_type) = 3;
9917 TYPE_FIELDS (union_type)
9918 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9919 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9920 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9921
9922 /* Put the discriminant must at index 0. */
9923 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9924 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9925 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9926 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9927
9928 /* The order of fields doesn't really matter, so put the real
9929 field at index 1 and the data-less field at index 2. */
9930 struct discriminant_info *disc
9931 = alloc_discriminant_info (union_type, 0, 1);
9932 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9933 TYPE_FIELD_NAME (union_type, 1)
9934 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9935 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9936 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9937 TYPE_FIELD_NAME (union_type, 1));
9938
9939 const char *dataless_name
9940 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9941 name);
9942 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9943 dataless_name);
9944 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9945 /* NAME points into the original discriminant name, which
9946 already has the correct lifetime. */
9947 TYPE_FIELD_NAME (union_type, 2) = name;
9948 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9949 disc->discriminants[2] = 0;
9950
9951 /* Smash this type to be a structure type. We have to do this
9952 because the type has already been recorded. */
9953 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9954 TYPE_NFIELDS (type) = 1;
9955 TYPE_FIELDS (type)
9956 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9957
9958 /* Install the variant part. */
9959 TYPE_FIELD_TYPE (type, 0) = union_type;
9960 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9961 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9962 }
9963 else if (TYPE_NFIELDS (type) == 1)
9964 {
9965 /* We assume that a union with a single field is a univariant
9966 enum. */
9967 /* Smash this type to be a structure type. We have to do this
9968 because the type has already been recorded. */
9969 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9970
9971 /* Make a union to hold the variants. */
9972 struct type *union_type = alloc_type (objfile);
9973 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9974 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9975 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9976 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9977 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9978
9979 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9980 const char *variant_name
9981 = rust_last_path_segment (TYPE_NAME (field_type));
9982 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9983 TYPE_NAME (field_type)
9984 = rust_fully_qualify (&objfile->objfile_obstack,
c7b15a66 9985 TYPE_NAME (type), variant_name);
c9317f21
TT
9986
9987 /* Install the union in the outer struct type. */
9988 TYPE_NFIELDS (type) = 1;
9989 TYPE_FIELDS (type)
9990 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9991 TYPE_FIELD_TYPE (type, 0) = union_type;
9992 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9993 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9994
9995 alloc_discriminant_info (union_type, -1, 0);
9996 }
9997 else
9998 {
9999 struct type *disr_type = nullptr;
10000 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
10001 {
10002 disr_type = TYPE_FIELD_TYPE (type, i);
10003
a037790e
TT
10004 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
10005 {
10006 /* All fields of a true enum will be structs. */
10007 return;
10008 }
10009 else if (TYPE_NFIELDS (disr_type) == 0)
c9317f21
TT
10010 {
10011 /* Could be data-less variant, so keep going. */
a037790e 10012 disr_type = nullptr;
c9317f21
TT
10013 }
10014 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
10015 "RUST$ENUM$DISR") != 0)
10016 {
10017 /* Not a Rust enum. */
10018 return;
10019 }
10020 else
10021 {
10022 /* Found one. */
10023 break;
10024 }
10025 }
10026
10027 /* If we got here without a discriminant, then it's probably
10028 just a union. */
10029 if (disr_type == nullptr)
10030 return;
10031
10032 /* Smash this type to be a structure type. We have to do this
10033 because the type has already been recorded. */
10034 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10035
10036 /* Make a union to hold the variants. */
10037 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10038 struct type *union_type = alloc_type (objfile);
10039 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10040 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10041 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10042 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10043 TYPE_FIELDS (union_type)
10044 = (struct field *) TYPE_ZALLOC (union_type,
10045 (TYPE_NFIELDS (union_type)
10046 * sizeof (struct field)));
10047
10048 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10049 TYPE_NFIELDS (type) * sizeof (struct field));
10050
10051 /* Install the discriminant at index 0 in the union. */
10052 TYPE_FIELD (union_type, 0) = *disr_field;
10053 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10054 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10055
10056 /* Install the union in the outer struct type. */
10057 TYPE_FIELD_TYPE (type, 0) = union_type;
10058 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10059 TYPE_NFIELDS (type) = 1;
10060
10061 /* Set the size and offset of the union type. */
10062 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10063
10064 /* We need a way to find the correct discriminant given a
10065 variant name. For convenience we build a map here. */
10066 struct type *enum_type = FIELD_TYPE (*disr_field);
10067 std::unordered_map<std::string, ULONGEST> discriminant_map;
10068 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10069 {
10070 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10071 {
10072 const char *name
10073 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10074 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10075 }
10076 }
10077
10078 int n_fields = TYPE_NFIELDS (union_type);
10079 struct discriminant_info *disc
10080 = alloc_discriminant_info (union_type, 0, -1);
10081 /* Skip the discriminant here. */
10082 for (int i = 1; i < n_fields; ++i)
10083 {
10084 /* Find the final word in the name of this variant's type.
10085 That name can be used to look up the correct
10086 discriminant. */
10087 const char *variant_name
10088 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10089 i)));
10090
10091 auto iter = discriminant_map.find (variant_name);
10092 if (iter != discriminant_map.end ())
10093 disc->discriminants[i] = iter->second;
10094
bedda9ac 10095 /* Remove the discriminant field, if it exists. */
c9317f21 10096 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
bedda9ac
TT
10097 if (TYPE_NFIELDS (sub_type) > 0)
10098 {
10099 --TYPE_NFIELDS (sub_type);
10100 ++TYPE_FIELDS (sub_type);
10101 }
c9317f21
TT
10102 TYPE_FIELD_NAME (union_type, i) = variant_name;
10103 TYPE_NAME (sub_type)
10104 = rust_fully_qualify (&objfile->objfile_obstack,
10105 TYPE_NAME (type), variant_name);
10106 }
10107 }
10108}
10109
10110/* Rewrite some Rust unions to be structures with variants parts. */
10111
10112static void
10113rust_union_quirks (struct dwarf2_cu *cu)
10114{
10115 gdb_assert (cu->language == language_rust);
52941706
SM
10116 for (type *type_ : cu->rust_unions)
10117 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
2d79090e
TT
10118 /* We don't need this any more. */
10119 cu->rust_unions.clear ();
c9317f21
TT
10120}
10121
95554aad
TT
10122/* Return the symtab for PER_CU. This works properly regardless of
10123 whether we're using the index or psymtabs. */
10124
43f3e411
DE
10125static struct compunit_symtab *
10126get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad 10127{
ed2dc618 10128 return (per_cu->dwarf2_per_objfile->using_index
43f3e411
DE
10129 ? per_cu->v.quick->compunit_symtab
10130 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
10131}
10132
10133/* A helper function for computing the list of all symbol tables
10134 included by PER_CU. */
10135
10136static void
43f3e411 10137recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
ec94af83 10138 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 10139 struct dwarf2_per_cu_data *per_cu,
43f3e411 10140 struct compunit_symtab *immediate_parent)
95554aad
TT
10141{
10142 void **slot;
10143 int ix;
43f3e411 10144 struct compunit_symtab *cust;
95554aad
TT
10145 struct dwarf2_per_cu_data *iter;
10146
10147 slot = htab_find_slot (all_children, per_cu, INSERT);
10148 if (*slot != NULL)
10149 {
10150 /* This inclusion and its children have been processed. */
10151 return;
10152 }
10153
10154 *slot = per_cu;
10155 /* Only add a CU if it has a symbol table. */
43f3e411
DE
10156 cust = get_compunit_symtab (per_cu);
10157 if (cust != NULL)
ec94af83
DE
10158 {
10159 /* If this is a type unit only add its symbol table if we haven't
10160 seen it yet (type unit per_cu's can share symtabs). */
10161 if (per_cu->is_debug_types)
10162 {
43f3e411 10163 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
10164 if (*slot == NULL)
10165 {
43f3e411
DE
10166 *slot = cust;
10167 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10168 if (cust->user == NULL)
10169 cust->user = immediate_parent;
ec94af83
DE
10170 }
10171 }
10172 else
f9125b6c 10173 {
43f3e411
DE
10174 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10175 if (cust->user == NULL)
10176 cust->user = immediate_parent;
f9125b6c 10177 }
ec94af83 10178 }
95554aad
TT
10179
10180 for (ix = 0;
796a7ff8 10181 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad 10182 ++ix)
ec94af83
DE
10183 {
10184 recursively_compute_inclusions (result, all_children,
43f3e411 10185 all_type_symtabs, iter, cust);
ec94af83 10186 }
95554aad
TT
10187}
10188
43f3e411 10189/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
10190 PER_CU. */
10191
10192static void
43f3e411 10193compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 10194{
f4dc4d17
DE
10195 gdb_assert (! per_cu->is_debug_types);
10196
796a7ff8 10197 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
10198 {
10199 int ix, len;
ec94af83 10200 struct dwarf2_per_cu_data *per_cu_iter;
43f3e411
DE
10201 struct compunit_symtab *compunit_symtab_iter;
10202 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
ec94af83 10203 htab_t all_children, all_type_symtabs;
43f3e411 10204 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
10205
10206 /* If we don't have a symtab, we can just skip this case. */
43f3e411 10207 if (cust == NULL)
95554aad
TT
10208 return;
10209
10210 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10211 NULL, xcalloc, xfree);
ec94af83
DE
10212 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10213 NULL, xcalloc, xfree);
95554aad
TT
10214
10215 for (ix = 0;
796a7ff8 10216 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
ec94af83 10217 ix, per_cu_iter);
95554aad 10218 ++ix)
ec94af83
DE
10219 {
10220 recursively_compute_inclusions (&result_symtabs, all_children,
f9125b6c 10221 all_type_symtabs, per_cu_iter,
43f3e411 10222 cust);
ec94af83 10223 }
95554aad 10224
ec94af83 10225 /* Now we have a transitive closure of all the included symtabs. */
43f3e411
DE
10226 len = VEC_length (compunit_symtab_ptr, result_symtabs);
10227 cust->includes
ed2dc618 10228 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 10229 struct compunit_symtab *, len + 1);
95554aad 10230 for (ix = 0;
43f3e411
DE
10231 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
10232 compunit_symtab_iter);
95554aad 10233 ++ix)
43f3e411
DE
10234 cust->includes[ix] = compunit_symtab_iter;
10235 cust->includes[len] = NULL;
95554aad 10236
43f3e411 10237 VEC_free (compunit_symtab_ptr, result_symtabs);
95554aad 10238 htab_delete (all_children);
ec94af83 10239 htab_delete (all_type_symtabs);
95554aad
TT
10240 }
10241}
10242
10243/* Compute the 'includes' field for the symtabs of all the CUs we just
10244 read. */
10245
10246static void
ed2dc618 10247process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 10248{
71b73764 10249 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
f4dc4d17
DE
10250 {
10251 if (! iter->is_debug_types)
43f3e411 10252 compute_compunit_symtab_includes (iter);
f4dc4d17 10253 }
95554aad 10254
c5d0225d 10255 dwarf2_per_objfile->just_read_cus.clear ();
95554aad
TT
10256}
10257
9cdd5dbd 10258/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
10259 already been loaded into memory. */
10260
10261static void
95554aad
TT
10262process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10263 enum language pretend_language)
10b3939b 10264{
10b3939b 10265 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10266 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10267 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 10268 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 10269 CORE_ADDR lowpc, highpc;
43f3e411 10270 struct compunit_symtab *cust;
10b3939b 10271 CORE_ADDR baseaddr;
4359dff1 10272 struct block *static_block;
3e29f34a 10273 CORE_ADDR addr;
10b3939b
DJ
10274
10275 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10276
c89b44cd
TT
10277 /* Clear the list here in case something was left over. */
10278 cu->method_list.clear ();
10b3939b 10279
95554aad
TT
10280 cu->language = pretend_language;
10281 cu->language_defn = language_def (cu->language);
10282
c906108c 10283 /* Do line number decoding in read_file_scope () */
10b3939b 10284 process_die (cu->dies, cu);
c906108c 10285
a766d390
DE
10286 /* For now fudge the Go package. */
10287 if (cu->language == language_go)
10288 fixup_go_packaging (cu);
10289
3da10d80
KS
10290 /* Now that we have processed all the DIEs in the CU, all the types
10291 should be complete, and it should now be safe to compute all of the
10292 physnames. */
10293 compute_delayed_physnames (cu);
3da10d80 10294
c9317f21
TT
10295 if (cu->language == language_rust)
10296 rust_union_quirks (cu);
10297
fae299cd
DC
10298 /* Some compilers don't define a DW_AT_high_pc attribute for the
10299 compilation unit. If the DW_AT_high_pc is missing, synthesize
10300 it, by scanning the DIE's below the compilation unit. */
10b3939b 10301 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 10302
3e29f34a 10303 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
804d2729 10304 static_block = cu->builder->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
10305
10306 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10307 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10308 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10309 addrmap to help ensure it has an accurate map of pc values belonging to
10310 this comp unit. */
10311 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10312
804d2729
TT
10313 cust = cu->builder->end_symtab_from_static_block (static_block,
10314 SECT_OFF_TEXT (objfile),
10315 0);
c906108c 10316
43f3e411 10317 if (cust != NULL)
c906108c 10318 {
df15bd07 10319 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 10320
8be455d7
JK
10321 /* Set symtab language to language from DW_AT_language. If the
10322 compilation is from a C file generated by language preprocessors, do
10323 not set the language if it was already deduced by start_subfile. */
43f3e411 10324 if (!(cu->language == language_c
40e3ad0e 10325 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 10326 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
10327
10328 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10329 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
10330 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10331 there were bugs in prologue debug info, fixed later in GCC-4.5
10332 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
10333
10334 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10335 needed, it would be wrong due to missing DW_AT_producer there.
10336
10337 Still one can confuse GDB by using non-standard GCC compilation
10338 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10339 */
ab260dad 10340 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 10341 cust->locations_valid = 1;
e0d00bc7
JK
10342
10343 if (gcc_4_minor >= 5)
43f3e411 10344 cust->epilogue_unwind_valid = 1;
96408a79 10345
43f3e411 10346 cust->call_site_htab = cu->call_site_htab;
c906108c 10347 }
9291a0cd
TT
10348
10349 if (dwarf2_per_objfile->using_index)
43f3e411 10350 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
10351 else
10352 {
10353 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10354 pst->compunit_symtab = cust;
9291a0cd
TT
10355 pst->readin = 1;
10356 }
c906108c 10357
95554aad 10358 /* Push it for inclusion processing later. */
c5d0225d 10359 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
804d2729
TT
10360
10361 /* Not needed any more. */
10362 cu->builder.reset ();
f4dc4d17 10363}
45cfd468 10364
f4dc4d17
DE
10365/* Generate full symbol information for type unit PER_CU, whose DIEs have
10366 already been loaded into memory. */
10367
10368static void
10369process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10370 enum language pretend_language)
10371{
10372 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10373 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10374 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 10375 struct compunit_symtab *cust;
0186c6a7
DE
10376 struct signatured_type *sig_type;
10377
10378 gdb_assert (per_cu->is_debug_types);
10379 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 10380
c89b44cd
TT
10381 /* Clear the list here in case something was left over. */
10382 cu->method_list.clear ();
f4dc4d17 10383
f4dc4d17
DE
10384 cu->language = pretend_language;
10385 cu->language_defn = language_def (cu->language);
10386
10387 /* The symbol tables are set up in read_type_unit_scope. */
10388 process_die (cu->dies, cu);
10389
10390 /* For now fudge the Go package. */
10391 if (cu->language == language_go)
10392 fixup_go_packaging (cu);
10393
10394 /* Now that we have processed all the DIEs in the CU, all the types
10395 should be complete, and it should now be safe to compute all of the
10396 physnames. */
10397 compute_delayed_physnames (cu);
f4dc4d17 10398
c9317f21
TT
10399 if (cu->language == language_rust)
10400 rust_union_quirks (cu);
10401
f4dc4d17
DE
10402 /* TUs share symbol tables.
10403 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
10404 of it with end_expandable_symtab. Otherwise, complete the addition of
10405 this TU's symbols to the existing symtab. */
43f3e411 10406 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 10407 {
804d2729 10408 cust = cu->builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
43f3e411 10409 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 10410
43f3e411 10411 if (cust != NULL)
f4dc4d17
DE
10412 {
10413 /* Set symtab language to language from DW_AT_language. If the
10414 compilation is from a C file generated by language preprocessors,
10415 do not set the language if it was already deduced by
10416 start_subfile. */
43f3e411
DE
10417 if (!(cu->language == language_c
10418 && COMPUNIT_FILETABS (cust)->language != language_c))
10419 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
10420 }
10421 }
10422 else
10423 {
804d2729 10424 cu->builder->augment_type_symtab ();
43f3e411 10425 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
10426 }
10427
10428 if (dwarf2_per_objfile->using_index)
43f3e411 10429 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
10430 else
10431 {
10432 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10433 pst->compunit_symtab = cust;
f4dc4d17 10434 pst->readin = 1;
45cfd468 10435 }
804d2729
TT
10436
10437 /* Not needed any more. */
10438 cu->builder.reset ();
c906108c
SS
10439}
10440
95554aad
TT
10441/* Process an imported unit DIE. */
10442
10443static void
10444process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10445{
10446 struct attribute *attr;
10447
f4dc4d17
DE
10448 /* For now we don't handle imported units in type units. */
10449 if (cu->per_cu->is_debug_types)
10450 {
10451 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10452 " supported in type units [in module %s]"),
518817b3 10453 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
10454 }
10455
95554aad
TT
10456 attr = dwarf2_attr (die, DW_AT_import, cu);
10457 if (attr != NULL)
10458 {
9c541725
PA
10459 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10460 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10461 dwarf2_per_cu_data *per_cu
e3b94546 10462 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
518817b3 10463 cu->per_cu->dwarf2_per_objfile);
95554aad 10464
69d751e3 10465 /* If necessary, add it to the queue and load its DIEs. */
95554aad 10466 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 10467 load_full_comp_unit (per_cu, false, cu->language);
95554aad 10468
796a7ff8 10469 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
10470 per_cu);
10471 }
10472}
10473
4c8aa72d
PA
10474/* RAII object that represents a process_die scope: i.e.,
10475 starts/finishes processing a DIE. */
10476class process_die_scope
adde2bff 10477{
4c8aa72d
PA
10478public:
10479 process_die_scope (die_info *die, dwarf2_cu *cu)
10480 : m_die (die), m_cu (cu)
10481 {
10482 /* We should only be processing DIEs not already in process. */
10483 gdb_assert (!m_die->in_process);
10484 m_die->in_process = true;
10485 }
8c3cb9fa 10486
4c8aa72d
PA
10487 ~process_die_scope ()
10488 {
10489 m_die->in_process = false;
10490
10491 /* If we're done processing the DIE for the CU that owns the line
10492 header, we don't need the line header anymore. */
10493 if (m_cu->line_header_die_owner == m_die)
10494 {
10495 delete m_cu->line_header;
10496 m_cu->line_header = NULL;
10497 m_cu->line_header_die_owner = NULL;
10498 }
10499 }
10500
10501private:
10502 die_info *m_die;
10503 dwarf2_cu *m_cu;
10504};
adde2bff 10505
c906108c
SS
10506/* Process a die and its children. */
10507
10508static void
e7c27a73 10509process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10510{
4c8aa72d 10511 process_die_scope scope (die, cu);
adde2bff 10512
c906108c
SS
10513 switch (die->tag)
10514 {
10515 case DW_TAG_padding:
10516 break;
10517 case DW_TAG_compile_unit:
95554aad 10518 case DW_TAG_partial_unit:
e7c27a73 10519 read_file_scope (die, cu);
c906108c 10520 break;
348e048f
DE
10521 case DW_TAG_type_unit:
10522 read_type_unit_scope (die, cu);
10523 break;
c906108c 10524 case DW_TAG_subprogram:
c906108c 10525 case DW_TAG_inlined_subroutine:
edb3359d 10526 read_func_scope (die, cu);
c906108c
SS
10527 break;
10528 case DW_TAG_lexical_block:
14898363
L
10529 case DW_TAG_try_block:
10530 case DW_TAG_catch_block:
e7c27a73 10531 read_lexical_block_scope (die, cu);
c906108c 10532 break;
216f72a1 10533 case DW_TAG_call_site:
96408a79
SA
10534 case DW_TAG_GNU_call_site:
10535 read_call_site_scope (die, cu);
10536 break;
c906108c 10537 case DW_TAG_class_type:
680b30c7 10538 case DW_TAG_interface_type:
c906108c
SS
10539 case DW_TAG_structure_type:
10540 case DW_TAG_union_type:
134d01f1 10541 process_structure_scope (die, cu);
c906108c
SS
10542 break;
10543 case DW_TAG_enumeration_type:
134d01f1 10544 process_enumeration_scope (die, cu);
c906108c 10545 break;
134d01f1 10546
f792889a
DJ
10547 /* These dies have a type, but processing them does not create
10548 a symbol or recurse to process the children. Therefore we can
10549 read them on-demand through read_type_die. */
c906108c 10550 case DW_TAG_subroutine_type:
72019c9c 10551 case DW_TAG_set_type:
c906108c 10552 case DW_TAG_array_type:
c906108c 10553 case DW_TAG_pointer_type:
c906108c 10554 case DW_TAG_ptr_to_member_type:
c906108c 10555 case DW_TAG_reference_type:
4297a3f0 10556 case DW_TAG_rvalue_reference_type:
c906108c 10557 case DW_TAG_string_type:
c906108c 10558 break;
134d01f1 10559
c906108c 10560 case DW_TAG_base_type:
a02abb62 10561 case DW_TAG_subrange_type:
cb249c71 10562 case DW_TAG_typedef:
134d01f1
DJ
10563 /* Add a typedef symbol for the type definition, if it has a
10564 DW_AT_name. */
f792889a 10565 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10566 break;
c906108c 10567 case DW_TAG_common_block:
e7c27a73 10568 read_common_block (die, cu);
c906108c
SS
10569 break;
10570 case DW_TAG_common_inclusion:
10571 break;
d9fa45fe 10572 case DW_TAG_namespace:
4d4ec4e5 10573 cu->processing_has_namespace_info = 1;
e7c27a73 10574 read_namespace (die, cu);
d9fa45fe 10575 break;
5d7cb8df 10576 case DW_TAG_module:
4d4ec4e5 10577 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
10578 read_module (die, cu);
10579 break;
d9fa45fe 10580 case DW_TAG_imported_declaration:
74921315
KS
10581 cu->processing_has_namespace_info = 1;
10582 if (read_namespace_alias (die, cu))
10583 break;
86a73007
TT
10584 /* The declaration is not a global namespace alias. */
10585 /* Fall through. */
d9fa45fe 10586 case DW_TAG_imported_module:
4d4ec4e5 10587 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
10588 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10589 || cu->language != language_fortran))
b98664d3 10590 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10591 dwarf_tag_name (die->tag));
10592 read_import_statement (die, cu);
d9fa45fe 10593 break;
95554aad
TT
10594
10595 case DW_TAG_imported_unit:
10596 process_imported_unit_die (die, cu);
10597 break;
10598
71a3c369
TT
10599 case DW_TAG_variable:
10600 read_variable (die, cu);
10601 break;
10602
c906108c 10603 default:
e7c27a73 10604 new_symbol (die, NULL, cu);
c906108c
SS
10605 break;
10606 }
10607}
ca69b9e6
DE
10608\f
10609/* DWARF name computation. */
c906108c 10610
94af9270
KS
10611/* A helper function for dwarf2_compute_name which determines whether DIE
10612 needs to have the name of the scope prepended to the name listed in the
10613 die. */
10614
10615static int
10616die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10617{
1c809c68
TT
10618 struct attribute *attr;
10619
94af9270
KS
10620 switch (die->tag)
10621 {
10622 case DW_TAG_namespace:
10623 case DW_TAG_typedef:
10624 case DW_TAG_class_type:
10625 case DW_TAG_interface_type:
10626 case DW_TAG_structure_type:
10627 case DW_TAG_union_type:
10628 case DW_TAG_enumeration_type:
10629 case DW_TAG_enumerator:
10630 case DW_TAG_subprogram:
08a76f8a 10631 case DW_TAG_inlined_subroutine:
94af9270 10632 case DW_TAG_member:
74921315 10633 case DW_TAG_imported_declaration:
94af9270
KS
10634 return 1;
10635
10636 case DW_TAG_variable:
c2b0a229 10637 case DW_TAG_constant:
94af9270
KS
10638 /* We only need to prefix "globally" visible variables. These include
10639 any variable marked with DW_AT_external or any variable that
10640 lives in a namespace. [Variables in anonymous namespaces
10641 require prefixing, but they are not DW_AT_external.] */
10642
10643 if (dwarf2_attr (die, DW_AT_specification, cu))
10644 {
10645 struct dwarf2_cu *spec_cu = cu;
9a619af0 10646
94af9270
KS
10647 return die_needs_namespace (die_specification (die, &spec_cu),
10648 spec_cu);
10649 }
10650
1c809c68 10651 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10652 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10653 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10654 return 0;
10655 /* A variable in a lexical block of some kind does not need a
10656 namespace, even though in C++ such variables may be external
10657 and have a mangled name. */
10658 if (die->parent->tag == DW_TAG_lexical_block
10659 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10660 || die->parent->tag == DW_TAG_catch_block
10661 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10662 return 0;
10663 return 1;
94af9270
KS
10664
10665 default:
10666 return 0;
10667 }
10668}
10669
73b9be8b
KS
10670/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10671 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10672 defined for the given DIE. */
10673
10674static struct attribute *
10675dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10676{
10677 struct attribute *attr;
10678
10679 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10680 if (attr == NULL)
10681 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10682
10683 return attr;
10684}
10685
10686/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10687 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10688 defined for the given DIE. */
10689
10690static const char *
10691dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10692{
10693 const char *linkage_name;
10694
10695 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10696 if (linkage_name == NULL)
10697 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10698
10699 return linkage_name;
10700}
10701
94af9270 10702/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10703 compute the physname for the object, which include a method's:
9c37b5ae 10704 - formal parameters (C++),
a766d390 10705 - receiver type (Go),
a766d390
DE
10706
10707 The term "physname" is a bit confusing.
10708 For C++, for example, it is the demangled name.
10709 For Go, for example, it's the mangled name.
94af9270 10710
af6b7be1
JB
10711 For Ada, return the DIE's linkage name rather than the fully qualified
10712 name. PHYSNAME is ignored..
10713
94af9270
KS
10714 The result is allocated on the objfile_obstack and canonicalized. */
10715
10716static const char *
15d034d0
TT
10717dwarf2_compute_name (const char *name,
10718 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10719 int physname)
10720{
518817b3 10721 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
bb5ed363 10722
94af9270
KS
10723 if (name == NULL)
10724 name = dwarf2_name (die, cu);
10725
2ee7123e
DE
10726 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10727 but otherwise compute it by typename_concat inside GDB.
10728 FIXME: Actually this is not really true, or at least not always true.
10729 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
5e2db402 10730 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10731 will set the demangled name to the result of dwarf2_full_name, and it is
10732 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10733 if (cu->language == language_ada
10734 || (cu->language == language_fortran && physname))
10735 {
10736 /* For Ada unit, we prefer the linkage name over the name, as
10737 the former contains the exported name, which the user expects
10738 to be able to reference. Ideally, we want the user to be able
10739 to reference this entity using either natural or linkage name,
10740 but we haven't started looking at this enhancement yet. */
73b9be8b 10741 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10742
2ee7123e
DE
10743 if (linkage_name != NULL)
10744 return linkage_name;
f55ee35c
JK
10745 }
10746
94af9270
KS
10747 /* These are the only languages we know how to qualify names in. */
10748 if (name != NULL
9c37b5ae 10749 && (cu->language == language_cplus
c44af4eb
TT
10750 || cu->language == language_fortran || cu->language == language_d
10751 || cu->language == language_rust))
94af9270
KS
10752 {
10753 if (die_needs_namespace (die, cu))
10754 {
0d5cff50 10755 const char *prefix;
34a68019 10756 const char *canonical_name = NULL;
94af9270 10757
d7e74731
PA
10758 string_file buf;
10759
94af9270 10760 prefix = determine_prefix (die, cu);
94af9270
KS
10761 if (*prefix != '\0')
10762 {
f55ee35c
JK
10763 char *prefixed_name = typename_concat (NULL, prefix, name,
10764 physname, cu);
9a619af0 10765
d7e74731 10766 buf.puts (prefixed_name);
94af9270
KS
10767 xfree (prefixed_name);
10768 }
10769 else
d7e74731 10770 buf.puts (name);
94af9270 10771
98bfdba5
PA
10772 /* Template parameters may be specified in the DIE's DW_AT_name, or
10773 as children with DW_TAG_template_type_param or
10774 DW_TAG_value_type_param. If the latter, add them to the name
10775 here. If the name already has template parameters, then
10776 skip this step; some versions of GCC emit both, and
10777 it is more efficient to use the pre-computed name.
10778
10779 Something to keep in mind about this process: it is very
10780 unlikely, or in some cases downright impossible, to produce
10781 something that will match the mangled name of a function.
10782 If the definition of the function has the same debug info,
10783 we should be able to match up with it anyway. But fallbacks
10784 using the minimal symbol, for instance to find a method
10785 implemented in a stripped copy of libstdc++, will not work.
10786 If we do not have debug info for the definition, we will have to
10787 match them up some other way.
10788
10789 When we do name matching there is a related problem with function
10790 templates; two instantiated function templates are allowed to
10791 differ only by their return types, which we do not add here. */
10792
10793 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10794 {
10795 struct attribute *attr;
10796 struct die_info *child;
10797 int first = 1;
10798
10799 die->building_fullname = 1;
10800
10801 for (child = die->child; child != NULL; child = child->sibling)
10802 {
10803 struct type *type;
12df843f 10804 LONGEST value;
d521ce57 10805 const gdb_byte *bytes;
98bfdba5
PA
10806 struct dwarf2_locexpr_baton *baton;
10807 struct value *v;
10808
10809 if (child->tag != DW_TAG_template_type_param
10810 && child->tag != DW_TAG_template_value_param)
10811 continue;
10812
10813 if (first)
10814 {
d7e74731 10815 buf.puts ("<");
98bfdba5
PA
10816 first = 0;
10817 }
10818 else
d7e74731 10819 buf.puts (", ");
98bfdba5
PA
10820
10821 attr = dwarf2_attr (child, DW_AT_type, cu);
10822 if (attr == NULL)
10823 {
b98664d3 10824 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10825 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10826 continue;
10827 }
10828 type = die_type (child, cu);
10829
10830 if (child->tag == DW_TAG_template_type_param)
10831 {
c1ec8cea
TT
10832 c_print_type (type, "", &buf, -1, 0, cu->language,
10833 &type_print_raw_options);
98bfdba5
PA
10834 continue;
10835 }
10836
10837 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10838 if (attr == NULL)
10839 {
b98664d3 10840 complaint (_("template parameter missing "
3e43a32a 10841 "DW_AT_const_value"));
d7e74731 10842 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10843 continue;
10844 }
10845
10846 dwarf2_const_value_attr (attr, type, name,
10847 &cu->comp_unit_obstack, cu,
10848 &value, &bytes, &baton);
10849
10850 if (TYPE_NOSIGN (type))
10851 /* GDB prints characters as NUMBER 'CHAR'. If that's
10852 changed, this can use value_print instead. */
d7e74731 10853 c_printchar (value, type, &buf);
98bfdba5
PA
10854 else
10855 {
10856 struct value_print_options opts;
10857
10858 if (baton != NULL)
10859 v = dwarf2_evaluate_loc_desc (type, NULL,
10860 baton->data,
10861 baton->size,
10862 baton->per_cu);
10863 else if (bytes != NULL)
10864 {
10865 v = allocate_value (type);
10866 memcpy (value_contents_writeable (v), bytes,
10867 TYPE_LENGTH (type));
10868 }
10869 else
10870 v = value_from_longest (type, value);
10871
3e43a32a
MS
10872 /* Specify decimal so that we do not depend on
10873 the radix. */
98bfdba5
PA
10874 get_formatted_print_options (&opts, 'd');
10875 opts.raw = 1;
d7e74731 10876 value_print (v, &buf, &opts);
98bfdba5 10877 release_value (v);
98bfdba5
PA
10878 }
10879 }
10880
10881 die->building_fullname = 0;
10882
10883 if (!first)
10884 {
10885 /* Close the argument list, with a space if necessary
10886 (nested templates). */
d7e74731
PA
10887 if (!buf.empty () && buf.string ().back () == '>')
10888 buf.puts (" >");
98bfdba5 10889 else
d7e74731 10890 buf.puts (">");
98bfdba5
PA
10891 }
10892 }
10893
9c37b5ae 10894 /* For C++ methods, append formal parameter type
94af9270 10895 information, if PHYSNAME. */
6e70227d 10896
94af9270 10897 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10898 && cu->language == language_cplus)
94af9270
KS
10899 {
10900 struct type *type = read_type_die (die, cu);
10901
d7e74731 10902 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10903 &type_print_raw_options);
94af9270 10904
9c37b5ae 10905 if (cu->language == language_cplus)
94af9270 10906 {
60430eff
DJ
10907 /* Assume that an artificial first parameter is
10908 "this", but do not crash if it is not. RealView
10909 marks unnamed (and thus unused) parameters as
10910 artificial; there is no way to differentiate
10911 the two cases. */
94af9270
KS
10912 if (TYPE_NFIELDS (type) > 0
10913 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 10914 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
10915 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10916 0))))
d7e74731 10917 buf.puts (" const");
94af9270
KS
10918 }
10919 }
10920
d7e74731 10921 const std::string &intermediate_name = buf.string ();
94af9270
KS
10922
10923 if (cu->language == language_cplus)
34a68019 10924 canonical_name
322a8516 10925 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
10926 &objfile->per_bfd->storage_obstack);
10927
10928 /* If we only computed INTERMEDIATE_NAME, or if
10929 INTERMEDIATE_NAME is already canonical, then we need to
10930 copy it to the appropriate obstack. */
322a8516 10931 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
224c3ddb
SM
10932 name = ((const char *)
10933 obstack_copy0 (&objfile->per_bfd->storage_obstack,
322a8516
PA
10934 intermediate_name.c_str (),
10935 intermediate_name.length ()));
34a68019
TT
10936 else
10937 name = canonical_name;
94af9270
KS
10938 }
10939 }
10940
10941 return name;
10942}
10943
0114d602
DJ
10944/* Return the fully qualified name of DIE, based on its DW_AT_name.
10945 If scope qualifiers are appropriate they will be added. The result
34a68019 10946 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10947 not have a name. NAME may either be from a previous call to
10948 dwarf2_name or NULL.
10949
9c37b5ae 10950 The output string will be canonicalized (if C++). */
0114d602
DJ
10951
10952static const char *
15d034d0 10953dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10954{
94af9270
KS
10955 return dwarf2_compute_name (name, die, cu, 0);
10956}
0114d602 10957
94af9270
KS
10958/* Construct a physname for the given DIE in CU. NAME may either be
10959 from a previous call to dwarf2_name or NULL. The result will be
10960 allocated on the objfile_objstack or NULL if the DIE does not have a
10961 name.
0114d602 10962
9c37b5ae 10963 The output string will be canonicalized (if C++). */
0114d602 10964
94af9270 10965static const char *
15d034d0 10966dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10967{
518817b3 10968 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
900e11f9 10969 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10970 int need_copy = 1;
10971
10972 /* In this case dwarf2_compute_name is just a shortcut not building anything
10973 on its own. */
10974 if (!die_needs_namespace (die, cu))
10975 return dwarf2_compute_name (name, die, cu, 1);
10976
73b9be8b 10977 mangled = dw2_linkage_name (die, cu);
900e11f9 10978
e98c9e7c
TT
10979 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10980 See https://github.com/rust-lang/rust/issues/32925. */
10981 if (cu->language == language_rust && mangled != NULL
10982 && strchr (mangled, '{') != NULL)
10983 mangled = NULL;
10984
900e11f9
JK
10985 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10986 has computed. */
791afaa2 10987 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10988 if (mangled != NULL)
900e11f9 10989 {
900e11f9 10990
59cc4834
JB
10991 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10992 {
10993 /* Do nothing (do not demangle the symbol name). */
10994 }
10995 else if (cu->language == language_go)
a766d390 10996 {
5e2db402
TT
10997 /* This is a lie, but we already lie to the caller new_symbol.
10998 new_symbol assumes we return the mangled name.
a766d390 10999 This just undoes that lie until things are cleaned up. */
a766d390
DE
11000 }
11001 else
11002 {
0eb876f5
JB
11003 /* Use DMGL_RET_DROP for C++ template functions to suppress
11004 their return type. It is easier for GDB users to search
11005 for such functions as `name(params)' than `long name(params)'.
11006 In such case the minimal symbol names do not match the full
11007 symbol names but for template functions there is never a need
11008 to look up their definition from their declaration so
11009 the only disadvantage remains the minimal symbol variant
11010 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
11011 demangled.reset (gdb_demangle (mangled,
11012 (DMGL_PARAMS | DMGL_ANSI
11013 | DMGL_RET_DROP)));
a766d390 11014 }
900e11f9 11015 if (demangled)
791afaa2 11016 canon = demangled.get ();
900e11f9
JK
11017 else
11018 {
11019 canon = mangled;
11020 need_copy = 0;
11021 }
11022 }
11023
11024 if (canon == NULL || check_physname)
11025 {
11026 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11027
11028 if (canon != NULL && strcmp (physname, canon) != 0)
11029 {
11030 /* It may not mean a bug in GDB. The compiler could also
11031 compute DW_AT_linkage_name incorrectly. But in such case
11032 GDB would need to be bug-to-bug compatible. */
11033
b98664d3 11034 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
11035 "(from linkage <%s>) - DIE at %s [in module %s]"),
11036 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 11037 objfile_name (objfile));
900e11f9
JK
11038
11039 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11040 is available here - over computed PHYSNAME. It is safer
11041 against both buggy GDB and buggy compilers. */
11042
11043 retval = canon;
11044 }
11045 else
11046 {
11047 retval = physname;
11048 need_copy = 0;
11049 }
11050 }
11051 else
11052 retval = canon;
11053
11054 if (need_copy)
224c3ddb
SM
11055 retval = ((const char *)
11056 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11057 retval, strlen (retval)));
900e11f9 11058
900e11f9 11059 return retval;
0114d602
DJ
11060}
11061
74921315
KS
11062/* Inspect DIE in CU for a namespace alias. If one exists, record
11063 a new symbol for it.
11064
11065 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11066
11067static int
11068read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11069{
11070 struct attribute *attr;
11071
11072 /* If the die does not have a name, this is not a namespace
11073 alias. */
11074 attr = dwarf2_attr (die, DW_AT_name, cu);
11075 if (attr != NULL)
11076 {
11077 int num;
11078 struct die_info *d = die;
11079 struct dwarf2_cu *imported_cu = cu;
11080
11081 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11082 keep inspecting DIEs until we hit the underlying import. */
11083#define MAX_NESTED_IMPORTED_DECLARATIONS 100
11084 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11085 {
11086 attr = dwarf2_attr (d, DW_AT_import, cu);
11087 if (attr == NULL)
11088 break;
11089
11090 d = follow_die_ref (d, attr, &imported_cu);
11091 if (d->tag != DW_TAG_imported_declaration)
11092 break;
11093 }
11094
11095 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11096 {
b98664d3 11097 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 11098 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
11099 return 0;
11100 }
11101
11102 if (attr != NULL)
11103 {
11104 struct type *type;
9c541725 11105 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 11106
9c541725 11107 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
11108 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11109 {
11110 /* This declaration is a global namespace alias. Add
11111 a symbol for it whose type is the aliased namespace. */
11112 new_symbol (die, type, cu);
11113 return 1;
11114 }
11115 }
11116 }
11117
11118 return 0;
11119}
11120
22cee43f 11121/* Return the using directives repository (global or local?) to use in the
804d2729 11122 current context for CU.
22cee43f
PMR
11123
11124 For Ada, imported declarations can materialize renamings, which *may* be
11125 global. However it is impossible (for now?) in DWARF to distinguish
11126 "external" imported declarations and "static" ones. As all imported
11127 declarations seem to be static in all other languages, make them all CU-wide
11128 global only in Ada. */
11129
11130static struct using_direct **
804d2729 11131using_directives (struct dwarf2_cu *cu)
22cee43f 11132{
804d2729
TT
11133 if (cu->language == language_ada && cu->builder->outermost_context_p ())
11134 return cu->builder->get_global_using_directives ();
22cee43f 11135 else
804d2729 11136 return cu->builder->get_local_using_directives ();
22cee43f
PMR
11137}
11138
27aa8d6a
SW
11139/* Read the import statement specified by the given die and record it. */
11140
11141static void
11142read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11143{
518817b3 11144 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
27aa8d6a 11145 struct attribute *import_attr;
32019081 11146 struct die_info *imported_die, *child_die;
de4affc9 11147 struct dwarf2_cu *imported_cu;
27aa8d6a 11148 const char *imported_name;
794684b6 11149 const char *imported_name_prefix;
13387711
SW
11150 const char *canonical_name;
11151 const char *import_alias;
11152 const char *imported_declaration = NULL;
794684b6 11153 const char *import_prefix;
eb1e02fd 11154 std::vector<const char *> excludes;
13387711 11155
27aa8d6a
SW
11156 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11157 if (import_attr == NULL)
11158 {
b98664d3 11159 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
11160 dwarf_tag_name (die->tag));
11161 return;
11162 }
11163
de4affc9
CC
11164 imported_cu = cu;
11165 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11166 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
11167 if (imported_name == NULL)
11168 {
11169 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11170
11171 The import in the following code:
11172 namespace A
11173 {
11174 typedef int B;
11175 }
11176
11177 int main ()
11178 {
11179 using A::B;
11180 B b;
11181 return b;
11182 }
11183
11184 ...
11185 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11186 <52> DW_AT_decl_file : 1
11187 <53> DW_AT_decl_line : 6
11188 <54> DW_AT_import : <0x75>
11189 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11190 <59> DW_AT_name : B
11191 <5b> DW_AT_decl_file : 1
11192 <5c> DW_AT_decl_line : 2
11193 <5d> DW_AT_type : <0x6e>
11194 ...
11195 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11196 <76> DW_AT_byte_size : 4
11197 <77> DW_AT_encoding : 5 (signed)
11198
11199 imports the wrong die ( 0x75 instead of 0x58 ).
11200 This case will be ignored until the gcc bug is fixed. */
11201 return;
11202 }
11203
82856980
SW
11204 /* Figure out the local name after import. */
11205 import_alias = dwarf2_name (die, cu);
27aa8d6a 11206
794684b6
SW
11207 /* Figure out where the statement is being imported to. */
11208 import_prefix = determine_prefix (die, cu);
11209
11210 /* Figure out what the scope of the imported die is and prepend it
11211 to the name of the imported die. */
de4affc9 11212 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 11213
f55ee35c
JK
11214 if (imported_die->tag != DW_TAG_namespace
11215 && imported_die->tag != DW_TAG_module)
794684b6 11216 {
13387711
SW
11217 imported_declaration = imported_name;
11218 canonical_name = imported_name_prefix;
794684b6 11219 }
13387711 11220 else if (strlen (imported_name_prefix) > 0)
12aaed36 11221 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
11222 imported_name_prefix,
11223 (cu->language == language_d ? "." : "::"),
11224 imported_name, (char *) NULL);
13387711
SW
11225 else
11226 canonical_name = imported_name;
794684b6 11227
32019081
JK
11228 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11229 for (child_die = die->child; child_die && child_die->tag;
11230 child_die = sibling_die (child_die))
11231 {
11232 /* DWARF-4: A Fortran use statement with a “rename list” may be
11233 represented by an imported module entry with an import attribute
11234 referring to the module and owned entries corresponding to those
11235 entities that are renamed as part of being imported. */
11236
11237 if (child_die->tag != DW_TAG_imported_declaration)
11238 {
b98664d3 11239 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
11240 "- DIE at %s [in module %s]"),
11241 sect_offset_str (child_die->sect_off),
11242 objfile_name (objfile));
32019081
JK
11243 continue;
11244 }
11245
11246 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11247 if (import_attr == NULL)
11248 {
b98664d3 11249 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
11250 dwarf_tag_name (child_die->tag));
11251 continue;
11252 }
11253
11254 imported_cu = cu;
11255 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11256 &imported_cu);
11257 imported_name = dwarf2_name (imported_die, imported_cu);
11258 if (imported_name == NULL)
11259 {
b98664d3 11260 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
11261 "imported name - DIE at %s [in module %s]"),
11262 sect_offset_str (child_die->sect_off),
11263 objfile_name (objfile));
32019081
JK
11264 continue;
11265 }
11266
eb1e02fd 11267 excludes.push_back (imported_name);
32019081
JK
11268
11269 process_die (child_die, cu);
11270 }
11271
804d2729 11272 add_using_directive (using_directives (cu),
22cee43f
PMR
11273 import_prefix,
11274 canonical_name,
11275 import_alias,
11276 imported_declaration,
11277 excludes,
11278 0,
11279 &objfile->objfile_obstack);
27aa8d6a
SW
11280}
11281
5230b05a
WT
11282/* ICC<14 does not output the required DW_AT_declaration on incomplete
11283 types, but gives them a size of zero. Starting with version 14,
11284 ICC is compatible with GCC. */
11285
11286static int
11287producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11288{
11289 if (!cu->checked_producer)
11290 check_producer (cu);
11291
11292 return cu->producer_is_icc_lt_14;
11293}
11294
1b80a9fa
JK
11295/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11296 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11297 this, it was first present in GCC release 4.3.0. */
11298
11299static int
11300producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11301{
11302 if (!cu->checked_producer)
11303 check_producer (cu);
11304
11305 return cu->producer_is_gcc_lt_4_3;
11306}
11307
d721ba37
PA
11308static file_and_directory
11309find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 11310{
d721ba37
PA
11311 file_and_directory res;
11312
9291a0cd
TT
11313 /* Find the filename. Do not use dwarf2_name here, since the filename
11314 is not a source language identifier. */
d721ba37
PA
11315 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11316 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 11317
d721ba37
PA
11318 if (res.comp_dir == NULL
11319 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11320 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 11321 {
d721ba37
PA
11322 res.comp_dir_storage = ldirname (res.name);
11323 if (!res.comp_dir_storage.empty ())
11324 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 11325 }
d721ba37 11326 if (res.comp_dir != NULL)
9291a0cd
TT
11327 {
11328 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11329 directory, get rid of it. */
d721ba37 11330 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 11331
d721ba37
PA
11332 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11333 res.comp_dir = cp + 1;
9291a0cd
TT
11334 }
11335
d721ba37
PA
11336 if (res.name == NULL)
11337 res.name = "<unknown>";
11338
11339 return res;
9291a0cd
TT
11340}
11341
f4dc4d17
DE
11342/* Handle DW_AT_stmt_list for a compilation unit.
11343 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
11344 COMP_DIR is the compilation directory. LOWPC is passed to
11345 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
11346
11347static void
11348handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 11349 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 11350{
518817b3
SM
11351 struct dwarf2_per_objfile *dwarf2_per_objfile
11352 = cu->per_cu->dwarf2_per_objfile;
527f3840 11353 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 11354 struct attribute *attr;
527f3840
JK
11355 struct line_header line_header_local;
11356 hashval_t line_header_local_hash;
527f3840
JK
11357 void **slot;
11358 int decode_mapping;
2ab95328 11359
f4dc4d17
DE
11360 gdb_assert (! cu->per_cu->is_debug_types);
11361
2ab95328 11362 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
11363 if (attr == NULL)
11364 return;
11365
9c541725 11366 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
11367
11368 /* The line header hash table is only created if needed (it exists to
11369 prevent redundant reading of the line table for partial_units).
11370 If we're given a partial_unit, we'll need it. If we're given a
11371 compile_unit, then use the line header hash table if it's already
11372 created, but don't create one just yet. */
11373
11374 if (dwarf2_per_objfile->line_header_hash == NULL
11375 && die->tag == DW_TAG_partial_unit)
2ab95328 11376 {
527f3840
JK
11377 dwarf2_per_objfile->line_header_hash
11378 = htab_create_alloc_ex (127, line_header_hash_voidp,
11379 line_header_eq_voidp,
11380 free_line_header_voidp,
11381 &objfile->objfile_obstack,
11382 hashtab_obstack_allocate,
11383 dummy_obstack_deallocate);
11384 }
2ab95328 11385
9c541725 11386 line_header_local.sect_off = line_offset;
527f3840
JK
11387 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11388 line_header_local_hash = line_header_hash (&line_header_local);
11389 if (dwarf2_per_objfile->line_header_hash != NULL)
11390 {
11391 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11392 &line_header_local,
11393 line_header_local_hash, NO_INSERT);
11394
11395 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11396 is not present in *SLOT (since if there is something in *SLOT then
11397 it will be for a partial_unit). */
11398 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 11399 {
527f3840 11400 gdb_assert (*slot != NULL);
9a3c8263 11401 cu->line_header = (struct line_header *) *slot;
527f3840 11402 return;
dee91e82 11403 }
2ab95328 11404 }
527f3840
JK
11405
11406 /* dwarf_decode_line_header does not yet provide sufficient information.
11407 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
11408 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11409 if (lh == NULL)
527f3840 11410 return;
4c8aa72d
PA
11411
11412 cu->line_header = lh.release ();
11413 cu->line_header_die_owner = die;
527f3840
JK
11414
11415 if (dwarf2_per_objfile->line_header_hash == NULL)
11416 slot = NULL;
11417 else
11418 {
11419 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11420 &line_header_local,
11421 line_header_local_hash, INSERT);
11422 gdb_assert (slot != NULL);
11423 }
11424 if (slot != NULL && *slot == NULL)
11425 {
11426 /* This newly decoded line number information unit will be owned
11427 by line_header_hash hash table. */
11428 *slot = cu->line_header;
4c8aa72d 11429 cu->line_header_die_owner = NULL;
527f3840
JK
11430 }
11431 else
11432 {
11433 /* We cannot free any current entry in (*slot) as that struct line_header
11434 may be already used by multiple CUs. Create only temporary decoded
11435 line_header for this CU - it may happen at most once for each line
11436 number information unit. And if we're not using line_header_hash
11437 then this is what we want as well. */
11438 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
11439 }
11440 decode_mapping = (die->tag != DW_TAG_partial_unit);
11441 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11442 decode_mapping);
fff8551c 11443
2ab95328
TT
11444}
11445
95554aad 11446/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 11447
c906108c 11448static void
e7c27a73 11449read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11450{
518817b3
SM
11451 struct dwarf2_per_objfile *dwarf2_per_objfile
11452 = cu->per_cu->dwarf2_per_objfile;
dee91e82 11453 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 11454 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 11455 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11456 CORE_ADDR highpc = ((CORE_ADDR) 0);
11457 struct attribute *attr;
c906108c 11458 struct die_info *child_die;
e142c38c 11459 CORE_ADDR baseaddr;
6e70227d 11460
380618d6 11461 prepare_one_comp_unit (cu, die, cu->language);
e142c38c 11462 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11463
fae299cd 11464 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11465
11466 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11467 from finish_block. */
2acceee2 11468 if (lowpc == ((CORE_ADDR) -1))
c906108c 11469 lowpc = highpc;
3e29f34a 11470 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 11471
d721ba37 11472 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 11473
f4b8a18d
KW
11474 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11475 standardised yet. As a workaround for the language detection we fall
11476 back to the DW_AT_producer string. */
11477 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11478 cu->language = language_opencl;
11479
3019eac3
DE
11480 /* Similar hack for Go. */
11481 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11482 set_cu_language (DW_LANG_Go, cu);
11483
d721ba37 11484 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11485
11486 /* Decode line number information if present. We do this before
11487 processing child DIEs, so that the line header table is available
11488 for DW_AT_decl_file. */
d721ba37 11489 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11490
11491 /* Process all dies in compilation unit. */
11492 if (die->child != NULL)
11493 {
11494 child_die = die->child;
11495 while (child_die && child_die->tag)
11496 {
11497 process_die (child_die, cu);
11498 child_die = sibling_die (child_die);
11499 }
11500 }
11501
11502 /* Decode macro information, if present. Dwarf 2 macro information
11503 refers to information in the line number info statement program
11504 header, so we can only read it if we've read the header
11505 successfully. */
0af92d60
JK
11506 attr = dwarf2_attr (die, DW_AT_macros, cu);
11507 if (attr == NULL)
11508 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
11509 if (attr && cu->line_header)
11510 {
11511 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 11512 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11513
43f3e411 11514 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
11515 }
11516 else
11517 {
11518 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11519 if (attr && cu->line_header)
11520 {
11521 unsigned int macro_offset = DW_UNSND (attr);
11522
43f3e411 11523 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11524 }
11525 }
3019eac3
DE
11526}
11527
f4dc4d17
DE
11528/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
11529 Create the set of symtabs used by this TU, or if this TU is sharing
11530 symtabs with another TU and the symtabs have already been created
11531 then restore those symtabs in the line header.
11532 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
11533
11534static void
f4dc4d17 11535setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 11536{
f4dc4d17
DE
11537 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
11538 struct type_unit_group *tu_group;
11539 int first_time;
3019eac3 11540 struct attribute *attr;
9c541725 11541 unsigned int i;
0186c6a7 11542 struct signatured_type *sig_type;
3019eac3 11543
f4dc4d17 11544 gdb_assert (per_cu->is_debug_types);
0186c6a7 11545 sig_type = (struct signatured_type *) per_cu;
3019eac3 11546
f4dc4d17 11547 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 11548
f4dc4d17 11549 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11550 per_cu->type_unit_group may not have been set up yet. */
0186c6a7
DE
11551 if (sig_type->type_unit_group == NULL)
11552 sig_type->type_unit_group = get_type_unit_group (cu, attr);
11553 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11554
11555 /* If we've already processed this stmt_list there's no real need to
11556 do it again, we could fake it and just recreate the part we need
11557 (file name,index -> symtab mapping). If data shows this optimization
11558 is useful we can do it then. */
43f3e411 11559 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
11560
11561 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11562 debug info. */
fff8551c 11563 line_header_up lh;
f4dc4d17 11564 if (attr != NULL)
3019eac3 11565 {
9c541725 11566 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
f4dc4d17
DE
11567 lh = dwarf_decode_line_header (line_offset, cu);
11568 }
11569 if (lh == NULL)
11570 {
11571 if (first_time)
11572 dwarf2_start_symtab (cu, "", NULL, 0);
11573 else
11574 {
11575 gdb_assert (tu_group->symtabs == NULL);
804d2729
TT
11576 gdb_assert (cu->builder == nullptr);
11577 struct compunit_symtab *cust = tu_group->compunit_symtab;
11578 cu->builder.reset (new struct buildsym_compunit
11579 (COMPUNIT_OBJFILE (cust), "",
11580 COMPUNIT_DIRNAME (cust),
11581 compunit_language (cust),
11582 0, cust));
f4dc4d17 11583 }
f4dc4d17 11584 return;
3019eac3
DE
11585 }
11586
4c8aa72d
PA
11587 cu->line_header = lh.release ();
11588 cu->line_header_die_owner = die;
3019eac3 11589
f4dc4d17
DE
11590 if (first_time)
11591 {
43f3e411 11592 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 11593
1fd60fc0
DE
11594 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11595 still initializing it, and our caller (a few levels up)
11596 process_full_type_unit still needs to know if this is the first
11597 time. */
11598
4c8aa72d
PA
11599 tu_group->num_symtabs = cu->line_header->file_names.size ();
11600 tu_group->symtabs = XNEWVEC (struct symtab *,
11601 cu->line_header->file_names.size ());
3019eac3 11602
4c8aa72d 11603 for (i = 0; i < cu->line_header->file_names.size (); ++i)
f4dc4d17 11604 {
4c8aa72d 11605 file_entry &fe = cu->line_header->file_names[i];
3019eac3 11606
804d2729 11607 dwarf2_start_subfile (cu, fe.name, fe.include_dir (cu->line_header));
3019eac3 11608
804d2729 11609 if (cu->builder->get_current_subfile ()->symtab == NULL)
f4dc4d17 11610 {
4c8aa72d
PA
11611 /* NOTE: start_subfile will recognize when it's been
11612 passed a file it has already seen. So we can't
11613 assume there's a simple mapping from
11614 cu->line_header->file_names to subfiles, plus
11615 cu->line_header->file_names may contain dups. */
804d2729
TT
11616 cu->builder->get_current_subfile ()->symtab
11617 = allocate_symtab (cust,
11618 cu->builder->get_current_subfile ()->name);
f4dc4d17
DE
11619 }
11620
804d2729 11621 fe.symtab = cu->builder->get_current_subfile ()->symtab;
8c43009f 11622 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
11623 }
11624 }
11625 else
3019eac3 11626 {
804d2729
TT
11627 gdb_assert (cu->builder == nullptr);
11628 struct compunit_symtab *cust = tu_group->compunit_symtab;
11629 cu->builder.reset (new struct buildsym_compunit
11630 (COMPUNIT_OBJFILE (cust), "",
11631 COMPUNIT_DIRNAME (cust),
11632 compunit_language (cust),
11633 0, cust));
f4dc4d17 11634
4c8aa72d 11635 for (i = 0; i < cu->line_header->file_names.size (); ++i)
f4dc4d17 11636 {
4c8aa72d 11637 file_entry &fe = cu->line_header->file_names[i];
f4dc4d17 11638
4c8aa72d 11639 fe.symtab = tu_group->symtabs[i];
f4dc4d17 11640 }
3019eac3
DE
11641 }
11642
f4dc4d17
DE
11643 /* The main symtab is allocated last. Type units don't have DW_AT_name
11644 so they don't have a "real" (so to speak) symtab anyway.
11645 There is later code that will assign the main symtab to all symbols
11646 that don't have one. We need to handle the case of a symbol with a
11647 missing symtab (DW_AT_decl_file) anyway. */
11648}
3019eac3 11649
f4dc4d17
DE
11650/* Process DW_TAG_type_unit.
11651 For TUs we want to skip the first top level sibling if it's not the
11652 actual type being defined by this TU. In this case the first top
11653 level sibling is there to provide context only. */
3019eac3 11654
f4dc4d17
DE
11655static void
11656read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11657{
11658 struct die_info *child_die;
3019eac3 11659
f4dc4d17
DE
11660 prepare_one_comp_unit (cu, die, language_minimal);
11661
11662 /* Initialize (or reinitialize) the machinery for building symtabs.
11663 We do this before processing child DIEs, so that the line header table
11664 is available for DW_AT_decl_file. */
11665 setup_type_unit_groups (die, cu);
11666
11667 if (die->child != NULL)
11668 {
11669 child_die = die->child;
11670 while (child_die && child_die->tag)
11671 {
11672 process_die (child_die, cu);
11673 child_die = sibling_die (child_die);
11674 }
11675 }
3019eac3
DE
11676}
11677\f
80626a55
DE
11678/* DWO/DWP files.
11679
11680 http://gcc.gnu.org/wiki/DebugFission
11681 http://gcc.gnu.org/wiki/DebugFissionDWP
11682
11683 To simplify handling of both DWO files ("object" files with the DWARF info)
11684 and DWP files (a file with the DWOs packaged up into one file), we treat
11685 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11686
11687static hashval_t
11688hash_dwo_file (const void *item)
11689{
9a3c8263 11690 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11691 hashval_t hash;
3019eac3 11692
a2ce51a0
DE
11693 hash = htab_hash_string (dwo_file->dwo_name);
11694 if (dwo_file->comp_dir != NULL)
11695 hash += htab_hash_string (dwo_file->comp_dir);
11696 return hash;
3019eac3
DE
11697}
11698
11699static int
11700eq_dwo_file (const void *item_lhs, const void *item_rhs)
11701{
9a3c8263
SM
11702 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11703 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11704
a2ce51a0
DE
11705 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11706 return 0;
11707 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11708 return lhs->comp_dir == rhs->comp_dir;
11709 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11710}
11711
11712/* Allocate a hash table for DWO files. */
11713
11714static htab_t
ed2dc618 11715allocate_dwo_file_hash_table (struct objfile *objfile)
3019eac3 11716{
3019eac3
DE
11717 return htab_create_alloc_ex (41,
11718 hash_dwo_file,
11719 eq_dwo_file,
11720 NULL,
11721 &objfile->objfile_obstack,
11722 hashtab_obstack_allocate,
11723 dummy_obstack_deallocate);
11724}
11725
80626a55
DE
11726/* Lookup DWO file DWO_NAME. */
11727
11728static void **
ed2dc618
SM
11729lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11730 const char *dwo_name,
11731 const char *comp_dir)
80626a55
DE
11732{
11733 struct dwo_file find_entry;
11734 void **slot;
11735
11736 if (dwarf2_per_objfile->dwo_files == NULL)
ed2dc618
SM
11737 dwarf2_per_objfile->dwo_files
11738 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
80626a55
DE
11739
11740 memset (&find_entry, 0, sizeof (find_entry));
0ac5b59e
DE
11741 find_entry.dwo_name = dwo_name;
11742 find_entry.comp_dir = comp_dir;
80626a55
DE
11743 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
11744
11745 return slot;
11746}
11747
3019eac3
DE
11748static hashval_t
11749hash_dwo_unit (const void *item)
11750{
9a3c8263 11751 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11752
11753 /* This drops the top 32 bits of the id, but is ok for a hash. */
11754 return dwo_unit->signature;
11755}
11756
11757static int
11758eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11759{
9a3c8263
SM
11760 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11761 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11762
11763 /* The signature is assumed to be unique within the DWO file.
11764 So while object file CU dwo_id's always have the value zero,
11765 that's OK, assuming each object file DWO file has only one CU,
11766 and that's the rule for now. */
11767 return lhs->signature == rhs->signature;
11768}
11769
11770/* Allocate a hash table for DWO CUs,TUs.
11771 There is one of these tables for each of CUs,TUs for each DWO file. */
11772
11773static htab_t
11774allocate_dwo_unit_table (struct objfile *objfile)
11775{
11776 /* Start out with a pretty small number.
11777 Generally DWO files contain only one CU and maybe some TUs. */
11778 return htab_create_alloc_ex (3,
11779 hash_dwo_unit,
11780 eq_dwo_unit,
11781 NULL,
11782 &objfile->objfile_obstack,
11783 hashtab_obstack_allocate,
11784 dummy_obstack_deallocate);
11785}
11786
80626a55 11787/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 11788
19c3d4c9 11789struct create_dwo_cu_data
3019eac3
DE
11790{
11791 struct dwo_file *dwo_file;
19c3d4c9 11792 struct dwo_unit dwo_unit;
3019eac3
DE
11793};
11794
19c3d4c9 11795/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11796
11797static void
19c3d4c9
DE
11798create_dwo_cu_reader (const struct die_reader_specs *reader,
11799 const gdb_byte *info_ptr,
11800 struct die_info *comp_unit_die,
11801 int has_children,
11802 void *datap)
3019eac3
DE
11803{
11804 struct dwarf2_cu *cu = reader->cu;
9c541725 11805 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11806 struct dwarf2_section_info *section = cu->per_cu->section;
9a3c8263 11807 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
3019eac3 11808 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 11809 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 11810 struct attribute *attr;
3019eac3
DE
11811
11812 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
11813 if (attr == NULL)
11814 {
b98664d3 11815 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11816 " its dwo_id [in module %s]"),
9d8780f0 11817 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11818 return;
11819 }
11820
3019eac3
DE
11821 dwo_unit->dwo_file = dwo_file;
11822 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 11823 dwo_unit->section = section;
9c541725 11824 dwo_unit->sect_off = sect_off;
3019eac3
DE
11825 dwo_unit->length = cu->per_cu->length;
11826
b4f54984 11827 if (dwarf_read_debug)
9d8780f0
SM
11828 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11829 sect_offset_str (sect_off),
9c541725 11830 hex_string (dwo_unit->signature));
3019eac3
DE
11831}
11832
33c5cd75 11833/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11834 Note: This function processes DWO files only, not DWP files. */
3019eac3 11835
33c5cd75 11836static void
ed2dc618
SM
11837create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11838 struct dwo_file &dwo_file, dwarf2_section_info &section,
33c5cd75 11839 htab_t &cus_htab)
3019eac3
DE
11840{
11841 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 11842 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11843
33c5cd75
DB
11844 dwarf2_read_section (objfile, &section);
11845 info_ptr = section.buffer;
3019eac3
DE
11846
11847 if (info_ptr == NULL)
33c5cd75 11848 return;
3019eac3 11849
b4f54984 11850 if (dwarf_read_debug)
19c3d4c9
DE
11851 {
11852 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
33c5cd75
DB
11853 get_section_name (&section),
11854 get_section_file_name (&section));
19c3d4c9 11855 }
3019eac3 11856
33c5cd75 11857 end_ptr = info_ptr + section.size;
3019eac3
DE
11858 while (info_ptr < end_ptr)
11859 {
11860 struct dwarf2_per_cu_data per_cu;
33c5cd75
DB
11861 struct create_dwo_cu_data create_dwo_cu_data;
11862 struct dwo_unit *dwo_unit;
11863 void **slot;
11864 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3 11865
19c3d4c9
DE
11866 memset (&create_dwo_cu_data.dwo_unit, 0,
11867 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3 11868 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 11869 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3019eac3 11870 per_cu.is_debug_types = 0;
33c5cd75
DB
11871 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11872 per_cu.section = &section;
c5ed0576 11873 create_dwo_cu_data.dwo_file = &dwo_file;
33c5cd75
DB
11874
11875 init_cutu_and_read_dies_no_follow (
11876 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
11877 info_ptr += per_cu.length;
11878
11879 // If the unit could not be parsed, skip it.
11880 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
11881 continue;
3019eac3 11882
33c5cd75
DB
11883 if (cus_htab == NULL)
11884 cus_htab = allocate_dwo_unit_table (objfile);
19c3d4c9 11885
33c5cd75
DB
11886 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11887 *dwo_unit = create_dwo_cu_data.dwo_unit;
11888 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
11889 gdb_assert (slot != NULL);
11890 if (*slot != NULL)
19c3d4c9 11891 {
33c5cd75
DB
11892 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11893 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11894
b98664d3 11895 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11896 " the entry at offset %s, signature %s"),
11897 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11898 hex_string (dwo_unit->signature));
19c3d4c9 11899 }
33c5cd75 11900 *slot = (void *)dwo_unit;
3019eac3 11901 }
3019eac3
DE
11902}
11903
80626a55
DE
11904/* DWP file .debug_{cu,tu}_index section format:
11905 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11906
d2415c6c
DE
11907 DWP Version 1:
11908
80626a55
DE
11909 Both index sections have the same format, and serve to map a 64-bit
11910 signature to a set of section numbers. Each section begins with a header,
11911 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11912 indexes, and a pool of 32-bit section numbers. The index sections will be
11913 aligned at 8-byte boundaries in the file.
11914
d2415c6c
DE
11915 The index section header consists of:
11916
11917 V, 32 bit version number
11918 -, 32 bits unused
11919 N, 32 bit number of compilation units or type units in the index
11920 M, 32 bit number of slots in the hash table
80626a55 11921
d2415c6c 11922 Numbers are recorded using the byte order of the application binary.
80626a55 11923
d2415c6c
DE
11924 The hash table begins at offset 16 in the section, and consists of an array
11925 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11926 order of the application binary). Unused slots in the hash table are 0.
11927 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11928
d2415c6c
DE
11929 The parallel table begins immediately after the hash table
11930 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11931 array of 32-bit indexes (using the byte order of the application binary),
11932 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11933 table contains a 32-bit index into the pool of section numbers. For unused
11934 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11935
73869dc2
DE
11936 The pool of section numbers begins immediately following the hash table
11937 (at offset 16 + 12 * M from the beginning of the section). The pool of
11938 section numbers consists of an array of 32-bit words (using the byte order
11939 of the application binary). Each item in the array is indexed starting
11940 from 0. The hash table entry provides the index of the first section
11941 number in the set. Additional section numbers in the set follow, and the
11942 set is terminated by a 0 entry (section number 0 is not used in ELF).
11943
11944 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11945 section must be the first entry in the set, and the .debug_abbrev.dwo must
11946 be the second entry. Other members of the set may follow in any order.
11947
11948 ---
11949
11950 DWP Version 2:
11951
11952 DWP Version 2 combines all the .debug_info, etc. sections into one,
11953 and the entries in the index tables are now offsets into these sections.
11954 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11955 section.
11956
11957 Index Section Contents:
11958 Header
11959 Hash Table of Signatures dwp_hash_table.hash_table
11960 Parallel Table of Indices dwp_hash_table.unit_table
11961 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11962 Table of Section Sizes dwp_hash_table.v2.sizes
11963
11964 The index section header consists of:
11965
11966 V, 32 bit version number
11967 L, 32 bit number of columns in the table of section offsets
11968 N, 32 bit number of compilation units or type units in the index
11969 M, 32 bit number of slots in the hash table
11970
11971 Numbers are recorded using the byte order of the application binary.
11972
11973 The hash table has the same format as version 1.
11974 The parallel table of indices has the same format as version 1,
11975 except that the entries are origin-1 indices into the table of sections
11976 offsets and the table of section sizes.
11977
11978 The table of offsets begins immediately following the parallel table
11979 (at offset 16 + 12 * M from the beginning of the section). The table is
11980 a two-dimensional array of 32-bit words (using the byte order of the
11981 application binary), with L columns and N+1 rows, in row-major order.
11982 Each row in the array is indexed starting from 0. The first row provides
11983 a key to the remaining rows: each column in this row provides an identifier
11984 for a debug section, and the offsets in the same column of subsequent rows
11985 refer to that section. The section identifiers are:
11986
11987 DW_SECT_INFO 1 .debug_info.dwo
11988 DW_SECT_TYPES 2 .debug_types.dwo
11989 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11990 DW_SECT_LINE 4 .debug_line.dwo
11991 DW_SECT_LOC 5 .debug_loc.dwo
11992 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11993 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11994 DW_SECT_MACRO 8 .debug_macro.dwo
11995
11996 The offsets provided by the CU and TU index sections are the base offsets
11997 for the contributions made by each CU or TU to the corresponding section
11998 in the package file. Each CU and TU header contains an abbrev_offset
11999 field, used to find the abbreviations table for that CU or TU within the
12000 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
12001 be interpreted as relative to the base offset given in the index section.
12002 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
12003 should be interpreted as relative to the base offset for .debug_line.dwo,
12004 and offsets into other debug sections obtained from DWARF attributes should
12005 also be interpreted as relative to the corresponding base offset.
12006
12007 The table of sizes begins immediately following the table of offsets.
12008 Like the table of offsets, it is a two-dimensional array of 32-bit words,
12009 with L columns and N rows, in row-major order. Each row in the array is
12010 indexed starting from 1 (row 0 is shared by the two tables).
12011
12012 ---
12013
12014 Hash table lookup is handled the same in version 1 and 2:
12015
12016 We assume that N and M will not exceed 2^32 - 1.
12017 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
12018
d2415c6c
DE
12019 Given a 64-bit compilation unit signature or a type signature S, an entry
12020 in the hash table is located as follows:
80626a55 12021
d2415c6c
DE
12022 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
12023 the low-order k bits all set to 1.
80626a55 12024
d2415c6c 12025 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 12026
d2415c6c
DE
12027 3) If the hash table entry at index H matches the signature, use that
12028 entry. If the hash table entry at index H is unused (all zeroes),
12029 terminate the search: the signature is not present in the table.
80626a55 12030
d2415c6c 12031 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 12032
d2415c6c 12033 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 12034 to stop at an unused slot or find the match. */
80626a55
DE
12035
12036/* Create a hash table to map DWO IDs to their CU/TU entry in
12037 .debug_{info,types}.dwo in DWP_FILE.
12038 Returns NULL if there isn't one.
12039 Note: This function processes DWP files only, not DWO files. */
12040
12041static struct dwp_hash_table *
ed2dc618
SM
12042create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12043 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
12044{
12045 struct objfile *objfile = dwarf2_per_objfile->objfile;
400174b1 12046 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 12047 const gdb_byte *index_ptr, *index_end;
80626a55 12048 struct dwarf2_section_info *index;
73869dc2 12049 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
12050 struct dwp_hash_table *htab;
12051
12052 if (is_debug_types)
12053 index = &dwp_file->sections.tu_index;
12054 else
12055 index = &dwp_file->sections.cu_index;
12056
12057 if (dwarf2_section_empty_p (index))
12058 return NULL;
12059 dwarf2_read_section (objfile, index);
12060
12061 index_ptr = index->buffer;
12062 index_end = index_ptr + index->size;
12063
12064 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
12065 index_ptr += 4;
12066 if (version == 2)
12067 nr_columns = read_4_bytes (dbfd, index_ptr);
12068 else
12069 nr_columns = 0;
12070 index_ptr += 4;
80626a55
DE
12071 nr_units = read_4_bytes (dbfd, index_ptr);
12072 index_ptr += 4;
12073 nr_slots = read_4_bytes (dbfd, index_ptr);
12074 index_ptr += 4;
12075
73869dc2 12076 if (version != 1 && version != 2)
80626a55 12077 {
21aa081e 12078 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 12079 " [in module %s]"),
21aa081e 12080 pulongest (version), dwp_file->name);
80626a55
DE
12081 }
12082 if (nr_slots != (nr_slots & -nr_slots))
12083 {
21aa081e 12084 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 12085 " is not power of 2 [in module %s]"),
21aa081e 12086 pulongest (nr_slots), dwp_file->name);
80626a55
DE
12087 }
12088
12089 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
12090 htab->version = version;
12091 htab->nr_columns = nr_columns;
80626a55
DE
12092 htab->nr_units = nr_units;
12093 htab->nr_slots = nr_slots;
12094 htab->hash_table = index_ptr;
12095 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
12096
12097 /* Exit early if the table is empty. */
12098 if (nr_slots == 0 || nr_units == 0
12099 || (version == 2 && nr_columns == 0))
12100 {
12101 /* All must be zero. */
12102 if (nr_slots != 0 || nr_units != 0
12103 || (version == 2 && nr_columns != 0))
12104 {
b98664d3 12105 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
12106 " all zero [in modules %s]"),
12107 dwp_file->name);
12108 }
12109 return htab;
12110 }
12111
12112 if (version == 1)
12113 {
12114 htab->section_pool.v1.indices =
12115 htab->unit_table + sizeof (uint32_t) * nr_slots;
12116 /* It's harder to decide whether the section is too small in v1.
12117 V1 is deprecated anyway so we punt. */
12118 }
12119 else
12120 {
12121 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12122 int *ids = htab->section_pool.v2.section_ids;
12123 /* Reverse map for error checking. */
12124 int ids_seen[DW_SECT_MAX + 1];
12125 int i;
12126
12127 if (nr_columns < 2)
12128 {
12129 error (_("Dwarf Error: bad DWP hash table, too few columns"
12130 " in section table [in module %s]"),
12131 dwp_file->name);
12132 }
12133 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12134 {
12135 error (_("Dwarf Error: bad DWP hash table, too many columns"
12136 " in section table [in module %s]"),
12137 dwp_file->name);
12138 }
12139 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12140 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12141 for (i = 0; i < nr_columns; ++i)
12142 {
12143 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12144
12145 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12146 {
12147 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12148 " in section table [in module %s]"),
12149 id, dwp_file->name);
12150 }
12151 if (ids_seen[id] != -1)
12152 {
12153 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12154 " id %d in section table [in module %s]"),
12155 id, dwp_file->name);
12156 }
12157 ids_seen[id] = i;
12158 ids[i] = id;
12159 }
12160 /* Must have exactly one info or types section. */
12161 if (((ids_seen[DW_SECT_INFO] != -1)
12162 + (ids_seen[DW_SECT_TYPES] != -1))
12163 != 1)
12164 {
12165 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12166 " DWO info/types section [in module %s]"),
12167 dwp_file->name);
12168 }
12169 /* Must have an abbrev section. */
12170 if (ids_seen[DW_SECT_ABBREV] == -1)
12171 {
12172 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12173 " section [in module %s]"),
12174 dwp_file->name);
12175 }
12176 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12177 htab->section_pool.v2.sizes =
12178 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12179 * nr_units * nr_columns);
12180 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12181 * nr_units * nr_columns))
12182 > index_end)
12183 {
12184 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12185 " [in module %s]"),
12186 dwp_file->name);
12187 }
12188 }
80626a55
DE
12189
12190 return htab;
12191}
12192
12193/* Update SECTIONS with the data from SECTP.
12194
12195 This function is like the other "locate" section routines that are
12196 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 12197 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
12198
12199 The result is non-zero for success, or zero if an error was found. */
12200
12201static int
73869dc2
DE
12202locate_v1_virtual_dwo_sections (asection *sectp,
12203 struct virtual_v1_dwo_sections *sections)
80626a55
DE
12204{
12205 const struct dwop_section_names *names = &dwop_section_names;
12206
12207 if (section_is_p (sectp->name, &names->abbrev_dwo))
12208 {
12209 /* There can be only one. */
049412e3 12210 if (sections->abbrev.s.section != NULL)
80626a55 12211 return 0;
049412e3 12212 sections->abbrev.s.section = sectp;
80626a55
DE
12213 sections->abbrev.size = bfd_get_section_size (sectp);
12214 }
12215 else if (section_is_p (sectp->name, &names->info_dwo)
12216 || section_is_p (sectp->name, &names->types_dwo))
12217 {
12218 /* There can be only one. */
049412e3 12219 if (sections->info_or_types.s.section != NULL)
80626a55 12220 return 0;
049412e3 12221 sections->info_or_types.s.section = sectp;
80626a55
DE
12222 sections->info_or_types.size = bfd_get_section_size (sectp);
12223 }
12224 else if (section_is_p (sectp->name, &names->line_dwo))
12225 {
12226 /* There can be only one. */
049412e3 12227 if (sections->line.s.section != NULL)
80626a55 12228 return 0;
049412e3 12229 sections->line.s.section = sectp;
80626a55
DE
12230 sections->line.size = bfd_get_section_size (sectp);
12231 }
12232 else if (section_is_p (sectp->name, &names->loc_dwo))
12233 {
12234 /* There can be only one. */
049412e3 12235 if (sections->loc.s.section != NULL)
80626a55 12236 return 0;
049412e3 12237 sections->loc.s.section = sectp;
80626a55
DE
12238 sections->loc.size = bfd_get_section_size (sectp);
12239 }
12240 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12241 {
12242 /* There can be only one. */
049412e3 12243 if (sections->macinfo.s.section != NULL)
80626a55 12244 return 0;
049412e3 12245 sections->macinfo.s.section = sectp;
80626a55
DE
12246 sections->macinfo.size = bfd_get_section_size (sectp);
12247 }
12248 else if (section_is_p (sectp->name, &names->macro_dwo))
12249 {
12250 /* There can be only one. */
049412e3 12251 if (sections->macro.s.section != NULL)
80626a55 12252 return 0;
049412e3 12253 sections->macro.s.section = sectp;
80626a55
DE
12254 sections->macro.size = bfd_get_section_size (sectp);
12255 }
12256 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12257 {
12258 /* There can be only one. */
049412e3 12259 if (sections->str_offsets.s.section != NULL)
80626a55 12260 return 0;
049412e3 12261 sections->str_offsets.s.section = sectp;
80626a55
DE
12262 sections->str_offsets.size = bfd_get_section_size (sectp);
12263 }
12264 else
12265 {
12266 /* No other kind of section is valid. */
12267 return 0;
12268 }
12269
12270 return 1;
12271}
12272
73869dc2
DE
12273/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12274 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12275 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12276 This is for DWP version 1 files. */
80626a55
DE
12277
12278static struct dwo_unit *
ed2dc618
SM
12279create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12280 struct dwp_file *dwp_file,
73869dc2
DE
12281 uint32_t unit_index,
12282 const char *comp_dir,
12283 ULONGEST signature, int is_debug_types)
80626a55
DE
12284{
12285 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
12286 const struct dwp_hash_table *dwp_htab =
12287 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12288 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
12289 const char *kind = is_debug_types ? "TU" : "CU";
12290 struct dwo_file *dwo_file;
12291 struct dwo_unit *dwo_unit;
73869dc2 12292 struct virtual_v1_dwo_sections sections;
80626a55 12293 void **dwo_file_slot;
80626a55
DE
12294 int i;
12295
73869dc2
DE
12296 gdb_assert (dwp_file->version == 1);
12297
b4f54984 12298 if (dwarf_read_debug)
80626a55 12299 {
73869dc2 12300 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 12301 kind,
73869dc2 12302 pulongest (unit_index), hex_string (signature),
80626a55
DE
12303 dwp_file->name);
12304 }
12305
19ac8c2e 12306 /* Fetch the sections of this DWO unit.
80626a55
DE
12307 Put a limit on the number of sections we look for so that bad data
12308 doesn't cause us to loop forever. */
12309
73869dc2 12310#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
12311 (1 /* .debug_info or .debug_types */ \
12312 + 1 /* .debug_abbrev */ \
12313 + 1 /* .debug_line */ \
12314 + 1 /* .debug_loc */ \
12315 + 1 /* .debug_str_offsets */ \
19ac8c2e 12316 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
12317 + 1 /* trailing zero */)
12318
12319 memset (&sections, 0, sizeof (sections));
80626a55 12320
73869dc2 12321 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
12322 {
12323 asection *sectp;
12324 uint32_t section_nr =
12325 read_4_bytes (dbfd,
73869dc2
DE
12326 dwp_htab->section_pool.v1.indices
12327 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
12328
12329 if (section_nr == 0)
12330 break;
12331 if (section_nr >= dwp_file->num_sections)
12332 {
12333 error (_("Dwarf Error: bad DWP hash table, section number too large"
12334 " [in module %s]"),
12335 dwp_file->name);
12336 }
12337
12338 sectp = dwp_file->elf_sections[section_nr];
73869dc2 12339 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
12340 {
12341 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12342 " [in module %s]"),
12343 dwp_file->name);
12344 }
12345 }
12346
12347 if (i < 2
a32a8923
DE
12348 || dwarf2_section_empty_p (&sections.info_or_types)
12349 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
12350 {
12351 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12352 " [in module %s]"),
12353 dwp_file->name);
12354 }
73869dc2 12355 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
12356 {
12357 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12358 " [in module %s]"),
12359 dwp_file->name);
12360 }
12361
12362 /* It's easier for the rest of the code if we fake a struct dwo_file and
12363 have dwo_unit "live" in that. At least for now.
12364
12365 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 12366 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
12367 file, we can combine them back into a virtual DWO file to save space
12368 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
12369 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12370
791afaa2
TT
12371 std::string virtual_dwo_name =
12372 string_printf ("virtual-dwo/%d-%d-%d-%d",
12373 get_section_id (&sections.abbrev),
12374 get_section_id (&sections.line),
12375 get_section_id (&sections.loc),
12376 get_section_id (&sections.str_offsets));
80626a55 12377 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12378 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12379 virtual_dwo_name.c_str (),
12380 comp_dir);
80626a55
DE
12381 /* Create one if necessary. */
12382 if (*dwo_file_slot == NULL)
12383 {
b4f54984 12384 if (dwarf_read_debug)
80626a55
DE
12385 {
12386 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12387 virtual_dwo_name.c_str ());
80626a55
DE
12388 }
12389 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
12390 dwo_file->dwo_name
12391 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
791afaa2
TT
12392 virtual_dwo_name.c_str (),
12393 virtual_dwo_name.size ());
0ac5b59e 12394 dwo_file->comp_dir = comp_dir;
80626a55
DE
12395 dwo_file->sections.abbrev = sections.abbrev;
12396 dwo_file->sections.line = sections.line;
12397 dwo_file->sections.loc = sections.loc;
12398 dwo_file->sections.macinfo = sections.macinfo;
12399 dwo_file->sections.macro = sections.macro;
12400 dwo_file->sections.str_offsets = sections.str_offsets;
12401 /* The "str" section is global to the entire DWP file. */
12402 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 12403 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
12404 there's no need to record it in dwo_file.
12405 Also, we can't simply record type sections in dwo_file because
12406 we record a pointer into the vector in dwo_unit. As we collect more
12407 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
12408 for it, invalidating all copies of pointers into the previous
12409 contents. */
80626a55
DE
12410 *dwo_file_slot = dwo_file;
12411 }
12412 else
12413 {
b4f54984 12414 if (dwarf_read_debug)
80626a55
DE
12415 {
12416 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12417 virtual_dwo_name.c_str ());
80626a55 12418 }
9a3c8263 12419 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 12420 }
80626a55
DE
12421
12422 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12423 dwo_unit->dwo_file = dwo_file;
12424 dwo_unit->signature = signature;
8d749320
SM
12425 dwo_unit->section =
12426 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 12427 *dwo_unit->section = sections.info_or_types;
57d63ce2 12428 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
12429
12430 return dwo_unit;
12431}
12432
73869dc2
DE
12433/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12434 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12435 piece within that section used by a TU/CU, return a virtual section
12436 of just that piece. */
12437
12438static struct dwarf2_section_info
ed2dc618
SM
12439create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12440 struct dwarf2_section_info *section,
73869dc2
DE
12441 bfd_size_type offset, bfd_size_type size)
12442{
12443 struct dwarf2_section_info result;
12444 asection *sectp;
12445
12446 gdb_assert (section != NULL);
12447 gdb_assert (!section->is_virtual);
12448
12449 memset (&result, 0, sizeof (result));
12450 result.s.containing_section = section;
12451 result.is_virtual = 1;
12452
12453 if (size == 0)
12454 return result;
12455
12456 sectp = get_section_bfd_section (section);
12457
12458 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12459 bounds of the real section. This is a pretty-rare event, so just
12460 flag an error (easier) instead of a warning and trying to cope. */
12461 if (sectp == NULL
12462 || offset + size > bfd_get_section_size (sectp))
12463 {
73869dc2
DE
12464 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12465 " in section %s [in module %s]"),
12466 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
12467 objfile_name (dwarf2_per_objfile->objfile));
12468 }
12469
12470 result.virtual_offset = offset;
12471 result.size = size;
12472 return result;
12473}
12474
12475/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12476 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12477 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12478 This is for DWP version 2 files. */
12479
12480static struct dwo_unit *
ed2dc618
SM
12481create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12482 struct dwp_file *dwp_file,
73869dc2
DE
12483 uint32_t unit_index,
12484 const char *comp_dir,
12485 ULONGEST signature, int is_debug_types)
12486{
12487 struct objfile *objfile = dwarf2_per_objfile->objfile;
12488 const struct dwp_hash_table *dwp_htab =
12489 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12490 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
12491 const char *kind = is_debug_types ? "TU" : "CU";
12492 struct dwo_file *dwo_file;
12493 struct dwo_unit *dwo_unit;
12494 struct virtual_v2_dwo_sections sections;
12495 void **dwo_file_slot;
73869dc2
DE
12496 int i;
12497
12498 gdb_assert (dwp_file->version == 2);
12499
b4f54984 12500 if (dwarf_read_debug)
73869dc2
DE
12501 {
12502 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12503 kind,
12504 pulongest (unit_index), hex_string (signature),
12505 dwp_file->name);
12506 }
12507
12508 /* Fetch the section offsets of this DWO unit. */
12509
12510 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12511
12512 for (i = 0; i < dwp_htab->nr_columns; ++i)
12513 {
12514 uint32_t offset = read_4_bytes (dbfd,
12515 dwp_htab->section_pool.v2.offsets
12516 + (((unit_index - 1) * dwp_htab->nr_columns
12517 + i)
12518 * sizeof (uint32_t)));
12519 uint32_t size = read_4_bytes (dbfd,
12520 dwp_htab->section_pool.v2.sizes
12521 + (((unit_index - 1) * dwp_htab->nr_columns
12522 + i)
12523 * sizeof (uint32_t)));
12524
12525 switch (dwp_htab->section_pool.v2.section_ids[i])
12526 {
12527 case DW_SECT_INFO:
12528 case DW_SECT_TYPES:
12529 sections.info_or_types_offset = offset;
12530 sections.info_or_types_size = size;
12531 break;
12532 case DW_SECT_ABBREV:
12533 sections.abbrev_offset = offset;
12534 sections.abbrev_size = size;
12535 break;
12536 case DW_SECT_LINE:
12537 sections.line_offset = offset;
12538 sections.line_size = size;
12539 break;
12540 case DW_SECT_LOC:
12541 sections.loc_offset = offset;
12542 sections.loc_size = size;
12543 break;
12544 case DW_SECT_STR_OFFSETS:
12545 sections.str_offsets_offset = offset;
12546 sections.str_offsets_size = size;
12547 break;
12548 case DW_SECT_MACINFO:
12549 sections.macinfo_offset = offset;
12550 sections.macinfo_size = size;
12551 break;
12552 case DW_SECT_MACRO:
12553 sections.macro_offset = offset;
12554 sections.macro_size = size;
12555 break;
12556 }
12557 }
12558
12559 /* It's easier for the rest of the code if we fake a struct dwo_file and
12560 have dwo_unit "live" in that. At least for now.
12561
12562 The DWP file can be made up of a random collection of CUs and TUs.
12563 However, for each CU + set of TUs that came from the same original DWO
12564 file, we can combine them back into a virtual DWO file to save space
12565 (fewer struct dwo_file objects to allocate). Remember that for really
12566 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12567
791afaa2
TT
12568 std::string virtual_dwo_name =
12569 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12570 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12571 (long) (sections.line_size ? sections.line_offset : 0),
12572 (long) (sections.loc_size ? sections.loc_offset : 0),
12573 (long) (sections.str_offsets_size
12574 ? sections.str_offsets_offset : 0));
73869dc2 12575 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12576 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12577 virtual_dwo_name.c_str (),
12578 comp_dir);
73869dc2
DE
12579 /* Create one if necessary. */
12580 if (*dwo_file_slot == NULL)
12581 {
b4f54984 12582 if (dwarf_read_debug)
73869dc2
DE
12583 {
12584 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12585 virtual_dwo_name.c_str ());
73869dc2
DE
12586 }
12587 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
12588 dwo_file->dwo_name
12589 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
791afaa2
TT
12590 virtual_dwo_name.c_str (),
12591 virtual_dwo_name.size ());
73869dc2
DE
12592 dwo_file->comp_dir = comp_dir;
12593 dwo_file->sections.abbrev =
ed2dc618 12594 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
12595 sections.abbrev_offset, sections.abbrev_size);
12596 dwo_file->sections.line =
ed2dc618 12597 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
12598 sections.line_offset, sections.line_size);
12599 dwo_file->sections.loc =
ed2dc618 12600 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
12601 sections.loc_offset, sections.loc_size);
12602 dwo_file->sections.macinfo =
ed2dc618 12603 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
12604 sections.macinfo_offset, sections.macinfo_size);
12605 dwo_file->sections.macro =
ed2dc618 12606 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
12607 sections.macro_offset, sections.macro_size);
12608 dwo_file->sections.str_offsets =
ed2dc618
SM
12609 create_dwp_v2_section (dwarf2_per_objfile,
12610 &dwp_file->sections.str_offsets,
73869dc2
DE
12611 sections.str_offsets_offset,
12612 sections.str_offsets_size);
12613 /* The "str" section is global to the entire DWP file. */
12614 dwo_file->sections.str = dwp_file->sections.str;
12615 /* The info or types section is assigned below to dwo_unit,
12616 there's no need to record it in dwo_file.
12617 Also, we can't simply record type sections in dwo_file because
12618 we record a pointer into the vector in dwo_unit. As we collect more
12619 types we'll grow the vector and eventually have to reallocate space
12620 for it, invalidating all copies of pointers into the previous
12621 contents. */
12622 *dwo_file_slot = dwo_file;
12623 }
12624 else
12625 {
b4f54984 12626 if (dwarf_read_debug)
73869dc2
DE
12627 {
12628 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12629 virtual_dwo_name.c_str ());
73869dc2 12630 }
9a3c8263 12631 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12632 }
73869dc2
DE
12633
12634 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12635 dwo_unit->dwo_file = dwo_file;
12636 dwo_unit->signature = signature;
8d749320
SM
12637 dwo_unit->section =
12638 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
ed2dc618
SM
12639 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12640 is_debug_types
73869dc2
DE
12641 ? &dwp_file->sections.types
12642 : &dwp_file->sections.info,
12643 sections.info_or_types_offset,
12644 sections.info_or_types_size);
12645 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12646
12647 return dwo_unit;
12648}
12649
57d63ce2
DE
12650/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12651 Returns NULL if the signature isn't found. */
80626a55
DE
12652
12653static struct dwo_unit *
ed2dc618
SM
12654lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12655 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12656 ULONGEST signature, int is_debug_types)
80626a55 12657{
57d63ce2
DE
12658 const struct dwp_hash_table *dwp_htab =
12659 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12660 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12661 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12662 uint32_t hash = signature & mask;
12663 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12664 unsigned int i;
12665 void **slot;
870f88f7 12666 struct dwo_unit find_dwo_cu;
80626a55
DE
12667
12668 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12669 find_dwo_cu.signature = signature;
19ac8c2e
DE
12670 slot = htab_find_slot (is_debug_types
12671 ? dwp_file->loaded_tus
12672 : dwp_file->loaded_cus,
12673 &find_dwo_cu, INSERT);
80626a55
DE
12674
12675 if (*slot != NULL)
9a3c8263 12676 return (struct dwo_unit *) *slot;
80626a55
DE
12677
12678 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12679 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12680 {
12681 ULONGEST signature_in_table;
12682
12683 signature_in_table =
57d63ce2 12684 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12685 if (signature_in_table == signature)
12686 {
57d63ce2
DE
12687 uint32_t unit_index =
12688 read_4_bytes (dbfd,
12689 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12690
73869dc2
DE
12691 if (dwp_file->version == 1)
12692 {
ed2dc618
SM
12693 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12694 dwp_file, unit_index,
73869dc2
DE
12695 comp_dir, signature,
12696 is_debug_types);
12697 }
12698 else
12699 {
ed2dc618
SM
12700 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12701 dwp_file, unit_index,
73869dc2
DE
12702 comp_dir, signature,
12703 is_debug_types);
12704 }
9a3c8263 12705 return (struct dwo_unit *) *slot;
80626a55
DE
12706 }
12707 if (signature_in_table == 0)
12708 return NULL;
12709 hash = (hash + hash2) & mask;
12710 }
12711
12712 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12713 " [in module %s]"),
12714 dwp_file->name);
12715}
12716
ab5088bf 12717/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12718 Open the file specified by FILE_NAME and hand it off to BFD for
12719 preliminary analysis. Return a newly initialized bfd *, which
12720 includes a canonicalized copy of FILE_NAME.
80626a55 12721 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12722 SEARCH_CWD is true if the current directory is to be searched.
12723 It will be searched before debug-file-directory.
13aaf454
DE
12724 If successful, the file is added to the bfd include table of the
12725 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12726 If unable to find/open the file, return NULL.
3019eac3
DE
12727 NOTE: This function is derived from symfile_bfd_open. */
12728
192b62ce 12729static gdb_bfd_ref_ptr
ed2dc618
SM
12730try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12731 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12732{
24b9144d 12733 int desc;
9c02c129
DE
12734 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12735 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12736 to debug_file_directory. */
e0cc99a6 12737 const char *search_path;
9c02c129
DE
12738 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12739
e0cc99a6 12740 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12741 if (search_cwd)
12742 {
12743 if (*debug_file_directory != '\0')
e0cc99a6
TT
12744 {
12745 search_path_holder.reset (concat (".", dirname_separator_string,
12746 debug_file_directory,
12747 (char *) NULL));
12748 search_path = search_path_holder.get ();
12749 }
6ac97d4c 12750 else
e0cc99a6 12751 search_path = ".";
6ac97d4c 12752 }
9c02c129 12753 else
e0cc99a6 12754 search_path = debug_file_directory;
3019eac3 12755
24b9144d 12756 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12757 if (is_dwp)
12758 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12759
12760 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12761 desc = openp (search_path, flags, file_name,
3019eac3
DE
12762 O_RDONLY | O_BINARY, &absolute_name);
12763 if (desc < 0)
12764 return NULL;
12765
e0cc99a6
TT
12766 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12767 gnutarget, desc));
9c02c129
DE
12768 if (sym_bfd == NULL)
12769 return NULL;
192b62ce 12770 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12771
192b62ce
TT
12772 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12773 return NULL;
3019eac3 12774
13aaf454
DE
12775 /* Success. Record the bfd as having been included by the objfile's bfd.
12776 This is important because things like demangled_names_hash lives in the
12777 objfile's per_bfd space and may have references to things like symbol
12778 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12779 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12780
3019eac3
DE
12781 return sym_bfd;
12782}
12783
ab5088bf 12784/* Try to open DWO file FILE_NAME.
3019eac3
DE
12785 COMP_DIR is the DW_AT_comp_dir attribute.
12786 The result is the bfd handle of the file.
12787 If there is a problem finding or opening the file, return NULL.
12788 Upon success, the canonicalized path of the file is stored in the bfd,
12789 same as symfile_bfd_open. */
12790
192b62ce 12791static gdb_bfd_ref_ptr
ed2dc618
SM
12792open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12793 const char *file_name, const char *comp_dir)
3019eac3 12794{
80626a55 12795 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12796 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12797 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12798
12799 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12800
12801 if (comp_dir != NULL)
12802 {
b36cec19
PA
12803 char *path_to_try = concat (comp_dir, SLASH_STRING,
12804 file_name, (char *) NULL);
3019eac3
DE
12805
12806 /* NOTE: If comp_dir is a relative path, this will also try the
12807 search path, which seems useful. */
ed2dc618
SM
12808 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12809 path_to_try,
12810 0 /*is_dwp*/,
192b62ce 12811 1 /*search_cwd*/));
3019eac3
DE
12812 xfree (path_to_try);
12813 if (abfd != NULL)
12814 return abfd;
12815 }
12816
12817 /* That didn't work, try debug-file-directory, which, despite its name,
12818 is a list of paths. */
12819
12820 if (*debug_file_directory == '\0')
12821 return NULL;
12822
ed2dc618
SM
12823 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12824 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12825}
12826
80626a55
DE
12827/* This function is mapped across the sections and remembers the offset and
12828 size of each of the DWO debugging sections we are interested in. */
12829
12830static void
12831dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12832{
9a3c8263 12833 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12834 const struct dwop_section_names *names = &dwop_section_names;
12835
12836 if (section_is_p (sectp->name, &names->abbrev_dwo))
12837 {
049412e3 12838 dwo_sections->abbrev.s.section = sectp;
80626a55
DE
12839 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
12840 }
12841 else if (section_is_p (sectp->name, &names->info_dwo))
12842 {
049412e3 12843 dwo_sections->info.s.section = sectp;
80626a55
DE
12844 dwo_sections->info.size = bfd_get_section_size (sectp);
12845 }
12846 else if (section_is_p (sectp->name, &names->line_dwo))
12847 {
049412e3 12848 dwo_sections->line.s.section = sectp;
80626a55
DE
12849 dwo_sections->line.size = bfd_get_section_size (sectp);
12850 }
12851 else if (section_is_p (sectp->name, &names->loc_dwo))
12852 {
049412e3 12853 dwo_sections->loc.s.section = sectp;
80626a55
DE
12854 dwo_sections->loc.size = bfd_get_section_size (sectp);
12855 }
12856 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12857 {
049412e3 12858 dwo_sections->macinfo.s.section = sectp;
80626a55
DE
12859 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
12860 }
12861 else if (section_is_p (sectp->name, &names->macro_dwo))
12862 {
049412e3 12863 dwo_sections->macro.s.section = sectp;
80626a55
DE
12864 dwo_sections->macro.size = bfd_get_section_size (sectp);
12865 }
12866 else if (section_is_p (sectp->name, &names->str_dwo))
12867 {
049412e3 12868 dwo_sections->str.s.section = sectp;
80626a55
DE
12869 dwo_sections->str.size = bfd_get_section_size (sectp);
12870 }
12871 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12872 {
049412e3 12873 dwo_sections->str_offsets.s.section = sectp;
80626a55
DE
12874 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
12875 }
12876 else if (section_is_p (sectp->name, &names->types_dwo))
12877 {
12878 struct dwarf2_section_info type_section;
12879
12880 memset (&type_section, 0, sizeof (type_section));
049412e3 12881 type_section.s.section = sectp;
80626a55
DE
12882 type_section.size = bfd_get_section_size (sectp);
12883 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
12884 &type_section);
12885 }
12886}
12887
ab5088bf 12888/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12889 by PER_CU. This is for the non-DWP case.
80626a55 12890 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12891
12892static struct dwo_file *
0ac5b59e
DE
12893open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12894 const char *dwo_name, const char *comp_dir)
3019eac3 12895{
ed2dc618 12896 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 12897 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 12898
ed2dc618 12899 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir));
80626a55
DE
12900 if (dbfd == NULL)
12901 {
b4f54984 12902 if (dwarf_read_debug)
80626a55
DE
12903 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12904 return NULL;
12905 }
263db9a1
TT
12906
12907 /* We use a unique pointer here, despite the obstack allocation,
12908 because a dwo_file needs some cleanup if it is abandoned. */
12909 dwo_file_up dwo_file (OBSTACK_ZALLOC (&objfile->objfile_obstack,
12910 struct dwo_file));
0ac5b59e
DE
12911 dwo_file->dwo_name = dwo_name;
12912 dwo_file->comp_dir = comp_dir;
192b62ce 12913 dwo_file->dbfd = dbfd.release ();
3019eac3 12914
192b62ce
TT
12915 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
12916 &dwo_file->sections);
3019eac3 12917
ed2dc618
SM
12918 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
12919 dwo_file->cus);
3019eac3 12920
263db9a1 12921 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 12922 dwo_file->sections.types, dwo_file->tus);
3019eac3 12923
b4f54984 12924 if (dwarf_read_debug)
80626a55
DE
12925 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12926
263db9a1 12927 return dwo_file.release ();
3019eac3
DE
12928}
12929
80626a55 12930/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12931 size of each of the DWP debugging sections common to version 1 and 2 that
12932 we are interested in. */
3019eac3 12933
80626a55 12934static void
73869dc2
DE
12935dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12936 void *dwp_file_ptr)
3019eac3 12937{
9a3c8263 12938 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
12939 const struct dwop_section_names *names = &dwop_section_names;
12940 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12941
80626a55 12942 /* Record the ELF section number for later lookup: this is what the
73869dc2 12943 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12944 gdb_assert (elf_section_nr < dwp_file->num_sections);
12945 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12946
80626a55
DE
12947 /* Look for specific sections that we need. */
12948 if (section_is_p (sectp->name, &names->str_dwo))
12949 {
049412e3 12950 dwp_file->sections.str.s.section = sectp;
80626a55
DE
12951 dwp_file->sections.str.size = bfd_get_section_size (sectp);
12952 }
12953 else if (section_is_p (sectp->name, &names->cu_index))
12954 {
049412e3 12955 dwp_file->sections.cu_index.s.section = sectp;
80626a55
DE
12956 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
12957 }
12958 else if (section_is_p (sectp->name, &names->tu_index))
12959 {
049412e3 12960 dwp_file->sections.tu_index.s.section = sectp;
80626a55
DE
12961 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
12962 }
12963}
3019eac3 12964
73869dc2
DE
12965/* This function is mapped across the sections and remembers the offset and
12966 size of each of the DWP version 2 debugging sections that we are interested
12967 in. This is split into a separate function because we don't know if we
12968 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12969
12970static void
12971dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12972{
9a3c8263 12973 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12974 const struct dwop_section_names *names = &dwop_section_names;
12975 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12976
12977 /* Record the ELF section number for later lookup: this is what the
12978 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12979 gdb_assert (elf_section_nr < dwp_file->num_sections);
12980 dwp_file->elf_sections[elf_section_nr] = sectp;
12981
12982 /* Look for specific sections that we need. */
12983 if (section_is_p (sectp->name, &names->abbrev_dwo))
12984 {
049412e3 12985 dwp_file->sections.abbrev.s.section = sectp;
73869dc2
DE
12986 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
12987 }
12988 else if (section_is_p (sectp->name, &names->info_dwo))
12989 {
049412e3 12990 dwp_file->sections.info.s.section = sectp;
73869dc2
DE
12991 dwp_file->sections.info.size = bfd_get_section_size (sectp);
12992 }
12993 else if (section_is_p (sectp->name, &names->line_dwo))
12994 {
049412e3 12995 dwp_file->sections.line.s.section = sectp;
73869dc2
DE
12996 dwp_file->sections.line.size = bfd_get_section_size (sectp);
12997 }
12998 else if (section_is_p (sectp->name, &names->loc_dwo))
12999 {
049412e3 13000 dwp_file->sections.loc.s.section = sectp;
73869dc2
DE
13001 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
13002 }
13003 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13004 {
049412e3 13005 dwp_file->sections.macinfo.s.section = sectp;
73869dc2
DE
13006 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
13007 }
13008 else if (section_is_p (sectp->name, &names->macro_dwo))
13009 {
049412e3 13010 dwp_file->sections.macro.s.section = sectp;
73869dc2
DE
13011 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
13012 }
13013 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13014 {
049412e3 13015 dwp_file->sections.str_offsets.s.section = sectp;
73869dc2
DE
13016 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
13017 }
13018 else if (section_is_p (sectp->name, &names->types_dwo))
13019 {
049412e3 13020 dwp_file->sections.types.s.section = sectp;
73869dc2
DE
13021 dwp_file->sections.types.size = bfd_get_section_size (sectp);
13022 }
13023}
13024
80626a55 13025/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 13026
80626a55
DE
13027static hashval_t
13028hash_dwp_loaded_cutus (const void *item)
13029{
9a3c8263 13030 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 13031
80626a55
DE
13032 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13033 return dwo_unit->signature;
3019eac3
DE
13034}
13035
80626a55 13036/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 13037
80626a55
DE
13038static int
13039eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 13040{
9a3c8263
SM
13041 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13042 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 13043
80626a55
DE
13044 return dua->signature == dub->signature;
13045}
3019eac3 13046
80626a55 13047/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 13048
80626a55
DE
13049static htab_t
13050allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13051{
13052 return htab_create_alloc_ex (3,
13053 hash_dwp_loaded_cutus,
13054 eq_dwp_loaded_cutus,
13055 NULL,
13056 &objfile->objfile_obstack,
13057 hashtab_obstack_allocate,
13058 dummy_obstack_deallocate);
13059}
3019eac3 13060
ab5088bf
DE
13061/* Try to open DWP file FILE_NAME.
13062 The result is the bfd handle of the file.
13063 If there is a problem finding or opening the file, return NULL.
13064 Upon success, the canonicalized path of the file is stored in the bfd,
13065 same as symfile_bfd_open. */
13066
192b62ce 13067static gdb_bfd_ref_ptr
ed2dc618
SM
13068open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13069 const char *file_name)
ab5088bf 13070{
ed2dc618
SM
13071 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13072 1 /*is_dwp*/,
192b62ce 13073 1 /*search_cwd*/));
6ac97d4c
DE
13074 if (abfd != NULL)
13075 return abfd;
13076
13077 /* Work around upstream bug 15652.
13078 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13079 [Whether that's a "bug" is debatable, but it is getting in our way.]
13080 We have no real idea where the dwp file is, because gdb's realpath-ing
13081 of the executable's path may have discarded the needed info.
13082 [IWBN if the dwp file name was recorded in the executable, akin to
13083 .gnu_debuglink, but that doesn't exist yet.]
13084 Strip the directory from FILE_NAME and search again. */
13085 if (*debug_file_directory != '\0')
13086 {
13087 /* Don't implicitly search the current directory here.
13088 If the user wants to search "." to handle this case,
13089 it must be added to debug-file-directory. */
ed2dc618
SM
13090 return try_open_dwop_file (dwarf2_per_objfile,
13091 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
13092 0 /*search_cwd*/);
13093 }
13094
13095 return NULL;
ab5088bf
DE
13096}
13097
80626a55
DE
13098/* Initialize the use of the DWP file for the current objfile.
13099 By convention the name of the DWP file is ${objfile}.dwp.
13100 The result is NULL if it can't be found. */
a766d390 13101
400174b1 13102static std::unique_ptr<struct dwp_file>
ed2dc618 13103open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
13104{
13105 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 13106
82bf32bc
JK
13107 /* Try to find first .dwp for the binary file before any symbolic links
13108 resolving. */
6c447423
DE
13109
13110 /* If the objfile is a debug file, find the name of the real binary
13111 file and get the name of dwp file from there. */
d721ba37 13112 std::string dwp_name;
6c447423
DE
13113 if (objfile->separate_debug_objfile_backlink != NULL)
13114 {
13115 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13116 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 13117
d721ba37 13118 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
13119 }
13120 else
d721ba37
PA
13121 dwp_name = objfile->original_name;
13122
13123 dwp_name += ".dwp";
80626a55 13124
ed2dc618 13125 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
13126 if (dbfd == NULL
13127 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13128 {
13129 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
13130 dwp_name = objfile_name (objfile);
13131 dwp_name += ".dwp";
ed2dc618 13132 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
13133 }
13134
80626a55
DE
13135 if (dbfd == NULL)
13136 {
b4f54984 13137 if (dwarf_read_debug)
d721ba37 13138 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 13139 return std::unique_ptr<dwp_file> ();
3019eac3 13140 }
400174b1
TT
13141
13142 const char *name = bfd_get_filename (dbfd.get ());
13143 std::unique_ptr<struct dwp_file> dwp_file
13144 (new struct dwp_file (name, std::move (dbfd)));
c906108c 13145
80626a55 13146 /* +1: section 0 is unused */
192b62ce 13147 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
80626a55
DE
13148 dwp_file->elf_sections =
13149 OBSTACK_CALLOC (&objfile->objfile_obstack,
13150 dwp_file->num_sections, asection *);
13151
400174b1
TT
13152 bfd_map_over_sections (dwp_file->dbfd.get (),
13153 dwarf2_locate_common_dwp_sections,
13154 dwp_file.get ());
80626a55 13155
400174b1
TT
13156 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13157 0);
80626a55 13158
400174b1
TT
13159 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13160 1);
80626a55 13161
73869dc2 13162 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
13163 if (dwp_file->cus && dwp_file->tus
13164 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
13165 {
13166 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 13167 pretty bizarre. We use pulongest here because that's the established
4d65956b 13168 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
13169 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13170 " TU version %s [in DWP file %s]"),
13171 pulongest (dwp_file->cus->version),
d721ba37 13172 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 13173 }
08302ed2
DE
13174
13175 if (dwp_file->cus)
13176 dwp_file->version = dwp_file->cus->version;
13177 else if (dwp_file->tus)
13178 dwp_file->version = dwp_file->tus->version;
13179 else
13180 dwp_file->version = 2;
73869dc2
DE
13181
13182 if (dwp_file->version == 2)
400174b1
TT
13183 bfd_map_over_sections (dwp_file->dbfd.get (),
13184 dwarf2_locate_v2_dwp_sections,
13185 dwp_file.get ());
73869dc2 13186
19ac8c2e
DE
13187 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13188 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 13189
b4f54984 13190 if (dwarf_read_debug)
80626a55
DE
13191 {
13192 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13193 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
13194 " %s CUs, %s TUs\n",
13195 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13196 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
13197 }
13198
13199 return dwp_file;
3019eac3 13200}
c906108c 13201
ab5088bf
DE
13202/* Wrapper around open_and_init_dwp_file, only open it once. */
13203
13204static struct dwp_file *
ed2dc618 13205get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf
DE
13206{
13207 if (! dwarf2_per_objfile->dwp_checked)
13208 {
ed2dc618
SM
13209 dwarf2_per_objfile->dwp_file
13210 = open_and_init_dwp_file (dwarf2_per_objfile);
ab5088bf
DE
13211 dwarf2_per_objfile->dwp_checked = 1;
13212 }
400174b1 13213 return dwarf2_per_objfile->dwp_file.get ();
ab5088bf
DE
13214}
13215
80626a55
DE
13216/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13217 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13218 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 13219 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
13220 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13221
13222 This is called, for example, when wanting to read a variable with a
13223 complex location. Therefore we don't want to do file i/o for every call.
13224 Therefore we don't want to look for a DWO file on every call.
13225 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13226 then we check if we've already seen DWO_NAME, and only THEN do we check
13227 for a DWO file.
13228
1c658ad5 13229 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 13230 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 13231
3019eac3 13232static struct dwo_unit *
80626a55
DE
13233lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13234 const char *dwo_name, const char *comp_dir,
13235 ULONGEST signature, int is_debug_types)
3019eac3 13236{
ed2dc618 13237 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
3019eac3 13238 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
13239 const char *kind = is_debug_types ? "TU" : "CU";
13240 void **dwo_file_slot;
3019eac3 13241 struct dwo_file *dwo_file;
80626a55 13242 struct dwp_file *dwp_file;
cb1df416 13243
6a506a2d
DE
13244 /* First see if there's a DWP file.
13245 If we have a DWP file but didn't find the DWO inside it, don't
13246 look for the original DWO file. It makes gdb behave differently
13247 depending on whether one is debugging in the build tree. */
cf2c3c16 13248
ed2dc618 13249 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 13250 if (dwp_file != NULL)
cf2c3c16 13251 {
80626a55
DE
13252 const struct dwp_hash_table *dwp_htab =
13253 is_debug_types ? dwp_file->tus : dwp_file->cus;
13254
13255 if (dwp_htab != NULL)
13256 {
13257 struct dwo_unit *dwo_cutu =
ed2dc618 13258 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 13259 signature, is_debug_types);
80626a55
DE
13260
13261 if (dwo_cutu != NULL)
13262 {
b4f54984 13263 if (dwarf_read_debug)
80626a55
DE
13264 {
13265 fprintf_unfiltered (gdb_stdlog,
13266 "Virtual DWO %s %s found: @%s\n",
13267 kind, hex_string (signature),
13268 host_address_to_string (dwo_cutu));
13269 }
13270 return dwo_cutu;
13271 }
13272 }
13273 }
6a506a2d 13274 else
80626a55 13275 {
6a506a2d 13276 /* No DWP file, look for the DWO file. */
80626a55 13277
ed2dc618
SM
13278 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13279 dwo_name, comp_dir);
6a506a2d 13280 if (*dwo_file_slot == NULL)
80626a55 13281 {
6a506a2d
DE
13282 /* Read in the file and build a table of the CUs/TUs it contains. */
13283 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 13284 }
6a506a2d 13285 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 13286 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 13287
6a506a2d 13288 if (dwo_file != NULL)
19c3d4c9 13289 {
6a506a2d
DE
13290 struct dwo_unit *dwo_cutu = NULL;
13291
13292 if (is_debug_types && dwo_file->tus)
13293 {
13294 struct dwo_unit find_dwo_cutu;
13295
13296 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13297 find_dwo_cutu.signature = signature;
9a3c8263
SM
13298 dwo_cutu
13299 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
6a506a2d 13300 }
33c5cd75 13301 else if (!is_debug_types && dwo_file->cus)
80626a55 13302 {
33c5cd75
DB
13303 struct dwo_unit find_dwo_cutu;
13304
13305 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13306 find_dwo_cutu.signature = signature;
13307 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13308 &find_dwo_cutu);
6a506a2d
DE
13309 }
13310
13311 if (dwo_cutu != NULL)
13312 {
b4f54984 13313 if (dwarf_read_debug)
6a506a2d
DE
13314 {
13315 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13316 kind, dwo_name, hex_string (signature),
13317 host_address_to_string (dwo_cutu));
13318 }
13319 return dwo_cutu;
80626a55
DE
13320 }
13321 }
2e276125 13322 }
9cdd5dbd 13323
80626a55
DE
13324 /* We didn't find it. This could mean a dwo_id mismatch, or
13325 someone deleted the DWO/DWP file, or the search path isn't set up
13326 correctly to find the file. */
13327
b4f54984 13328 if (dwarf_read_debug)
80626a55
DE
13329 {
13330 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13331 kind, dwo_name, hex_string (signature));
13332 }
3019eac3 13333
6656a72d
DE
13334 /* This is a warning and not a complaint because it can be caused by
13335 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
13336 {
13337 /* Print the name of the DWP file if we looked there, helps the user
13338 better diagnose the problem. */
791afaa2 13339 std::string dwp_text;
43942612
DE
13340
13341 if (dwp_file != NULL)
791afaa2
TT
13342 dwp_text = string_printf (" [in DWP file %s]",
13343 lbasename (dwp_file->name));
43942612 13344
9d8780f0 13345 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612
DE
13346 " [in module %s]"),
13347 kind, dwo_name, hex_string (signature),
791afaa2 13348 dwp_text.c_str (),
43942612 13349 this_unit->is_debug_types ? "TU" : "CU",
9d8780f0 13350 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
43942612 13351 }
3019eac3 13352 return NULL;
5fb290d7
DJ
13353}
13354
80626a55
DE
13355/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13356 See lookup_dwo_cutu_unit for details. */
13357
13358static struct dwo_unit *
13359lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13360 const char *dwo_name, const char *comp_dir,
13361 ULONGEST signature)
13362{
13363 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13364}
13365
13366/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13367 See lookup_dwo_cutu_unit for details. */
13368
13369static struct dwo_unit *
13370lookup_dwo_type_unit (struct signatured_type *this_tu,
13371 const char *dwo_name, const char *comp_dir)
13372{
13373 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13374}
13375
89e63ee4
DE
13376/* Traversal function for queue_and_load_all_dwo_tus. */
13377
13378static int
13379queue_and_load_dwo_tu (void **slot, void *info)
13380{
13381 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13382 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13383 ULONGEST signature = dwo_unit->signature;
13384 struct signatured_type *sig_type =
13385 lookup_dwo_signatured_type (per_cu->cu, signature);
13386
13387 if (sig_type != NULL)
13388 {
13389 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13390
13391 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13392 a real dependency of PER_CU on SIG_TYPE. That is detected later
13393 while processing PER_CU. */
13394 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13395 load_full_type_unit (sig_cu);
13396 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
13397 }
13398
13399 return 1;
13400}
13401
13402/* Queue all TUs contained in the DWO of PER_CU to be read in.
13403 The DWO may have the only definition of the type, though it may not be
13404 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13405 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13406
13407static void
13408queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13409{
13410 struct dwo_unit *dwo_unit;
13411 struct dwo_file *dwo_file;
13412
13413 gdb_assert (!per_cu->is_debug_types);
ed2dc618 13414 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
89e63ee4
DE
13415 gdb_assert (per_cu->cu != NULL);
13416
13417 dwo_unit = per_cu->cu->dwo_unit;
13418 gdb_assert (dwo_unit != NULL);
13419
13420 dwo_file = dwo_unit->dwo_file;
13421 if (dwo_file->tus != NULL)
13422 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13423}
13424
3019eac3 13425/* Free all resources associated with DWO_FILE.
5dafb3d1 13426 Close the DWO file and munmap the sections. */
348e048f
DE
13427
13428static void
5dafb3d1 13429free_dwo_file (struct dwo_file *dwo_file)
348e048f 13430{
5c6fa7ab 13431 /* Note: dbfd is NULL for virtual DWO files. */
80626a55 13432 gdb_bfd_unref (dwo_file->dbfd);
348e048f 13433
3019eac3
DE
13434 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
13435}
348e048f 13436
3019eac3 13437/* Traversal function for free_dwo_files. */
2ab95328 13438
3019eac3
DE
13439static int
13440free_dwo_file_from_slot (void **slot, void *info)
13441{
13442 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
348e048f 13443
5dafb3d1 13444 free_dwo_file (dwo_file);
348e048f 13445
3019eac3
DE
13446 return 1;
13447}
348e048f 13448
3019eac3 13449/* Free all resources associated with DWO_FILES. */
348e048f 13450
3019eac3
DE
13451static void
13452free_dwo_files (htab_t dwo_files, struct objfile *objfile)
13453{
13454 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 13455}
3019eac3
DE
13456\f
13457/* Read in various DIEs. */
348e048f 13458
d389af10 13459/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
13460 Inherit only the children of the DW_AT_abstract_origin DIE not being
13461 already referenced by DW_AT_abstract_origin from the children of the
13462 current DIE. */
d389af10
JK
13463
13464static void
13465inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13466{
13467 struct die_info *child_die;
791afaa2 13468 sect_offset *offsetp;
d389af10
JK
13469 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13470 struct die_info *origin_die;
13471 /* Iterator of the ORIGIN_DIE children. */
13472 struct die_info *origin_child_die;
d389af10 13473 struct attribute *attr;
cd02d79d
PA
13474 struct dwarf2_cu *origin_cu;
13475 struct pending **origin_previous_list_in_scope;
d389af10
JK
13476
13477 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13478 if (!attr)
13479 return;
13480
cd02d79d
PA
13481 /* Note that following die references may follow to a die in a
13482 different cu. */
13483
13484 origin_cu = cu;
13485 origin_die = follow_die_ref (die, attr, &origin_cu);
13486
13487 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13488 symbols in. */
13489 origin_previous_list_in_scope = origin_cu->list_in_scope;
13490 origin_cu->list_in_scope = cu->list_in_scope;
13491
edb3359d
DJ
13492 if (die->tag != origin_die->tag
13493 && !(die->tag == DW_TAG_inlined_subroutine
13494 && origin_die->tag == DW_TAG_subprogram))
b98664d3 13495 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
13496 sect_offset_str (die->sect_off),
13497 sect_offset_str (origin_die->sect_off));
d389af10 13498
791afaa2 13499 std::vector<sect_offset> offsets;
d389af10 13500
3ea89b92
PMR
13501 for (child_die = die->child;
13502 child_die && child_die->tag;
13503 child_die = sibling_die (child_die))
13504 {
13505 struct die_info *child_origin_die;
13506 struct dwarf2_cu *child_origin_cu;
13507
13508 /* We are trying to process concrete instance entries:
216f72a1 13509 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13510 it's not relevant to our analysis here. i.e. detecting DIEs that are
13511 present in the abstract instance but not referenced in the concrete
13512 one. */
216f72a1
JK
13513 if (child_die->tag == DW_TAG_call_site
13514 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
13515 continue;
13516
c38f313d
DJ
13517 /* For each CHILD_DIE, find the corresponding child of
13518 ORIGIN_DIE. If there is more than one layer of
13519 DW_AT_abstract_origin, follow them all; there shouldn't be,
13520 but GCC versions at least through 4.4 generate this (GCC PR
13521 40573). */
3ea89b92
PMR
13522 child_origin_die = child_die;
13523 child_origin_cu = cu;
c38f313d
DJ
13524 while (1)
13525 {
cd02d79d
PA
13526 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13527 child_origin_cu);
c38f313d
DJ
13528 if (attr == NULL)
13529 break;
cd02d79d
PA
13530 child_origin_die = follow_die_ref (child_origin_die, attr,
13531 &child_origin_cu);
c38f313d
DJ
13532 }
13533
d389af10
JK
13534 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13535 counterpart may exist. */
c38f313d 13536 if (child_origin_die != child_die)
d389af10 13537 {
edb3359d
DJ
13538 if (child_die->tag != child_origin_die->tag
13539 && !(child_die->tag == DW_TAG_inlined_subroutine
13540 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 13541 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13542 "different tags"),
9d8780f0
SM
13543 sect_offset_str (child_die->sect_off),
13544 sect_offset_str (child_origin_die->sect_off));
c38f313d 13545 if (child_origin_die->parent != origin_die)
b98664d3 13546 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13547 "different parents"),
9d8780f0
SM
13548 sect_offset_str (child_die->sect_off),
13549 sect_offset_str (child_origin_die->sect_off));
c38f313d 13550 else
791afaa2 13551 offsets.push_back (child_origin_die->sect_off);
d389af10 13552 }
d389af10 13553 }
791afaa2
TT
13554 std::sort (offsets.begin (), offsets.end ());
13555 sect_offset *offsets_end = offsets.data () + offsets.size ();
13556 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13557 if (offsetp[-1] == *offsetp)
b98664d3 13558 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
13559 "to DIE %s as their abstract origin"),
13560 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13561
791afaa2 13562 offsetp = offsets.data ();
d389af10
JK
13563 origin_child_die = origin_die->child;
13564 while (origin_child_die && origin_child_die->tag)
13565 {
13566 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13567 while (offsetp < offsets_end
9c541725 13568 && *offsetp < origin_child_die->sect_off)
d389af10 13569 offsetp++;
b64f50a1 13570 if (offsetp >= offsets_end
9c541725 13571 || *offsetp > origin_child_die->sect_off)
d389af10 13572 {
adde2bff
DE
13573 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13574 Check whether we're already processing ORIGIN_CHILD_DIE.
13575 This can happen with mutually referenced abstract_origins.
13576 PR 16581. */
13577 if (!origin_child_die->in_process)
13578 process_die (origin_child_die, origin_cu);
d389af10
JK
13579 }
13580 origin_child_die = sibling_die (origin_child_die);
13581 }
cd02d79d 13582 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
13583}
13584
c906108c 13585static void
e7c27a73 13586read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13587{
518817b3 13588 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13589 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 13590 struct context_stack *newobj;
c906108c
SS
13591 CORE_ADDR lowpc;
13592 CORE_ADDR highpc;
13593 struct die_info *child_die;
edb3359d 13594 struct attribute *attr, *call_line, *call_file;
15d034d0 13595 const char *name;
e142c38c 13596 CORE_ADDR baseaddr;
801e3a5b 13597 struct block *block;
edb3359d 13598 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13599 std::vector<struct symbol *> template_args;
34eaf542 13600 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13601
13602 if (inlined_func)
13603 {
13604 /* If we do not have call site information, we can't show the
13605 caller of this inlined function. That's too confusing, so
13606 only use the scope for local variables. */
13607 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13608 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13609 if (call_line == NULL || call_file == NULL)
13610 {
13611 read_lexical_block_scope (die, cu);
13612 return;
13613 }
13614 }
c906108c 13615
e142c38c
DJ
13616 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13617
94af9270 13618 name = dwarf2_name (die, cu);
c906108c 13619
e8d05480
JB
13620 /* Ignore functions with missing or empty names. These are actually
13621 illegal according to the DWARF standard. */
13622 if (name == NULL)
13623 {
b98664d3 13624 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13625 sect_offset_str (die->sect_off));
e8d05480
JB
13626 return;
13627 }
13628
13629 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13630 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13631 <= PC_BOUNDS_INVALID)
e8d05480 13632 {
ae4d0c03
PM
13633 attr = dwarf2_attr (die, DW_AT_external, cu);
13634 if (!attr || !DW_UNSND (attr))
b98664d3 13635 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13636 "for subprogram DIE at %s"),
13637 sect_offset_str (die->sect_off));
e8d05480
JB
13638 return;
13639 }
c906108c 13640
3e29f34a
MR
13641 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13642 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13643
34eaf542
TT
13644 /* If we have any template arguments, then we must allocate a
13645 different sort of symbol. */
13646 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13647 {
13648 if (child_die->tag == DW_TAG_template_type_param
13649 || child_die->tag == DW_TAG_template_value_param)
13650 {
e623cf5d 13651 templ_func = allocate_template_symbol (objfile);
cf724bc9 13652 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13653 break;
13654 }
13655 }
13656
804d2729 13657 newobj = cu->builder->push_context (0, lowpc);
5e2db402
TT
13658 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13659 (struct symbol *) templ_func);
4c2df51b 13660
4cecd739
DJ
13661 /* If there is a location expression for DW_AT_frame_base, record
13662 it. */
e142c38c 13663 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 13664 if (attr)
fe978cb0 13665 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13666
63e43d3a
PMR
13667 /* If there is a location for the static link, record it. */
13668 newobj->static_link = NULL;
13669 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13670 if (attr)
13671 {
224c3ddb
SM
13672 newobj->static_link
13673 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
63e43d3a
PMR
13674 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
13675 }
13676
804d2729 13677 cu->list_in_scope = cu->builder->get_local_symbols ();
c906108c 13678
639d11d3 13679 if (die->child != NULL)
c906108c 13680 {
639d11d3 13681 child_die = die->child;
c906108c
SS
13682 while (child_die && child_die->tag)
13683 {
34eaf542
TT
13684 if (child_die->tag == DW_TAG_template_type_param
13685 || child_die->tag == DW_TAG_template_value_param)
13686 {
13687 struct symbol *arg = new_symbol (child_die, NULL, cu);
13688
f1078f66 13689 if (arg != NULL)
2f4732b0 13690 template_args.push_back (arg);
34eaf542
TT
13691 }
13692 else
13693 process_die (child_die, cu);
c906108c
SS
13694 child_die = sibling_die (child_die);
13695 }
13696 }
13697
d389af10
JK
13698 inherit_abstract_dies (die, cu);
13699
4a811a97
UW
13700 /* If we have a DW_AT_specification, we might need to import using
13701 directives from the context of the specification DIE. See the
13702 comment in determine_prefix. */
13703 if (cu->language == language_cplus
13704 && dwarf2_attr (die, DW_AT_specification, cu))
13705 {
13706 struct dwarf2_cu *spec_cu = cu;
13707 struct die_info *spec_die = die_specification (die, &spec_cu);
13708
13709 while (spec_die)
13710 {
13711 child_die = spec_die->child;
13712 while (child_die && child_die->tag)
13713 {
13714 if (child_die->tag == DW_TAG_imported_module)
13715 process_die (child_die, spec_cu);
13716 child_die = sibling_die (child_die);
13717 }
13718
13719 /* In some cases, GCC generates specification DIEs that
13720 themselves contain DW_AT_specification attributes. */
13721 spec_die = die_specification (spec_die, &spec_cu);
13722 }
13723 }
13724
804d2729 13725 struct context_stack cstk = cu->builder->pop_context ();
c906108c 13726 /* Make a block for the local symbols within. */
804d2729
TT
13727 block = cu->builder->finish_block (cstk.name, cstk.old_blocks,
13728 cstk.static_link, lowpc, highpc);
801e3a5b 13729
df8a16a1 13730 /* For C++, set the block's scope. */
45280282
IB
13731 if ((cu->language == language_cplus
13732 || cu->language == language_fortran
c44af4eb
TT
13733 || cu->language == language_d
13734 || cu->language == language_rust)
4d4ec4e5 13735 && cu->processing_has_namespace_info)
195a3f6c
TT
13736 block_set_scope (block, determine_prefix (die, cu),
13737 &objfile->objfile_obstack);
df8a16a1 13738
801e3a5b
JB
13739 /* If we have address ranges, record them. */
13740 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13741
a60f3166 13742 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13743
34eaf542 13744 /* Attach template arguments to function. */
2f4732b0 13745 if (!template_args.empty ())
34eaf542
TT
13746 {
13747 gdb_assert (templ_func != NULL);
13748
2f4732b0 13749 templ_func->n_template_arguments = template_args.size ();
34eaf542 13750 templ_func->template_arguments
8d749320
SM
13751 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13752 templ_func->n_template_arguments);
34eaf542 13753 memcpy (templ_func->template_arguments,
2f4732b0 13754 template_args.data (),
34eaf542 13755 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13756
13757 /* Make sure that the symtab is set on the new symbols. Even
13758 though they don't appear in this symtab directly, other parts
13759 of gdb assume that symbols do, and this is reasonably
13760 true. */
13761 for (struct symbol *sym : template_args)
13762 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13763 }
13764
208d8187
JB
13765 /* In C++, we can have functions nested inside functions (e.g., when
13766 a function declares a class that has methods). This means that
13767 when we finish processing a function scope, we may need to go
13768 back to building a containing block's symbol lists. */
804d2729
TT
13769 *cu->builder->get_local_symbols () = cstk.locals;
13770 cu->builder->set_local_using_directives (cstk.local_using_directives);
208d8187 13771
921e78cf
JB
13772 /* If we've finished processing a top-level function, subsequent
13773 symbols go in the file symbol list. */
804d2729
TT
13774 if (cu->builder->outermost_context_p ())
13775 cu->list_in_scope = cu->builder->get_file_symbols ();
c906108c
SS
13776}
13777
13778/* Process all the DIES contained within a lexical block scope. Start
13779 a new scope, process the dies, and then close the scope. */
13780
13781static void
e7c27a73 13782read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13783{
518817b3 13784 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13785 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
13786 CORE_ADDR lowpc, highpc;
13787 struct die_info *child_die;
e142c38c
DJ
13788 CORE_ADDR baseaddr;
13789
13790 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
13791
13792 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13793 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13794 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13795 be nasty. Might be easier to properly extend generic blocks to
af34e669 13796 describe ranges. */
e385593e
JK
13797 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13798 {
13799 case PC_BOUNDS_NOT_PRESENT:
13800 /* DW_TAG_lexical_block has no attributes, process its children as if
13801 there was no wrapping by that DW_TAG_lexical_block.
13802 GCC does no longer produces such DWARF since GCC r224161. */
13803 for (child_die = die->child;
13804 child_die != NULL && child_die->tag;
13805 child_die = sibling_die (child_die))
13806 process_die (child_die, cu);
13807 return;
13808 case PC_BOUNDS_INVALID:
13809 return;
13810 }
3e29f34a
MR
13811 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13812 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13813
804d2729 13814 cu->builder->push_context (0, lowpc);
639d11d3 13815 if (die->child != NULL)
c906108c 13816 {
639d11d3 13817 child_die = die->child;
c906108c
SS
13818 while (child_die && child_die->tag)
13819 {
e7c27a73 13820 process_die (child_die, cu);
c906108c
SS
13821 child_die = sibling_die (child_die);
13822 }
13823 }
3ea89b92 13824 inherit_abstract_dies (die, cu);
804d2729 13825 struct context_stack cstk = cu->builder->pop_context ();
c906108c 13826
804d2729
TT
13827 if (*cu->builder->get_local_symbols () != NULL
13828 || (*cu->builder->get_local_using_directives ()) != NULL)
c906108c 13829 {
801e3a5b 13830 struct block *block
804d2729
TT
13831 = cu->builder->finish_block (0, cstk.old_blocks, NULL,
13832 cstk.start_addr, highpc);
801e3a5b
JB
13833
13834 /* Note that recording ranges after traversing children, as we
13835 do here, means that recording a parent's ranges entails
13836 walking across all its children's ranges as they appear in
13837 the address map, which is quadratic behavior.
13838
13839 It would be nicer to record the parent's ranges before
13840 traversing its children, simply overriding whatever you find
13841 there. But since we don't even decide whether to create a
13842 block until after we've traversed its children, that's hard
13843 to do. */
13844 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13845 }
804d2729
TT
13846 *cu->builder->get_local_symbols () = cstk.locals;
13847 cu->builder->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13848}
13849
216f72a1 13850/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13851
13852static void
13853read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13854{
518817b3 13855 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
96408a79
SA
13856 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13857 CORE_ADDR pc, baseaddr;
13858 struct attribute *attr;
13859 struct call_site *call_site, call_site_local;
13860 void **slot;
13861 int nparams;
13862 struct die_info *child_die;
13863
13864 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13865
216f72a1
JK
13866 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13867 if (attr == NULL)
13868 {
13869 /* This was a pre-DWARF-5 GNU extension alias
13870 for DW_AT_call_return_pc. */
13871 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13872 }
96408a79
SA
13873 if (!attr)
13874 {
b98664d3 13875 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13876 "DIE %s [in module %s]"),
13877 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13878 return;
13879 }
31aa7e4e 13880 pc = attr_value_as_address (attr) + baseaddr;
3e29f34a 13881 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13882
13883 if (cu->call_site_htab == NULL)
13884 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13885 NULL, &objfile->objfile_obstack,
13886 hashtab_obstack_allocate, NULL);
13887 call_site_local.pc = pc;
13888 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13889 if (*slot != NULL)
13890 {
b98664d3 13891 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13892 "DIE %s [in module %s]"),
13893 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13894 objfile_name (objfile));
96408a79
SA
13895 return;
13896 }
13897
13898 /* Count parameters at the caller. */
13899
13900 nparams = 0;
13901 for (child_die = die->child; child_die && child_die->tag;
13902 child_die = sibling_die (child_die))
13903 {
216f72a1
JK
13904 if (child_die->tag != DW_TAG_call_site_parameter
13905 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13906 {
b98664d3 13907 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13908 "DW_TAG_call_site child DIE %s [in module %s]"),
13909 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13910 objfile_name (objfile));
96408a79
SA
13911 continue;
13912 }
13913
13914 nparams++;
13915 }
13916
224c3ddb
SM
13917 call_site
13918 = ((struct call_site *)
13919 obstack_alloc (&objfile->objfile_obstack,
13920 sizeof (*call_site)
13921 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13922 *slot = call_site;
13923 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13924 call_site->pc = pc;
13925
216f72a1
JK
13926 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13927 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13928 {
13929 struct die_info *func_die;
13930
13931 /* Skip also over DW_TAG_inlined_subroutine. */
13932 for (func_die = die->parent;
13933 func_die && func_die->tag != DW_TAG_subprogram
13934 && func_die->tag != DW_TAG_subroutine_type;
13935 func_die = func_die->parent);
13936
216f72a1
JK
13937 /* DW_AT_call_all_calls is a superset
13938 of DW_AT_call_all_tail_calls. */
96408a79 13939 if (func_die
216f72a1 13940 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 13941 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13942 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13943 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13944 {
13945 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13946 not complete. But keep CALL_SITE for look ups via call_site_htab,
13947 both the initial caller containing the real return address PC and
13948 the final callee containing the current PC of a chain of tail
13949 calls do not need to have the tail call list complete. But any
13950 function candidate for a virtual tail call frame searched via
13951 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13952 determined unambiguously. */
13953 }
13954 else
13955 {
13956 struct type *func_type = NULL;
13957
13958 if (func_die)
13959 func_type = get_die_type (func_die, cu);
13960 if (func_type != NULL)
13961 {
13962 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13963
13964 /* Enlist this call site to the function. */
13965 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13966 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13967 }
13968 else
b98664d3 13969 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13970 "DIE %s [in module %s]"),
13971 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13972 }
13973 }
13974
216f72a1
JK
13975 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13976 if (attr == NULL)
13977 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13978 if (attr == NULL)
13979 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13980 if (attr == NULL)
216f72a1
JK
13981 {
13982 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13983 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13984 }
96408a79
SA
13985 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13986 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
13987 /* Keep NULL DWARF_BLOCK. */;
13988 else if (attr_form_is_block (attr))
13989 {
13990 struct dwarf2_locexpr_baton *dlbaton;
13991
8d749320 13992 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
13993 dlbaton->data = DW_BLOCK (attr)->data;
13994 dlbaton->size = DW_BLOCK (attr)->size;
13995 dlbaton->per_cu = cu->per_cu;
13996
13997 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13998 }
7771576e 13999 else if (attr_form_is_ref (attr))
96408a79 14000 {
96408a79
SA
14001 struct dwarf2_cu *target_cu = cu;
14002 struct die_info *target_die;
14003
ac9ec31b 14004 target_die = follow_die_ref (die, attr, &target_cu);
518817b3 14005 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
96408a79
SA
14006 if (die_is_declaration (target_die, target_cu))
14007 {
7d45c7c3 14008 const char *target_physname;
9112db09
JK
14009
14010 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 14011 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 14012 if (target_physname == NULL)
9112db09 14013 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 14014 if (target_physname == NULL)
b98664d3 14015 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
14016 "physname, for referencing DIE %s [in module %s]"),
14017 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14018 else
7d455152 14019 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
14020 }
14021 else
14022 {
14023 CORE_ADDR lowpc;
14024
14025 /* DW_AT_entry_pc should be preferred. */
3a2b436a 14026 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 14027 <= PC_BOUNDS_INVALID)
b98664d3 14028 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
14029 "low pc, for referencing DIE %s [in module %s]"),
14030 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14031 else
3e29f34a
MR
14032 {
14033 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14034 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14035 }
96408a79
SA
14036 }
14037 }
14038 else
b98664d3 14039 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
14040 "block nor reference, for DIE %s [in module %s]"),
14041 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14042
14043 call_site->per_cu = cu->per_cu;
14044
14045 for (child_die = die->child;
14046 child_die && child_die->tag;
14047 child_die = sibling_die (child_die))
14048 {
96408a79 14049 struct call_site_parameter *parameter;
1788b2d3 14050 struct attribute *loc, *origin;
96408a79 14051
216f72a1
JK
14052 if (child_die->tag != DW_TAG_call_site_parameter
14053 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
14054 {
14055 /* Already printed the complaint above. */
14056 continue;
14057 }
14058
14059 gdb_assert (call_site->parameter_count < nparams);
14060 parameter = &call_site->parameter[call_site->parameter_count];
14061
1788b2d3
JK
14062 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14063 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 14064 register is contained in DW_AT_call_value. */
96408a79 14065
24c5c679 14066 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
14067 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14068 if (origin == NULL)
14069 {
14070 /* This was a pre-DWARF-5 GNU extension alias
14071 for DW_AT_call_parameter. */
14072 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14073 }
7771576e 14074 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3 14075 {
1788b2d3 14076 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
14077
14078 sect_offset sect_off
14079 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14080 if (!offset_in_cu_p (&cu->header, sect_off))
d76b7dbc
JK
14081 {
14082 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14083 binding can be done only inside one CU. Such referenced DIE
14084 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 14085 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
14086 "DW_TAG_call_site child DIE %s [in module %s]"),
14087 sect_offset_str (child_die->sect_off),
9c541725 14088 objfile_name (objfile));
d76b7dbc
JK
14089 continue;
14090 }
9c541725
PA
14091 parameter->u.param_cu_off
14092 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3
JK
14093 }
14094 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79 14095 {
b98664d3 14096 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
14097 "DW_TAG_call_site child DIE %s [in module %s]"),
14098 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
14099 continue;
14100 }
24c5c679 14101 else
96408a79 14102 {
24c5c679
JK
14103 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14104 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14105 if (parameter->u.dwarf_reg != -1)
14106 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14107 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14108 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14109 &parameter->u.fb_offset))
14110 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14111 else
14112 {
b98664d3 14113 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 14114 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 14115 "DW_TAG_call_site child DIE %s "
24c5c679 14116 "[in module %s]"),
9d8780f0 14117 sect_offset_str (child_die->sect_off),
9c541725 14118 objfile_name (objfile));
24c5c679
JK
14119 continue;
14120 }
96408a79
SA
14121 }
14122
216f72a1
JK
14123 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14124 if (attr == NULL)
14125 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
96408a79
SA
14126 if (!attr_form_is_block (attr))
14127 {
b98664d3 14128 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
14129 "DW_TAG_call_site child DIE %s [in module %s]"),
14130 sect_offset_str (child_die->sect_off),
9c541725 14131 objfile_name (objfile));
96408a79
SA
14132 continue;
14133 }
14134 parameter->value = DW_BLOCK (attr)->data;
14135 parameter->value_size = DW_BLOCK (attr)->size;
14136
14137 /* Parameters are not pre-cleared by memset above. */
14138 parameter->data_value = NULL;
14139 parameter->data_value_size = 0;
14140 call_site->parameter_count++;
14141
216f72a1
JK
14142 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14143 if (attr == NULL)
14144 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
96408a79
SA
14145 if (attr)
14146 {
14147 if (!attr_form_is_block (attr))
b98664d3 14148 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
14149 "DW_TAG_call_site child DIE %s [in module %s]"),
14150 sect_offset_str (child_die->sect_off),
9c541725 14151 objfile_name (objfile));
96408a79
SA
14152 else
14153 {
14154 parameter->data_value = DW_BLOCK (attr)->data;
14155 parameter->data_value_size = DW_BLOCK (attr)->size;
14156 }
14157 }
14158 }
14159}
14160
71a3c369
TT
14161/* Helper function for read_variable. If DIE represents a virtual
14162 table, then return the type of the concrete object that is
14163 associated with the virtual table. Otherwise, return NULL. */
14164
14165static struct type *
14166rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14167{
14168 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14169 if (attr == NULL)
14170 return NULL;
14171
14172 /* Find the type DIE. */
14173 struct die_info *type_die = NULL;
14174 struct dwarf2_cu *type_cu = cu;
14175
14176 if (attr_form_is_ref (attr))
14177 type_die = follow_die_ref (die, attr, &type_cu);
14178 if (type_die == NULL)
14179 return NULL;
14180
14181 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14182 return NULL;
14183 return die_containing_type (type_die, type_cu);
14184}
14185
14186/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14187
14188static void
14189read_variable (struct die_info *die, struct dwarf2_cu *cu)
14190{
14191 struct rust_vtable_symbol *storage = NULL;
14192
14193 if (cu->language == language_rust)
14194 {
14195 struct type *containing_type = rust_containing_type (die, cu);
14196
14197 if (containing_type != NULL)
14198 {
518817b3 14199 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
71a3c369
TT
14200
14201 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14202 struct rust_vtable_symbol);
14203 initialize_objfile_symbol (storage);
14204 storage->concrete_type = containing_type;
cf724bc9 14205 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
14206 }
14207 }
14208
5e2db402 14209 new_symbol (die, NULL, cu, storage);
71a3c369
TT
14210}
14211
43988095
JK
14212/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14213 reading .debug_rnglists.
14214 Callback's type should be:
14215 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14216 Return true if the attributes are present and valid, otherwise,
14217 return false. */
14218
14219template <typename Callback>
14220static bool
14221dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14222 Callback &&callback)
14223{
ed2dc618 14224 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14225 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14226 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 14227 bfd *obfd = objfile->obfd;
43988095
JK
14228 /* Base address selection entry. */
14229 CORE_ADDR base;
14230 int found_base;
43988095 14231 const gdb_byte *buffer;
43988095
JK
14232 CORE_ADDR baseaddr;
14233 bool overflow = false;
14234
14235 found_base = cu->base_known;
14236 base = cu->base_address;
14237
14238 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14239 if (offset >= dwarf2_per_objfile->rnglists.size)
14240 {
b98664d3 14241 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
14242 offset);
14243 return false;
14244 }
14245 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14246
14247 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14248
14249 while (1)
14250 {
7814882a
JK
14251 /* Initialize it due to a false compiler warning. */
14252 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
14253 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14254 + dwarf2_per_objfile->rnglists.size);
14255 unsigned int bytes_read;
14256
14257 if (buffer == buf_end)
14258 {
14259 overflow = true;
14260 break;
14261 }
14262 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14263 switch (rlet)
14264 {
14265 case DW_RLE_end_of_list:
14266 break;
14267 case DW_RLE_base_address:
14268 if (buffer + cu->header.addr_size > buf_end)
14269 {
14270 overflow = true;
14271 break;
14272 }
14273 base = read_address (obfd, buffer, cu, &bytes_read);
14274 found_base = 1;
14275 buffer += bytes_read;
14276 break;
14277 case DW_RLE_start_length:
14278 if (buffer + cu->header.addr_size > buf_end)
14279 {
14280 overflow = true;
14281 break;
14282 }
14283 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14284 buffer += bytes_read;
14285 range_end = (range_beginning
14286 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14287 buffer += bytes_read;
14288 if (buffer > buf_end)
14289 {
14290 overflow = true;
14291 break;
14292 }
14293 break;
14294 case DW_RLE_offset_pair:
14295 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14296 buffer += bytes_read;
14297 if (buffer > buf_end)
14298 {
14299 overflow = true;
14300 break;
14301 }
14302 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14303 buffer += bytes_read;
14304 if (buffer > buf_end)
14305 {
14306 overflow = true;
14307 break;
14308 }
14309 break;
14310 case DW_RLE_start_end:
14311 if (buffer + 2 * cu->header.addr_size > buf_end)
14312 {
14313 overflow = true;
14314 break;
14315 }
14316 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14317 buffer += bytes_read;
14318 range_end = read_address (obfd, buffer, cu, &bytes_read);
14319 buffer += bytes_read;
14320 break;
14321 default:
b98664d3 14322 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14323 return false;
14324 }
14325 if (rlet == DW_RLE_end_of_list || overflow)
14326 break;
14327 if (rlet == DW_RLE_base_address)
14328 continue;
14329
14330 if (!found_base)
14331 {
14332 /* We have no valid base address for the ranges
14333 data. */
b98664d3 14334 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14335 return false;
14336 }
14337
14338 if (range_beginning > range_end)
14339 {
14340 /* Inverted range entries are invalid. */
b98664d3 14341 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
14342 return false;
14343 }
14344
14345 /* Empty range entries have no effect. */
14346 if (range_beginning == range_end)
14347 continue;
14348
14349 range_beginning += base;
14350 range_end += base;
14351
14352 /* A not-uncommon case of bad debug info.
14353 Don't pollute the addrmap with bad data. */
14354 if (range_beginning + baseaddr == 0
14355 && !dwarf2_per_objfile->has_section_at_zero)
14356 {
b98664d3 14357 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
14358 " [in module %s]"), objfile_name (objfile));
14359 continue;
14360 }
14361
14362 callback (range_beginning, range_end);
14363 }
14364
14365 if (overflow)
14366 {
b98664d3 14367 complaint (_("Offset %d is not terminated "
43988095
JK
14368 "for DW_AT_ranges attribute"),
14369 offset);
14370 return false;
14371 }
14372
14373 return true;
14374}
14375
14376/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14377 Callback's type should be:
14378 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 14379 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 14380
43988095 14381template <typename Callback>
43039443 14382static int
5f46c5a5 14383dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 14384 Callback &&callback)
43039443 14385{
ed2dc618 14386 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14387 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14388 struct objfile *objfile = dwarf2_per_objfile->objfile;
43039443
JK
14389 struct comp_unit_head *cu_header = &cu->header;
14390 bfd *obfd = objfile->obfd;
14391 unsigned int addr_size = cu_header->addr_size;
14392 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14393 /* Base address selection entry. */
14394 CORE_ADDR base;
14395 int found_base;
14396 unsigned int dummy;
d521ce57 14397 const gdb_byte *buffer;
ff013f42 14398 CORE_ADDR baseaddr;
43039443 14399
43988095
JK
14400 if (cu_header->version >= 5)
14401 return dwarf2_rnglists_process (offset, cu, callback);
14402
d00adf39
DE
14403 found_base = cu->base_known;
14404 base = cu->base_address;
43039443 14405
be391dca 14406 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 14407 if (offset >= dwarf2_per_objfile->ranges.size)
43039443 14408 {
b98664d3 14409 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
14410 offset);
14411 return 0;
14412 }
dce234bc 14413 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 14414
e7030f15 14415 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 14416
43039443
JK
14417 while (1)
14418 {
14419 CORE_ADDR range_beginning, range_end;
14420
14421 range_beginning = read_address (obfd, buffer, cu, &dummy);
14422 buffer += addr_size;
14423 range_end = read_address (obfd, buffer, cu, &dummy);
14424 buffer += addr_size;
14425 offset += 2 * addr_size;
14426
14427 /* An end of list marker is a pair of zero addresses. */
14428 if (range_beginning == 0 && range_end == 0)
14429 /* Found the end of list entry. */
14430 break;
14431
14432 /* Each base address selection entry is a pair of 2 values.
14433 The first is the largest possible address, the second is
14434 the base address. Check for a base address here. */
14435 if ((range_beginning & mask) == mask)
14436 {
28d2bfb9
AB
14437 /* If we found the largest possible address, then we already
14438 have the base address in range_end. */
14439 base = range_end;
43039443
JK
14440 found_base = 1;
14441 continue;
14442 }
14443
14444 if (!found_base)
14445 {
14446 /* We have no valid base address for the ranges
14447 data. */
b98664d3 14448 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
14449 return 0;
14450 }
14451
9277c30c
UW
14452 if (range_beginning > range_end)
14453 {
14454 /* Inverted range entries are invalid. */
b98664d3 14455 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
14456 return 0;
14457 }
14458
14459 /* Empty range entries have no effect. */
14460 if (range_beginning == range_end)
14461 continue;
14462
43039443
JK
14463 range_beginning += base;
14464 range_end += base;
14465
01093045
DE
14466 /* A not-uncommon case of bad debug info.
14467 Don't pollute the addrmap with bad data. */
14468 if (range_beginning + baseaddr == 0
14469 && !dwarf2_per_objfile->has_section_at_zero)
14470 {
b98664d3 14471 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 14472 " [in module %s]"), objfile_name (objfile));
01093045
DE
14473 continue;
14474 }
14475
5f46c5a5
JK
14476 callback (range_beginning, range_end);
14477 }
14478
14479 return 1;
14480}
14481
14482/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14483 Return 1 if the attributes are present and valid, otherwise, return 0.
14484 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14485
14486static int
14487dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14488 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14489 struct partial_symtab *ranges_pst)
14490{
518817b3 14491 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5f46c5a5
JK
14492 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14493 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14494 SECT_OFF_TEXT (objfile));
14495 int low_set = 0;
14496 CORE_ADDR low = 0;
14497 CORE_ADDR high = 0;
14498 int retval;
14499
14500 retval = dwarf2_ranges_process (offset, cu,
14501 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14502 {
9277c30c 14503 if (ranges_pst != NULL)
3e29f34a
MR
14504 {
14505 CORE_ADDR lowpc;
14506 CORE_ADDR highpc;
14507
79748972
TT
14508 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14509 range_beginning + baseaddr)
14510 - baseaddr);
14511 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14512 range_end + baseaddr)
14513 - baseaddr);
3e29f34a
MR
14514 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
14515 ranges_pst);
14516 }
ff013f42 14517
43039443
JK
14518 /* FIXME: This is recording everything as a low-high
14519 segment of consecutive addresses. We should have a
14520 data structure for discontiguous block ranges
14521 instead. */
14522 if (! low_set)
14523 {
14524 low = range_beginning;
14525 high = range_end;
14526 low_set = 1;
14527 }
14528 else
14529 {
14530 if (range_beginning < low)
14531 low = range_beginning;
14532 if (range_end > high)
14533 high = range_end;
14534 }
5f46c5a5
JK
14535 });
14536 if (!retval)
14537 return 0;
43039443
JK
14538
14539 if (! low_set)
14540 /* If the first entry is an end-of-list marker, the range
14541 describes an empty scope, i.e. no instructions. */
14542 return 0;
14543
14544 if (low_return)
14545 *low_return = low;
14546 if (high_return)
14547 *high_return = high;
14548 return 1;
14549}
14550
3a2b436a
JK
14551/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14552 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14553 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14554
3a2b436a 14555static enum pc_bounds_kind
af34e669 14556dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
14557 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14558 struct partial_symtab *pst)
c906108c 14559{
518817b3
SM
14560 struct dwarf2_per_objfile *dwarf2_per_objfile
14561 = cu->per_cu->dwarf2_per_objfile;
c906108c 14562 struct attribute *attr;
91da1414 14563 struct attribute *attr_high;
af34e669
DJ
14564 CORE_ADDR low = 0;
14565 CORE_ADDR high = 0;
e385593e 14566 enum pc_bounds_kind ret;
c906108c 14567
91da1414
MW
14568 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14569 if (attr_high)
af34e669 14570 {
e142c38c 14571 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 14572 if (attr)
91da1414 14573 {
31aa7e4e
JB
14574 low = attr_value_as_address (attr);
14575 high = attr_value_as_address (attr_high);
14576 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14577 high += low;
91da1414 14578 }
af34e669
DJ
14579 else
14580 /* Found high w/o low attribute. */
e385593e 14581 return PC_BOUNDS_INVALID;
af34e669
DJ
14582
14583 /* Found consecutive range of addresses. */
3a2b436a 14584 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14585 }
c906108c 14586 else
af34e669 14587 {
e142c38c 14588 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
14589 if (attr != NULL)
14590 {
ab435259
DE
14591 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14592 We take advantage of the fact that DW_AT_ranges does not appear
14593 in DW_TAG_compile_unit of DWO files. */
14594 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14595 unsigned int ranges_offset = (DW_UNSND (attr)
14596 + (need_ranges_base
14597 ? cu->ranges_base
14598 : 0));
2e3cf129 14599
af34e669 14600 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14601 .debug_ranges section. */
2e3cf129 14602 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 14603 return PC_BOUNDS_INVALID;
43039443 14604 /* Found discontinuous range of addresses. */
3a2b436a 14605 ret = PC_BOUNDS_RANGES;
af34e669 14606 }
e385593e
JK
14607 else
14608 return PC_BOUNDS_NOT_PRESENT;
af34e669 14609 }
c906108c 14610
48fbe735 14611 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14612 if (high <= low)
e385593e 14613 return PC_BOUNDS_INVALID;
c906108c
SS
14614
14615 /* When using the GNU linker, .gnu.linkonce. sections are used to
14616 eliminate duplicate copies of functions and vtables and such.
14617 The linker will arbitrarily choose one and discard the others.
14618 The AT_*_pc values for such functions refer to local labels in
14619 these sections. If the section from that file was discarded, the
14620 labels are not in the output, so the relocs get a value of 0.
14621 If this is a discarded function, mark the pc bounds as invalid,
14622 so that GDB will ignore it. */
72dca2f5 14623 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 14624 return PC_BOUNDS_INVALID;
c906108c
SS
14625
14626 *lowpc = low;
96408a79
SA
14627 if (highpc)
14628 *highpc = high;
af34e669 14629 return ret;
c906108c
SS
14630}
14631
b084d499
JB
14632/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14633 its low and high PC addresses. Do nothing if these addresses could not
14634 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14635 and HIGHPC to the high address if greater than HIGHPC. */
14636
14637static void
14638dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14639 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14640 struct dwarf2_cu *cu)
14641{
14642 CORE_ADDR low, high;
14643 struct die_info *child = die->child;
14644
e385593e 14645 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14646 {
325fac50
PA
14647 *lowpc = std::min (*lowpc, low);
14648 *highpc = std::max (*highpc, high);
b084d499
JB
14649 }
14650
14651 /* If the language does not allow nested subprograms (either inside
14652 subprograms or lexical blocks), we're done. */
14653 if (cu->language != language_ada)
14654 return;
6e70227d 14655
b084d499
JB
14656 /* Check all the children of the given DIE. If it contains nested
14657 subprograms, then check their pc bounds. Likewise, we need to
14658 check lexical blocks as well, as they may also contain subprogram
14659 definitions. */
14660 while (child && child->tag)
14661 {
14662 if (child->tag == DW_TAG_subprogram
14663 || child->tag == DW_TAG_lexical_block)
14664 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14665 child = sibling_die (child);
14666 }
14667}
14668
fae299cd
DC
14669/* Get the low and high pc's represented by the scope DIE, and store
14670 them in *LOWPC and *HIGHPC. If the correct values can't be
14671 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14672
14673static void
14674get_scope_pc_bounds (struct die_info *die,
14675 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14676 struct dwarf2_cu *cu)
14677{
14678 CORE_ADDR best_low = (CORE_ADDR) -1;
14679 CORE_ADDR best_high = (CORE_ADDR) 0;
14680 CORE_ADDR current_low, current_high;
14681
3a2b436a 14682 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14683 >= PC_BOUNDS_RANGES)
fae299cd
DC
14684 {
14685 best_low = current_low;
14686 best_high = current_high;
14687 }
14688 else
14689 {
14690 struct die_info *child = die->child;
14691
14692 while (child && child->tag)
14693 {
14694 switch (child->tag) {
14695 case DW_TAG_subprogram:
b084d499 14696 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14697 break;
14698 case DW_TAG_namespace:
f55ee35c 14699 case DW_TAG_module:
fae299cd
DC
14700 /* FIXME: carlton/2004-01-16: Should we do this for
14701 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14702 that current GCC's always emit the DIEs corresponding
14703 to definitions of methods of classes as children of a
14704 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14705 the DIEs giving the declarations, which could be
14706 anywhere). But I don't see any reason why the
14707 standards says that they have to be there. */
14708 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14709
14710 if (current_low != ((CORE_ADDR) -1))
14711 {
325fac50
PA
14712 best_low = std::min (best_low, current_low);
14713 best_high = std::max (best_high, current_high);
fae299cd
DC
14714 }
14715 break;
14716 default:
0963b4bd 14717 /* Ignore. */
fae299cd
DC
14718 break;
14719 }
14720
14721 child = sibling_die (child);
14722 }
14723 }
14724
14725 *lowpc = best_low;
14726 *highpc = best_high;
14727}
14728
801e3a5b
JB
14729/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14730 in DIE. */
380bca97 14731
801e3a5b
JB
14732static void
14733dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14734 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14735{
518817b3 14736 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 14737 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 14738 struct attribute *attr;
91da1414 14739 struct attribute *attr_high;
801e3a5b 14740
91da1414
MW
14741 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14742 if (attr_high)
801e3a5b 14743 {
801e3a5b
JB
14744 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14745 if (attr)
14746 {
31aa7e4e
JB
14747 CORE_ADDR low = attr_value_as_address (attr);
14748 CORE_ADDR high = attr_value_as_address (attr_high);
14749
14750 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14751 high += low;
9a619af0 14752
3e29f34a
MR
14753 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14754 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
804d2729 14755 cu->builder->record_block_range (block, low, high - 1);
801e3a5b
JB
14756 }
14757 }
14758
14759 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14760 if (attr)
14761 {
ab435259
DE
14762 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14763 We take advantage of the fact that DW_AT_ranges does not appear
14764 in DW_TAG_compile_unit of DWO files. */
14765 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14766
14767 /* The value of the DW_AT_ranges attribute is the offset of the
14768 address range list in the .debug_ranges section. */
ab435259
DE
14769 unsigned long offset = (DW_UNSND (attr)
14770 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14771
5f46c5a5
JK
14772 dwarf2_ranges_process (offset, cu,
14773 [&] (CORE_ADDR start, CORE_ADDR end)
14774 {
58fdfd2c
JK
14775 start += baseaddr;
14776 end += baseaddr;
5f46c5a5
JK
14777 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14778 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
804d2729 14779 cu->builder->record_block_range (block, start, end - 1);
5f46c5a5 14780 });
801e3a5b
JB
14781 }
14782}
14783
685b1105
JK
14784/* Check whether the producer field indicates either of GCC < 4.6, or the
14785 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14786
685b1105
JK
14787static void
14788check_producer (struct dwarf2_cu *cu)
60d5a603 14789{
38360086 14790 int major, minor;
60d5a603
JK
14791
14792 if (cu->producer == NULL)
14793 {
14794 /* For unknown compilers expect their behavior is DWARF version
14795 compliant.
14796
14797 GCC started to support .debug_types sections by -gdwarf-4 since
14798 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14799 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14800 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14801 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14802 }
b1ffba5a 14803 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14804 {
38360086
MW
14805 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14806 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14807 }
5230b05a
WT
14808 else if (producer_is_icc (cu->producer, &major, &minor))
14809 cu->producer_is_icc_lt_14 = major < 14;
685b1105
JK
14810 else
14811 {
14812 /* For other non-GCC compilers, expect their behavior is DWARF version
14813 compliant. */
60d5a603
JK
14814 }
14815
ba919b58 14816 cu->checked_producer = 1;
685b1105 14817}
ba919b58 14818
685b1105
JK
14819/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14820 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14821 during 4.6.0 experimental. */
14822
14823static int
14824producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14825{
14826 if (!cu->checked_producer)
14827 check_producer (cu);
14828
14829 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14830}
14831
14832/* Return the default accessibility type if it is not overriden by
14833 DW_AT_accessibility. */
14834
14835static enum dwarf_access_attribute
14836dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14837{
14838 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14839 {
14840 /* The default DWARF 2 accessibility for members is public, the default
14841 accessibility for inheritance is private. */
14842
14843 if (die->tag != DW_TAG_inheritance)
14844 return DW_ACCESS_public;
14845 else
14846 return DW_ACCESS_private;
14847 }
14848 else
14849 {
14850 /* DWARF 3+ defines the default accessibility a different way. The same
14851 rules apply now for DW_TAG_inheritance as for the members and it only
14852 depends on the container kind. */
14853
14854 if (die->parent->tag == DW_TAG_class_type)
14855 return DW_ACCESS_private;
14856 else
14857 return DW_ACCESS_public;
14858 }
14859}
14860
74ac6d43
TT
14861/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14862 offset. If the attribute was not found return 0, otherwise return
14863 1. If it was found but could not properly be handled, set *OFFSET
14864 to 0. */
14865
14866static int
14867handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14868 LONGEST *offset)
14869{
14870 struct attribute *attr;
14871
14872 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14873 if (attr != NULL)
14874 {
14875 *offset = 0;
14876
14877 /* Note that we do not check for a section offset first here.
14878 This is because DW_AT_data_member_location is new in DWARF 4,
14879 so if we see it, we can assume that a constant form is really
14880 a constant and not a section offset. */
14881 if (attr_form_is_constant (attr))
14882 *offset = dwarf2_get_attr_constant_value (attr, 0);
14883 else if (attr_form_is_section_offset (attr))
14884 dwarf2_complex_location_expr_complaint ();
14885 else if (attr_form_is_block (attr))
14886 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14887 else
14888 dwarf2_complex_location_expr_complaint ();
14889
14890 return 1;
14891 }
14892
14893 return 0;
14894}
14895
c906108c
SS
14896/* Add an aggregate field to the field list. */
14897
14898static void
107d2387 14899dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14900 struct dwarf2_cu *cu)
6e70227d 14901{
518817b3 14902 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5e2b427d 14903 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
14904 struct nextfield *new_field;
14905 struct attribute *attr;
14906 struct field *fp;
15d034d0 14907 const char *fieldname = "";
c906108c 14908
7d0ccb61
DJ
14909 if (die->tag == DW_TAG_inheritance)
14910 {
be2daae6
TT
14911 fip->baseclasses.emplace_back ();
14912 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14913 }
14914 else
14915 {
be2daae6
TT
14916 fip->fields.emplace_back ();
14917 new_field = &fip->fields.back ();
7d0ccb61 14918 }
be2daae6 14919
c906108c
SS
14920 fip->nfields++;
14921
e142c38c 14922 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
14923 if (attr)
14924 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
14925 else
14926 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
14927 if (new_field->accessibility != DW_ACCESS_public)
14928 fip->non_public_fields = 1;
60d5a603 14929
e142c38c 14930 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
14931 if (attr)
14932 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
14933 else
14934 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
14935
14936 fp = &new_field->field;
a9a9bd0f 14937
e142c38c 14938 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 14939 {
74ac6d43
TT
14940 LONGEST offset;
14941
a9a9bd0f 14942 /* Data member other than a C++ static data member. */
6e70227d 14943
c906108c 14944 /* Get type of field. */
e7c27a73 14945 fp->type = die_type (die, cu);
c906108c 14946
d6a843b5 14947 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 14948
c906108c 14949 /* Get bit size of field (zero if none). */
e142c38c 14950 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
14951 if (attr)
14952 {
14953 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14954 }
14955 else
14956 {
14957 FIELD_BITSIZE (*fp) = 0;
14958 }
14959
14960 /* Get bit offset of field. */
74ac6d43
TT
14961 if (handle_data_member_location (die, cu, &offset))
14962 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 14963 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
14964 if (attr)
14965 {
5e2b427d 14966 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
14967 {
14968 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
14969 additional bit offset from the MSB of the containing
14970 anonymous object to the MSB of the field. We don't
14971 have to do anything special since we don't need to
14972 know the size of the anonymous object. */
f41f5e61 14973 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
14974 }
14975 else
14976 {
14977 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
14978 MSB of the anonymous object, subtract off the number of
14979 bits from the MSB of the field to the MSB of the
14980 object, and then subtract off the number of bits of
14981 the field itself. The result is the bit offset of
14982 the LSB of the field. */
c906108c
SS
14983 int anonymous_size;
14984 int bit_offset = DW_UNSND (attr);
14985
e142c38c 14986 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
14987 if (attr)
14988 {
14989 /* The size of the anonymous object containing
14990 the bit field is explicit, so use the
14991 indicated size (in bytes). */
14992 anonymous_size = DW_UNSND (attr);
14993 }
14994 else
14995 {
14996 /* The size of the anonymous object containing
14997 the bit field must be inferred from the type
14998 attribute of the data member containing the
14999 bit field. */
15000 anonymous_size = TYPE_LENGTH (fp->type);
15001 }
f41f5e61
PA
15002 SET_FIELD_BITPOS (*fp,
15003 (FIELD_BITPOS (*fp)
15004 + anonymous_size * bits_per_byte
15005 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
15006 }
15007 }
da5b30da
AA
15008 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15009 if (attr != NULL)
15010 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
15011 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
15012
15013 /* Get name of field. */
39cbfefa
DJ
15014 fieldname = dwarf2_name (die, cu);
15015 if (fieldname == NULL)
15016 fieldname = "";
d8151005
DJ
15017
15018 /* The name is already allocated along with this objfile, so we don't
15019 need to duplicate it for the type. */
15020 fp->name = fieldname;
c906108c
SS
15021
15022 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 15023 pointer or virtual base class pointer) to private. */
e142c38c 15024 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 15025 {
d48cc9dd 15026 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
15027 new_field->accessibility = DW_ACCESS_private;
15028 fip->non_public_fields = 1;
15029 }
15030 }
a9a9bd0f 15031 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 15032 {
a9a9bd0f
DC
15033 /* C++ static member. */
15034
15035 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15036 is a declaration, but all versions of G++ as of this writing
15037 (so through at least 3.2.1) incorrectly generate
15038 DW_TAG_variable tags. */
6e70227d 15039
ff355380 15040 const char *physname;
c906108c 15041
a9a9bd0f 15042 /* Get name of field. */
39cbfefa
DJ
15043 fieldname = dwarf2_name (die, cu);
15044 if (fieldname == NULL)
c906108c
SS
15045 return;
15046
254e6b9e 15047 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
15048 if (attr
15049 /* Only create a symbol if this is an external value.
15050 new_symbol checks this and puts the value in the global symbol
15051 table, which we want. If it is not external, new_symbol
15052 will try to put the value in cu->list_in_scope which is wrong. */
15053 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
15054 {
15055 /* A static const member, not much different than an enum as far as
15056 we're concerned, except that we can support more types. */
15057 new_symbol (die, NULL, cu);
15058 }
15059
2df3850c 15060 /* Get physical name. */
ff355380 15061 physname = dwarf2_physname (fieldname, die, cu);
c906108c 15062
d8151005
DJ
15063 /* The name is already allocated along with this objfile, so we don't
15064 need to duplicate it for the type. */
15065 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 15066 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 15067 FIELD_NAME (*fp) = fieldname;
c906108c
SS
15068 }
15069 else if (die->tag == DW_TAG_inheritance)
15070 {
74ac6d43 15071 LONGEST offset;
d4b96c9a 15072
74ac6d43
TT
15073 /* C++ base class field. */
15074 if (handle_data_member_location (die, cu, &offset))
15075 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 15076 FIELD_BITSIZE (*fp) = 0;
e7c27a73 15077 FIELD_TYPE (*fp) = die_type (die, cu);
a737d952 15078 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
c906108c 15079 }
2ddeaf8a
TT
15080 else if (die->tag == DW_TAG_variant_part)
15081 {
15082 /* process_structure_scope will treat this DIE as a union. */
15083 process_structure_scope (die, cu);
15084
15085 /* The variant part is relative to the start of the enclosing
15086 structure. */
15087 SET_FIELD_BITPOS (*fp, 0);
15088 fp->type = get_die_type (die, cu);
15089 fp->artificial = 1;
15090 fp->name = "<<variant>>";
15091 }
15092 else
15093 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
15094}
15095
883fd55a
KS
15096/* Can the type given by DIE define another type? */
15097
15098static bool
15099type_can_define_types (const struct die_info *die)
15100{
15101 switch (die->tag)
15102 {
15103 case DW_TAG_typedef:
15104 case DW_TAG_class_type:
15105 case DW_TAG_structure_type:
15106 case DW_TAG_union_type:
15107 case DW_TAG_enumeration_type:
15108 return true;
15109
15110 default:
15111 return false;
15112 }
15113}
15114
15115/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
15116
15117static void
883fd55a
KS
15118dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15119 struct dwarf2_cu *cu)
6e70227d 15120{
be2daae6
TT
15121 struct decl_field fp;
15122 memset (&fp, 0, sizeof (fp));
98751a41 15123
883fd55a 15124 gdb_assert (type_can_define_types (die));
98751a41 15125
883fd55a 15126 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
15127 fp.name = dwarf2_name (die, cu);
15128 fp.type = read_type_die (die, cu);
98751a41 15129
c191a687
KS
15130 /* Save accessibility. */
15131 enum dwarf_access_attribute accessibility;
15132 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15133 if (attr != NULL)
15134 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15135 else
15136 accessibility = dwarf2_default_access_attribute (die, cu);
15137 switch (accessibility)
15138 {
15139 case DW_ACCESS_public:
15140 /* The assumed value if neither private nor protected. */
15141 break;
15142 case DW_ACCESS_private:
be2daae6 15143 fp.is_private = 1;
c191a687
KS
15144 break;
15145 case DW_ACCESS_protected:
be2daae6 15146 fp.is_protected = 1;
c191a687
KS
15147 break;
15148 default:
b98664d3 15149 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
15150 }
15151
883fd55a 15152 if (die->tag == DW_TAG_typedef)
be2daae6 15153 fip->typedef_field_list.push_back (fp);
883fd55a 15154 else
be2daae6 15155 fip->nested_types_list.push_back (fp);
98751a41
JK
15156}
15157
c906108c
SS
15158/* Create the vector of fields, and attach it to the type. */
15159
15160static void
fba45db2 15161dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15162 struct dwarf2_cu *cu)
c906108c
SS
15163{
15164 int nfields = fip->nfields;
15165
15166 /* Record the field count, allocate space for the array of fields,
15167 and create blank accessibility bitfields if necessary. */
15168 TYPE_NFIELDS (type) = nfields;
15169 TYPE_FIELDS (type) = (struct field *)
be2daae6 15170 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
c906108c 15171
b4ba55a1 15172 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
15173 {
15174 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15175
15176 TYPE_FIELD_PRIVATE_BITS (type) =
15177 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15178 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15179
15180 TYPE_FIELD_PROTECTED_BITS (type) =
15181 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15182 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15183
774b6a14
TT
15184 TYPE_FIELD_IGNORE_BITS (type) =
15185 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15186 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
15187 }
15188
15189 /* If the type has baseclasses, allocate and clear a bit vector for
15190 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 15191 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 15192 {
be2daae6 15193 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 15194 unsigned char *pointer;
c906108c
SS
15195
15196 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 15197 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 15198 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
15199 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15200 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
15201 }
15202
2ddeaf8a
TT
15203 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15204 {
15205 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15206
be2daae6 15207 for (int index = 0; index < nfields; ++index)
2ddeaf8a 15208 {
be2daae6
TT
15209 struct nextfield &field = fip->fields[index];
15210
15211 if (field.variant.is_discriminant)
2ddeaf8a 15212 di->discriminant_index = index;
be2daae6 15213 else if (field.variant.default_branch)
2ddeaf8a
TT
15214 di->default_index = index;
15215 else
be2daae6 15216 di->discriminants[index] = field.variant.discriminant_value;
2ddeaf8a
TT
15217 }
15218 }
15219
be2daae6
TT
15220 /* Copy the saved-up fields into the field vector. */
15221 for (int i = 0; i < nfields; ++i)
c906108c 15222 {
be2daae6
TT
15223 struct nextfield &field
15224 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15225 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 15226
be2daae6
TT
15227 TYPE_FIELD (type, i) = field.field;
15228 switch (field.accessibility)
c906108c 15229 {
c5aa993b 15230 case DW_ACCESS_private:
b4ba55a1 15231 if (cu->language != language_ada)
be2daae6 15232 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 15233 break;
c906108c 15234
c5aa993b 15235 case DW_ACCESS_protected:
b4ba55a1 15236 if (cu->language != language_ada)
be2daae6 15237 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 15238 break;
c906108c 15239
c5aa993b
JM
15240 case DW_ACCESS_public:
15241 break;
c906108c 15242
c5aa993b
JM
15243 default:
15244 /* Unknown accessibility. Complain and treat it as public. */
15245 {
b98664d3 15246 complaint (_("unsupported accessibility %d"),
be2daae6 15247 field.accessibility);
c5aa993b
JM
15248 }
15249 break;
c906108c 15250 }
be2daae6 15251 if (i < fip->baseclasses.size ())
c906108c 15252 {
be2daae6 15253 switch (field.virtuality)
c906108c 15254 {
c5aa993b
JM
15255 case DW_VIRTUALITY_virtual:
15256 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15257 if (cu->language == language_ada)
a73c6dcd 15258 error (_("unexpected virtuality in component of Ada type"));
be2daae6 15259 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 15260 break;
c906108c
SS
15261 }
15262 }
c906108c
SS
15263 }
15264}
15265
7d27a96d
TT
15266/* Return true if this member function is a constructor, false
15267 otherwise. */
15268
15269static int
15270dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15271{
15272 const char *fieldname;
fe978cb0 15273 const char *type_name;
7d27a96d
TT
15274 int len;
15275
15276 if (die->parent == NULL)
15277 return 0;
15278
15279 if (die->parent->tag != DW_TAG_structure_type
15280 && die->parent->tag != DW_TAG_union_type
15281 && die->parent->tag != DW_TAG_class_type)
15282 return 0;
15283
15284 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15285 type_name = dwarf2_name (die->parent, cu);
15286 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15287 return 0;
15288
15289 len = strlen (fieldname);
fe978cb0
PA
15290 return (strncmp (fieldname, type_name, len) == 0
15291 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15292}
15293
c906108c
SS
15294/* Add a member function to the proper fieldlist. */
15295
15296static void
107d2387 15297dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15298 struct type *type, struct dwarf2_cu *cu)
c906108c 15299{
518817b3 15300 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15301 struct attribute *attr;
c906108c 15302 int i;
be2daae6 15303 struct fnfieldlist *flp = nullptr;
c906108c 15304 struct fn_field *fnp;
15d034d0 15305 const char *fieldname;
f792889a 15306 struct type *this_type;
60d5a603 15307 enum dwarf_access_attribute accessibility;
c906108c 15308
b4ba55a1 15309 if (cu->language == language_ada)
a73c6dcd 15310 error (_("unexpected member function in Ada type"));
b4ba55a1 15311
2df3850c 15312 /* Get name of member function. */
39cbfefa
DJ
15313 fieldname = dwarf2_name (die, cu);
15314 if (fieldname == NULL)
2df3850c 15315 return;
c906108c 15316
c906108c 15317 /* Look up member function name in fieldlist. */
be2daae6 15318 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15319 {
27bfe10e 15320 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15321 {
15322 flp = &fip->fnfieldlists[i];
15323 break;
15324 }
c906108c
SS
15325 }
15326
be2daae6
TT
15327 /* Create a new fnfieldlist if necessary. */
15328 if (flp == nullptr)
c906108c 15329 {
be2daae6
TT
15330 fip->fnfieldlists.emplace_back ();
15331 flp = &fip->fnfieldlists.back ();
c906108c 15332 flp->name = fieldname;
be2daae6 15333 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15334 }
15335
be2daae6
TT
15336 /* Create a new member function field and add it to the vector of
15337 fnfieldlists. */
15338 flp->fnfields.emplace_back ();
15339 fnp = &flp->fnfields.back ();
3da10d80
KS
15340
15341 /* Delay processing of the physname until later. */
9c37b5ae 15342 if (cu->language == language_cplus)
be2daae6
TT
15343 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15344 die, cu);
3da10d80
KS
15345 else
15346 {
1d06ead6 15347 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15348 fnp->physname = physname ? physname : "";
15349 }
15350
c906108c 15351 fnp->type = alloc_type (objfile);
f792889a
DJ
15352 this_type = read_type_die (die, cu);
15353 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 15354 {
f792889a 15355 int nparams = TYPE_NFIELDS (this_type);
c906108c 15356
f792889a 15357 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15358 of the method itself (TYPE_CODE_METHOD). */
15359 smash_to_method_type (fnp->type, type,
f792889a
DJ
15360 TYPE_TARGET_TYPE (this_type),
15361 TYPE_FIELDS (this_type),
15362 TYPE_NFIELDS (this_type),
15363 TYPE_VARARGS (this_type));
c906108c
SS
15364
15365 /* Handle static member functions.
c5aa993b 15366 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15367 member functions. G++ helps GDB by marking the first
15368 parameter for non-static member functions (which is the this
15369 pointer) as artificial. We obtain this information from
15370 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15371 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15372 fnp->voffset = VOFFSET_STATIC;
15373 }
15374 else
b98664d3 15375 complaint (_("member function type missing for '%s'"),
3da10d80 15376 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15377
15378 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15379 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15380 fnp->fcontext = die_containing_type (die, cu);
c906108c 15381
3e43a32a
MS
15382 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15383 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15384
15385 /* Get accessibility. */
e142c38c 15386 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 15387 if (attr)
aead7601 15388 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
15389 else
15390 accessibility = dwarf2_default_access_attribute (die, cu);
15391 switch (accessibility)
c906108c 15392 {
60d5a603
JK
15393 case DW_ACCESS_private:
15394 fnp->is_private = 1;
15395 break;
15396 case DW_ACCESS_protected:
15397 fnp->is_protected = 1;
15398 break;
c906108c
SS
15399 }
15400
b02dede2 15401 /* Check for artificial methods. */
e142c38c 15402 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15403 if (attr && DW_UNSND (attr) != 0)
15404 fnp->is_artificial = 1;
15405
7d27a96d
TT
15406 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15407
0d564a31 15408 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15409 function. For older versions of GCC, this is an offset in the
15410 appropriate virtual table, as specified by DW_AT_containing_type.
15411 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15412 to the object address. */
15413
e142c38c 15414 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 15415 if (attr)
8e19ed76 15416 {
aec5aa8b 15417 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 15418 {
aec5aa8b
TT
15419 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15420 {
15421 /* Old-style GCC. */
15422 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15423 }
15424 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15425 || (DW_BLOCK (attr)->size > 1
15426 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15427 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15428 {
aec5aa8b
TT
15429 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15430 if ((fnp->voffset % cu->header.addr_size) != 0)
15431 dwarf2_complex_location_expr_complaint ();
15432 else
15433 fnp->voffset /= cu->header.addr_size;
15434 fnp->voffset += 2;
15435 }
15436 else
15437 dwarf2_complex_location_expr_complaint ();
15438
15439 if (!fnp->fcontext)
7e993ebf
KS
15440 {
15441 /* If there is no `this' field and no DW_AT_containing_type,
15442 we cannot actually find a base class context for the
15443 vtable! */
15444 if (TYPE_NFIELDS (this_type) == 0
15445 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15446 {
b98664d3 15447 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15448 "function \"%s\" (offset %s)"),
15449 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15450 }
15451 else
15452 {
15453 fnp->fcontext
15454 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15455 }
15456 }
aec5aa8b 15457 }
3690dd37 15458 else if (attr_form_is_section_offset (attr))
8e19ed76 15459 {
4d3c2250 15460 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15461 }
15462 else
15463 {
4d3c2250
KB
15464 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15465 fieldname);
8e19ed76 15466 }
0d564a31 15467 }
d48cc9dd
DJ
15468 else
15469 {
15470 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15471 if (attr && DW_UNSND (attr))
15472 {
15473 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15474 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15475 "but the vtable offset is not specified"),
9d8780f0 15476 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15477 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15478 TYPE_CPLUS_DYNAMIC (type) = 1;
15479 }
15480 }
c906108c
SS
15481}
15482
15483/* Create the vector of member function fields, and attach it to the type. */
15484
15485static void
fba45db2 15486dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15487 struct dwarf2_cu *cu)
c906108c 15488{
b4ba55a1 15489 if (cu->language == language_ada)
a73c6dcd 15490 error (_("unexpected member functions in Ada type"));
b4ba55a1 15491
c906108c
SS
15492 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15493 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15494 TYPE_ALLOC (type,
15495 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15496
be2daae6 15497 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15498 {
be2daae6 15499 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15500 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15501
be2daae6
TT
15502 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15503 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15504 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15505 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15506
15507 for (int k = 0; k < nf.fnfields.size (); ++k)
15508 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15509 }
15510
be2daae6 15511 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15512}
15513
1168df01
JB
15514/* Returns non-zero if NAME is the name of a vtable member in CU's
15515 language, zero otherwise. */
15516static int
15517is_vtable_name (const char *name, struct dwarf2_cu *cu)
15518{
15519 static const char vptr[] = "_vptr";
15520
9c37b5ae
TT
15521 /* Look for the C++ form of the vtable. */
15522 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15523 return 1;
15524
15525 return 0;
15526}
15527
c0dd20ea 15528/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15529 functions, with the ABI-specified layout. If TYPE describes
15530 such a structure, smash it into a member function type.
61049d3b
DJ
15531
15532 GCC shouldn't do this; it should just output pointer to member DIEs.
15533 This is GCC PR debug/28767. */
c0dd20ea 15534
0b92b5bb
TT
15535static void
15536quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15537{
09e2d7c7 15538 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15539
15540 /* Check for a structure with no name and two children. */
0b92b5bb
TT
15541 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15542 return;
c0dd20ea
DJ
15543
15544 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15545 if (TYPE_FIELD_NAME (type, 0) == NULL
15546 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15547 || TYPE_FIELD_NAME (type, 1) == NULL
15548 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15549 return;
c0dd20ea
DJ
15550
15551 /* Find the type of the method. */
0b92b5bb 15552 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
15553 if (pfn_type == NULL
15554 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15555 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 15556 return;
c0dd20ea
DJ
15557
15558 /* Look for the "this" argument. */
15559 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15560 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 15561 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 15562 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 15563 return;
c0dd20ea 15564
09e2d7c7 15565 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 15566 new_type = alloc_type (objfile);
09e2d7c7 15567 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
15568 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15569 TYPE_VARARGS (pfn_type));
0b92b5bb 15570 smash_to_methodptr_type (type, new_type);
c0dd20ea 15571}
1168df01 15572
2b4424c3
TT
15573/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15574 appropriate error checking and issuing complaints if there is a
15575 problem. */
15576
15577static ULONGEST
15578get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15579{
15580 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15581
15582 if (attr == nullptr)
15583 return 0;
15584
15585 if (!attr_form_is_constant (attr))
15586 {
b98664d3 15587 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15588 " - DIE at %s [in module %s]"),
15589 sect_offset_str (die->sect_off),
15590 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15591 return 0;
15592 }
15593
15594 ULONGEST align;
15595 if (attr->form == DW_FORM_sdata)
15596 {
15597 LONGEST val = DW_SND (attr);
15598 if (val < 0)
15599 {
b98664d3 15600 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
15601 " - DIE at %s [in module %s]"),
15602 sect_offset_str (die->sect_off),
15603 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15604 return 0;
15605 }
15606 align = val;
15607 }
15608 else
15609 align = DW_UNSND (attr);
15610
15611 if (align == 0)
15612 {
b98664d3 15613 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15614 " - DIE at %s [in module %s]"),
15615 sect_offset_str (die->sect_off),
15616 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15617 return 0;
15618 }
15619 if ((align & (align - 1)) != 0)
15620 {
b98664d3 15621 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15622 " - DIE at %s [in module %s]"),
15623 sect_offset_str (die->sect_off),
15624 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15625 return 0;
15626 }
15627
15628 return align;
15629}
15630
15631/* If the DIE has a DW_AT_alignment attribute, use its value to set
15632 the alignment for TYPE. */
15633
15634static void
15635maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15636 struct type *type)
15637{
15638 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15639 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15640 " - DIE at %s [in module %s]"),
15641 sect_offset_str (die->sect_off),
15642 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15643}
685b1105 15644
c906108c 15645/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15646 (definition) to create a type for the structure or union. Fill in
15647 the type's name and general properties; the members will not be
83655187
DE
15648 processed until process_structure_scope. A symbol table entry for
15649 the type will also not be done until process_structure_scope (assuming
15650 the type has a name).
c906108c 15651
c767944b
DJ
15652 NOTE: we need to call these functions regardless of whether or not the
15653 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15654 structure or union. This gets the type entered into our set of
83655187 15655 user defined types. */
c906108c 15656
f792889a 15657static struct type *
134d01f1 15658read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15659{
518817b3 15660 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
15661 struct type *type;
15662 struct attribute *attr;
15d034d0 15663 const char *name;
c906108c 15664
348e048f
DE
15665 /* If the definition of this type lives in .debug_types, read that type.
15666 Don't follow DW_AT_specification though, that will take us back up
15667 the chain and we want to go down. */
45e58e77 15668 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
15669 if (attr)
15670 {
ac9ec31b 15671 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15672
ac9ec31b 15673 /* The type's CU may not be the same as CU.
02142a6c 15674 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15675 return set_die_type (die, type, cu);
15676 }
15677
c0dd20ea 15678 type = alloc_type (objfile);
c906108c 15679 INIT_CPLUS_SPECIFIC (type);
93311388 15680
39cbfefa
DJ
15681 name = dwarf2_name (die, cu);
15682 if (name != NULL)
c906108c 15683 {
987504bb 15684 if (cu->language == language_cplus
c44af4eb
TT
15685 || cu->language == language_d
15686 || cu->language == language_rust)
63d06c5c 15687 {
15d034d0 15688 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15689
15690 /* dwarf2_full_name might have already finished building the DIE's
15691 type. If so, there is no need to continue. */
15692 if (get_die_type (die, cu) != NULL)
15693 return get_die_type (die, cu);
15694
e86ca25f 15695 TYPE_NAME (type) = full_name;
63d06c5c
DC
15696 }
15697 else
15698 {
d8151005
DJ
15699 /* The name is already allocated along with this objfile, so
15700 we don't need to duplicate it for the type. */
e86ca25f 15701 TYPE_NAME (type) = name;
63d06c5c 15702 }
c906108c
SS
15703 }
15704
15705 if (die->tag == DW_TAG_structure_type)
15706 {
15707 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15708 }
15709 else if (die->tag == DW_TAG_union_type)
15710 {
15711 TYPE_CODE (type) = TYPE_CODE_UNION;
15712 }
2ddeaf8a
TT
15713 else if (die->tag == DW_TAG_variant_part)
15714 {
15715 TYPE_CODE (type) = TYPE_CODE_UNION;
15716 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15717 }
c906108c
SS
15718 else
15719 {
4753d33b 15720 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
15721 }
15722
0cc2414c
TT
15723 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15724 TYPE_DECLARED_CLASS (type) = 1;
15725
e142c38c 15726 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
15727 if (attr)
15728 {
155bfbd3
JB
15729 if (attr_form_is_constant (attr))
15730 TYPE_LENGTH (type) = DW_UNSND (attr);
15731 else
15732 {
15733 /* For the moment, dynamic type sizes are not supported
15734 by GDB's struct type. The actual size is determined
15735 on-demand when resolving the type of a given object,
15736 so set the type's length to zero for now. Otherwise,
15737 we record an expression as the length, and that expression
15738 could lead to a very large value, which could eventually
15739 lead to us trying to allocate that much memory when creating
15740 a value of that type. */
15741 TYPE_LENGTH (type) = 0;
15742 }
c906108c
SS
15743 }
15744 else
15745 {
15746 TYPE_LENGTH (type) = 0;
15747 }
15748
2b4424c3
TT
15749 maybe_set_alignment (cu, die, type);
15750
5230b05a 15751 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15752 {
5230b05a
WT
15753 /* ICC<14 does not output the required DW_AT_declaration on
15754 incomplete types, but gives them a size of zero. */
422b1cb0 15755 TYPE_STUB (type) = 1;
685b1105
JK
15756 }
15757 else
15758 TYPE_STUB_SUPPORTED (type) = 1;
15759
dc718098 15760 if (die_is_declaration (die, cu))
876cecd0 15761 TYPE_STUB (type) = 1;
a6c727b2
DJ
15762 else if (attr == NULL && die->child == NULL
15763 && producer_is_realview (cu->producer))
15764 /* RealView does not output the required DW_AT_declaration
15765 on incomplete types. */
15766 TYPE_STUB (type) = 1;
dc718098 15767
c906108c
SS
15768 /* We need to add the type field to the die immediately so we don't
15769 infinitely recurse when dealing with pointers to the structure
0963b4bd 15770 type within the structure itself. */
1c379e20 15771 set_die_type (die, type, cu);
c906108c 15772
7e314c57
JK
15773 /* set_die_type should be already done. */
15774 set_descriptive_type (type, die, cu);
15775
c767944b
DJ
15776 return type;
15777}
15778
2ddeaf8a
TT
15779/* A helper for process_structure_scope that handles a single member
15780 DIE. */
15781
15782static void
15783handle_struct_member_die (struct die_info *child_die, struct type *type,
15784 struct field_info *fi,
15785 std::vector<struct symbol *> *template_args,
15786 struct dwarf2_cu *cu)
15787{
15788 if (child_die->tag == DW_TAG_member
15789 || child_die->tag == DW_TAG_variable
15790 || child_die->tag == DW_TAG_variant_part)
15791 {
15792 /* NOTE: carlton/2002-11-05: A C++ static data member
15793 should be a DW_TAG_member that is a declaration, but
15794 all versions of G++ as of this writing (so through at
15795 least 3.2.1) incorrectly generate DW_TAG_variable
15796 tags for them instead. */
15797 dwarf2_add_field (fi, child_die, cu);
15798 }
15799 else if (child_die->tag == DW_TAG_subprogram)
15800 {
15801 /* Rust doesn't have member functions in the C++ sense.
15802 However, it does emit ordinary functions as children
15803 of a struct DIE. */
15804 if (cu->language == language_rust)
15805 read_func_scope (child_die, cu);
15806 else
15807 {
15808 /* C++ member function. */
15809 dwarf2_add_member_fn (fi, child_die, type, cu);
15810 }
15811 }
15812 else if (child_die->tag == DW_TAG_inheritance)
15813 {
15814 /* C++ base class field. */
15815 dwarf2_add_field (fi, child_die, cu);
15816 }
15817 else if (type_can_define_types (child_die))
15818 dwarf2_add_type_defn (fi, child_die, cu);
15819 else if (child_die->tag == DW_TAG_template_type_param
15820 || child_die->tag == DW_TAG_template_value_param)
15821 {
15822 struct symbol *arg = new_symbol (child_die, NULL, cu);
15823
15824 if (arg != NULL)
15825 template_args->push_back (arg);
15826 }
15827 else if (child_die->tag == DW_TAG_variant)
15828 {
15829 /* In a variant we want to get the discriminant and also add a
15830 field for our sole member child. */
15831 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15832
15833 for (struct die_info *variant_child = child_die->child;
15834 variant_child != NULL;
15835 variant_child = sibling_die (variant_child))
15836 {
15837 if (variant_child->tag == DW_TAG_member)
15838 {
15839 handle_struct_member_die (variant_child, type, fi,
15840 template_args, cu);
15841 /* Only handle the one. */
15842 break;
15843 }
15844 }
15845
15846 /* We don't handle this but we might as well report it if we see
15847 it. */
15848 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
b98664d3 15849 complaint (_("DW_AT_discr_list is not supported yet"
2ddeaf8a
TT
15850 " - DIE at %s [in module %s]"),
15851 sect_offset_str (child_die->sect_off),
15852 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15853
15854 /* The first field was just added, so we can stash the
15855 discriminant there. */
be2daae6 15856 gdb_assert (!fi->fields.empty ());
2ddeaf8a 15857 if (discr == NULL)
be2daae6 15858 fi->fields.back ().variant.default_branch = true;
2ddeaf8a 15859 else
be2daae6 15860 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
2ddeaf8a
TT
15861 }
15862}
15863
c767944b
DJ
15864/* Finish creating a structure or union type, including filling in
15865 its members and creating a symbol for it. */
15866
15867static void
15868process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15869{
518817b3 15870 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
ca040673 15871 struct die_info *child_die;
c767944b
DJ
15872 struct type *type;
15873
15874 type = get_die_type (die, cu);
15875 if (type == NULL)
15876 type = read_structure_type (die, cu);
15877
2ddeaf8a
TT
15878 /* When reading a DW_TAG_variant_part, we need to notice when we
15879 read the discriminant member, so we can record it later in the
15880 discriminant_info. */
15881 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
15882 sect_offset discr_offset;
3e1d3d8c 15883 bool has_template_parameters = false;
2ddeaf8a
TT
15884
15885 if (is_variant_part)
15886 {
15887 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15888 if (discr == NULL)
15889 {
15890 /* Maybe it's a univariant form, an extension we support.
15891 In this case arrange not to check the offset. */
15892 is_variant_part = false;
15893 }
15894 else if (attr_form_is_ref (discr))
15895 {
15896 struct dwarf2_cu *target_cu = cu;
15897 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15898
15899 discr_offset = target_die->sect_off;
15900 }
15901 else
15902 {
b98664d3 15903 complaint (_("DW_AT_discr does not have DIE reference form"
2ddeaf8a
TT
15904 " - DIE at %s [in module %s]"),
15905 sect_offset_str (die->sect_off),
15906 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15907 is_variant_part = false;
15908 }
15909 }
15910
e142c38c 15911 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15912 {
15913 struct field_info fi;
2f4732b0 15914 std::vector<struct symbol *> template_args;
c906108c 15915
639d11d3 15916 child_die = die->child;
c906108c
SS
15917
15918 while (child_die && child_die->tag)
15919 {
2ddeaf8a 15920 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
34eaf542 15921
2ddeaf8a 15922 if (is_variant_part && discr_offset == child_die->sect_off)
be2daae6 15923 fi.fields.back ().variant.is_discriminant = true;
34eaf542 15924
c906108c
SS
15925 child_die = sibling_die (child_die);
15926 }
15927
34eaf542 15928 /* Attach template arguments to type. */
2f4732b0 15929 if (!template_args.empty ())
34eaf542 15930 {
3e1d3d8c 15931 has_template_parameters = true;
34eaf542 15932 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 15933 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 15934 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
15935 = XOBNEWVEC (&objfile->objfile_obstack,
15936 struct symbol *,
15937 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 15938 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 15939 template_args.data (),
34eaf542
TT
15940 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15941 * sizeof (struct symbol *)));
34eaf542
TT
15942 }
15943
c906108c
SS
15944 /* Attach fields and member functions to the type. */
15945 if (fi.nfields)
e7c27a73 15946 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 15947 if (!fi.fnfieldlists.empty ())
c906108c 15948 {
e7c27a73 15949 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 15950
c5aa993b 15951 /* Get the type which refers to the base class (possibly this
c906108c 15952 class itself) which contains the vtable pointer for the current
0d564a31
DJ
15953 class from the DW_AT_containing_type attribute. This use of
15954 DW_AT_containing_type is a GNU extension. */
c906108c 15955
e142c38c 15956 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 15957 {
e7c27a73 15958 struct type *t = die_containing_type (die, cu);
c906108c 15959
ae6ae975 15960 set_type_vptr_basetype (type, t);
c906108c
SS
15961 if (type == t)
15962 {
c906108c
SS
15963 int i;
15964
15965 /* Our own class provides vtbl ptr. */
15966 for (i = TYPE_NFIELDS (t) - 1;
15967 i >= TYPE_N_BASECLASSES (t);
15968 --i)
15969 {
0d5cff50 15970 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 15971
1168df01 15972 if (is_vtable_name (fieldname, cu))
c906108c 15973 {
ae6ae975 15974 set_type_vptr_fieldno (type, i);
c906108c
SS
15975 break;
15976 }
15977 }
15978
15979 /* Complain if virtual function table field not found. */
15980 if (i < TYPE_N_BASECLASSES (t))
b98664d3 15981 complaint (_("virtual function table pointer "
3e43a32a 15982 "not found when defining class '%s'"),
e86ca25f 15983 TYPE_NAME (type) ? TYPE_NAME (type) : "");
c906108c
SS
15984 }
15985 else
15986 {
ae6ae975 15987 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
15988 }
15989 }
f6235d4c 15990 else if (cu->producer
61012eef 15991 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
15992 {
15993 /* The IBM XLC compiler does not provide direct indication
15994 of the containing type, but the vtable pointer is
15995 always named __vfp. */
15996
15997 int i;
15998
15999 for (i = TYPE_NFIELDS (type) - 1;
16000 i >= TYPE_N_BASECLASSES (type);
16001 --i)
16002 {
16003 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16004 {
ae6ae975
DE
16005 set_type_vptr_fieldno (type, i);
16006 set_type_vptr_basetype (type, type);
f6235d4c
EZ
16007 break;
16008 }
16009 }
16010 }
c906108c 16011 }
98751a41
JK
16012
16013 /* Copy fi.typedef_field_list linked list elements content into the
16014 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 16015 if (!fi.typedef_field_list.empty ())
98751a41 16016 {
be2daae6 16017 int count = fi.typedef_field_list.size ();
98751a41 16018
a0d7a4ff 16019 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 16020 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 16021 = ((struct decl_field *)
be2daae6
TT
16022 TYPE_ALLOC (type,
16023 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16024 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 16025
be2daae6
TT
16026 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16027 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 16028 }
c767944b 16029
883fd55a
KS
16030 /* Copy fi.nested_types_list linked list elements content into the
16031 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 16032 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 16033 {
be2daae6 16034 int count = fi.nested_types_list.size ();
883fd55a
KS
16035
16036 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16037 TYPE_NESTED_TYPES_ARRAY (type)
16038 = ((struct decl_field *)
be2daae6
TT
16039 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16040 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 16041
be2daae6
TT
16042 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16043 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 16044 }
c906108c 16045 }
63d06c5c 16046
bb5ed363 16047 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
16048 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16049 cu->rust_unions.push_back (type);
0b92b5bb 16050
90aeadfc
DC
16051 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16052 snapshots) has been known to create a die giving a declaration
16053 for a class that has, as a child, a die giving a definition for a
16054 nested class. So we have to process our children even if the
16055 current die is a declaration. Normally, of course, a declaration
16056 won't have any children at all. */
134d01f1 16057
ca040673
DE
16058 child_die = die->child;
16059
90aeadfc
DC
16060 while (child_die != NULL && child_die->tag)
16061 {
16062 if (child_die->tag == DW_TAG_member
16063 || child_die->tag == DW_TAG_variable
34eaf542
TT
16064 || child_die->tag == DW_TAG_inheritance
16065 || child_die->tag == DW_TAG_template_value_param
16066 || child_die->tag == DW_TAG_template_type_param)
134d01f1 16067 {
90aeadfc 16068 /* Do nothing. */
134d01f1 16069 }
90aeadfc
DC
16070 else
16071 process_die (child_die, cu);
134d01f1 16072
90aeadfc 16073 child_die = sibling_die (child_die);
134d01f1
DJ
16074 }
16075
fa4028e9
JB
16076 /* Do not consider external references. According to the DWARF standard,
16077 these DIEs are identified by the fact that they have no byte_size
16078 attribute, and a declaration attribute. */
16079 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16080 || !die_is_declaration (die, cu))
3e1d3d8c
TT
16081 {
16082 struct symbol *sym = new_symbol (die, type, cu);
16083
16084 if (has_template_parameters)
16085 {
16086 /* Make sure that the symtab is set on the new symbols.
16087 Even though they don't appear in this symtab directly,
16088 other parts of gdb assume that symbols do, and this is
16089 reasonably true. */
16090 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16091 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i),
16092 symbol_symtab (sym));
16093 }
16094 }
134d01f1
DJ
16095}
16096
55426c9d
JB
16097/* Assuming DIE is an enumeration type, and TYPE is its associated type,
16098 update TYPE using some information only available in DIE's children. */
16099
16100static void
16101update_enumeration_type_from_children (struct die_info *die,
16102 struct type *type,
16103 struct dwarf2_cu *cu)
16104{
60f7655a 16105 struct die_info *child_die;
55426c9d
JB
16106 int unsigned_enum = 1;
16107 int flag_enum = 1;
16108 ULONGEST mask = 0;
55426c9d 16109
8268c778 16110 auto_obstack obstack;
55426c9d 16111
60f7655a
DE
16112 for (child_die = die->child;
16113 child_die != NULL && child_die->tag;
16114 child_die = sibling_die (child_die))
55426c9d
JB
16115 {
16116 struct attribute *attr;
16117 LONGEST value;
16118 const gdb_byte *bytes;
16119 struct dwarf2_locexpr_baton *baton;
16120 const char *name;
60f7655a 16121
55426c9d
JB
16122 if (child_die->tag != DW_TAG_enumerator)
16123 continue;
16124
16125 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16126 if (attr == NULL)
16127 continue;
16128
16129 name = dwarf2_name (child_die, cu);
16130 if (name == NULL)
16131 name = "<anonymous enumerator>";
16132
16133 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16134 &value, &bytes, &baton);
16135 if (value < 0)
16136 {
16137 unsigned_enum = 0;
16138 flag_enum = 0;
16139 }
16140 else if ((mask & value) != 0)
16141 flag_enum = 0;
16142 else
16143 mask |= value;
16144
16145 /* If we already know that the enum type is neither unsigned, nor
16146 a flag type, no need to look at the rest of the enumerates. */
16147 if (!unsigned_enum && !flag_enum)
16148 break;
55426c9d
JB
16149 }
16150
16151 if (unsigned_enum)
16152 TYPE_UNSIGNED (type) = 1;
16153 if (flag_enum)
16154 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16155}
16156
134d01f1
DJ
16157/* Given a DW_AT_enumeration_type die, set its type. We do not
16158 complete the type's fields yet, or create any symbols. */
c906108c 16159
f792889a 16160static struct type *
134d01f1 16161read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16162{
518817b3 16163 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16164 struct type *type;
c906108c 16165 struct attribute *attr;
0114d602 16166 const char *name;
134d01f1 16167
348e048f
DE
16168 /* If the definition of this type lives in .debug_types, read that type.
16169 Don't follow DW_AT_specification though, that will take us back up
16170 the chain and we want to go down. */
45e58e77 16171 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
16172 if (attr)
16173 {
ac9ec31b 16174 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16175
ac9ec31b 16176 /* The type's CU may not be the same as CU.
02142a6c 16177 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16178 return set_die_type (die, type, cu);
16179 }
16180
c906108c
SS
16181 type = alloc_type (objfile);
16182
16183 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 16184 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16185 if (name != NULL)
e86ca25f 16186 TYPE_NAME (type) = name;
c906108c 16187
0626fc76
TT
16188 attr = dwarf2_attr (die, DW_AT_type, cu);
16189 if (attr != NULL)
16190 {
16191 struct type *underlying_type = die_type (die, cu);
16192
16193 TYPE_TARGET_TYPE (type) = underlying_type;
16194 }
16195
e142c38c 16196 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
16197 if (attr)
16198 {
16199 TYPE_LENGTH (type) = DW_UNSND (attr);
16200 }
16201 else
16202 {
16203 TYPE_LENGTH (type) = 0;
16204 }
16205
2b4424c3
TT
16206 maybe_set_alignment (cu, die, type);
16207
137033e9
JB
16208 /* The enumeration DIE can be incomplete. In Ada, any type can be
16209 declared as private in the package spec, and then defined only
16210 inside the package body. Such types are known as Taft Amendment
16211 Types. When another package uses such a type, an incomplete DIE
16212 may be generated by the compiler. */
02eb380e 16213 if (die_is_declaration (die, cu))
876cecd0 16214 TYPE_STUB (type) = 1;
02eb380e 16215
0626fc76
TT
16216 /* Finish the creation of this type by using the enum's children.
16217 We must call this even when the underlying type has been provided
16218 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
16219 update_enumeration_type_from_children (die, type, cu);
16220
0626fc76
TT
16221 /* If this type has an underlying type that is not a stub, then we
16222 may use its attributes. We always use the "unsigned" attribute
16223 in this situation, because ordinarily we guess whether the type
16224 is unsigned -- but the guess can be wrong and the underlying type
16225 can tell us the reality. However, we defer to a local size
16226 attribute if one exists, because this lets the compiler override
16227 the underlying type if needed. */
16228 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16229 {
16230 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16231 if (TYPE_LENGTH (type) == 0)
16232 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
2b4424c3
TT
16233 if (TYPE_RAW_ALIGN (type) == 0
16234 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16235 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
0626fc76
TT
16236 }
16237
3d567982
TT
16238 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16239
f792889a 16240 return set_die_type (die, type, cu);
134d01f1
DJ
16241}
16242
16243/* Given a pointer to a die which begins an enumeration, process all
16244 the dies that define the members of the enumeration, and create the
16245 symbol for the enumeration type.
16246
16247 NOTE: We reverse the order of the element list. */
16248
16249static void
16250process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16251{
f792889a 16252 struct type *this_type;
134d01f1 16253
f792889a
DJ
16254 this_type = get_die_type (die, cu);
16255 if (this_type == NULL)
16256 this_type = read_enumeration_type (die, cu);
9dc481d3 16257
639d11d3 16258 if (die->child != NULL)
c906108c 16259 {
9dc481d3
DE
16260 struct die_info *child_die;
16261 struct symbol *sym;
16262 struct field *fields = NULL;
16263 int num_fields = 0;
15d034d0 16264 const char *name;
9dc481d3 16265
639d11d3 16266 child_die = die->child;
c906108c
SS
16267 while (child_die && child_die->tag)
16268 {
16269 if (child_die->tag != DW_TAG_enumerator)
16270 {
e7c27a73 16271 process_die (child_die, cu);
c906108c
SS
16272 }
16273 else
16274 {
39cbfefa
DJ
16275 name = dwarf2_name (child_die, cu);
16276 if (name)
c906108c 16277 {
f792889a 16278 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
16279
16280 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16281 {
16282 fields = (struct field *)
16283 xrealloc (fields,
16284 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 16285 * sizeof (struct field));
c906108c
SS
16286 }
16287
3567439c 16288 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 16289 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 16290 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
16291 FIELD_BITSIZE (fields[num_fields]) = 0;
16292
16293 num_fields++;
16294 }
16295 }
16296
16297 child_die = sibling_die (child_die);
16298 }
16299
16300 if (num_fields)
16301 {
f792889a
DJ
16302 TYPE_NFIELDS (this_type) = num_fields;
16303 TYPE_FIELDS (this_type) = (struct field *)
16304 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16305 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 16306 sizeof (struct field) * num_fields);
b8c9b27d 16307 xfree (fields);
c906108c 16308 }
c906108c 16309 }
134d01f1 16310
6c83ed52
TT
16311 /* If we are reading an enum from a .debug_types unit, and the enum
16312 is a declaration, and the enum is not the signatured type in the
16313 unit, then we do not want to add a symbol for it. Adding a
16314 symbol would in some cases obscure the true definition of the
16315 enum, giving users an incomplete type when the definition is
16316 actually available. Note that we do not want to do this for all
16317 enums which are just declarations, because C++0x allows forward
16318 enum declarations. */
3019eac3 16319 if (cu->per_cu->is_debug_types
6c83ed52
TT
16320 && die_is_declaration (die, cu))
16321 {
52dc124a 16322 struct signatured_type *sig_type;
6c83ed52 16323
c0f78cd4 16324 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16325 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16326 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16327 return;
16328 }
16329
f792889a 16330 new_symbol (die, this_type, cu);
c906108c
SS
16331}
16332
16333/* Extract all information from a DW_TAG_array_type DIE and put it in
16334 the DIE's type field. For now, this only handles one dimensional
16335 arrays. */
16336
f792889a 16337static struct type *
e7c27a73 16338read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16339{
518817b3 16340 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16341 struct die_info *child_die;
7e314c57 16342 struct type *type;
c906108c 16343 struct type *element_type, *range_type, *index_type;
c906108c 16344 struct attribute *attr;
15d034d0 16345 const char *name;
a405673c 16346 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16347 unsigned int bit_stride = 0;
c906108c 16348
e7c27a73 16349 element_type = die_type (die, cu);
c906108c 16350
7e314c57
JK
16351 /* The die_type call above may have already set the type for this DIE. */
16352 type = get_die_type (die, cu);
16353 if (type)
16354 return type;
16355
dc53a7ad
JB
16356 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16357 if (attr != NULL)
a405673c
JB
16358 {
16359 int stride_ok;
16360
16361 byte_stride_prop
16362 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16363 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop);
16364 if (!stride_ok)
16365 {
b98664d3 16366 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16367 " - DIE at %s [in module %s]"),
16368 sect_offset_str (die->sect_off),
518817b3 16369 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a405673c
JB
16370 /* Ignore this attribute. We will likely not be able to print
16371 arrays of this type correctly, but there is little we can do
16372 to help if we cannot read the attribute's value. */
16373 byte_stride_prop = NULL;
16374 }
16375 }
dc53a7ad
JB
16376
16377 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16378 if (attr != NULL)
16379 bit_stride = DW_UNSND (attr);
16380
c906108c
SS
16381 /* Irix 6.2 native cc creates array types without children for
16382 arrays with unspecified length. */
639d11d3 16383 if (die->child == NULL)
c906108c 16384 {
46bf5051 16385 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16386 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16387 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16388 byte_stride_prop, bit_stride);
f792889a 16389 return set_die_type (die, type, cu);
c906108c
SS
16390 }
16391
791afaa2 16392 std::vector<struct type *> range_types;
639d11d3 16393 child_die = die->child;
c906108c
SS
16394 while (child_die && child_die->tag)
16395 {
16396 if (child_die->tag == DW_TAG_subrange_type)
16397 {
f792889a 16398 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16399
f792889a 16400 if (child_type != NULL)
a02abb62 16401 {
0963b4bd
MS
16402 /* The range type was succesfully read. Save it for the
16403 array type creation. */
791afaa2 16404 range_types.push_back (child_type);
a02abb62 16405 }
c906108c
SS
16406 }
16407 child_die = sibling_die (child_die);
16408 }
16409
16410 /* Dwarf2 dimensions are output from left to right, create the
16411 necessary array types in backwards order. */
7ca2d3a3 16412
c906108c 16413 type = element_type;
7ca2d3a3
DL
16414
16415 if (read_array_order (die, cu) == DW_ORD_col_major)
16416 {
16417 int i = 0;
9a619af0 16418
791afaa2 16419 while (i < range_types.size ())
dc53a7ad 16420 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16421 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16422 }
16423 else
16424 {
791afaa2 16425 size_t ndim = range_types.size ();
7ca2d3a3 16426 while (ndim-- > 0)
dc53a7ad 16427 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16428 byte_stride_prop, bit_stride);
7ca2d3a3 16429 }
c906108c 16430
f5f8a009
EZ
16431 /* Understand Dwarf2 support for vector types (like they occur on
16432 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16433 array type. This is not part of the Dwarf2/3 standard yet, but a
16434 custom vendor extension. The main difference between a regular
16435 array and the vector variant is that vectors are passed by value
16436 to functions. */
e142c38c 16437 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 16438 if (attr)
ea37ba09 16439 make_vector_type (type);
f5f8a009 16440
dbc98a8b
KW
16441 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16442 implementation may choose to implement triple vectors using this
16443 attribute. */
16444 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16445 if (attr)
16446 {
16447 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16448 TYPE_LENGTH (type) = DW_UNSND (attr);
16449 else
b98664d3 16450 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16451 "than the total size of elements"));
dbc98a8b
KW
16452 }
16453
39cbfefa
DJ
16454 name = dwarf2_name (die, cu);
16455 if (name)
16456 TYPE_NAME (type) = name;
6e70227d 16457
2b4424c3
TT
16458 maybe_set_alignment (cu, die, type);
16459
0963b4bd 16460 /* Install the type in the die. */
7e314c57
JK
16461 set_die_type (die, type, cu);
16462
16463 /* set_die_type should be already done. */
b4ba55a1
JB
16464 set_descriptive_type (type, die, cu);
16465
7e314c57 16466 return type;
c906108c
SS
16467}
16468
7ca2d3a3 16469static enum dwarf_array_dim_ordering
6e70227d 16470read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16471{
16472 struct attribute *attr;
16473
16474 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16475
aead7601
SM
16476 if (attr)
16477 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16478
0963b4bd
MS
16479 /* GNU F77 is a special case, as at 08/2004 array type info is the
16480 opposite order to the dwarf2 specification, but data is still
16481 laid out as per normal fortran.
7ca2d3a3 16482
0963b4bd
MS
16483 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16484 version checking. */
7ca2d3a3 16485
905e0470
PM
16486 if (cu->language == language_fortran
16487 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16488 {
16489 return DW_ORD_row_major;
16490 }
16491
6e70227d 16492 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16493 {
16494 case array_column_major:
16495 return DW_ORD_col_major;
16496 case array_row_major:
16497 default:
16498 return DW_ORD_row_major;
16499 };
16500}
16501
72019c9c 16502/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16503 the DIE's type field. */
72019c9c 16504
f792889a 16505static struct type *
72019c9c
GM
16506read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16507{
7e314c57
JK
16508 struct type *domain_type, *set_type;
16509 struct attribute *attr;
f792889a 16510
7e314c57
JK
16511 domain_type = die_type (die, cu);
16512
16513 /* The die_type call above may have already set the type for this DIE. */
16514 set_type = get_die_type (die, cu);
16515 if (set_type)
16516 return set_type;
16517
16518 set_type = create_set_type (NULL, domain_type);
16519
16520 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
16521 if (attr)
16522 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16523
2b4424c3
TT
16524 maybe_set_alignment (cu, die, set_type);
16525
f792889a 16526 return set_die_type (die, set_type, cu);
72019c9c 16527}
7ca2d3a3 16528
0971de02
TT
16529/* A helper for read_common_block that creates a locexpr baton.
16530 SYM is the symbol which we are marking as computed.
16531 COMMON_DIE is the DIE for the common block.
16532 COMMON_LOC is the location expression attribute for the common
16533 block itself.
16534 MEMBER_LOC is the location expression attribute for the particular
16535 member of the common block that we are processing.
16536 CU is the CU from which the above come. */
16537
16538static void
16539mark_common_block_symbol_computed (struct symbol *sym,
16540 struct die_info *common_die,
16541 struct attribute *common_loc,
16542 struct attribute *member_loc,
16543 struct dwarf2_cu *cu)
16544{
518817b3
SM
16545 struct dwarf2_per_objfile *dwarf2_per_objfile
16546 = cu->per_cu->dwarf2_per_objfile;
0971de02
TT
16547 struct objfile *objfile = dwarf2_per_objfile->objfile;
16548 struct dwarf2_locexpr_baton *baton;
16549 gdb_byte *ptr;
16550 unsigned int cu_off;
16551 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16552 LONGEST offset = 0;
16553
16554 gdb_assert (common_loc && member_loc);
16555 gdb_assert (attr_form_is_block (common_loc));
16556 gdb_assert (attr_form_is_block (member_loc)
16557 || attr_form_is_constant (member_loc));
16558
8d749320 16559 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
16560 baton->per_cu = cu->per_cu;
16561 gdb_assert (baton->per_cu);
16562
16563 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16564
16565 if (attr_form_is_constant (member_loc))
16566 {
16567 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16568 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16569 }
16570 else
16571 baton->size += DW_BLOCK (member_loc)->size;
16572
224c3ddb 16573 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16574 baton->data = ptr;
16575
16576 *ptr++ = DW_OP_call4;
9c541725 16577 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16578 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16579 ptr += 4;
16580
16581 if (attr_form_is_constant (member_loc))
16582 {
16583 *ptr++ = DW_OP_addr;
16584 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16585 ptr += cu->header.addr_size;
16586 }
16587 else
16588 {
16589 /* We have to copy the data here, because DW_OP_call4 will only
16590 use a DW_AT_location attribute. */
16591 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16592 ptr += DW_BLOCK (member_loc)->size;
16593 }
16594
16595 *ptr++ = DW_OP_plus;
16596 gdb_assert (ptr - baton->data == baton->size);
16597
0971de02 16598 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16599 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16600}
16601
4357ac6c
TT
16602/* Create appropriate locally-scoped variables for all the
16603 DW_TAG_common_block entries. Also create a struct common_block
16604 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16605 is used to sepate the common blocks name namespace from regular
16606 variable names. */
c906108c
SS
16607
16608static void
e7c27a73 16609read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16610{
0971de02
TT
16611 struct attribute *attr;
16612
16613 attr = dwarf2_attr (die, DW_AT_location, cu);
16614 if (attr)
16615 {
16616 /* Support the .debug_loc offsets. */
16617 if (attr_form_is_block (attr))
16618 {
16619 /* Ok. */
16620 }
16621 else if (attr_form_is_section_offset (attr))
16622 {
16623 dwarf2_complex_location_expr_complaint ();
16624 attr = NULL;
16625 }
16626 else
16627 {
16628 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16629 "common block member");
16630 attr = NULL;
16631 }
16632 }
16633
639d11d3 16634 if (die->child != NULL)
c906108c 16635 {
518817b3 16636 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
4357ac6c
TT
16637 struct die_info *child_die;
16638 size_t n_entries = 0, size;
16639 struct common_block *common_block;
16640 struct symbol *sym;
74ac6d43 16641
4357ac6c
TT
16642 for (child_die = die->child;
16643 child_die && child_die->tag;
16644 child_die = sibling_die (child_die))
16645 ++n_entries;
16646
16647 size = (sizeof (struct common_block)
16648 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16649 common_block
16650 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16651 size);
4357ac6c
TT
16652 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16653 common_block->n_entries = 0;
16654
16655 for (child_die = die->child;
16656 child_die && child_die->tag;
16657 child_die = sibling_die (child_die))
16658 {
16659 /* Create the symbol in the DW_TAG_common_block block in the current
16660 symbol scope. */
e7c27a73 16661 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16662 if (sym != NULL)
16663 {
16664 struct attribute *member_loc;
16665
16666 common_block->contents[common_block->n_entries++] = sym;
16667
16668 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16669 cu);
16670 if (member_loc)
16671 {
16672 /* GDB has handled this for a long time, but it is
16673 not specified by DWARF. It seems to have been
16674 emitted by gfortran at least as recently as:
16675 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16676 complaint (_("Variable in common block has "
0971de02 16677 "DW_AT_data_member_location "
9d8780f0
SM
16678 "- DIE at %s [in module %s]"),
16679 sect_offset_str (child_die->sect_off),
518817b3 16680 objfile_name (objfile));
0971de02
TT
16681
16682 if (attr_form_is_section_offset (member_loc))
16683 dwarf2_complex_location_expr_complaint ();
16684 else if (attr_form_is_constant (member_loc)
16685 || attr_form_is_block (member_loc))
16686 {
16687 if (attr)
16688 mark_common_block_symbol_computed (sym, die, attr,
16689 member_loc, cu);
16690 }
16691 else
16692 dwarf2_complex_location_expr_complaint ();
16693 }
16694 }
c906108c 16695 }
4357ac6c
TT
16696
16697 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16698 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16699 }
16700}
16701
0114d602 16702/* Create a type for a C++ namespace. */
d9fa45fe 16703
0114d602
DJ
16704static struct type *
16705read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16706{
518817b3 16707 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16708 const char *previous_prefix, *name;
9219021c 16709 int is_anonymous;
0114d602
DJ
16710 struct type *type;
16711
16712 /* For extensions, reuse the type of the original namespace. */
16713 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16714 {
16715 struct die_info *ext_die;
16716 struct dwarf2_cu *ext_cu = cu;
9a619af0 16717
0114d602
DJ
16718 ext_die = dwarf2_extension (die, &ext_cu);
16719 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16720
16721 /* EXT_CU may not be the same as CU.
02142a6c 16722 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16723 return set_die_type (die, type, cu);
16724 }
9219021c 16725
e142c38c 16726 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16727
16728 /* Now build the name of the current namespace. */
16729
0114d602
DJ
16730 previous_prefix = determine_prefix (die, cu);
16731 if (previous_prefix[0] != '\0')
16732 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16733 previous_prefix, name, 0, cu);
0114d602
DJ
16734
16735 /* Create the type. */
19f392bc 16736 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16737
60531b24 16738 return set_die_type (die, type, cu);
0114d602
DJ
16739}
16740
22cee43f 16741/* Read a namespace scope. */
0114d602
DJ
16742
16743static void
16744read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16745{
518817b3 16746 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16747 int is_anonymous;
9219021c 16748
5c4e30ca
DC
16749 /* Add a symbol associated to this if we haven't seen the namespace
16750 before. Also, add a using directive if it's an anonymous
16751 namespace. */
9219021c 16752
f2f0e013 16753 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16754 {
16755 struct type *type;
16756
0114d602 16757 type = read_type_die (die, cu);
e7c27a73 16758 new_symbol (die, type, cu);
5c4e30ca 16759
e8e80198 16760 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16761 if (is_anonymous)
0114d602
DJ
16762 {
16763 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16764
eb1e02fd 16765 std::vector<const char *> excludes;
804d2729 16766 add_using_directive (using_directives (cu),
22cee43f 16767 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 16768 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16769 }
5c4e30ca 16770 }
9219021c 16771
639d11d3 16772 if (die->child != NULL)
d9fa45fe 16773 {
639d11d3 16774 struct die_info *child_die = die->child;
6e70227d 16775
d9fa45fe
DC
16776 while (child_die && child_die->tag)
16777 {
e7c27a73 16778 process_die (child_die, cu);
d9fa45fe
DC
16779 child_die = sibling_die (child_die);
16780 }
16781 }
38d518c9
EZ
16782}
16783
f55ee35c
JK
16784/* Read a Fortran module as type. This DIE can be only a declaration used for
16785 imported module. Still we need that type as local Fortran "use ... only"
16786 declaration imports depend on the created type in determine_prefix. */
16787
16788static struct type *
16789read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16790{
518817b3 16791 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15d034d0 16792 const char *module_name;
f55ee35c
JK
16793 struct type *type;
16794
16795 module_name = dwarf2_name (die, cu);
16796 if (!module_name)
b98664d3 16797 complaint (_("DW_TAG_module has no name, offset %s"),
9d8780f0 16798 sect_offset_str (die->sect_off));
19f392bc 16799 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 16800
f55ee35c
JK
16801 return set_die_type (die, type, cu);
16802}
16803
5d7cb8df
JK
16804/* Read a Fortran module. */
16805
16806static void
16807read_module (struct die_info *die, struct dwarf2_cu *cu)
16808{
16809 struct die_info *child_die = die->child;
530e8392
KB
16810 struct type *type;
16811
16812 type = read_type_die (die, cu);
16813 new_symbol (die, type, cu);
5d7cb8df 16814
5d7cb8df
JK
16815 while (child_die && child_die->tag)
16816 {
16817 process_die (child_die, cu);
16818 child_die = sibling_die (child_die);
16819 }
16820}
16821
38d518c9
EZ
16822/* Return the name of the namespace represented by DIE. Set
16823 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16824 namespace. */
16825
16826static const char *
e142c38c 16827namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16828{
16829 struct die_info *current_die;
16830 const char *name = NULL;
16831
16832 /* Loop through the extensions until we find a name. */
16833
16834 for (current_die = die;
16835 current_die != NULL;
f2f0e013 16836 current_die = dwarf2_extension (die, &cu))
38d518c9 16837 {
96553a0c
DE
16838 /* We don't use dwarf2_name here so that we can detect the absence
16839 of a name -> anonymous namespace. */
7d45c7c3 16840 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16841
38d518c9
EZ
16842 if (name != NULL)
16843 break;
16844 }
16845
16846 /* Is it an anonymous namespace? */
16847
16848 *is_anonymous = (name == NULL);
16849 if (*is_anonymous)
2b1dbab0 16850 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16851
16852 return name;
d9fa45fe
DC
16853}
16854
c906108c
SS
16855/* Extract all information from a DW_TAG_pointer_type DIE and add to
16856 the user defined type vector. */
16857
f792889a 16858static struct type *
e7c27a73 16859read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16860{
518817b3
SM
16861 struct gdbarch *gdbarch
16862 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
e7c27a73 16863 struct comp_unit_head *cu_header = &cu->header;
c906108c 16864 struct type *type;
8b2dbe47
KB
16865 struct attribute *attr_byte_size;
16866 struct attribute *attr_address_class;
16867 int byte_size, addr_class;
7e314c57
JK
16868 struct type *target_type;
16869
16870 target_type = die_type (die, cu);
c906108c 16871
7e314c57
JK
16872 /* The die_type call above may have already set the type for this DIE. */
16873 type = get_die_type (die, cu);
16874 if (type)
16875 return type;
16876
16877 type = lookup_pointer_type (target_type);
8b2dbe47 16878
e142c38c 16879 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
16880 if (attr_byte_size)
16881 byte_size = DW_UNSND (attr_byte_size);
c906108c 16882 else
8b2dbe47
KB
16883 byte_size = cu_header->addr_size;
16884
e142c38c 16885 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
16886 if (attr_address_class)
16887 addr_class = DW_UNSND (attr_address_class);
16888 else
16889 addr_class = DW_ADDR_none;
16890
2b4424c3
TT
16891 ULONGEST alignment = get_alignment (cu, die);
16892
16893 /* If the pointer size, alignment, or address class is different
16894 than the default, create a type variant marked as such and set
16895 the length accordingly. */
16896 if (TYPE_LENGTH (type) != byte_size
16897 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16898 && alignment != TYPE_RAW_ALIGN (type))
16899 || addr_class != DW_ADDR_none)
c906108c 16900 {
5e2b427d 16901 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
16902 {
16903 int type_flags;
16904
849957d9 16905 type_flags = gdbarch_address_class_type_flags
5e2b427d 16906 (gdbarch, byte_size, addr_class);
876cecd0
TT
16907 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16908 == 0);
8b2dbe47
KB
16909 type = make_type_with_address_space (type, type_flags);
16910 }
16911 else if (TYPE_LENGTH (type) != byte_size)
16912 {
b98664d3 16913 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 16914 }
2b4424c3
TT
16915 else if (TYPE_RAW_ALIGN (type) != alignment)
16916 {
b98664d3 16917 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
16918 " - DIE at %s [in module %s]"),
16919 sect_offset_str (die->sect_off),
16920 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16921 }
6e70227d 16922 else
9a619af0
MS
16923 {
16924 /* Should we also complain about unhandled address classes? */
16925 }
c906108c 16926 }
8b2dbe47
KB
16927
16928 TYPE_LENGTH (type) = byte_size;
2b4424c3 16929 set_type_align (type, alignment);
f792889a 16930 return set_die_type (die, type, cu);
c906108c
SS
16931}
16932
16933/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16934 the user defined type vector. */
16935
f792889a 16936static struct type *
e7c27a73 16937read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
16938{
16939 struct type *type;
16940 struct type *to_type;
16941 struct type *domain;
16942
e7c27a73
DJ
16943 to_type = die_type (die, cu);
16944 domain = die_containing_type (die, cu);
0d5de010 16945
7e314c57
JK
16946 /* The calls above may have already set the type for this DIE. */
16947 type = get_die_type (die, cu);
16948 if (type)
16949 return type;
16950
0d5de010
DJ
16951 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16952 type = lookup_methodptr_type (to_type);
7078baeb
TT
16953 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16954 {
518817b3
SM
16955 struct type *new_type
16956 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
7078baeb
TT
16957
16958 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16959 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16960 TYPE_VARARGS (to_type));
16961 type = lookup_methodptr_type (new_type);
16962 }
0d5de010
DJ
16963 else
16964 type = lookup_memberptr_type (to_type, domain);
c906108c 16965
f792889a 16966 return set_die_type (die, type, cu);
c906108c
SS
16967}
16968
4297a3f0 16969/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
16970 the user defined type vector. */
16971
f792889a 16972static struct type *
4297a3f0
AV
16973read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16974 enum type_code refcode)
c906108c 16975{
e7c27a73 16976 struct comp_unit_head *cu_header = &cu->header;
7e314c57 16977 struct type *type, *target_type;
c906108c
SS
16978 struct attribute *attr;
16979
4297a3f0
AV
16980 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16981
7e314c57
JK
16982 target_type = die_type (die, cu);
16983
16984 /* The die_type call above may have already set the type for this DIE. */
16985 type = get_die_type (die, cu);
16986 if (type)
16987 return type;
16988
4297a3f0 16989 type = lookup_reference_type (target_type, refcode);
e142c38c 16990 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
16991 if (attr)
16992 {
16993 TYPE_LENGTH (type) = DW_UNSND (attr);
16994 }
16995 else
16996 {
107d2387 16997 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 16998 }
2b4424c3 16999 maybe_set_alignment (cu, die, type);
f792889a 17000 return set_die_type (die, type, cu);
c906108c
SS
17001}
17002
cf363f18
MW
17003/* Add the given cv-qualifiers to the element type of the array. GCC
17004 outputs DWARF type qualifiers that apply to an array, not the
17005 element type. But GDB relies on the array element type to carry
17006 the cv-qualifiers. This mimics section 6.7.3 of the C99
17007 specification. */
17008
17009static struct type *
17010add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17011 struct type *base_type, int cnst, int voltl)
17012{
17013 struct type *el_type, *inner_array;
17014
17015 base_type = copy_type (base_type);
17016 inner_array = base_type;
17017
17018 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
17019 {
17020 TYPE_TARGET_TYPE (inner_array) =
17021 copy_type (TYPE_TARGET_TYPE (inner_array));
17022 inner_array = TYPE_TARGET_TYPE (inner_array);
17023 }
17024
17025 el_type = TYPE_TARGET_TYPE (inner_array);
17026 cnst |= TYPE_CONST (el_type);
17027 voltl |= TYPE_VOLATILE (el_type);
17028 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17029
17030 return set_die_type (die, base_type, cu);
17031}
17032
f792889a 17033static struct type *
e7c27a73 17034read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17035{
f792889a 17036 struct type *base_type, *cv_type;
c906108c 17037
e7c27a73 17038 base_type = die_type (die, cu);
7e314c57
JK
17039
17040 /* The die_type call above may have already set the type for this DIE. */
17041 cv_type = get_die_type (die, cu);
17042 if (cv_type)
17043 return cv_type;
17044
2f608a3a
KW
17045 /* In case the const qualifier is applied to an array type, the element type
17046 is so qualified, not the array type (section 6.7.3 of C99). */
17047 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 17048 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 17049
f792889a
DJ
17050 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17051 return set_die_type (die, cv_type, cu);
c906108c
SS
17052}
17053
f792889a 17054static struct type *
e7c27a73 17055read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17056{
f792889a 17057 struct type *base_type, *cv_type;
c906108c 17058
e7c27a73 17059 base_type = die_type (die, cu);
7e314c57
JK
17060
17061 /* The die_type call above may have already set the type for this DIE. */
17062 cv_type = get_die_type (die, cu);
17063 if (cv_type)
17064 return cv_type;
17065
cf363f18
MW
17066 /* In case the volatile qualifier is applied to an array type, the
17067 element type is so qualified, not the array type (section 6.7.3
17068 of C99). */
17069 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17070 return add_array_cv_type (die, cu, base_type, 0, 1);
17071
f792889a
DJ
17072 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17073 return set_die_type (die, cv_type, cu);
c906108c
SS
17074}
17075
06d66ee9
TT
17076/* Handle DW_TAG_restrict_type. */
17077
17078static struct type *
17079read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17080{
17081 struct type *base_type, *cv_type;
17082
17083 base_type = die_type (die, cu);
17084
17085 /* The die_type call above may have already set the type for this DIE. */
17086 cv_type = get_die_type (die, cu);
17087 if (cv_type)
17088 return cv_type;
17089
17090 cv_type = make_restrict_type (base_type);
17091 return set_die_type (die, cv_type, cu);
17092}
17093
a2c2acaf
MW
17094/* Handle DW_TAG_atomic_type. */
17095
17096static struct type *
17097read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17098{
17099 struct type *base_type, *cv_type;
17100
17101 base_type = die_type (die, cu);
17102
17103 /* The die_type call above may have already set the type for this DIE. */
17104 cv_type = get_die_type (die, cu);
17105 if (cv_type)
17106 return cv_type;
17107
17108 cv_type = make_atomic_type (base_type);
17109 return set_die_type (die, cv_type, cu);
17110}
17111
c906108c
SS
17112/* Extract all information from a DW_TAG_string_type DIE and add to
17113 the user defined type vector. It isn't really a user defined type,
17114 but it behaves like one, with other DIE's using an AT_user_def_type
17115 attribute to reference it. */
17116
f792889a 17117static struct type *
e7c27a73 17118read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17119{
518817b3 17120 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3b7538c0 17121 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
17122 struct type *type, *range_type, *index_type, *char_type;
17123 struct attribute *attr;
17124 unsigned int length;
17125
e142c38c 17126 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
17127 if (attr)
17128 {
17129 length = DW_UNSND (attr);
17130 }
17131 else
17132 {
0963b4bd 17133 /* Check for the DW_AT_byte_size attribute. */
e142c38c 17134 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
17135 if (attr)
17136 {
17137 length = DW_UNSND (attr);
17138 }
17139 else
17140 {
17141 length = 1;
17142 }
c906108c 17143 }
6ccb9162 17144
46bf5051 17145 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 17146 range_type = create_static_range_type (NULL, index_type, 1, length);
3b7538c0
UW
17147 char_type = language_string_char_type (cu->language_defn, gdbarch);
17148 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17149
f792889a 17150 return set_die_type (die, type, cu);
c906108c
SS
17151}
17152
4d804846
JB
17153/* Assuming that DIE corresponds to a function, returns nonzero
17154 if the function is prototyped. */
17155
17156static int
17157prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17158{
17159 struct attribute *attr;
17160
17161 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17162 if (attr && (DW_UNSND (attr) != 0))
17163 return 1;
17164
17165 /* The DWARF standard implies that the DW_AT_prototyped attribute
17166 is only meaninful for C, but the concept also extends to other
17167 languages that allow unprototyped functions (Eg: Objective C).
17168 For all other languages, assume that functions are always
17169 prototyped. */
17170 if (cu->language != language_c
17171 && cu->language != language_objc
17172 && cu->language != language_opencl)
17173 return 1;
17174
17175 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17176 prototyped and unprototyped functions; default to prototyped,
17177 since that is more common in modern code (and RealView warns
17178 about unprototyped functions). */
17179 if (producer_is_realview (cu->producer))
17180 return 1;
17181
17182 return 0;
17183}
17184
c906108c
SS
17185/* Handle DIES due to C code like:
17186
17187 struct foo
c5aa993b
JM
17188 {
17189 int (*funcp)(int a, long l);
17190 int b;
17191 };
c906108c 17192
0963b4bd 17193 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17194
f792889a 17195static struct type *
e7c27a73 17196read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17197{
518817b3 17198 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0963b4bd
MS
17199 struct type *type; /* Type that this function returns. */
17200 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17201 struct attribute *attr;
17202
e7c27a73 17203 type = die_type (die, cu);
7e314c57
JK
17204
17205 /* The die_type call above may have already set the type for this DIE. */
17206 ftype = get_die_type (die, cu);
17207 if (ftype)
17208 return ftype;
17209
0c8b41f1 17210 ftype = lookup_function_type (type);
c906108c 17211
4d804846 17212 if (prototyped_function_p (die, cu))
a6c727b2 17213 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17214
c055b101
CV
17215 /* Store the calling convention in the type if it's available in
17216 the subroutine die. Otherwise set the calling convention to
17217 the default value DW_CC_normal. */
17218 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
17219 if (attr)
17220 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17221 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17222 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17223 else
17224 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17225
743649fd
MW
17226 /* Record whether the function returns normally to its caller or not
17227 if the DWARF producer set that information. */
17228 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17229 if (attr && (DW_UNSND (attr) != 0))
17230 TYPE_NO_RETURN (ftype) = 1;
17231
76c10ea2
GM
17232 /* We need to add the subroutine type to the die immediately so
17233 we don't infinitely recurse when dealing with parameters
0963b4bd 17234 declared as the same subroutine type. */
76c10ea2 17235 set_die_type (die, ftype, cu);
6e70227d 17236
639d11d3 17237 if (die->child != NULL)
c906108c 17238 {
bb5ed363 17239 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17240 struct die_info *child_die;
8072405b 17241 int nparams, iparams;
c906108c
SS
17242
17243 /* Count the number of parameters.
17244 FIXME: GDB currently ignores vararg functions, but knows about
17245 vararg member functions. */
8072405b 17246 nparams = 0;
639d11d3 17247 child_die = die->child;
c906108c
SS
17248 while (child_die && child_die->tag)
17249 {
17250 if (child_die->tag == DW_TAG_formal_parameter)
17251 nparams++;
17252 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17253 TYPE_VARARGS (ftype) = 1;
c906108c
SS
17254 child_die = sibling_die (child_die);
17255 }
17256
17257 /* Allocate storage for parameters and fill them in. */
17258 TYPE_NFIELDS (ftype) = nparams;
17259 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 17260 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 17261
8072405b
JK
17262 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17263 even if we error out during the parameters reading below. */
17264 for (iparams = 0; iparams < nparams; iparams++)
17265 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17266
17267 iparams = 0;
639d11d3 17268 child_die = die->child;
c906108c
SS
17269 while (child_die && child_die->tag)
17270 {
17271 if (child_die->tag == DW_TAG_formal_parameter)
17272 {
3ce3b1ba
PA
17273 struct type *arg_type;
17274
17275 /* DWARF version 2 has no clean way to discern C++
17276 static and non-static member functions. G++ helps
17277 GDB by marking the first parameter for non-static
17278 member functions (which is the this pointer) as
17279 artificial. We pass this information to
17280 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17281
17282 DWARF version 3 added DW_AT_object_pointer, which GCC
17283 4.5 does not yet generate. */
e142c38c 17284 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
17285 if (attr)
17286 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17287 else
9c37b5ae 17288 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17289 arg_type = die_type (child_die, cu);
17290
17291 /* RealView does not mark THIS as const, which the testsuite
17292 expects. GCC marks THIS as const in method definitions,
17293 but not in the class specifications (GCC PR 43053). */
17294 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17295 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17296 {
17297 int is_this = 0;
17298 struct dwarf2_cu *arg_cu = cu;
17299 const char *name = dwarf2_name (child_die, cu);
17300
17301 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17302 if (attr)
17303 {
17304 /* If the compiler emits this, use it. */
17305 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17306 is_this = 1;
17307 }
17308 else if (name && strcmp (name, "this") == 0)
17309 /* Function definitions will have the argument names. */
17310 is_this = 1;
17311 else if (name == NULL && iparams == 0)
17312 /* Declarations may not have the names, so like
17313 elsewhere in GDB, assume an artificial first
17314 argument is "this". */
17315 is_this = 1;
17316
17317 if (is_this)
17318 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17319 arg_type, 0);
17320 }
17321
17322 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
17323 iparams++;
17324 }
17325 child_die = sibling_die (child_die);
17326 }
17327 }
17328
76c10ea2 17329 return ftype;
c906108c
SS
17330}
17331
f792889a 17332static struct type *
e7c27a73 17333read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17334{
518817b3 17335 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 17336 const char *name = NULL;
3c8e0968 17337 struct type *this_type, *target_type;
c906108c 17338
94af9270 17339 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17340 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17341 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17342 set_die_type (die, this_type, cu);
3c8e0968
DE
17343 target_type = die_type (die, cu);
17344 if (target_type != this_type)
17345 TYPE_TARGET_TYPE (this_type) = target_type;
17346 else
17347 {
17348 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17349 spec and cause infinite loops in GDB. */
b98664d3 17350 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17351 "- DIE at %s [in module %s]"),
17352 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17353 TYPE_TARGET_TYPE (this_type) = NULL;
17354 }
f792889a 17355 return this_type;
c906108c
SS
17356}
17357
9b790ce7
UW
17358/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17359 (which may be different from NAME) to the architecture back-end to allow
17360 it to guess the correct format if necessary. */
17361
17362static struct type *
17363dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17364 const char *name_hint)
17365{
17366 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17367 const struct floatformat **format;
17368 struct type *type;
17369
17370 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17371 if (format)
17372 type = init_float_type (objfile, bits, name, format);
17373 else
77b7c781 17374 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17375
17376 return type;
17377}
17378
c906108c
SS
17379/* Find a representation of a given base type and install
17380 it in the TYPE field of the die. */
17381
f792889a 17382static struct type *
e7c27a73 17383read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17384{
518817b3 17385 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
17386 struct type *type;
17387 struct attribute *attr;
19f392bc 17388 int encoding = 0, bits = 0;
15d034d0 17389 const char *name;
c906108c 17390
e142c38c 17391 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
17392 if (attr)
17393 {
17394 encoding = DW_UNSND (attr);
17395 }
e142c38c 17396 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
17397 if (attr)
17398 {
19f392bc 17399 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
c906108c 17400 }
39cbfefa 17401 name = dwarf2_name (die, cu);
6ccb9162 17402 if (!name)
c906108c 17403 {
b98664d3 17404 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
c906108c 17405 }
6ccb9162
UW
17406
17407 switch (encoding)
c906108c 17408 {
6ccb9162
UW
17409 case DW_ATE_address:
17410 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17411 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17412 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17413 break;
17414 case DW_ATE_boolean:
19f392bc 17415 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17416 break;
17417 case DW_ATE_complex_float:
9b790ce7 17418 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
19f392bc 17419 type = init_complex_type (objfile, name, type);
6ccb9162
UW
17420 break;
17421 case DW_ATE_decimal_float:
19f392bc 17422 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17423 break;
17424 case DW_ATE_float:
9b790ce7 17425 type = dwarf2_init_float_type (objfile, bits, name, name);
6ccb9162
UW
17426 break;
17427 case DW_ATE_signed:
19f392bc 17428 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
17429 break;
17430 case DW_ATE_unsigned:
3b2b8fea
TT
17431 if (cu->language == language_fortran
17432 && name
61012eef 17433 && startswith (name, "character("))
19f392bc
UW
17434 type = init_character_type (objfile, bits, 1, name);
17435 else
17436 type = init_integer_type (objfile, bits, 1, name);
6ccb9162
UW
17437 break;
17438 case DW_ATE_signed_char:
6e70227d 17439 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17440 || cu->language == language_pascal
17441 || cu->language == language_fortran)
19f392bc
UW
17442 type = init_character_type (objfile, bits, 0, name);
17443 else
17444 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
17445 break;
17446 case DW_ATE_unsigned_char:
868a0084 17447 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17448 || cu->language == language_pascal
c44af4eb
TT
17449 || cu->language == language_fortran
17450 || cu->language == language_rust)
19f392bc
UW
17451 type = init_character_type (objfile, bits, 1, name);
17452 else
17453 type = init_integer_type (objfile, bits, 1, name);
6ccb9162 17454 break;
75079b2b 17455 case DW_ATE_UTF:
53e710ac
PA
17456 {
17457 gdbarch *arch = get_objfile_arch (objfile);
17458
17459 if (bits == 16)
17460 type = builtin_type (arch)->builtin_char16;
17461 else if (bits == 32)
17462 type = builtin_type (arch)->builtin_char32;
17463 else
17464 {
b98664d3 17465 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac
PA
17466 bits);
17467 type = init_integer_type (objfile, bits, 1, name);
17468 }
17469 return set_die_type (die, type, cu);
17470 }
75079b2b
TT
17471 break;
17472
6ccb9162 17473 default:
b98664d3 17474 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 17475 dwarf_type_encoding_name (encoding));
77b7c781 17476 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17477 break;
c906108c 17478 }
6ccb9162 17479
0114d602 17480 if (name && strcmp (name, "char") == 0)
876cecd0 17481 TYPE_NOSIGN (type) = 1;
0114d602 17482
2b4424c3
TT
17483 maybe_set_alignment (cu, die, type);
17484
f792889a 17485 return set_die_type (die, type, cu);
c906108c
SS
17486}
17487
80180f79
SA
17488/* Parse dwarf attribute if it's a block, reference or constant and put the
17489 resulting value of the attribute into struct bound_prop.
17490 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17491
17492static int
17493attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17494 struct dwarf2_cu *cu, struct dynamic_prop *prop)
17495{
17496 struct dwarf2_property_baton *baton;
518817b3
SM
17497 struct obstack *obstack
17498 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
80180f79
SA
17499
17500 if (attr == NULL || prop == NULL)
17501 return 0;
17502
17503 if (attr_form_is_block (attr))
17504 {
8d749320 17505 baton = XOBNEW (obstack, struct dwarf2_property_baton);
80180f79
SA
17506 baton->referenced_type = NULL;
17507 baton->locexpr.per_cu = cu->per_cu;
17508 baton->locexpr.size = DW_BLOCK (attr)->size;
17509 baton->locexpr.data = DW_BLOCK (attr)->data;
17510 prop->data.baton = baton;
17511 prop->kind = PROP_LOCEXPR;
17512 gdb_assert (prop->data.baton != NULL);
17513 }
17514 else if (attr_form_is_ref (attr))
17515 {
17516 struct dwarf2_cu *target_cu = cu;
17517 struct die_info *target_die;
17518 struct attribute *target_attr;
17519
17520 target_die = follow_die_ref (die, attr, &target_cu);
17521 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17522 if (target_attr == NULL)
17523 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17524 target_cu);
80180f79
SA
17525 if (target_attr == NULL)
17526 return 0;
17527
df25ebbd 17528 switch (target_attr->name)
80180f79 17529 {
df25ebbd
JB
17530 case DW_AT_location:
17531 if (attr_form_is_section_offset (target_attr))
17532 {
8d749320 17533 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
17534 baton->referenced_type = die_type (target_die, target_cu);
17535 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17536 prop->data.baton = baton;
17537 prop->kind = PROP_LOCLIST;
17538 gdb_assert (prop->data.baton != NULL);
17539 }
17540 else if (attr_form_is_block (target_attr))
17541 {
8d749320 17542 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
17543 baton->referenced_type = die_type (target_die, target_cu);
17544 baton->locexpr.per_cu = cu->per_cu;
17545 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17546 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17547 prop->data.baton = baton;
17548 prop->kind = PROP_LOCEXPR;
17549 gdb_assert (prop->data.baton != NULL);
17550 }
17551 else
17552 {
17553 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17554 "dynamic property");
17555 return 0;
17556 }
17557 break;
17558 case DW_AT_data_member_location:
17559 {
17560 LONGEST offset;
17561
17562 if (!handle_data_member_location (target_die, target_cu,
17563 &offset))
17564 return 0;
17565
8d749320 17566 baton = XOBNEW (obstack, struct dwarf2_property_baton);
6ad395a7
JB
17567 baton->referenced_type = read_type_die (target_die->parent,
17568 target_cu);
df25ebbd
JB
17569 baton->offset_info.offset = offset;
17570 baton->offset_info.type = die_type (target_die, target_cu);
17571 prop->data.baton = baton;
17572 prop->kind = PROP_ADDR_OFFSET;
17573 break;
17574 }
80180f79
SA
17575 }
17576 }
17577 else if (attr_form_is_constant (attr))
17578 {
17579 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17580 prop->kind = PROP_CONST;
17581 }
17582 else
17583 {
17584 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17585 dwarf2_name (die, cu));
17586 return 0;
17587 }
17588
17589 return 1;
17590}
17591
a02abb62
JB
17592/* Read the given DW_AT_subrange DIE. */
17593
f792889a 17594static struct type *
a02abb62
JB
17595read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17596{
4c9ad8c2 17597 struct type *base_type, *orig_base_type;
a02abb62
JB
17598 struct type *range_type;
17599 struct attribute *attr;
729efb13 17600 struct dynamic_prop low, high;
4fae6e18 17601 int low_default_is_valid;
c451ebe5 17602 int high_bound_is_count = 0;
15d034d0 17603 const char *name;
43bbcdc2 17604 LONGEST negative_mask;
e77813c8 17605
4c9ad8c2
TT
17606 orig_base_type = die_type (die, cu);
17607 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17608 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17609 creating the range type, but we use the result of check_typedef
17610 when examining properties of the type. */
17611 base_type = check_typedef (orig_base_type);
a02abb62 17612
7e314c57
JK
17613 /* The die_type call above may have already set the type for this DIE. */
17614 range_type = get_die_type (die, cu);
17615 if (range_type)
17616 return range_type;
17617
729efb13
SA
17618 low.kind = PROP_CONST;
17619 high.kind = PROP_CONST;
17620 high.data.const_val = 0;
17621
4fae6e18
JK
17622 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17623 omitting DW_AT_lower_bound. */
17624 switch (cu->language)
6e70227d 17625 {
4fae6e18
JK
17626 case language_c:
17627 case language_cplus:
729efb13 17628 low.data.const_val = 0;
4fae6e18
JK
17629 low_default_is_valid = 1;
17630 break;
17631 case language_fortran:
729efb13 17632 low.data.const_val = 1;
4fae6e18
JK
17633 low_default_is_valid = 1;
17634 break;
17635 case language_d:
4fae6e18 17636 case language_objc:
c44af4eb 17637 case language_rust:
729efb13 17638 low.data.const_val = 0;
4fae6e18
JK
17639 low_default_is_valid = (cu->header.version >= 4);
17640 break;
17641 case language_ada:
17642 case language_m2:
17643 case language_pascal:
729efb13 17644 low.data.const_val = 1;
4fae6e18
JK
17645 low_default_is_valid = (cu->header.version >= 4);
17646 break;
17647 default:
729efb13 17648 low.data.const_val = 0;
4fae6e18
JK
17649 low_default_is_valid = 0;
17650 break;
a02abb62
JB
17651 }
17652
e142c38c 17653 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 17654 if (attr)
11c1ba78 17655 attr_to_dynamic_prop (attr, die, cu, &low);
4fae6e18 17656 else if (!low_default_is_valid)
b98664d3 17657 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
17658 "- DIE at %s [in module %s]"),
17659 sect_offset_str (die->sect_off),
518817b3 17660 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a02abb62 17661
506f5c41
TV
17662 struct attribute *attr_ub, *attr_count;
17663 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
80180f79 17664 if (!attr_to_dynamic_prop (attr, die, cu, &high))
e77813c8 17665 {
506f5c41 17666 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
c451ebe5 17667 if (attr_to_dynamic_prop (attr, die, cu, &high))
6b662e19 17668 {
c451ebe5
SA
17669 /* If bounds are constant do the final calculation here. */
17670 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17671 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17672 else
17673 high_bound_is_count = 1;
c2ff108b 17674 }
506f5c41
TV
17675 else
17676 {
17677 if (attr_ub != NULL)
17678 complaint (_("Unresolved DW_AT_upper_bound "
17679 "- DIE at %s [in module %s]"),
17680 sect_offset_str (die->sect_off),
17681 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17682 if (attr_count != NULL)
17683 complaint (_("Unresolved DW_AT_count "
17684 "- DIE at %s [in module %s]"),
17685 sect_offset_str (die->sect_off),
17686 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17687 }
17688
e77813c8
PM
17689 }
17690
17691 /* Dwarf-2 specifications explicitly allows to create subrange types
17692 without specifying a base type.
17693 In that case, the base type must be set to the type of
17694 the lower bound, upper bound or count, in that order, if any of these
17695 three attributes references an object that has a type.
17696 If no base type is found, the Dwarf-2 specifications say that
17697 a signed integer type of size equal to the size of an address should
17698 be used.
17699 For the following C code: `extern char gdb_int [];'
17700 GCC produces an empty range DIE.
17701 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 17702 high bound or count are not yet handled by this code. */
e77813c8
PM
17703 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
17704 {
518817b3 17705 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e77813c8
PM
17706 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17707 int addr_size = gdbarch_addr_bit (gdbarch) /8;
17708 struct type *int_type = objfile_type (objfile)->builtin_int;
17709
17710 /* Test "int", "long int", and "long long int" objfile types,
17711 and select the first one having a size above or equal to the
17712 architecture address size. */
17713 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17714 base_type = int_type;
17715 else
17716 {
17717 int_type = objfile_type (objfile)->builtin_long;
17718 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17719 base_type = int_type;
17720 else
17721 {
17722 int_type = objfile_type (objfile)->builtin_long_long;
17723 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17724 base_type = int_type;
17725 }
17726 }
17727 }
a02abb62 17728
dbb9c2b1
JB
17729 /* Normally, the DWARF producers are expected to use a signed
17730 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17731 But this is unfortunately not always the case, as witnessed
17732 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17733 is used instead. To work around that ambiguity, we treat
17734 the bounds as signed, and thus sign-extend their values, when
17735 the base type is signed. */
6e70227d 17736 negative_mask =
66c6502d 17737 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17738 if (low.kind == PROP_CONST
17739 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17740 low.data.const_val |= negative_mask;
17741 if (high.kind == PROP_CONST
17742 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17743 high.data.const_val |= negative_mask;
43bbcdc2 17744
729efb13 17745 range_type = create_range_type (NULL, orig_base_type, &low, &high);
a02abb62 17746
c451ebe5
SA
17747 if (high_bound_is_count)
17748 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17749
c2ff108b
JK
17750 /* Ada expects an empty array on no boundary attributes. */
17751 if (attr == NULL && cu->language != language_ada)
729efb13 17752 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 17753
39cbfefa
DJ
17754 name = dwarf2_name (die, cu);
17755 if (name)
17756 TYPE_NAME (range_type) = name;
6e70227d 17757
e142c38c 17758 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
17759 if (attr)
17760 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17761
2b4424c3
TT
17762 maybe_set_alignment (cu, die, range_type);
17763
7e314c57
JK
17764 set_die_type (die, range_type, cu);
17765
17766 /* set_die_type should be already done. */
b4ba55a1
JB
17767 set_descriptive_type (range_type, die, cu);
17768
7e314c57 17769 return range_type;
a02abb62 17770}
6e70227d 17771
f792889a 17772static struct type *
81a17f79
JB
17773read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17774{
17775 struct type *type;
81a17f79 17776
518817b3
SM
17777 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17778 NULL);
0114d602 17779 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 17780
74a2f8ff
JB
17781 /* In Ada, an unspecified type is typically used when the description
17782 of the type is defered to a different unit. When encountering
17783 such a type, we treat it as a stub, and try to resolve it later on,
17784 when needed. */
17785 if (cu->language == language_ada)
17786 TYPE_STUB (type) = 1;
17787
f792889a 17788 return set_die_type (die, type, cu);
81a17f79 17789}
a02abb62 17790
639d11d3
DC
17791/* Read a single die and all its descendents. Set the die's sibling
17792 field to NULL; set other fields in the die correctly, and set all
17793 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17794 location of the info_ptr after reading all of those dies. PARENT
17795 is the parent of the die in question. */
17796
17797static struct die_info *
dee91e82 17798read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
17799 const gdb_byte *info_ptr,
17800 const gdb_byte **new_info_ptr,
dee91e82 17801 struct die_info *parent)
639d11d3
DC
17802{
17803 struct die_info *die;
d521ce57 17804 const gdb_byte *cur_ptr;
639d11d3
DC
17805 int has_children;
17806
bf6af496 17807 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
17808 if (die == NULL)
17809 {
17810 *new_info_ptr = cur_ptr;
17811 return NULL;
17812 }
93311388 17813 store_in_ref_table (die, reader->cu);
639d11d3
DC
17814
17815 if (has_children)
bf6af496 17816 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
17817 else
17818 {
17819 die->child = NULL;
17820 *new_info_ptr = cur_ptr;
17821 }
17822
17823 die->sibling = NULL;
17824 die->parent = parent;
17825 return die;
17826}
17827
17828/* Read a die, all of its descendents, and all of its siblings; set
17829 all of the fields of all of the dies correctly. Arguments are as
17830 in read_die_and_children. */
17831
17832static struct die_info *
bf6af496 17833read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
17834 const gdb_byte *info_ptr,
17835 const gdb_byte **new_info_ptr,
bf6af496 17836 struct die_info *parent)
639d11d3
DC
17837{
17838 struct die_info *first_die, *last_sibling;
d521ce57 17839 const gdb_byte *cur_ptr;
639d11d3 17840
c906108c 17841 cur_ptr = info_ptr;
639d11d3
DC
17842 first_die = last_sibling = NULL;
17843
17844 while (1)
c906108c 17845 {
639d11d3 17846 struct die_info *die
dee91e82 17847 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 17848
1d325ec1 17849 if (die == NULL)
c906108c 17850 {
639d11d3
DC
17851 *new_info_ptr = cur_ptr;
17852 return first_die;
c906108c 17853 }
1d325ec1
DJ
17854
17855 if (!first_die)
17856 first_die = die;
c906108c 17857 else
1d325ec1
DJ
17858 last_sibling->sibling = die;
17859
17860 last_sibling = die;
c906108c 17861 }
c906108c
SS
17862}
17863
bf6af496
DE
17864/* Read a die, all of its descendents, and all of its siblings; set
17865 all of the fields of all of the dies correctly. Arguments are as
17866 in read_die_and_children.
17867 This the main entry point for reading a DIE and all its children. */
17868
17869static struct die_info *
17870read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
17871 const gdb_byte *info_ptr,
17872 const gdb_byte **new_info_ptr,
bf6af496
DE
17873 struct die_info *parent)
17874{
17875 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17876 new_info_ptr, parent);
17877
b4f54984 17878 if (dwarf_die_debug)
bf6af496
DE
17879 {
17880 fprintf_unfiltered (gdb_stdlog,
17881 "Read die from %s@0x%x of %s:\n",
a32a8923 17882 get_section_name (reader->die_section),
bf6af496
DE
17883 (unsigned) (info_ptr - reader->die_section->buffer),
17884 bfd_get_filename (reader->abfd));
b4f54984 17885 dump_die (die, dwarf_die_debug);
bf6af496
DE
17886 }
17887
17888 return die;
17889}
17890
3019eac3
DE
17891/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17892 attributes.
17893 The caller is responsible for filling in the extra attributes
17894 and updating (*DIEP)->num_attrs.
17895 Set DIEP to point to a newly allocated die with its information,
17896 except for its child, sibling, and parent fields.
17897 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 17898
d521ce57 17899static const gdb_byte *
3019eac3 17900read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 17901 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 17902 int *has_children, int num_extra_attrs)
93311388 17903{
b64f50a1 17904 unsigned int abbrev_number, bytes_read, i;
93311388
DE
17905 struct abbrev_info *abbrev;
17906 struct die_info *die;
17907 struct dwarf2_cu *cu = reader->cu;
17908 bfd *abfd = reader->abfd;
17909
9c541725 17910 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
17911 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17912 info_ptr += bytes_read;
17913 if (!abbrev_number)
17914 {
17915 *diep = NULL;
17916 *has_children = 0;
17917 return info_ptr;
17918 }
17919
685af9cd 17920 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 17921 if (!abbrev)
348e048f
DE
17922 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17923 abbrev_number,
17924 bfd_get_filename (abfd));
17925
3019eac3 17926 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 17927 die->sect_off = sect_off;
93311388
DE
17928 die->tag = abbrev->tag;
17929 die->abbrev = abbrev_number;
17930
3019eac3
DE
17931 /* Make the result usable.
17932 The caller needs to update num_attrs after adding the extra
17933 attributes. */
93311388
DE
17934 die->num_attrs = abbrev->num_attrs;
17935
17936 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
17937 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17938 info_ptr);
93311388
DE
17939
17940 *diep = die;
17941 *has_children = abbrev->has_children;
17942 return info_ptr;
17943}
17944
3019eac3
DE
17945/* Read a die and all its attributes.
17946 Set DIEP to point to a newly allocated die with its information,
17947 except for its child, sibling, and parent fields.
17948 Set HAS_CHILDREN to tell whether the die has children or not. */
17949
d521ce57 17950static const gdb_byte *
3019eac3 17951read_full_die (const struct die_reader_specs *reader,
d521ce57 17952 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
17953 int *has_children)
17954{
d521ce57 17955 const gdb_byte *result;
bf6af496
DE
17956
17957 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
17958
b4f54984 17959 if (dwarf_die_debug)
bf6af496
DE
17960 {
17961 fprintf_unfiltered (gdb_stdlog,
17962 "Read die from %s@0x%x of %s:\n",
a32a8923 17963 get_section_name (reader->die_section),
bf6af496
DE
17964 (unsigned) (info_ptr - reader->die_section->buffer),
17965 bfd_get_filename (reader->abfd));
b4f54984 17966 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
17967 }
17968
17969 return result;
3019eac3 17970}
433df2d4
DE
17971\f
17972/* Abbreviation tables.
3019eac3 17973
433df2d4 17974 In DWARF version 2, the description of the debugging information is
c906108c
SS
17975 stored in a separate .debug_abbrev section. Before we read any
17976 dies from a section we read in all abbreviations and install them
433df2d4
DE
17977 in a hash table. */
17978
17979/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
17980
685af9cd
TT
17981struct abbrev_info *
17982abbrev_table::alloc_abbrev ()
433df2d4
DE
17983{
17984 struct abbrev_info *abbrev;
17985
685af9cd 17986 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
433df2d4 17987 memset (abbrev, 0, sizeof (struct abbrev_info));
8d749320 17988
433df2d4
DE
17989 return abbrev;
17990}
17991
17992/* Add an abbreviation to the table. */
c906108c 17993
685af9cd
TT
17994void
17995abbrev_table::add_abbrev (unsigned int abbrev_number,
17996 struct abbrev_info *abbrev)
433df2d4
DE
17997{
17998 unsigned int hash_number;
17999
18000 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768
YQ
18001 abbrev->next = m_abbrevs[hash_number];
18002 m_abbrevs[hash_number] = abbrev;
433df2d4 18003}
dee91e82 18004
433df2d4
DE
18005/* Look up an abbrev in the table.
18006 Returns NULL if the abbrev is not found. */
18007
685af9cd
TT
18008struct abbrev_info *
18009abbrev_table::lookup_abbrev (unsigned int abbrev_number)
c906108c 18010{
433df2d4
DE
18011 unsigned int hash_number;
18012 struct abbrev_info *abbrev;
18013
18014 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768 18015 abbrev = m_abbrevs[hash_number];
433df2d4
DE
18016
18017 while (abbrev)
18018 {
18019 if (abbrev->number == abbrev_number)
18020 return abbrev;
18021 abbrev = abbrev->next;
18022 }
18023 return NULL;
18024}
18025
18026/* Read in an abbrev table. */
18027
685af9cd 18028static abbrev_table_up
ed2dc618
SM
18029abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18030 struct dwarf2_section_info *section,
9c541725 18031 sect_offset sect_off)
433df2d4
DE
18032{
18033 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 18034 bfd *abfd = get_section_bfd_owner (section);
d521ce57 18035 const gdb_byte *abbrev_ptr;
c906108c
SS
18036 struct abbrev_info *cur_abbrev;
18037 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 18038 unsigned int abbrev_form;
f3dd6933
DJ
18039 struct attr_abbrev *cur_attrs;
18040 unsigned int allocated_attrs;
c906108c 18041
685af9cd 18042 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
c906108c 18043
433df2d4 18044 dwarf2_read_section (objfile, section);
9c541725 18045 abbrev_ptr = section->buffer + to_underlying (sect_off);
c906108c
SS
18046 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18047 abbrev_ptr += bytes_read;
18048
f3dd6933 18049 allocated_attrs = ATTR_ALLOC_CHUNK;
8d749320 18050 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
6e70227d 18051
0963b4bd 18052 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
18053 while (abbrev_number)
18054 {
685af9cd 18055 cur_abbrev = abbrev_table->alloc_abbrev ();
c906108c
SS
18056
18057 /* read in abbrev header */
18058 cur_abbrev->number = abbrev_number;
aead7601
SM
18059 cur_abbrev->tag
18060 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
c906108c
SS
18061 abbrev_ptr += bytes_read;
18062 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
18063 abbrev_ptr += 1;
18064
18065 /* now read in declarations */
22d2f3ab 18066 for (;;)
c906108c 18067 {
43988095
JK
18068 LONGEST implicit_const;
18069
22d2f3ab
JK
18070 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18071 abbrev_ptr += bytes_read;
18072 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18073 abbrev_ptr += bytes_read;
43988095
JK
18074 if (abbrev_form == DW_FORM_implicit_const)
18075 {
18076 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18077 &bytes_read);
18078 abbrev_ptr += bytes_read;
18079 }
18080 else
18081 {
18082 /* Initialize it due to a false compiler warning. */
18083 implicit_const = -1;
18084 }
22d2f3ab
JK
18085
18086 if (abbrev_name == 0)
18087 break;
18088
f3dd6933 18089 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 18090 {
f3dd6933
DJ
18091 allocated_attrs += ATTR_ALLOC_CHUNK;
18092 cur_attrs
224c3ddb 18093 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
c906108c 18094 }
ae038cb0 18095
aead7601
SM
18096 cur_attrs[cur_abbrev->num_attrs].name
18097 = (enum dwarf_attribute) abbrev_name;
22d2f3ab 18098 cur_attrs[cur_abbrev->num_attrs].form
aead7601 18099 = (enum dwarf_form) abbrev_form;
43988095 18100 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
22d2f3ab 18101 ++cur_abbrev->num_attrs;
c906108c
SS
18102 }
18103
8d749320
SM
18104 cur_abbrev->attrs =
18105 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18106 cur_abbrev->num_attrs);
f3dd6933
DJ
18107 memcpy (cur_abbrev->attrs, cur_attrs,
18108 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18109
685af9cd 18110 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
c906108c
SS
18111
18112 /* Get next abbreviation.
18113 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
18114 always properly terminated with an abbrev number of 0.
18115 Exit loop if we encounter an abbreviation which we have
18116 already read (which means we are about to read the abbreviations
18117 for the next compile unit) or if the end of the abbreviation
18118 table is reached. */
433df2d4 18119 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
18120 break;
18121 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18122 abbrev_ptr += bytes_read;
685af9cd 18123 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
c906108c
SS
18124 break;
18125 }
f3dd6933
DJ
18126
18127 xfree (cur_attrs);
433df2d4 18128 return abbrev_table;
c906108c
SS
18129}
18130
72bf9492
DJ
18131/* Returns nonzero if TAG represents a type that we might generate a partial
18132 symbol for. */
18133
18134static int
18135is_type_tag_for_partial (int tag)
18136{
18137 switch (tag)
18138 {
18139#if 0
18140 /* Some types that would be reasonable to generate partial symbols for,
18141 that we don't at present. */
18142 case DW_TAG_array_type:
18143 case DW_TAG_file_type:
18144 case DW_TAG_ptr_to_member_type:
18145 case DW_TAG_set_type:
18146 case DW_TAG_string_type:
18147 case DW_TAG_subroutine_type:
18148#endif
18149 case DW_TAG_base_type:
18150 case DW_TAG_class_type:
680b30c7 18151 case DW_TAG_interface_type:
72bf9492
DJ
18152 case DW_TAG_enumeration_type:
18153 case DW_TAG_structure_type:
18154 case DW_TAG_subrange_type:
18155 case DW_TAG_typedef:
18156 case DW_TAG_union_type:
18157 return 1;
18158 default:
18159 return 0;
18160 }
18161}
18162
18163/* Load all DIEs that are interesting for partial symbols into memory. */
18164
18165static struct partial_die_info *
dee91e82 18166load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18167 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18168{
dee91e82 18169 struct dwarf2_cu *cu = reader->cu;
518817b3 18170 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
72bf9492 18171 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18172 unsigned int bytes_read;
5afb4e99 18173 unsigned int load_all = 0;
72bf9492
DJ
18174 int nesting_level = 1;
18175
18176 parent_die = NULL;
18177 last_die = NULL;
18178
7adf1e79
DE
18179 gdb_assert (cu->per_cu != NULL);
18180 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18181 load_all = 1;
18182
72bf9492
DJ
18183 cu->partial_dies
18184 = htab_create_alloc_ex (cu->header.length / 12,
18185 partial_die_hash,
18186 partial_die_eq,
18187 NULL,
18188 &cu->comp_unit_obstack,
18189 hashtab_obstack_allocate,
18190 dummy_obstack_deallocate);
18191
72bf9492
DJ
18192 while (1)
18193 {
685af9cd 18194 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18195
18196 /* A NULL abbrev means the end of a series of children. */
18197 if (abbrev == NULL)
18198 {
18199 if (--nesting_level == 0)
cd9983dd
YQ
18200 return first_die;
18201
72bf9492
DJ
18202 info_ptr += bytes_read;
18203 last_die = parent_die;
18204 parent_die = parent_die->die_parent;
18205 continue;
18206 }
18207
98bfdba5
PA
18208 /* Check for template arguments. We never save these; if
18209 they're seen, we just mark the parent, and go on our way. */
18210 if (parent_die != NULL
18211 && cu->language == language_cplus
18212 && (abbrev->tag == DW_TAG_template_type_param
18213 || abbrev->tag == DW_TAG_template_value_param))
18214 {
18215 parent_die->has_template_arguments = 1;
18216
18217 if (!load_all)
18218 {
18219 /* We don't need a partial DIE for the template argument. */
dee91e82 18220 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18221 continue;
18222 }
18223 }
18224
0d99eb77 18225 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18226 Skip their other children. */
18227 if (!load_all
18228 && cu->language == language_cplus
18229 && parent_die != NULL
18230 && parent_die->tag == DW_TAG_subprogram)
18231 {
dee91e82 18232 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18233 continue;
18234 }
18235
5afb4e99
DJ
18236 /* Check whether this DIE is interesting enough to save. Normally
18237 we would not be interested in members here, but there may be
18238 later variables referencing them via DW_AT_specification (for
18239 static members). */
18240 if (!load_all
18241 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18242 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18243 && abbrev->tag != DW_TAG_enumerator
18244 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18245 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18246 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18247 && abbrev->tag != DW_TAG_variable
5afb4e99 18248 && abbrev->tag != DW_TAG_namespace
f55ee35c 18249 && abbrev->tag != DW_TAG_module
95554aad 18250 && abbrev->tag != DW_TAG_member
74921315
KS
18251 && abbrev->tag != DW_TAG_imported_unit
18252 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18253 {
18254 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18255 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18256 continue;
18257 }
18258
6f06d47b
YQ
18259 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18260 abbrev);
cd9983dd 18261
48fbe735 18262 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18263
18264 /* This two-pass algorithm for processing partial symbols has a
18265 high cost in cache pressure. Thus, handle some simple cases
18266 here which cover the majority of C partial symbols. DIEs
18267 which neither have specification tags in them, nor could have
18268 specification tags elsewhere pointing at them, can simply be
18269 processed and discarded.
18270
18271 This segment is also optional; scan_partial_symbols and
18272 add_partial_symbol will handle these DIEs if we chain
18273 them in normally. When compilers which do not emit large
18274 quantities of duplicate debug information are more common,
18275 this code can probably be removed. */
18276
18277 /* Any complete simple types at the top level (pretty much all
18278 of them, for a language without namespaces), can be processed
18279 directly. */
18280 if (parent_die == NULL
cd9983dd
YQ
18281 && pdi.has_specification == 0
18282 && pdi.is_declaration == 0
18283 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18284 || pdi.tag == DW_TAG_base_type
18285 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18286 {
cd9983dd
YQ
18287 if (building_psymtab && pdi.name != NULL)
18288 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
79748972 18289 VAR_DOMAIN, LOC_TYPEDEF, -1,
bb5ed363 18290 &objfile->static_psymbols,
1762568f 18291 0, cu->language, objfile);
cd9983dd 18292 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18293 continue;
18294 }
18295
d8228535
JK
18296 /* The exception for DW_TAG_typedef with has_children above is
18297 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 18298 type_name_or_error will error on such types later.
d8228535
JK
18299
18300 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18301 it could not find the child DIEs referenced later, this is checked
18302 above. In correct DWARF DW_TAG_typedef should have no children. */
18303
cd9983dd 18304 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 18305 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18306 "- DIE at %s [in module %s]"),
cd9983dd 18307 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18308
72bf9492
DJ
18309 /* If we're at the second level, and we're an enumerator, and
18310 our parent has no specification (meaning possibly lives in a
18311 namespace elsewhere), then we can add the partial symbol now
18312 instead of queueing it. */
cd9983dd 18313 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18314 && parent_die != NULL
18315 && parent_die->die_parent == NULL
18316 && parent_die->tag == DW_TAG_enumeration_type
18317 && parent_die->has_specification == 0)
18318 {
cd9983dd 18319 if (pdi.name == NULL)
b98664d3 18320 complaint (_("malformed enumerator DIE ignored"));
72bf9492 18321 else if (building_psymtab)
cd9983dd 18322 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
79748972 18323 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 18324 cu->language == language_cplus
bb5ed363
DE
18325 ? &objfile->global_psymbols
18326 : &objfile->static_psymbols,
1762568f 18327 0, cu->language, objfile);
72bf9492 18328
cd9983dd 18329 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18330 continue;
18331 }
18332
cd9983dd 18333 struct partial_die_info *part_die
6f06d47b 18334 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18335
72bf9492
DJ
18336 /* We'll save this DIE so link it in. */
18337 part_die->die_parent = parent_die;
18338 part_die->die_sibling = NULL;
18339 part_die->die_child = NULL;
18340
18341 if (last_die && last_die == parent_die)
18342 last_die->die_child = part_die;
18343 else if (last_die)
18344 last_die->die_sibling = part_die;
18345
18346 last_die = part_die;
18347
18348 if (first_die == NULL)
18349 first_die = part_die;
18350
18351 /* Maybe add the DIE to the hash table. Not all DIEs that we
18352 find interesting need to be in the hash table, because we
18353 also have the parent/sibling/child chains; only those that we
18354 might refer to by offset later during partial symbol reading.
18355
18356 For now this means things that might have be the target of a
18357 DW_AT_specification, DW_AT_abstract_origin, or
18358 DW_AT_extension. DW_AT_extension will refer only to
18359 namespaces; DW_AT_abstract_origin refers to functions (and
18360 many things under the function DIE, but we do not recurse
18361 into function DIEs during partial symbol reading) and
18362 possibly variables as well; DW_AT_specification refers to
18363 declarations. Declarations ought to have the DW_AT_declaration
18364 flag. It happens that GCC forgets to put it in sometimes, but
18365 only for functions, not for types.
18366
18367 Adding more things than necessary to the hash table is harmless
18368 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18369 wasted time in find_partial_die, when we reread the compilation
18370 unit with load_all_dies set. */
72bf9492 18371
5afb4e99 18372 if (load_all
72929c62 18373 || abbrev->tag == DW_TAG_constant
5afb4e99 18374 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18375 || abbrev->tag == DW_TAG_variable
18376 || abbrev->tag == DW_TAG_namespace
18377 || part_die->is_declaration)
18378 {
18379 void **slot;
18380
18381 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18382 to_underlying (part_die->sect_off),
18383 INSERT);
72bf9492
DJ
18384 *slot = part_die;
18385 }
18386
72bf9492 18387 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18388 we have no reason to follow the children of structures; for other
98bfdba5
PA
18389 languages we have to, so that we can get at method physnames
18390 to infer fully qualified class names, for DW_AT_specification,
18391 and for C++ template arguments. For C++, we also look one level
18392 inside functions to find template arguments (if the name of the
18393 function does not already contain the template arguments).
bc30ff58
JB
18394
18395 For Ada, we need to scan the children of subprograms and lexical
18396 blocks as well because Ada allows the definition of nested
18397 entities that could be interesting for the debugger, such as
18398 nested subprograms for instance. */
72bf9492 18399 if (last_die->has_children
5afb4e99
DJ
18400 && (load_all
18401 || last_die->tag == DW_TAG_namespace
f55ee35c 18402 || last_die->tag == DW_TAG_module
72bf9492 18403 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18404 || (cu->language == language_cplus
18405 && last_die->tag == DW_TAG_subprogram
18406 && (last_die->name == NULL
18407 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
18408 || (cu->language != language_c
18409 && (last_die->tag == DW_TAG_class_type
680b30c7 18410 || last_die->tag == DW_TAG_interface_type
72bf9492 18411 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
18412 || last_die->tag == DW_TAG_union_type))
18413 || (cu->language == language_ada
18414 && (last_die->tag == DW_TAG_subprogram
18415 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18416 {
18417 nesting_level++;
18418 parent_die = last_die;
18419 continue;
18420 }
18421
18422 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18423 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18424
18425 /* Back to the top, do it again. */
18426 }
18427}
18428
6f06d47b
YQ
18429partial_die_info::partial_die_info (sect_offset sect_off_,
18430 struct abbrev_info *abbrev)
18431 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18432{
18433}
18434
35cc7ed7
YQ
18435/* Read a minimal amount of information into the minimal die structure.
18436 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 18437
48fbe735
YQ
18438const gdb_byte *
18439partial_die_info::read (const struct die_reader_specs *reader,
18440 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 18441{
dee91e82 18442 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18443 struct dwarf2_per_objfile *dwarf2_per_objfile
18444 = cu->per_cu->dwarf2_per_objfile;
fa238c03 18445 unsigned int i;
c5aa993b 18446 int has_low_pc_attr = 0;
c906108c 18447 int has_high_pc_attr = 0;
91da1414 18448 int high_pc_relative = 0;
c906108c 18449
fd0a254f 18450 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 18451 {
48fbe735
YQ
18452 struct attribute attr;
18453
fd0a254f 18454 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
c906108c
SS
18455
18456 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18457 partial symbol table. */
c906108c
SS
18458 switch (attr.name)
18459 {
18460 case DW_AT_name:
48fbe735 18461 switch (tag)
71c25dea
TT
18462 {
18463 case DW_TAG_compile_unit:
95554aad 18464 case DW_TAG_partial_unit:
348e048f 18465 case DW_TAG_type_unit:
71c25dea
TT
18466 /* Compilation units have a DW_AT_name that is a filename, not
18467 a source language identifier. */
18468 case DW_TAG_enumeration_type:
18469 case DW_TAG_enumerator:
18470 /* These tags always have simple identifiers already; no need
18471 to canonicalize them. */
48fbe735 18472 name = DW_STRING (&attr);
71c25dea
TT
18473 break;
18474 default:
48fbe735
YQ
18475 {
18476 struct objfile *objfile = dwarf2_per_objfile->objfile;
18477
18478 name
18479 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18480 &objfile->per_bfd->storage_obstack);
18481 }
71c25dea
TT
18482 break;
18483 }
c906108c 18484 break;
31ef98ae 18485 case DW_AT_linkage_name:
c906108c 18486 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18487 /* Note that both forms of linkage name might appear. We
18488 assume they will be the same, and we only store the last
18489 one we see. */
94af9270 18490 if (cu->language == language_ada)
48fbe735
YQ
18491 name = DW_STRING (&attr);
18492 linkage_name = DW_STRING (&attr);
c906108c
SS
18493 break;
18494 case DW_AT_low_pc:
18495 has_low_pc_attr = 1;
48fbe735 18496 lowpc = attr_value_as_address (&attr);
c906108c
SS
18497 break;
18498 case DW_AT_high_pc:
18499 has_high_pc_attr = 1;
48fbe735 18500 highpc = attr_value_as_address (&attr);
31aa7e4e
JB
18501 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18502 high_pc_relative = 1;
c906108c
SS
18503 break;
18504 case DW_AT_location:
0963b4bd 18505 /* Support the .debug_loc offsets. */
8e19ed76
PS
18506 if (attr_form_is_block (&attr))
18507 {
48fbe735 18508 d.locdesc = DW_BLOCK (&attr);
8e19ed76 18509 }
3690dd37 18510 else if (attr_form_is_section_offset (&attr))
8e19ed76 18511 {
4d3c2250 18512 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18513 }
18514 else
18515 {
4d3c2250
KB
18516 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18517 "partial symbol information");
8e19ed76 18518 }
c906108c 18519 break;
c906108c 18520 case DW_AT_external:
48fbe735 18521 is_external = DW_UNSND (&attr);
c906108c
SS
18522 break;
18523 case DW_AT_declaration:
48fbe735 18524 is_declaration = DW_UNSND (&attr);
c906108c
SS
18525 break;
18526 case DW_AT_type:
48fbe735 18527 has_type = 1;
c906108c
SS
18528 break;
18529 case DW_AT_abstract_origin:
18530 case DW_AT_specification:
72bf9492 18531 case DW_AT_extension:
48fbe735
YQ
18532 has_specification = 1;
18533 spec_offset = dwarf2_get_ref_die_offset (&attr);
18534 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 18535 || cu->per_cu->is_dwz);
c906108c
SS
18536 break;
18537 case DW_AT_sibling:
18538 /* Ignore absolute siblings, they might point outside of
18539 the current compile unit. */
18540 if (attr.form == DW_FORM_ref_addr)
b98664d3 18541 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 18542 else
b9502d3f 18543 {
48fbe735 18544 const gdb_byte *buffer = reader->buffer;
9c541725
PA
18545 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18546 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18547
18548 if (sibling_ptr < info_ptr)
b98664d3 18549 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
18550 else if (sibling_ptr > reader->buffer_end)
18551 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f 18552 else
48fbe735 18553 sibling = sibling_ptr;
b9502d3f 18554 }
c906108c 18555 break;
fa4028e9 18556 case DW_AT_byte_size:
48fbe735 18557 has_byte_size = 1;
fa4028e9 18558 break;
ff908ebf 18559 case DW_AT_const_value:
48fbe735 18560 has_const_value = 1;
ff908ebf 18561 break;
68511cec
CES
18562 case DW_AT_calling_convention:
18563 /* DWARF doesn't provide a way to identify a program's source-level
18564 entry point. DW_AT_calling_convention attributes are only meant
18565 to describe functions' calling conventions.
18566
18567 However, because it's a necessary piece of information in
0c1b455e
TT
18568 Fortran, and before DWARF 4 DW_CC_program was the only
18569 piece of debugging information whose definition refers to
18570 a 'main program' at all, several compilers marked Fortran
18571 main programs with DW_CC_program --- even when those
18572 functions use the standard calling conventions.
18573
18574 Although DWARF now specifies a way to provide this
18575 information, we support this practice for backward
18576 compatibility. */
68511cec 18577 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 18578 && cu->language == language_fortran)
48fbe735 18579 main_subprogram = 1;
68511cec 18580 break;
481860b3
GB
18581 case DW_AT_inline:
18582 if (DW_UNSND (&attr) == DW_INL_inlined
18583 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 18584 may_be_inlined = 1;
481860b3 18585 break;
95554aad
TT
18586
18587 case DW_AT_import:
48fbe735 18588 if (tag == DW_TAG_imported_unit)
36586728 18589 {
48fbe735
YQ
18590 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18591 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
18592 || cu->per_cu->is_dwz);
18593 }
95554aad
TT
18594 break;
18595
0c1b455e 18596 case DW_AT_main_subprogram:
48fbe735 18597 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
18598 break;
18599
c906108c
SS
18600 default:
18601 break;
18602 }
18603 }
18604
91da1414 18605 if (high_pc_relative)
48fbe735 18606 highpc += lowpc;
91da1414 18607
9373cf26
JK
18608 if (has_low_pc_attr && has_high_pc_attr)
18609 {
18610 /* When using the GNU linker, .gnu.linkonce. sections are used to
18611 eliminate duplicate copies of functions and vtables and such.
18612 The linker will arbitrarily choose one and discard the others.
18613 The AT_*_pc values for such functions refer to local labels in
18614 these sections. If the section from that file was discarded, the
18615 labels are not in the output, so the relocs get a value of 0.
18616 If this is a discarded function, mark the pc bounds as invalid,
18617 so that GDB will ignore it. */
48fbe735 18618 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9373cf26 18619 {
48fbe735 18620 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18621 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 18622
b98664d3 18623 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 18624 "for DIE at %s [in module %s]"),
48fbe735
YQ
18625 paddress (gdbarch, lowpc),
18626 sect_offset_str (sect_off),
9d8780f0 18627 objfile_name (objfile));
9373cf26
JK
18628 }
18629 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 18630 else if (lowpc >= highpc)
9373cf26 18631 {
48fbe735 18632 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18633 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 18634
b98664d3 18635 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 18636 "for DIE at %s [in module %s]"),
48fbe735
YQ
18637 paddress (gdbarch, lowpc),
18638 paddress (gdbarch, highpc),
18639 sect_offset_str (sect_off),
9c541725 18640 objfile_name (objfile));
9373cf26
JK
18641 }
18642 else
48fbe735 18643 has_pc_info = 1;
9373cf26 18644 }
85cbf3d3 18645
c906108c
SS
18646 return info_ptr;
18647}
18648
72bf9492
DJ
18649/* Find a cached partial DIE at OFFSET in CU. */
18650
d590ff25
YQ
18651struct partial_die_info *
18652dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
18653{
18654 struct partial_die_info *lookup_die = NULL;
6f06d47b 18655 struct partial_die_info part_die (sect_off);
72bf9492 18656
9a3c8263 18657 lookup_die = ((struct partial_die_info *)
d590ff25 18658 htab_find_with_hash (partial_dies, &part_die,
9c541725 18659 to_underlying (sect_off)));
72bf9492 18660
72bf9492
DJ
18661 return lookup_die;
18662}
18663
348e048f
DE
18664/* Find a partial DIE at OFFSET, which may or may not be in CU,
18665 except in the case of .debug_types DIEs which do not reference
18666 outside their CU (they do however referencing other types via
55f1336d 18667 DW_FORM_ref_sig8). */
72bf9492
DJ
18668
18669static struct partial_die_info *
9c541725 18670find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18671{
518817b3
SM
18672 struct dwarf2_per_objfile *dwarf2_per_objfile
18673 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18674 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
18675 struct dwarf2_per_cu_data *per_cu = NULL;
18676 struct partial_die_info *pd = NULL;
72bf9492 18677
36586728 18678 if (offset_in_dwz == cu->per_cu->is_dwz
9c541725 18679 && offset_in_cu_p (&cu->header, sect_off))
5afb4e99 18680 {
d590ff25 18681 pd = cu->find_partial_die (sect_off);
5afb4e99
DJ
18682 if (pd != NULL)
18683 return pd;
0d99eb77
DE
18684 /* We missed recording what we needed.
18685 Load all dies and try again. */
18686 per_cu = cu->per_cu;
5afb4e99 18687 }
0d99eb77
DE
18688 else
18689 {
18690 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18691 if (cu->per_cu->is_debug_types)
0d99eb77 18692 {
9d8780f0
SM
18693 error (_("Dwarf Error: Type Unit at offset %s contains"
18694 " external reference to offset %s [in module %s].\n"),
18695 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
18696 bfd_get_filename (objfile->obfd));
18697 }
9c541725 18698 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 18699 dwarf2_per_objfile);
72bf9492 18700
0d99eb77
DE
18701 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18702 load_partial_comp_unit (per_cu);
ae038cb0 18703
0d99eb77 18704 per_cu->cu->last_used = 0;
d590ff25 18705 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 18706 }
5afb4e99 18707
dee91e82
DE
18708 /* If we didn't find it, and not all dies have been loaded,
18709 load them all and try again. */
18710
5afb4e99
DJ
18711 if (pd == NULL && per_cu->load_all_dies == 0)
18712 {
5afb4e99 18713 per_cu->load_all_dies = 1;
fd820528
DE
18714
18715 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18716 THIS_CU->cu may already be in use. So we can't just free it and
18717 replace its DIEs with the ones we read in. Instead, we leave those
18718 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18719 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18720 set. */
dee91e82 18721 load_partial_comp_unit (per_cu);
5afb4e99 18722
d590ff25 18723 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
18724 }
18725
18726 if (pd == NULL)
18727 internal_error (__FILE__, __LINE__,
9d8780f0 18728 _("could not find partial DIE %s "
3e43a32a 18729 "in cache [from module %s]\n"),
9d8780f0 18730 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
5afb4e99 18731 return pd;
72bf9492
DJ
18732}
18733
abc72ce4
DE
18734/* See if we can figure out if the class lives in a namespace. We do
18735 this by looking for a member function; its demangled name will
18736 contain namespace info, if there is any. */
18737
18738static void
18739guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18740 struct dwarf2_cu *cu)
18741{
18742 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18743 what template types look like, because the demangler
18744 frequently doesn't give the same name as the debug info. We
18745 could fix this by only using the demangled name to get the
18746 prefix (but see comment in read_structure_type). */
18747
18748 struct partial_die_info *real_pdi;
18749 struct partial_die_info *child_pdi;
18750
18751 /* If this DIE (this DIE's specification, if any) has a parent, then
18752 we should not do this. We'll prepend the parent's fully qualified
18753 name when we create the partial symbol. */
18754
18755 real_pdi = struct_pdi;
18756 while (real_pdi->has_specification)
36586728
TT
18757 real_pdi = find_partial_die (real_pdi->spec_offset,
18758 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
18759
18760 if (real_pdi->die_parent != NULL)
18761 return;
18762
18763 for (child_pdi = struct_pdi->die_child;
18764 child_pdi != NULL;
18765 child_pdi = child_pdi->die_sibling)
18766 {
18767 if (child_pdi->tag == DW_TAG_subprogram
18768 && child_pdi->linkage_name != NULL)
18769 {
18770 char *actual_class_name
18771 = language_class_name_from_physname (cu->language_defn,
18772 child_pdi->linkage_name);
18773 if (actual_class_name != NULL)
18774 {
518817b3 18775 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4 18776 struct_pdi->name
224c3ddb 18777 = ((const char *)
e3b94546 18778 obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb
SM
18779 actual_class_name,
18780 strlen (actual_class_name)));
abc72ce4
DE
18781 xfree (actual_class_name);
18782 }
18783 break;
18784 }
18785 }
18786}
18787
52356b79
YQ
18788void
18789partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 18790{
abc72ce4
DE
18791 /* Once we've fixed up a die, there's no point in doing so again.
18792 This also avoids a memory leak if we were to call
18793 guess_partial_die_structure_name multiple times. */
52356b79 18794 if (fixup_called)
abc72ce4
DE
18795 return;
18796
72bf9492
DJ
18797 /* If we found a reference attribute and the DIE has no name, try
18798 to find a name in the referred to DIE. */
18799
52356b79 18800 if (name == NULL && has_specification)
72bf9492
DJ
18801 {
18802 struct partial_die_info *spec_die;
72bf9492 18803
52356b79 18804 spec_die = find_partial_die (spec_offset, spec_is_dwz, cu);
72bf9492 18805
52356b79 18806 spec_die->fixup (cu);
72bf9492
DJ
18807
18808 if (spec_die->name)
18809 {
52356b79 18810 name = spec_die->name;
72bf9492
DJ
18811
18812 /* Copy DW_AT_external attribute if it is set. */
18813 if (spec_die->is_external)
52356b79 18814 is_external = spec_die->is_external;
72bf9492
DJ
18815 }
18816 }
18817
18818 /* Set default names for some unnamed DIEs. */
72bf9492 18819
52356b79
YQ
18820 if (name == NULL && tag == DW_TAG_namespace)
18821 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 18822
abc72ce4
DE
18823 /* If there is no parent die to provide a namespace, and there are
18824 children, see if we can determine the namespace from their linkage
122d1940 18825 name. */
abc72ce4 18826 if (cu->language == language_cplus
518817b3
SM
18827 && !VEC_empty (dwarf2_section_info_def,
18828 cu->per_cu->dwarf2_per_objfile->types)
52356b79
YQ
18829 && die_parent == NULL
18830 && has_children
18831 && (tag == DW_TAG_class_type
18832 || tag == DW_TAG_structure_type
18833 || tag == DW_TAG_union_type))
18834 guess_partial_die_structure_name (this, cu);
abc72ce4 18835
53832f31
TT
18836 /* GCC might emit a nameless struct or union that has a linkage
18837 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
18838 if (name == NULL
18839 && (tag == DW_TAG_class_type
18840 || tag == DW_TAG_interface_type
18841 || tag == DW_TAG_structure_type
18842 || tag == DW_TAG_union_type)
18843 && linkage_name != NULL)
53832f31
TT
18844 {
18845 char *demangled;
18846
52356b79 18847 demangled = gdb_demangle (linkage_name, DMGL_TYPES);
53832f31
TT
18848 if (demangled)
18849 {
96408a79
SA
18850 const char *base;
18851
18852 /* Strip any leading namespaces/classes, keep only the base name.
18853 DW_AT_name for named DIEs does not contain the prefixes. */
18854 base = strrchr (demangled, ':');
18855 if (base && base > demangled && base[-1] == ':')
18856 base++;
18857 else
18858 base = demangled;
18859
518817b3 18860 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
52356b79 18861 name
224c3ddb 18862 = ((const char *)
e3b94546 18863 obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb 18864 base, strlen (base)));
53832f31
TT
18865 xfree (demangled);
18866 }
18867 }
18868
52356b79 18869 fixup_called = 1;
72bf9492
DJ
18870}
18871
a8329558 18872/* Read an attribute value described by an attribute form. */
c906108c 18873
d521ce57 18874static const gdb_byte *
dee91e82
DE
18875read_attribute_value (const struct die_reader_specs *reader,
18876 struct attribute *attr, unsigned form,
43988095 18877 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 18878{
dee91e82 18879 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18880 struct dwarf2_per_objfile *dwarf2_per_objfile
18881 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18882 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 18883 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 18884 bfd *abfd = reader->abfd;
e7c27a73 18885 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
18886 unsigned int bytes_read;
18887 struct dwarf_block *blk;
18888
aead7601 18889 attr->form = (enum dwarf_form) form;
a8329558 18890 switch (form)
c906108c 18891 {
c906108c 18892 case DW_FORM_ref_addr:
ae411497 18893 if (cu->header.version == 2)
4568ecf9 18894 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 18895 else
4568ecf9
DE
18896 DW_UNSND (attr) = read_offset (abfd, info_ptr,
18897 &cu->header, &bytes_read);
ae411497
TT
18898 info_ptr += bytes_read;
18899 break;
36586728
TT
18900 case DW_FORM_GNU_ref_alt:
18901 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18902 info_ptr += bytes_read;
18903 break;
ae411497 18904 case DW_FORM_addr:
e7c27a73 18905 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 18906 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 18907 info_ptr += bytes_read;
c906108c
SS
18908 break;
18909 case DW_FORM_block2:
7b5a2f43 18910 blk = dwarf_alloc_block (cu);
c906108c
SS
18911 blk->size = read_2_bytes (abfd, info_ptr);
18912 info_ptr += 2;
18913 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18914 info_ptr += blk->size;
18915 DW_BLOCK (attr) = blk;
18916 break;
18917 case DW_FORM_block4:
7b5a2f43 18918 blk = dwarf_alloc_block (cu);
c906108c
SS
18919 blk->size = read_4_bytes (abfd, info_ptr);
18920 info_ptr += 4;
18921 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18922 info_ptr += blk->size;
18923 DW_BLOCK (attr) = blk;
18924 break;
18925 case DW_FORM_data2:
18926 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18927 info_ptr += 2;
18928 break;
18929 case DW_FORM_data4:
18930 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18931 info_ptr += 4;
18932 break;
18933 case DW_FORM_data8:
18934 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18935 info_ptr += 8;
18936 break;
0224619f
JK
18937 case DW_FORM_data16:
18938 blk = dwarf_alloc_block (cu);
18939 blk->size = 16;
18940 blk->data = read_n_bytes (abfd, info_ptr, 16);
18941 info_ptr += 16;
18942 DW_BLOCK (attr) = blk;
18943 break;
2dc7f7b3
TT
18944 case DW_FORM_sec_offset:
18945 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18946 info_ptr += bytes_read;
18947 break;
c906108c 18948 case DW_FORM_string:
9b1c24c8 18949 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 18950 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
18951 info_ptr += bytes_read;
18952 break;
4bdf3d34 18953 case DW_FORM_strp:
36586728
TT
18954 if (!cu->per_cu->is_dwz)
18955 {
ed2dc618
SM
18956 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18957 abfd, info_ptr, cu_header,
36586728
TT
18958 &bytes_read);
18959 DW_STRING_IS_CANONICAL (attr) = 0;
18960 info_ptr += bytes_read;
18961 break;
18962 }
18963 /* FALLTHROUGH */
43988095
JK
18964 case DW_FORM_line_strp:
18965 if (!cu->per_cu->is_dwz)
18966 {
ed2dc618
SM
18967 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
18968 abfd, info_ptr,
43988095
JK
18969 cu_header, &bytes_read);
18970 DW_STRING_IS_CANONICAL (attr) = 0;
18971 info_ptr += bytes_read;
18972 break;
18973 }
18974 /* FALLTHROUGH */
36586728
TT
18975 case DW_FORM_GNU_strp_alt:
18976 {
ed2dc618 18977 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
18978 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
18979 &bytes_read);
18980
ed2dc618
SM
18981 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
18982 dwz, str_offset);
36586728
TT
18983 DW_STRING_IS_CANONICAL (attr) = 0;
18984 info_ptr += bytes_read;
18985 }
4bdf3d34 18986 break;
2dc7f7b3 18987 case DW_FORM_exprloc:
c906108c 18988 case DW_FORM_block:
7b5a2f43 18989 blk = dwarf_alloc_block (cu);
c906108c
SS
18990 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18991 info_ptr += bytes_read;
18992 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18993 info_ptr += blk->size;
18994 DW_BLOCK (attr) = blk;
18995 break;
18996 case DW_FORM_block1:
7b5a2f43 18997 blk = dwarf_alloc_block (cu);
c906108c
SS
18998 blk->size = read_1_byte (abfd, info_ptr);
18999 info_ptr += 1;
19000 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19001 info_ptr += blk->size;
19002 DW_BLOCK (attr) = blk;
19003 break;
19004 case DW_FORM_data1:
19005 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19006 info_ptr += 1;
19007 break;
19008 case DW_FORM_flag:
19009 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19010 info_ptr += 1;
19011 break;
2dc7f7b3
TT
19012 case DW_FORM_flag_present:
19013 DW_UNSND (attr) = 1;
19014 break;
c906108c
SS
19015 case DW_FORM_sdata:
19016 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19017 info_ptr += bytes_read;
19018 break;
19019 case DW_FORM_udata:
19020 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19021 info_ptr += bytes_read;
19022 break;
19023 case DW_FORM_ref1:
9c541725 19024 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19025 + read_1_byte (abfd, info_ptr));
c906108c
SS
19026 info_ptr += 1;
19027 break;
19028 case DW_FORM_ref2:
9c541725 19029 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19030 + read_2_bytes (abfd, info_ptr));
c906108c
SS
19031 info_ptr += 2;
19032 break;
19033 case DW_FORM_ref4:
9c541725 19034 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19035 + read_4_bytes (abfd, info_ptr));
c906108c
SS
19036 info_ptr += 4;
19037 break;
613e1657 19038 case DW_FORM_ref8:
9c541725 19039 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19040 + read_8_bytes (abfd, info_ptr));
613e1657
KB
19041 info_ptr += 8;
19042 break;
55f1336d 19043 case DW_FORM_ref_sig8:
ac9ec31b 19044 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
19045 info_ptr += 8;
19046 break;
c906108c 19047 case DW_FORM_ref_udata:
9c541725 19048 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19049 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19050 info_ptr += bytes_read;
19051 break;
c906108c 19052 case DW_FORM_indirect:
a8329558
KW
19053 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19054 info_ptr += bytes_read;
43988095
JK
19055 if (form == DW_FORM_implicit_const)
19056 {
19057 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19058 info_ptr += bytes_read;
19059 }
19060 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
19061 info_ptr);
19062 break;
19063 case DW_FORM_implicit_const:
19064 DW_SND (attr) = implicit_const;
a8329558 19065 break;
3019eac3
DE
19066 case DW_FORM_GNU_addr_index:
19067 if (reader->dwo_file == NULL)
19068 {
19069 /* For now flag a hard error.
19070 Later we can turn this into a complaint. */
19071 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19072 dwarf_form_name (form),
19073 bfd_get_filename (abfd));
19074 }
19075 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19076 info_ptr += bytes_read;
19077 break;
19078 case DW_FORM_GNU_str_index:
19079 if (reader->dwo_file == NULL)
19080 {
19081 /* For now flag a hard error.
19082 Later we can turn this into a complaint if warranted. */
19083 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19084 dwarf_form_name (form),
19085 bfd_get_filename (abfd));
19086 }
19087 {
19088 ULONGEST str_index =
19089 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19090
342587c4 19091 DW_STRING (attr) = read_str_index (reader, str_index);
3019eac3
DE
19092 DW_STRING_IS_CANONICAL (attr) = 0;
19093 info_ptr += bytes_read;
19094 }
19095 break;
c906108c 19096 default:
8a3fe4f8 19097 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19098 dwarf_form_name (form),
19099 bfd_get_filename (abfd));
c906108c 19100 }
28e94949 19101
36586728 19102 /* Super hack. */
7771576e 19103 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
19104 attr->form = DW_FORM_GNU_ref_alt;
19105
28e94949
JB
19106 /* We have seen instances where the compiler tried to emit a byte
19107 size attribute of -1 which ended up being encoded as an unsigned
19108 0xffffffff. Although 0xffffffff is technically a valid size value,
19109 an object of this size seems pretty unlikely so we can relatively
19110 safely treat these cases as if the size attribute was invalid and
19111 treat them as zero by default. */
19112 if (attr->name == DW_AT_byte_size
19113 && form == DW_FORM_data4
19114 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19115 {
19116 complaint
b98664d3 19117 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 19118 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19119 DW_UNSND (attr) = 0;
19120 }
28e94949 19121
c906108c
SS
19122 return info_ptr;
19123}
19124
a8329558
KW
19125/* Read an attribute described by an abbreviated attribute. */
19126
d521ce57 19127static const gdb_byte *
dee91e82
DE
19128read_attribute (const struct die_reader_specs *reader,
19129 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 19130 const gdb_byte *info_ptr)
a8329558
KW
19131{
19132 attr->name = abbrev->name;
43988095
JK
19133 return read_attribute_value (reader, attr, abbrev->form,
19134 abbrev->implicit_const, info_ptr);
a8329558
KW
19135}
19136
0963b4bd 19137/* Read dwarf information from a buffer. */
c906108c
SS
19138
19139static unsigned int
a1855c1d 19140read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19141{
fe1b8b76 19142 return bfd_get_8 (abfd, buf);
c906108c
SS
19143}
19144
19145static int
a1855c1d 19146read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19147{
fe1b8b76 19148 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
19149}
19150
19151static unsigned int
a1855c1d 19152read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19153{
fe1b8b76 19154 return bfd_get_16 (abfd, buf);
c906108c
SS
19155}
19156
21ae7a4d 19157static int
a1855c1d 19158read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19159{
19160 return bfd_get_signed_16 (abfd, buf);
19161}
19162
c906108c 19163static unsigned int
a1855c1d 19164read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19165{
fe1b8b76 19166 return bfd_get_32 (abfd, buf);
c906108c
SS
19167}
19168
21ae7a4d 19169static int
a1855c1d 19170read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19171{
19172 return bfd_get_signed_32 (abfd, buf);
19173}
19174
93311388 19175static ULONGEST
a1855c1d 19176read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19177{
fe1b8b76 19178 return bfd_get_64 (abfd, buf);
c906108c
SS
19179}
19180
19181static CORE_ADDR
d521ce57 19182read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 19183 unsigned int *bytes_read)
c906108c 19184{
e7c27a73 19185 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19186 CORE_ADDR retval = 0;
19187
107d2387 19188 if (cu_header->signed_addr_p)
c906108c 19189 {
107d2387
AC
19190 switch (cu_header->addr_size)
19191 {
19192 case 2:
fe1b8b76 19193 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
19194 break;
19195 case 4:
fe1b8b76 19196 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
19197 break;
19198 case 8:
fe1b8b76 19199 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
19200 break;
19201 default:
8e65ff28 19202 internal_error (__FILE__, __LINE__,
e2e0b3e5 19203 _("read_address: bad switch, signed [in module %s]"),
659b0389 19204 bfd_get_filename (abfd));
107d2387
AC
19205 }
19206 }
19207 else
19208 {
19209 switch (cu_header->addr_size)
19210 {
19211 case 2:
fe1b8b76 19212 retval = bfd_get_16 (abfd, buf);
107d2387
AC
19213 break;
19214 case 4:
fe1b8b76 19215 retval = bfd_get_32 (abfd, buf);
107d2387
AC
19216 break;
19217 case 8:
fe1b8b76 19218 retval = bfd_get_64 (abfd, buf);
107d2387
AC
19219 break;
19220 default:
8e65ff28 19221 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
19222 _("read_address: bad switch, "
19223 "unsigned [in module %s]"),
659b0389 19224 bfd_get_filename (abfd));
107d2387 19225 }
c906108c 19226 }
64367e0a 19227
107d2387
AC
19228 *bytes_read = cu_header->addr_size;
19229 return retval;
c906108c
SS
19230}
19231
f7ef9339 19232/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
19233 specification allows the initial length to take up either 4 bytes
19234 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19235 bytes describe the length and all offsets will be 8 bytes in length
19236 instead of 4.
19237
f7ef9339
KB
19238 An older, non-standard 64-bit format is also handled by this
19239 function. The older format in question stores the initial length
19240 as an 8-byte quantity without an escape value. Lengths greater
19241 than 2^32 aren't very common which means that the initial 4 bytes
19242 is almost always zero. Since a length value of zero doesn't make
19243 sense for the 32-bit format, this initial zero can be considered to
19244 be an escape value which indicates the presence of the older 64-bit
19245 format. As written, the code can't detect (old format) lengths
917c78fc
MK
19246 greater than 4GB. If it becomes necessary to handle lengths
19247 somewhat larger than 4GB, we could allow other small values (such
19248 as the non-sensical values of 1, 2, and 3) to also be used as
19249 escape values indicating the presence of the old format.
f7ef9339 19250
917c78fc
MK
19251 The value returned via bytes_read should be used to increment the
19252 relevant pointer after calling read_initial_length().
c764a876 19253
613e1657
KB
19254 [ Note: read_initial_length() and read_offset() are based on the
19255 document entitled "DWARF Debugging Information Format", revision
f7ef9339 19256 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
19257 from:
19258
f7ef9339 19259 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 19260
613e1657
KB
19261 This document is only a draft and is subject to change. (So beware.)
19262
f7ef9339 19263 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
19264 determined empirically by examining 64-bit ELF files produced by
19265 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
19266
19267 - Kevin, July 16, 2002
613e1657
KB
19268 ] */
19269
19270static LONGEST
d521ce57 19271read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 19272{
fe1b8b76 19273 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 19274
dd373385 19275 if (length == 0xffffffff)
613e1657 19276 {
fe1b8b76 19277 length = bfd_get_64 (abfd, buf + 4);
613e1657 19278 *bytes_read = 12;
613e1657 19279 }
dd373385 19280 else if (length == 0)
f7ef9339 19281 {
dd373385 19282 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 19283 length = bfd_get_64 (abfd, buf);
f7ef9339 19284 *bytes_read = 8;
f7ef9339 19285 }
613e1657
KB
19286 else
19287 {
19288 *bytes_read = 4;
613e1657
KB
19289 }
19290
c764a876
DE
19291 return length;
19292}
dd373385 19293
c764a876
DE
19294/* Cover function for read_initial_length.
19295 Returns the length of the object at BUF, and stores the size of the
19296 initial length in *BYTES_READ and stores the size that offsets will be in
19297 *OFFSET_SIZE.
19298 If the initial length size is not equivalent to that specified in
19299 CU_HEADER then issue a complaint.
19300 This is useful when reading non-comp-unit headers. */
dd373385 19301
c764a876 19302static LONGEST
d521ce57 19303read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
19304 const struct comp_unit_head *cu_header,
19305 unsigned int *bytes_read,
19306 unsigned int *offset_size)
19307{
19308 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19309
19310 gdb_assert (cu_header->initial_length_size == 4
19311 || cu_header->initial_length_size == 8
19312 || cu_header->initial_length_size == 12);
19313
19314 if (cu_header->initial_length_size != *bytes_read)
b98664d3 19315 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 19316
c764a876 19317 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 19318 return length;
613e1657
KB
19319}
19320
19321/* Read an offset from the data stream. The size of the offset is
917c78fc 19322 given by cu_header->offset_size. */
613e1657
KB
19323
19324static LONGEST
d521ce57
TT
19325read_offset (bfd *abfd, const gdb_byte *buf,
19326 const struct comp_unit_head *cu_header,
891d2f0b 19327 unsigned int *bytes_read)
c764a876
DE
19328{
19329 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 19330
c764a876
DE
19331 *bytes_read = cu_header->offset_size;
19332 return offset;
19333}
19334
19335/* Read an offset from the data stream. */
19336
19337static LONGEST
d521ce57 19338read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
19339{
19340 LONGEST retval = 0;
19341
c764a876 19342 switch (offset_size)
613e1657
KB
19343 {
19344 case 4:
fe1b8b76 19345 retval = bfd_get_32 (abfd, buf);
613e1657
KB
19346 break;
19347 case 8:
fe1b8b76 19348 retval = bfd_get_64 (abfd, buf);
613e1657
KB
19349 break;
19350 default:
8e65ff28 19351 internal_error (__FILE__, __LINE__,
c764a876 19352 _("read_offset_1: bad switch [in module %s]"),
659b0389 19353 bfd_get_filename (abfd));
613e1657
KB
19354 }
19355
917c78fc 19356 return retval;
613e1657
KB
19357}
19358
d521ce57
TT
19359static const gdb_byte *
19360read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
19361{
19362 /* If the size of a host char is 8 bits, we can return a pointer
19363 to the buffer, otherwise we have to copy the data to a buffer
19364 allocated on the temporary obstack. */
4bdf3d34 19365 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 19366 return buf;
c906108c
SS
19367}
19368
d521ce57
TT
19369static const char *
19370read_direct_string (bfd *abfd, const gdb_byte *buf,
19371 unsigned int *bytes_read_ptr)
c906108c
SS
19372{
19373 /* If the size of a host char is 8 bits, we can return a pointer
19374 to the string, otherwise we have to copy the string to a buffer
19375 allocated on the temporary obstack. */
4bdf3d34 19376 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
19377 if (*buf == '\0')
19378 {
19379 *bytes_read_ptr = 1;
19380 return NULL;
19381 }
d521ce57
TT
19382 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19383 return (const char *) buf;
4bdf3d34
JJ
19384}
19385
43988095
JK
19386/* Return pointer to string at section SECT offset STR_OFFSET with error
19387 reporting strings FORM_NAME and SECT_NAME. */
19388
d521ce57 19389static const char *
ed2dc618
SM
19390read_indirect_string_at_offset_from (struct objfile *objfile,
19391 bfd *abfd, LONGEST str_offset,
43988095
JK
19392 struct dwarf2_section_info *sect,
19393 const char *form_name,
19394 const char *sect_name)
19395{
ed2dc618 19396 dwarf2_read_section (objfile, sect);
43988095
JK
19397 if (sect->buffer == NULL)
19398 error (_("%s used without %s section [in module %s]"),
19399 form_name, sect_name, bfd_get_filename (abfd));
19400 if (str_offset >= sect->size)
19401 error (_("%s pointing outside of %s section [in module %s]"),
19402 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 19403 gdb_assert (HOST_CHAR_BIT == 8);
43988095 19404 if (sect->buffer[str_offset] == '\0')
4bdf3d34 19405 return NULL;
43988095
JK
19406 return (const char *) (sect->buffer + str_offset);
19407}
19408
19409/* Return pointer to string at .debug_str offset STR_OFFSET. */
19410
19411static const char *
ed2dc618
SM
19412read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19413 bfd *abfd, LONGEST str_offset)
43988095 19414{
ed2dc618
SM
19415 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19416 abfd, str_offset,
43988095
JK
19417 &dwarf2_per_objfile->str,
19418 "DW_FORM_strp", ".debug_str");
19419}
19420
19421/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19422
19423static const char *
ed2dc618
SM
19424read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19425 bfd *abfd, LONGEST str_offset)
43988095 19426{
ed2dc618
SM
19427 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19428 abfd, str_offset,
43988095
JK
19429 &dwarf2_per_objfile->line_str,
19430 "DW_FORM_line_strp",
19431 ".debug_line_str");
c906108c
SS
19432}
19433
36586728
TT
19434/* Read a string at offset STR_OFFSET in the .debug_str section from
19435 the .dwz file DWZ. Throw an error if the offset is too large. If
19436 the string consists of a single NUL byte, return NULL; otherwise
19437 return a pointer to the string. */
19438
d521ce57 19439static const char *
ed2dc618
SM
19440read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19441 LONGEST str_offset)
36586728 19442{
ed2dc618 19443 dwarf2_read_section (objfile, &dwz->str);
36586728
TT
19444
19445 if (dwz->str.buffer == NULL)
19446 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19447 "section [in module %s]"),
19448 bfd_get_filename (dwz->dwz_bfd));
19449 if (str_offset >= dwz->str.size)
19450 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19451 ".debug_str section [in module %s]"),
19452 bfd_get_filename (dwz->dwz_bfd));
19453 gdb_assert (HOST_CHAR_BIT == 8);
19454 if (dwz->str.buffer[str_offset] == '\0')
19455 return NULL;
d521ce57 19456 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
19457}
19458
43988095
JK
19459/* Return pointer to string at .debug_str offset as read from BUF.
19460 BUF is assumed to be in a compilation unit described by CU_HEADER.
19461 Return *BYTES_READ_PTR count of bytes read from BUF. */
19462
d521ce57 19463static const char *
ed2dc618
SM
19464read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19465 const gdb_byte *buf,
cf2c3c16
TT
19466 const struct comp_unit_head *cu_header,
19467 unsigned int *bytes_read_ptr)
19468{
19469 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19470
ed2dc618 19471 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
cf2c3c16
TT
19472}
19473
43988095
JK
19474/* Return pointer to string at .debug_line_str offset as read from BUF.
19475 BUF is assumed to be in a compilation unit described by CU_HEADER.
19476 Return *BYTES_READ_PTR count of bytes read from BUF. */
19477
19478static const char *
ed2dc618
SM
19479read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19480 bfd *abfd, const gdb_byte *buf,
43988095
JK
19481 const struct comp_unit_head *cu_header,
19482 unsigned int *bytes_read_ptr)
19483{
19484 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19485
ed2dc618
SM
19486 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19487 str_offset);
43988095
JK
19488}
19489
19490ULONGEST
d521ce57 19491read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
43988095 19492 unsigned int *bytes_read_ptr)
c906108c 19493{
12df843f 19494 ULONGEST result;
ce5d95e1 19495 unsigned int num_read;
870f88f7 19496 int shift;
c906108c
SS
19497 unsigned char byte;
19498
19499 result = 0;
19500 shift = 0;
19501 num_read = 0;
c906108c
SS
19502 while (1)
19503 {
fe1b8b76 19504 byte = bfd_get_8 (abfd, buf);
c906108c
SS
19505 buf++;
19506 num_read++;
12df843f 19507 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
19508 if ((byte & 128) == 0)
19509 {
19510 break;
19511 }
19512 shift += 7;
19513 }
19514 *bytes_read_ptr = num_read;
19515 return result;
19516}
19517
12df843f 19518static LONGEST
d521ce57
TT
19519read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19520 unsigned int *bytes_read_ptr)
c906108c 19521{
12df843f 19522 LONGEST result;
870f88f7 19523 int shift, num_read;
c906108c
SS
19524 unsigned char byte;
19525
19526 result = 0;
19527 shift = 0;
c906108c 19528 num_read = 0;
c906108c
SS
19529 while (1)
19530 {
fe1b8b76 19531 byte = bfd_get_8 (abfd, buf);
c906108c
SS
19532 buf++;
19533 num_read++;
12df843f 19534 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
19535 shift += 7;
19536 if ((byte & 128) == 0)
19537 {
19538 break;
19539 }
19540 }
77e0b926 19541 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 19542 result |= -(((LONGEST) 1) << shift);
c906108c
SS
19543 *bytes_read_ptr = num_read;
19544 return result;
19545}
19546
3019eac3
DE
19547/* Given index ADDR_INDEX in .debug_addr, fetch the value.
19548 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
19549 ADDR_SIZE is the size of addresses from the CU header. */
19550
19551static CORE_ADDR
ed2dc618
SM
19552read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19553 unsigned int addr_index, ULONGEST addr_base, int addr_size)
3019eac3
DE
19554{
19555 struct objfile *objfile = dwarf2_per_objfile->objfile;
19556 bfd *abfd = objfile->obfd;
19557 const gdb_byte *info_ptr;
19558
19559 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
19560 if (dwarf2_per_objfile->addr.buffer == NULL)
19561 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 19562 objfile_name (objfile));
3019eac3
DE
19563 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
19564 error (_("DW_FORM_addr_index pointing outside of "
19565 ".debug_addr section [in module %s]"),
4262abfb 19566 objfile_name (objfile));
3019eac3
DE
19567 info_ptr = (dwarf2_per_objfile->addr.buffer
19568 + addr_base + addr_index * addr_size);
19569 if (addr_size == 4)
19570 return bfd_get_32 (abfd, info_ptr);
19571 else
19572 return bfd_get_64 (abfd, info_ptr);
19573}
19574
19575/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19576
19577static CORE_ADDR
19578read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19579{
518817b3
SM
19580 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19581 cu->addr_base, cu->header.addr_size);
3019eac3
DE
19582}
19583
19584/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19585
19586static CORE_ADDR
d521ce57 19587read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
19588 unsigned int *bytes_read)
19589{
518817b3 19590 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
3019eac3
DE
19591 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19592
19593 return read_addr_index (cu, addr_index);
19594}
19595
19596/* Data structure to pass results from dwarf2_read_addr_index_reader
19597 back to dwarf2_read_addr_index. */
19598
19599struct dwarf2_read_addr_index_data
19600{
19601 ULONGEST addr_base;
19602 int addr_size;
19603};
19604
19605/* die_reader_func for dwarf2_read_addr_index. */
19606
19607static void
19608dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 19609 const gdb_byte *info_ptr,
3019eac3
DE
19610 struct die_info *comp_unit_die,
19611 int has_children,
19612 void *data)
19613{
19614 struct dwarf2_cu *cu = reader->cu;
19615 struct dwarf2_read_addr_index_data *aidata =
19616 (struct dwarf2_read_addr_index_data *) data;
19617
19618 aidata->addr_base = cu->addr_base;
19619 aidata->addr_size = cu->header.addr_size;
19620}
19621
19622/* Given an index in .debug_addr, fetch the value.
19623 NOTE: This can be called during dwarf expression evaluation,
19624 long after the debug information has been read, and thus per_cu->cu
19625 may no longer exist. */
19626
19627CORE_ADDR
19628dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19629 unsigned int addr_index)
19630{
ed2dc618 19631 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3
DE
19632 struct dwarf2_cu *cu = per_cu->cu;
19633 ULONGEST addr_base;
19634 int addr_size;
19635
3019eac3
DE
19636 /* We need addr_base and addr_size.
19637 If we don't have PER_CU->cu, we have to get it.
19638 Nasty, but the alternative is storing the needed info in PER_CU,
19639 which at this point doesn't seem justified: it's not clear how frequently
19640 it would get used and it would increase the size of every PER_CU.
19641 Entry points like dwarf2_per_cu_addr_size do a similar thing
19642 so we're not in uncharted territory here.
19643 Alas we need to be a bit more complicated as addr_base is contained
19644 in the DIE.
19645
19646 We don't need to read the entire CU(/TU).
19647 We just need the header and top level die.
a1b64ce1 19648
3019eac3 19649 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 19650 For now we skip this optimization. */
3019eac3
DE
19651
19652 if (cu != NULL)
19653 {
19654 addr_base = cu->addr_base;
19655 addr_size = cu->header.addr_size;
19656 }
19657 else
19658 {
19659 struct dwarf2_read_addr_index_data aidata;
19660
a1b64ce1
DE
19661 /* Note: We can't use init_cutu_and_read_dies_simple here,
19662 we need addr_base. */
58f0c718 19663 init_cutu_and_read_dies (per_cu, NULL, 0, 0, false,
a1b64ce1 19664 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
19665 addr_base = aidata.addr_base;
19666 addr_size = aidata.addr_size;
19667 }
19668
ed2dc618
SM
19669 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19670 addr_size);
3019eac3
DE
19671}
19672
57d63ce2
DE
19673/* Given a DW_FORM_GNU_str_index, fetch the string.
19674 This is only used by the Fission support. */
3019eac3 19675
d521ce57 19676static const char *
342587c4 19677read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
3019eac3 19678{
ed2dc618 19679 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
19680 struct dwarf2_per_objfile *dwarf2_per_objfile
19681 = cu->per_cu->dwarf2_per_objfile;
3019eac3 19682 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 19683 const char *objf_name = objfile_name (objfile);
3019eac3 19684 bfd *abfd = objfile->obfd;
73869dc2
DE
19685 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
19686 struct dwarf2_section_info *str_offsets_section =
19687 &reader->dwo_file->sections.str_offsets;
d521ce57 19688 const gdb_byte *info_ptr;
3019eac3 19689 ULONGEST str_offset;
57d63ce2 19690 static const char form_name[] = "DW_FORM_GNU_str_index";
3019eac3 19691
73869dc2
DE
19692 dwarf2_read_section (objfile, str_section);
19693 dwarf2_read_section (objfile, str_offsets_section);
19694 if (str_section->buffer == NULL)
57d63ce2 19695 error (_("%s used without .debug_str.dwo section"
9d8780f0
SM
19696 " in CU at offset %s [in module %s]"),
19697 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19698 if (str_offsets_section->buffer == NULL)
57d63ce2 19699 error (_("%s used without .debug_str_offsets.dwo section"
9d8780f0
SM
19700 " in CU at offset %s [in module %s]"),
19701 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19702 if (str_index * cu->header.offset_size >= str_offsets_section->size)
57d63ce2 19703 error (_("%s pointing outside of .debug_str_offsets.dwo"
9d8780f0
SM
19704 " section in CU at offset %s [in module %s]"),
19705 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19706 info_ptr = (str_offsets_section->buffer
3019eac3
DE
19707 + str_index * cu->header.offset_size);
19708 if (cu->header.offset_size == 4)
19709 str_offset = bfd_get_32 (abfd, info_ptr);
19710 else
19711 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 19712 if (str_offset >= str_section->size)
57d63ce2 19713 error (_("Offset from %s pointing outside of"
9d8780f0
SM
19714 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19715 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19716 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
19717}
19718
3019eac3
DE
19719/* Return the length of an LEB128 number in BUF. */
19720
19721static int
19722leb128_size (const gdb_byte *buf)
19723{
19724 const gdb_byte *begin = buf;
19725 gdb_byte byte;
19726
19727 while (1)
19728 {
19729 byte = *buf++;
19730 if ((byte & 128) == 0)
19731 return buf - begin;
19732 }
19733}
19734
c906108c 19735static void
e142c38c 19736set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
19737{
19738 switch (lang)
19739 {
19740 case DW_LANG_C89:
76bee0cc 19741 case DW_LANG_C99:
0cfd832f 19742 case DW_LANG_C11:
c906108c 19743 case DW_LANG_C:
d1be3247 19744 case DW_LANG_UPC:
e142c38c 19745 cu->language = language_c;
c906108c 19746 break;
9c37b5ae 19747 case DW_LANG_Java:
c906108c 19748 case DW_LANG_C_plus_plus:
0cfd832f
MW
19749 case DW_LANG_C_plus_plus_11:
19750 case DW_LANG_C_plus_plus_14:
e142c38c 19751 cu->language = language_cplus;
c906108c 19752 break;
6aecb9c2
JB
19753 case DW_LANG_D:
19754 cu->language = language_d;
19755 break;
c906108c
SS
19756 case DW_LANG_Fortran77:
19757 case DW_LANG_Fortran90:
b21b22e0 19758 case DW_LANG_Fortran95:
f7de9aab
MW
19759 case DW_LANG_Fortran03:
19760 case DW_LANG_Fortran08:
e142c38c 19761 cu->language = language_fortran;
c906108c 19762 break;
a766d390
DE
19763 case DW_LANG_Go:
19764 cu->language = language_go;
19765 break;
c906108c 19766 case DW_LANG_Mips_Assembler:
e142c38c 19767 cu->language = language_asm;
c906108c
SS
19768 break;
19769 case DW_LANG_Ada83:
8aaf0b47 19770 case DW_LANG_Ada95:
bc5f45f8
JB
19771 cu->language = language_ada;
19772 break;
72019c9c
GM
19773 case DW_LANG_Modula2:
19774 cu->language = language_m2;
19775 break;
fe8e67fd
PM
19776 case DW_LANG_Pascal83:
19777 cu->language = language_pascal;
19778 break;
22566fbd
DJ
19779 case DW_LANG_ObjC:
19780 cu->language = language_objc;
19781 break;
c44af4eb
TT
19782 case DW_LANG_Rust:
19783 case DW_LANG_Rust_old:
19784 cu->language = language_rust;
19785 break;
c906108c
SS
19786 case DW_LANG_Cobol74:
19787 case DW_LANG_Cobol85:
c906108c 19788 default:
e142c38c 19789 cu->language = language_minimal;
c906108c
SS
19790 break;
19791 }
e142c38c 19792 cu->language_defn = language_def (cu->language);
c906108c
SS
19793}
19794
19795/* Return the named attribute or NULL if not there. */
19796
19797static struct attribute *
e142c38c 19798dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 19799{
a48e046c 19800 for (;;)
c906108c 19801 {
a48e046c
TT
19802 unsigned int i;
19803 struct attribute *spec = NULL;
19804
19805 for (i = 0; i < die->num_attrs; ++i)
19806 {
19807 if (die->attrs[i].name == name)
19808 return &die->attrs[i];
19809 if (die->attrs[i].name == DW_AT_specification
19810 || die->attrs[i].name == DW_AT_abstract_origin)
19811 spec = &die->attrs[i];
19812 }
19813
19814 if (!spec)
19815 break;
c906108c 19816
f2f0e013 19817 die = follow_die_ref (die, spec, &cu);
f2f0e013 19818 }
c5aa993b 19819
c906108c
SS
19820 return NULL;
19821}
19822
348e048f
DE
19823/* Return the named attribute or NULL if not there,
19824 but do not follow DW_AT_specification, etc.
19825 This is for use in contexts where we're reading .debug_types dies.
19826 Following DW_AT_specification, DW_AT_abstract_origin will take us
19827 back up the chain, and we want to go down. */
19828
19829static struct attribute *
45e58e77 19830dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
19831{
19832 unsigned int i;
19833
19834 for (i = 0; i < die->num_attrs; ++i)
19835 if (die->attrs[i].name == name)
19836 return &die->attrs[i];
19837
19838 return NULL;
19839}
19840
7d45c7c3
KB
19841/* Return the string associated with a string-typed attribute, or NULL if it
19842 is either not found or is of an incorrect type. */
19843
19844static const char *
19845dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19846{
19847 struct attribute *attr;
19848 const char *str = NULL;
19849
19850 attr = dwarf2_attr (die, name, cu);
19851
19852 if (attr != NULL)
19853 {
43988095 19854 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438
L
19855 || attr->form == DW_FORM_string
19856 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 19857 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
19858 str = DW_STRING (attr);
19859 else
b98664d3 19860 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
19861 "DIE at %s in module %s"),
19862 dwarf_attr_name (name), sect_offset_str (die->sect_off),
518817b3 19863 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7d45c7c3
KB
19864 }
19865
19866 return str;
19867}
19868
05cf31d1
JB
19869/* Return non-zero iff the attribute NAME is defined for the given DIE,
19870 and holds a non-zero value. This function should only be used for
2dc7f7b3 19871 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
19872
19873static int
19874dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19875{
19876 struct attribute *attr = dwarf2_attr (die, name, cu);
19877
19878 return (attr && DW_UNSND (attr));
19879}
19880
3ca72b44 19881static int
e142c38c 19882die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 19883{
05cf31d1
JB
19884 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19885 which value is non-zero. However, we have to be careful with
19886 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19887 (via dwarf2_flag_true_p) follows this attribute. So we may
19888 end up accidently finding a declaration attribute that belongs
19889 to a different DIE referenced by the specification attribute,
19890 even though the given DIE does not have a declaration attribute. */
19891 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19892 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
19893}
19894
63d06c5c 19895/* Return the die giving the specification for DIE, if there is
f2f0e013 19896 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
19897 containing the return value on output. If there is no
19898 specification, but there is an abstract origin, that is
19899 returned. */
63d06c5c
DC
19900
19901static struct die_info *
f2f0e013 19902die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 19903{
f2f0e013
DJ
19904 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19905 *spec_cu);
63d06c5c 19906
edb3359d
DJ
19907 if (spec_attr == NULL)
19908 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19909
63d06c5c
DC
19910 if (spec_attr == NULL)
19911 return NULL;
19912 else
f2f0e013 19913 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 19914}
c906108c 19915
527f3840
JK
19916/* Stub for free_line_header to match void * callback types. */
19917
19918static void
19919free_line_header_voidp (void *arg)
19920{
9a3c8263 19921 struct line_header *lh = (struct line_header *) arg;
527f3840 19922
fff8551c 19923 delete lh;
527f3840
JK
19924}
19925
fff8551c
PA
19926void
19927line_header::add_include_dir (const char *include_dir)
c906108c 19928{
27e0867f 19929 if (dwarf_line_debug >= 2)
fff8551c
PA
19930 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
19931 include_dirs.size () + 1, include_dir);
27e0867f 19932
fff8551c 19933 include_dirs.push_back (include_dir);
debd256d 19934}
6e70227d 19935
fff8551c
PA
19936void
19937line_header::add_file_name (const char *name,
ecfb656c 19938 dir_index d_index,
fff8551c
PA
19939 unsigned int mod_time,
19940 unsigned int length)
debd256d 19941{
27e0867f
DE
19942 if (dwarf_line_debug >= 2)
19943 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
fff8551c 19944 (unsigned) file_names.size () + 1, name);
27e0867f 19945
ecfb656c 19946 file_names.emplace_back (name, d_index, mod_time, length);
debd256d 19947}
6e70227d 19948
83769d0b 19949/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
19950
19951static struct dwarf2_section_info *
19952get_debug_line_section (struct dwarf2_cu *cu)
19953{
19954 struct dwarf2_section_info *section;
518817b3
SM
19955 struct dwarf2_per_objfile *dwarf2_per_objfile
19956 = cu->per_cu->dwarf2_per_objfile;
36586728
TT
19957
19958 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19959 DWO file. */
19960 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19961 section = &cu->dwo_unit->dwo_file->sections.line;
19962 else if (cu->per_cu->is_dwz)
19963 {
ed2dc618 19964 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
19965
19966 section = &dwz->line;
19967 }
19968 else
19969 section = &dwarf2_per_objfile->line;
19970
19971 return section;
19972}
19973
43988095
JK
19974/* Read directory or file name entry format, starting with byte of
19975 format count entries, ULEB128 pairs of entry formats, ULEB128 of
19976 entries count and the entries themselves in the described entry
19977 format. */
19978
19979static void
ed2dc618
SM
19980read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
19981 bfd *abfd, const gdb_byte **bufp,
43988095
JK
19982 struct line_header *lh,
19983 const struct comp_unit_head *cu_header,
19984 void (*callback) (struct line_header *lh,
19985 const char *name,
ecfb656c 19986 dir_index d_index,
43988095
JK
19987 unsigned int mod_time,
19988 unsigned int length))
19989{
19990 gdb_byte format_count, formati;
19991 ULONGEST data_count, datai;
19992 const gdb_byte *buf = *bufp;
19993 const gdb_byte *format_header_data;
43988095
JK
19994 unsigned int bytes_read;
19995
19996 format_count = read_1_byte (abfd, buf);
19997 buf += 1;
19998 format_header_data = buf;
19999 for (formati = 0; formati < format_count; formati++)
20000 {
20001 read_unsigned_leb128 (abfd, buf, &bytes_read);
20002 buf += bytes_read;
20003 read_unsigned_leb128 (abfd, buf, &bytes_read);
20004 buf += bytes_read;
20005 }
20006
20007 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
20008 buf += bytes_read;
20009 for (datai = 0; datai < data_count; datai++)
20010 {
20011 const gdb_byte *format = format_header_data;
20012 struct file_entry fe;
20013
43988095
JK
20014 for (formati = 0; formati < format_count; formati++)
20015 {
ecfb656c 20016 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20017 format += bytes_read;
43988095 20018
ecfb656c 20019 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 20020 format += bytes_read;
ecfb656c
PA
20021
20022 gdb::optional<const char *> string;
20023 gdb::optional<unsigned int> uint;
20024
43988095
JK
20025 switch (form)
20026 {
20027 case DW_FORM_string:
ecfb656c 20028 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
20029 buf += bytes_read;
20030 break;
20031
20032 case DW_FORM_line_strp:
ed2dc618
SM
20033 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
20034 abfd, buf,
ecfb656c
PA
20035 cu_header,
20036 &bytes_read));
43988095
JK
20037 buf += bytes_read;
20038 break;
20039
20040 case DW_FORM_data1:
ecfb656c 20041 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
20042 buf += 1;
20043 break;
20044
20045 case DW_FORM_data2:
ecfb656c 20046 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
20047 buf += 2;
20048 break;
20049
20050 case DW_FORM_data4:
ecfb656c 20051 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
20052 buf += 4;
20053 break;
20054
20055 case DW_FORM_data8:
ecfb656c 20056 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
20057 buf += 8;
20058 break;
20059
20060 case DW_FORM_udata:
ecfb656c 20061 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
20062 buf += bytes_read;
20063 break;
20064
20065 case DW_FORM_block:
20066 /* It is valid only for DW_LNCT_timestamp which is ignored by
20067 current GDB. */
20068 break;
20069 }
ecfb656c
PA
20070
20071 switch (content_type)
20072 {
20073 case DW_LNCT_path:
20074 if (string.has_value ())
20075 fe.name = *string;
20076 break;
20077 case DW_LNCT_directory_index:
20078 if (uint.has_value ())
20079 fe.d_index = (dir_index) *uint;
20080 break;
20081 case DW_LNCT_timestamp:
20082 if (uint.has_value ())
20083 fe.mod_time = *uint;
20084 break;
20085 case DW_LNCT_size:
20086 if (uint.has_value ())
20087 fe.length = *uint;
20088 break;
20089 case DW_LNCT_MD5:
20090 break;
20091 default:
b98664d3 20092 complaint (_("Unknown format content type %s"),
ecfb656c
PA
20093 pulongest (content_type));
20094 }
43988095
JK
20095 }
20096
ecfb656c 20097 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
20098 }
20099
20100 *bufp = buf;
20101}
20102
debd256d 20103/* Read the statement program header starting at OFFSET in
3019eac3 20104 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 20105 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
20106 Returns NULL if there is a problem reading the header, e.g., if it
20107 has a version we don't understand.
debd256d
JB
20108
20109 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
20110 the returned object point into the dwarf line section buffer,
20111 and must not be freed. */
ae2de4f8 20112
fff8551c 20113static line_header_up
9c541725 20114dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 20115{
d521ce57 20116 const gdb_byte *line_ptr;
c764a876 20117 unsigned int bytes_read, offset_size;
debd256d 20118 int i;
d521ce57 20119 const char *cur_dir, *cur_file;
3019eac3
DE
20120 struct dwarf2_section_info *section;
20121 bfd *abfd;
518817b3
SM
20122 struct dwarf2_per_objfile *dwarf2_per_objfile
20123 = cu->per_cu->dwarf2_per_objfile;
3019eac3 20124
36586728 20125 section = get_debug_line_section (cu);
3019eac3
DE
20126 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20127 if (section->buffer == NULL)
debd256d 20128 {
3019eac3 20129 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 20130 complaint (_("missing .debug_line.dwo section"));
3019eac3 20131 else
b98664d3 20132 complaint (_("missing .debug_line section"));
debd256d
JB
20133 return 0;
20134 }
20135
fceca515
DE
20136 /* We can't do this until we know the section is non-empty.
20137 Only then do we know we have such a section. */
a32a8923 20138 abfd = get_section_bfd_owner (section);
fceca515 20139
a738430d
MK
20140 /* Make sure that at least there's room for the total_length field.
20141 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 20142 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 20143 {
4d3c2250 20144 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20145 return 0;
20146 }
20147
fff8551c 20148 line_header_up lh (new line_header ());
debd256d 20149
9c541725 20150 lh->sect_off = sect_off;
527f3840
JK
20151 lh->offset_in_dwz = cu->per_cu->is_dwz;
20152
9c541725 20153 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 20154
a738430d 20155 /* Read in the header. */
6e70227d 20156 lh->total_length =
c764a876
DE
20157 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20158 &bytes_read, &offset_size);
debd256d 20159 line_ptr += bytes_read;
3019eac3 20160 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 20161 {
4d3c2250 20162 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20163 return 0;
20164 }
20165 lh->statement_program_end = line_ptr + lh->total_length;
20166 lh->version = read_2_bytes (abfd, line_ptr);
20167 line_ptr += 2;
43988095 20168 if (lh->version > 5)
cd366ee8
DE
20169 {
20170 /* This is a version we don't understand. The format could have
20171 changed in ways we don't handle properly so just punt. */
b98664d3 20172 complaint (_("unsupported version in .debug_line section"));
cd366ee8
DE
20173 return NULL;
20174 }
43988095
JK
20175 if (lh->version >= 5)
20176 {
20177 gdb_byte segment_selector_size;
20178
20179 /* Skip address size. */
20180 read_1_byte (abfd, line_ptr);
20181 line_ptr += 1;
20182
20183 segment_selector_size = read_1_byte (abfd, line_ptr);
20184 line_ptr += 1;
20185 if (segment_selector_size != 0)
20186 {
b98664d3 20187 complaint (_("unsupported segment selector size %u "
43988095
JK
20188 "in .debug_line section"),
20189 segment_selector_size);
20190 return NULL;
20191 }
20192 }
c764a876
DE
20193 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20194 line_ptr += offset_size;
debd256d
JB
20195 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20196 line_ptr += 1;
2dc7f7b3
TT
20197 if (lh->version >= 4)
20198 {
20199 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20200 line_ptr += 1;
20201 }
20202 else
20203 lh->maximum_ops_per_instruction = 1;
20204
20205 if (lh->maximum_ops_per_instruction == 0)
20206 {
20207 lh->maximum_ops_per_instruction = 1;
b98664d3 20208 complaint (_("invalid maximum_ops_per_instruction "
3e43a32a 20209 "in `.debug_line' section"));
2dc7f7b3
TT
20210 }
20211
debd256d
JB
20212 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20213 line_ptr += 1;
20214 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20215 line_ptr += 1;
20216 lh->line_range = read_1_byte (abfd, line_ptr);
20217 line_ptr += 1;
20218 lh->opcode_base = read_1_byte (abfd, line_ptr);
20219 line_ptr += 1;
fff8551c 20220 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
20221
20222 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20223 for (i = 1; i < lh->opcode_base; ++i)
20224 {
20225 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20226 line_ptr += 1;
20227 }
20228
43988095 20229 if (lh->version >= 5)
debd256d 20230 {
43988095 20231 /* Read directory table. */
ed2dc618
SM
20232 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20233 &cu->header,
fff8551c 20234 [] (struct line_header *lh, const char *name,
ecfb656c 20235 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20236 unsigned int length)
20237 {
20238 lh->add_include_dir (name);
20239 });
debd256d 20240
43988095 20241 /* Read file name table. */
ed2dc618
SM
20242 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20243 &cu->header,
fff8551c 20244 [] (struct line_header *lh, const char *name,
ecfb656c 20245 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20246 unsigned int length)
20247 {
ecfb656c 20248 lh->add_file_name (name, d_index, mod_time, length);
fff8551c 20249 });
43988095
JK
20250 }
20251 else
debd256d 20252 {
43988095
JK
20253 /* Read directory table. */
20254 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20255 {
20256 line_ptr += bytes_read;
fff8551c 20257 lh->add_include_dir (cur_dir);
43988095 20258 }
debd256d
JB
20259 line_ptr += bytes_read;
20260
43988095
JK
20261 /* Read file name table. */
20262 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20263 {
ecfb656c
PA
20264 unsigned int mod_time, length;
20265 dir_index d_index;
43988095
JK
20266
20267 line_ptr += bytes_read;
ecfb656c 20268 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
20269 line_ptr += bytes_read;
20270 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20271 line_ptr += bytes_read;
20272 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20273 line_ptr += bytes_read;
20274
ecfb656c 20275 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
20276 }
20277 line_ptr += bytes_read;
debd256d 20278 }
6e70227d 20279 lh->statement_program_start = line_ptr;
debd256d 20280
3019eac3 20281 if (line_ptr > (section->buffer + section->size))
b98664d3 20282 complaint (_("line number info header doesn't "
3e43a32a 20283 "fit in `.debug_line' section"));
debd256d 20284
debd256d
JB
20285 return lh;
20286}
c906108c 20287
c6da4cef
DE
20288/* Subroutine of dwarf_decode_lines to simplify it.
20289 Return the file name of the psymtab for included file FILE_INDEX
20290 in line header LH of PST.
20291 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
20292 If space for the result is malloc'd, *NAME_HOLDER will be set.
20293 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 20294
d521ce57 20295static const char *
c6da4cef
DE
20296psymtab_include_file_name (const struct line_header *lh, int file_index,
20297 const struct partial_symtab *pst,
c89b44cd
TT
20298 const char *comp_dir,
20299 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 20300{
8c43009f 20301 const file_entry &fe = lh->file_names[file_index];
d521ce57
TT
20302 const char *include_name = fe.name;
20303 const char *include_name_to_compare = include_name;
72b9f47f 20304 const char *pst_filename;
c6da4cef
DE
20305 int file_is_pst;
20306
8c43009f 20307 const char *dir_name = fe.include_dir (lh);
c6da4cef 20308
c89b44cd 20309 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
20310 if (!IS_ABSOLUTE_PATH (include_name)
20311 && (dir_name != NULL || comp_dir != NULL))
20312 {
20313 /* Avoid creating a duplicate psymtab for PST.
20314 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20315 Before we do the comparison, however, we need to account
20316 for DIR_NAME and COMP_DIR.
20317 First prepend dir_name (if non-NULL). If we still don't
20318 have an absolute path prepend comp_dir (if non-NULL).
20319 However, the directory we record in the include-file's
20320 psymtab does not contain COMP_DIR (to match the
20321 corresponding symtab(s)).
20322
20323 Example:
20324
20325 bash$ cd /tmp
20326 bash$ gcc -g ./hello.c
20327 include_name = "hello.c"
20328 dir_name = "."
20329 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
20330 DW_AT_name = "./hello.c"
20331
20332 */
c6da4cef
DE
20333
20334 if (dir_name != NULL)
20335 {
c89b44cd
TT
20336 name_holder->reset (concat (dir_name, SLASH_STRING,
20337 include_name, (char *) NULL));
20338 include_name = name_holder->get ();
c6da4cef 20339 include_name_to_compare = include_name;
c6da4cef
DE
20340 }
20341 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20342 {
c89b44cd
TT
20343 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20344 include_name, (char *) NULL));
20345 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
20346 }
20347 }
20348
20349 pst_filename = pst->filename;
c89b44cd 20350 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
20351 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20352 {
c89b44cd
TT
20353 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20354 pst_filename, (char *) NULL));
20355 pst_filename = copied_name.get ();
c6da4cef
DE
20356 }
20357
1e3fad37 20358 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 20359
c6da4cef
DE
20360 if (file_is_pst)
20361 return NULL;
20362 return include_name;
20363}
20364
d9b3de22
DE
20365/* State machine to track the state of the line number program. */
20366
6f77053d 20367class lnp_state_machine
d9b3de22 20368{
6f77053d
PA
20369public:
20370 /* Initialize a machine state for the start of a line number
20371 program. */
804d2729
TT
20372 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
20373 bool record_lines_p);
6f77053d 20374
8c43009f
PA
20375 file_entry *current_file ()
20376 {
20377 /* lh->file_names is 0-based, but the file name numbers in the
20378 statement program are 1-based. */
6f77053d
PA
20379 return m_line_header->file_name_at (m_file);
20380 }
20381
20382 /* Record the line in the state machine. END_SEQUENCE is true if
20383 we're processing the end of a sequence. */
20384 void record_line (bool end_sequence);
20385
7ab6656f
OJ
20386 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
20387 nop-out rest of the lines in this sequence. */
6f77053d
PA
20388 void check_line_address (struct dwarf2_cu *cu,
20389 const gdb_byte *line_ptr,
7ab6656f 20390 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
20391
20392 void handle_set_discriminator (unsigned int discriminator)
20393 {
20394 m_discriminator = discriminator;
20395 m_line_has_non_zero_discriminator |= discriminator != 0;
20396 }
20397
20398 /* Handle DW_LNE_set_address. */
20399 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20400 {
20401 m_op_index = 0;
20402 address += baseaddr;
20403 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20404 }
20405
20406 /* Handle DW_LNS_advance_pc. */
20407 void handle_advance_pc (CORE_ADDR adjust);
20408
20409 /* Handle a special opcode. */
20410 void handle_special_opcode (unsigned char op_code);
20411
20412 /* Handle DW_LNS_advance_line. */
20413 void handle_advance_line (int line_delta)
20414 {
20415 advance_line (line_delta);
20416 }
20417
20418 /* Handle DW_LNS_set_file. */
20419 void handle_set_file (file_name_index file);
20420
20421 /* Handle DW_LNS_negate_stmt. */
20422 void handle_negate_stmt ()
20423 {
20424 m_is_stmt = !m_is_stmt;
20425 }
20426
20427 /* Handle DW_LNS_const_add_pc. */
20428 void handle_const_add_pc ();
20429
20430 /* Handle DW_LNS_fixed_advance_pc. */
20431 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20432 {
20433 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20434 m_op_index = 0;
20435 }
20436
20437 /* Handle DW_LNS_copy. */
20438 void handle_copy ()
20439 {
20440 record_line (false);
20441 m_discriminator = 0;
20442 }
20443
20444 /* Handle DW_LNE_end_sequence. */
20445 void handle_end_sequence ()
20446 {
804d2729 20447 m_currently_recording_lines = true;
6f77053d
PA
20448 }
20449
20450private:
20451 /* Advance the line by LINE_DELTA. */
20452 void advance_line (int line_delta)
20453 {
20454 m_line += line_delta;
20455
20456 if (line_delta != 0)
20457 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
20458 }
20459
804d2729
TT
20460 struct dwarf2_cu *m_cu;
20461
6f77053d
PA
20462 gdbarch *m_gdbarch;
20463
20464 /* True if we're recording lines.
20465 Otherwise we're building partial symtabs and are just interested in
20466 finding include files mentioned by the line number program. */
20467 bool m_record_lines_p;
20468
8c43009f 20469 /* The line number header. */
6f77053d 20470 line_header *m_line_header;
8c43009f 20471
6f77053d
PA
20472 /* These are part of the standard DWARF line number state machine,
20473 and initialized according to the DWARF spec. */
d9b3de22 20474
6f77053d 20475 unsigned char m_op_index = 0;
8c43009f 20476 /* The line table index (1-based) of the current file. */
6f77053d
PA
20477 file_name_index m_file = (file_name_index) 1;
20478 unsigned int m_line = 1;
20479
20480 /* These are initialized in the constructor. */
20481
20482 CORE_ADDR m_address;
20483 bool m_is_stmt;
20484 unsigned int m_discriminator;
d9b3de22
DE
20485
20486 /* Additional bits of state we need to track. */
20487
20488 /* The last file that we called dwarf2_start_subfile for.
20489 This is only used for TLLs. */
6f77053d 20490 unsigned int m_last_file = 0;
d9b3de22 20491 /* The last file a line number was recorded for. */
6f77053d 20492 struct subfile *m_last_subfile = NULL;
d9b3de22 20493
804d2729
TT
20494 /* When true, record the lines we decode. */
20495 bool m_currently_recording_lines = false;
d9b3de22
DE
20496
20497 /* The last line number that was recorded, used to coalesce
20498 consecutive entries for the same line. This can happen, for
20499 example, when discriminators are present. PR 17276. */
6f77053d
PA
20500 unsigned int m_last_line = 0;
20501 bool m_line_has_non_zero_discriminator = false;
8c43009f 20502};
d9b3de22 20503
6f77053d
PA
20504void
20505lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20506{
20507 CORE_ADDR addr_adj = (((m_op_index + adjust)
20508 / m_line_header->maximum_ops_per_instruction)
20509 * m_line_header->minimum_instruction_length);
20510 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20511 m_op_index = ((m_op_index + adjust)
20512 % m_line_header->maximum_ops_per_instruction);
20513}
d9b3de22 20514
6f77053d
PA
20515void
20516lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 20517{
6f77053d
PA
20518 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20519 CORE_ADDR addr_adj = (((m_op_index
20520 + (adj_opcode / m_line_header->line_range))
20521 / m_line_header->maximum_ops_per_instruction)
20522 * m_line_header->minimum_instruction_length);
20523 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20524 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20525 % m_line_header->maximum_ops_per_instruction);
d9b3de22 20526
6f77053d
PA
20527 int line_delta = (m_line_header->line_base
20528 + (adj_opcode % m_line_header->line_range));
20529 advance_line (line_delta);
20530 record_line (false);
20531 m_discriminator = 0;
20532}
d9b3de22 20533
6f77053d
PA
20534void
20535lnp_state_machine::handle_set_file (file_name_index file)
20536{
20537 m_file = file;
20538
20539 const file_entry *fe = current_file ();
20540 if (fe == NULL)
20541 dwarf2_debug_line_missing_file_complaint ();
20542 else if (m_record_lines_p)
20543 {
20544 const char *dir = fe->include_dir (m_line_header);
20545
804d2729 20546 m_last_subfile = m_cu->builder->get_current_subfile ();
6f77053d 20547 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 20548 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
20549 }
20550}
20551
20552void
20553lnp_state_machine::handle_const_add_pc ()
20554{
20555 CORE_ADDR adjust
20556 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20557
20558 CORE_ADDR addr_adj
20559 = (((m_op_index + adjust)
20560 / m_line_header->maximum_ops_per_instruction)
20561 * m_line_header->minimum_instruction_length);
20562
20563 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20564 m_op_index = ((m_op_index + adjust)
20565 % m_line_header->maximum_ops_per_instruction);
20566}
d9b3de22 20567
a05a36a5
DE
20568/* Return non-zero if we should add LINE to the line number table.
20569 LINE is the line to add, LAST_LINE is the last line that was added,
20570 LAST_SUBFILE is the subfile for LAST_LINE.
20571 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20572 had a non-zero discriminator.
20573
20574 We have to be careful in the presence of discriminators.
20575 E.g., for this line:
20576
20577 for (i = 0; i < 100000; i++);
20578
20579 clang can emit four line number entries for that one line,
20580 each with a different discriminator.
20581 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20582
20583 However, we want gdb to coalesce all four entries into one.
20584 Otherwise the user could stepi into the middle of the line and
20585 gdb would get confused about whether the pc really was in the
20586 middle of the line.
20587
20588 Things are further complicated by the fact that two consecutive
20589 line number entries for the same line is a heuristic used by gcc
20590 to denote the end of the prologue. So we can't just discard duplicate
20591 entries, we have to be selective about it. The heuristic we use is
20592 that we only collapse consecutive entries for the same line if at least
20593 one of those entries has a non-zero discriminator. PR 17276.
20594
20595 Note: Addresses in the line number state machine can never go backwards
20596 within one sequence, thus this coalescing is ok. */
20597
20598static int
804d2729
TT
20599dwarf_record_line_p (struct dwarf2_cu *cu,
20600 unsigned int line, unsigned int last_line,
a05a36a5
DE
20601 int line_has_non_zero_discriminator,
20602 struct subfile *last_subfile)
20603{
804d2729 20604 if (cu->builder->get_current_subfile () != last_subfile)
a05a36a5
DE
20605 return 1;
20606 if (line != last_line)
20607 return 1;
20608 /* Same line for the same file that we've seen already.
20609 As a last check, for pr 17276, only record the line if the line
20610 has never had a non-zero discriminator. */
20611 if (!line_has_non_zero_discriminator)
20612 return 1;
20613 return 0;
20614}
20615
804d2729
TT
20616/* Use the CU's builder to record line number LINE beginning at
20617 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
20618
20619static void
d9b3de22
DE
20620dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20621 unsigned int line, CORE_ADDR address,
804d2729 20622 struct dwarf2_cu *cu)
252a6764
DE
20623{
20624 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20625
27e0867f
DE
20626 if (dwarf_line_debug)
20627 {
20628 fprintf_unfiltered (gdb_stdlog,
20629 "Recording line %u, file %s, address %s\n",
20630 line, lbasename (subfile->name),
20631 paddress (gdbarch, address));
20632 }
20633
804d2729
TT
20634 if (cu != nullptr)
20635 cu->builder->record_line (subfile, line, addr);
252a6764
DE
20636}
20637
20638/* Subroutine of dwarf_decode_lines_1 to simplify it.
20639 Mark the end of a set of line number records.
d9b3de22 20640 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
20641 If SUBFILE is NULL the request is ignored. */
20642
20643static void
20644dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 20645 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 20646{
27e0867f
DE
20647 if (subfile == NULL)
20648 return;
20649
20650 if (dwarf_line_debug)
20651 {
20652 fprintf_unfiltered (gdb_stdlog,
20653 "Finishing current line, file %s, address %s\n",
20654 lbasename (subfile->name),
20655 paddress (gdbarch, address));
20656 }
20657
804d2729 20658 dwarf_record_line_1 (gdbarch, subfile, 0, address, cu);
d9b3de22
DE
20659}
20660
6f77053d
PA
20661void
20662lnp_state_machine::record_line (bool end_sequence)
d9b3de22 20663{
d9b3de22
DE
20664 if (dwarf_line_debug)
20665 {
20666 fprintf_unfiltered (gdb_stdlog,
20667 "Processing actual line %u: file %u,"
20668 " address %s, is_stmt %u, discrim %u\n",
6f77053d
PA
20669 m_line, to_underlying (m_file),
20670 paddress (m_gdbarch, m_address),
20671 m_is_stmt, m_discriminator);
d9b3de22
DE
20672 }
20673
6f77053d 20674 file_entry *fe = current_file ();
8c43009f
PA
20675
20676 if (fe == NULL)
d9b3de22
DE
20677 dwarf2_debug_line_missing_file_complaint ();
20678 /* For now we ignore lines not starting on an instruction boundary.
20679 But not when processing end_sequence for compatibility with the
20680 previous version of the code. */
6f77053d 20681 else if (m_op_index == 0 || end_sequence)
d9b3de22 20682 {
8c43009f 20683 fe->included_p = 1;
6f77053d 20684 if (m_record_lines_p && m_is_stmt)
d9b3de22 20685 {
804d2729
TT
20686 if (m_last_subfile != m_cu->builder->get_current_subfile ()
20687 || end_sequence)
d9b3de22 20688 {
804d2729
TT
20689 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20690 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
20691 }
20692
20693 if (!end_sequence)
20694 {
804d2729 20695 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
20696 m_line_has_non_zero_discriminator,
20697 m_last_subfile))
d9b3de22 20698 {
804d2729
TT
20699 dwarf_record_line_1 (m_gdbarch,
20700 m_cu->builder->get_current_subfile (),
6f77053d 20701 m_line, m_address,
804d2729 20702 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 20703 }
804d2729 20704 m_last_subfile = m_cu->builder->get_current_subfile ();
6f77053d 20705 m_last_line = m_line;
d9b3de22
DE
20706 }
20707 }
20708 }
20709}
20710
804d2729
TT
20711lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20712 line_header *lh, bool record_lines_p)
d9b3de22 20713{
804d2729 20714 m_cu = cu;
6f77053d
PA
20715 m_gdbarch = arch;
20716 m_record_lines_p = record_lines_p;
20717 m_line_header = lh;
d9b3de22 20718
804d2729 20719 m_currently_recording_lines = true;
d9b3de22 20720
d9b3de22
DE
20721 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20722 was a line entry for it so that the backend has a chance to adjust it
20723 and also record it in case it needs it. This is currently used by MIPS
20724 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
20725 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20726 m_is_stmt = lh->default_is_stmt;
20727 m_discriminator = 0;
252a6764
DE
20728}
20729
6f77053d
PA
20730void
20731lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20732 const gdb_byte *line_ptr,
7ab6656f 20733 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 20734{
7ab6656f
OJ
20735 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20736 the pc range of the CU. However, we restrict the test to only ADDRESS
20737 values of zero to preserve GDB's previous behaviour which is to handle
20738 the specific case of a function being GC'd by the linker. */
924c2928 20739
7ab6656f 20740 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
20741 {
20742 /* This line table is for a function which has been
20743 GCd by the linker. Ignore it. PR gdb/12528 */
20744
518817b3 20745 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
924c2928
DE
20746 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20747
b98664d3 20748 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 20749 line_offset, objfile_name (objfile));
804d2729
TT
20750 m_currently_recording_lines = false;
20751 /* Note: m_currently_recording_lines is left as false until we see
20752 DW_LNE_end_sequence. */
924c2928
DE
20753 }
20754}
20755
f3f5162e 20756/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
20757 Process the line number information in LH.
20758 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20759 program in order to set included_p for every referenced header. */
debd256d 20760
c906108c 20761static void
43f3e411
DE
20762dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20763 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 20764{
d521ce57
TT
20765 const gdb_byte *line_ptr, *extended_end;
20766 const gdb_byte *line_end;
a8c50c1f 20767 unsigned int bytes_read, extended_len;
699ca60a 20768 unsigned char op_code, extended_op;
e142c38c 20769 CORE_ADDR baseaddr;
518817b3 20770 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 20771 bfd *abfd = objfile->obfd;
fbf65064 20772 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
20773 /* True if we're recording line info (as opposed to building partial
20774 symtabs and just interested in finding include files mentioned by
20775 the line number program). */
20776 bool record_lines_p = !decode_for_pst_p;
e142c38c
DJ
20777
20778 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 20779
debd256d
JB
20780 line_ptr = lh->statement_program_start;
20781 line_end = lh->statement_program_end;
c906108c
SS
20782
20783 /* Read the statement sequences until there's nothing left. */
20784 while (line_ptr < line_end)
20785 {
6f77053d
PA
20786 /* The DWARF line number program state machine. Reset the state
20787 machine at the start of each sequence. */
804d2729 20788 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 20789 bool end_sequence = false;
d9b3de22 20790
8c43009f 20791 if (record_lines_p)
c906108c 20792 {
8c43009f
PA
20793 /* Start a subfile for the current file of the state
20794 machine. */
20795 const file_entry *fe = state_machine.current_file ();
20796
20797 if (fe != NULL)
804d2729 20798 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
20799 }
20800
a738430d 20801 /* Decode the table. */
d9b3de22 20802 while (line_ptr < line_end && !end_sequence)
c906108c
SS
20803 {
20804 op_code = read_1_byte (abfd, line_ptr);
20805 line_ptr += 1;
9aa1fe7e 20806
debd256d 20807 if (op_code >= lh->opcode_base)
6e70227d 20808 {
8e07a239 20809 /* Special opcode. */
6f77053d 20810 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
20811 }
20812 else switch (op_code)
c906108c
SS
20813 {
20814 case DW_LNS_extended_op:
3e43a32a
MS
20815 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20816 &bytes_read);
473b7be6 20817 line_ptr += bytes_read;
a8c50c1f 20818 extended_end = line_ptr + extended_len;
c906108c
SS
20819 extended_op = read_1_byte (abfd, line_ptr);
20820 line_ptr += 1;
20821 switch (extended_op)
20822 {
20823 case DW_LNE_end_sequence:
6f77053d
PA
20824 state_machine.handle_end_sequence ();
20825 end_sequence = true;
c906108c
SS
20826 break;
20827 case DW_LNE_set_address:
d9b3de22
DE
20828 {
20829 CORE_ADDR address
20830 = read_address (abfd, line_ptr, cu, &bytes_read);
d9b3de22 20831 line_ptr += bytes_read;
6f77053d
PA
20832
20833 state_machine.check_line_address (cu, line_ptr,
7ab6656f 20834 lowpc - baseaddr, address);
6f77053d 20835 state_machine.handle_set_address (baseaddr, address);
d9b3de22 20836 }
c906108c
SS
20837 break;
20838 case DW_LNE_define_file:
debd256d 20839 {
d521ce57 20840 const char *cur_file;
ecfb656c
PA
20841 unsigned int mod_time, length;
20842 dir_index dindex;
6e70227d 20843
3e43a32a
MS
20844 cur_file = read_direct_string (abfd, line_ptr,
20845 &bytes_read);
debd256d 20846 line_ptr += bytes_read;
ecfb656c 20847 dindex = (dir_index)
debd256d
JB
20848 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20849 line_ptr += bytes_read;
20850 mod_time =
20851 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20852 line_ptr += bytes_read;
20853 length =
20854 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20855 line_ptr += bytes_read;
ecfb656c 20856 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 20857 }
c906108c 20858 break;
d0c6ba3d 20859 case DW_LNE_set_discriminator:
6f77053d
PA
20860 {
20861 /* The discriminator is not interesting to the
20862 debugger; just ignore it. We still need to
20863 check its value though:
20864 if there are consecutive entries for the same
20865 (non-prologue) line we want to coalesce them.
20866 PR 17276. */
20867 unsigned int discr
20868 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20869 line_ptr += bytes_read;
20870
20871 state_machine.handle_set_discriminator (discr);
20872 }
d0c6ba3d 20873 break;
c906108c 20874 default:
b98664d3 20875 complaint (_("mangled .debug_line section"));
debd256d 20876 return;
c906108c 20877 }
a8c50c1f
DJ
20878 /* Make sure that we parsed the extended op correctly. If e.g.
20879 we expected a different address size than the producer used,
20880 we may have read the wrong number of bytes. */
20881 if (line_ptr != extended_end)
20882 {
b98664d3 20883 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
20884 return;
20885 }
c906108c
SS
20886 break;
20887 case DW_LNS_copy:
6f77053d 20888 state_machine.handle_copy ();
c906108c
SS
20889 break;
20890 case DW_LNS_advance_pc:
2dc7f7b3
TT
20891 {
20892 CORE_ADDR adjust
20893 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 20894 line_ptr += bytes_read;
6f77053d
PA
20895
20896 state_machine.handle_advance_pc (adjust);
2dc7f7b3 20897 }
c906108c
SS
20898 break;
20899 case DW_LNS_advance_line:
a05a36a5
DE
20900 {
20901 int line_delta
20902 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 20903 line_ptr += bytes_read;
6f77053d
PA
20904
20905 state_machine.handle_advance_line (line_delta);
a05a36a5 20906 }
c906108c
SS
20907 break;
20908 case DW_LNS_set_file:
d9b3de22 20909 {
6f77053d 20910 file_name_index file
ecfb656c
PA
20911 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20912 &bytes_read);
d9b3de22 20913 line_ptr += bytes_read;
8c43009f 20914
6f77053d 20915 state_machine.handle_set_file (file);
d9b3de22 20916 }
c906108c
SS
20917 break;
20918 case DW_LNS_set_column:
0ad93d4f 20919 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
20920 line_ptr += bytes_read;
20921 break;
20922 case DW_LNS_negate_stmt:
6f77053d 20923 state_machine.handle_negate_stmt ();
c906108c
SS
20924 break;
20925 case DW_LNS_set_basic_block:
c906108c 20926 break;
c2c6d25f
JM
20927 /* Add to the address register of the state machine the
20928 address increment value corresponding to special opcode
a738430d
MK
20929 255. I.e., this value is scaled by the minimum
20930 instruction length since special opcode 255 would have
b021a221 20931 scaled the increment. */
c906108c 20932 case DW_LNS_const_add_pc:
6f77053d 20933 state_machine.handle_const_add_pc ();
c906108c
SS
20934 break;
20935 case DW_LNS_fixed_advance_pc:
3e29f34a 20936 {
6f77053d 20937 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 20938 line_ptr += 2;
6f77053d
PA
20939
20940 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 20941 }
c906108c 20942 break;
9aa1fe7e 20943 default:
a738430d
MK
20944 {
20945 /* Unknown standard opcode, ignore it. */
9aa1fe7e 20946 int i;
a738430d 20947
debd256d 20948 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
20949 {
20950 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20951 line_ptr += bytes_read;
20952 }
20953 }
c906108c
SS
20954 }
20955 }
d9b3de22
DE
20956
20957 if (!end_sequence)
20958 dwarf2_debug_line_missing_end_sequence_complaint ();
20959
20960 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20961 in which case we still finish recording the last line). */
6f77053d 20962 state_machine.record_line (true);
c906108c 20963 }
f3f5162e
DE
20964}
20965
20966/* Decode the Line Number Program (LNP) for the given line_header
20967 structure and CU. The actual information extracted and the type
20968 of structures created from the LNP depends on the value of PST.
20969
20970 1. If PST is NULL, then this procedure uses the data from the program
20971 to create all necessary symbol tables, and their linetables.
20972
20973 2. If PST is not NULL, this procedure reads the program to determine
20974 the list of files included by the unit represented by PST, and
20975 builds all the associated partial symbol tables.
20976
20977 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20978 It is used for relative paths in the line table.
20979 NOTE: When processing partial symtabs (pst != NULL),
20980 comp_dir == pst->dirname.
20981
20982 NOTE: It is important that psymtabs have the same file name (via strcmp)
20983 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20984 symtab we don't use it in the name of the psymtabs we create.
20985 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
20986 A good testcase for this is mb-inline.exp.
20987
527f3840
JK
20988 LOWPC is the lowest address in CU (or 0 if not known).
20989
20990 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20991 for its PC<->lines mapping information. Otherwise only the filename
20992 table is read in. */
f3f5162e
DE
20993
20994static void
20995dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
c3b7b696 20996 struct dwarf2_cu *cu, struct partial_symtab *pst,
527f3840 20997 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 20998{
518817b3 20999 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 21000 const int decode_for_pst_p = (pst != NULL);
f3f5162e 21001
527f3840
JK
21002 if (decode_mapping)
21003 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
21004
21005 if (decode_for_pst_p)
21006 {
21007 int file_index;
21008
21009 /* Now that we're done scanning the Line Header Program, we can
21010 create the psymtab of each included file. */
fff8551c 21011 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
aaa75496
JB
21012 if (lh->file_names[file_index].included_p == 1)
21013 {
c89b44cd 21014 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 21015 const char *include_name =
c89b44cd
TT
21016 psymtab_include_file_name (lh, file_index, pst, comp_dir,
21017 &name_holder);
c6da4cef 21018 if (include_name != NULL)
aaa75496
JB
21019 dwarf2_create_include_psymtab (include_name, pst, objfile);
21020 }
21021 }
cb1df416
DJ
21022 else
21023 {
21024 /* Make sure a symtab is created for every file, even files
21025 which contain only variables (i.e. no code with associated
21026 line numbers). */
804d2729 21027 struct compunit_symtab *cust = cu->builder->get_compunit_symtab ();
cb1df416 21028 int i;
cb1df416 21029
fff8551c 21030 for (i = 0; i < lh->file_names.size (); i++)
cb1df416 21031 {
8c43009f 21032 file_entry &fe = lh->file_names[i];
9a619af0 21033
804d2729 21034 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
cb1df416 21035
804d2729 21036 if (cu->builder->get_current_subfile ()->symtab == NULL)
43f3e411 21037 {
804d2729
TT
21038 cu->builder->get_current_subfile ()->symtab
21039 = allocate_symtab (cust,
21040 cu->builder->get_current_subfile ()->name);
43f3e411 21041 }
804d2729 21042 fe.symtab = cu->builder->get_current_subfile ()->symtab;
cb1df416
DJ
21043 }
21044 }
c906108c
SS
21045}
21046
21047/* Start a subfile for DWARF. FILENAME is the name of the file and
21048 DIRNAME the name of the source directory which contains FILENAME
4d663531 21049 or NULL if not known.
c906108c
SS
21050 This routine tries to keep line numbers from identical absolute and
21051 relative file names in a common subfile.
21052
21053 Using the `list' example from the GDB testsuite, which resides in
21054 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21055 of /srcdir/list0.c yields the following debugging information for list0.c:
21056
c5aa993b 21057 DW_AT_name: /srcdir/list0.c
4d663531 21058 DW_AT_comp_dir: /compdir
357e46e7 21059 files.files[0].name: list0.h
c5aa993b 21060 files.files[0].dir: /srcdir
357e46e7 21061 files.files[1].name: list0.c
c5aa993b 21062 files.files[1].dir: /srcdir
c906108c
SS
21063
21064 The line number information for list0.c has to end up in a single
4f1520fb
FR
21065 subfile, so that `break /srcdir/list0.c:1' works as expected.
21066 start_subfile will ensure that this happens provided that we pass the
21067 concatenation of files.files[1].dir and files.files[1].name as the
21068 subfile's name. */
c906108c
SS
21069
21070static void
804d2729
TT
21071dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21072 const char *dirname)
c906108c 21073{
d521ce57 21074 char *copy = NULL;
4f1520fb 21075
4d663531 21076 /* In order not to lose the line information directory,
4f1520fb
FR
21077 we concatenate it to the filename when it makes sense.
21078 Note that the Dwarf3 standard says (speaking of filenames in line
21079 information): ``The directory index is ignored for file names
21080 that represent full path names''. Thus ignoring dirname in the
21081 `else' branch below isn't an issue. */
c906108c 21082
d5166ae1 21083 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
21084 {
21085 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21086 filename = copy;
21087 }
c906108c 21088
804d2729 21089 cu->builder->start_subfile (filename);
4f1520fb 21090
d521ce57
TT
21091 if (copy != NULL)
21092 xfree (copy);
c906108c
SS
21093}
21094
804d2729
TT
21095/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21096 buildsym_compunit constructor. */
f4dc4d17 21097
43f3e411 21098static struct compunit_symtab *
f4dc4d17 21099dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 21100 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17 21101{
804d2729 21102 gdb_assert (cu->builder == nullptr);
43f3e411 21103
804d2729
TT
21104 cu->builder.reset (new struct buildsym_compunit
21105 (cu->per_cu->dwarf2_per_objfile->objfile,
21106 name, comp_dir, cu->language, low_pc));
93b8bea4 21107
804d2729
TT
21108 cu->list_in_scope = cu->builder->get_file_symbols ();
21109
21110 cu->builder->record_debugformat ("DWARF 2");
21111 cu->builder->record_producer (cu->producer);
f4dc4d17 21112
4d4ec4e5 21113 cu->processing_has_namespace_info = 0;
43f3e411 21114
804d2729 21115 return cu->builder->get_compunit_symtab ();
f4dc4d17
DE
21116}
21117
4c2df51b
DJ
21118static void
21119var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 21120 struct dwarf2_cu *cu)
4c2df51b 21121{
518817b3 21122 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e7c27a73
DJ
21123 struct comp_unit_head *cu_header = &cu->header;
21124
4c2df51b
DJ
21125 /* NOTE drow/2003-01-30: There used to be a comment and some special
21126 code here to turn a symbol with DW_AT_external and a
21127 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21128 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21129 with some versions of binutils) where shared libraries could have
21130 relocations against symbols in their debug information - the
21131 minimal symbol would have the right address, but the debug info
21132 would not. It's no longer necessary, because we will explicitly
21133 apply relocations when we read in the debug information now. */
21134
21135 /* A DW_AT_location attribute with no contents indicates that a
21136 variable has been optimized away. */
21137 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21138 {
f1e6e072 21139 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
21140 return;
21141 }
21142
21143 /* Handle one degenerate form of location expression specially, to
21144 preserve GDB's previous behavior when section offsets are
3019eac3
DE
21145 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
21146 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
21147
21148 if (attr_form_is_block (attr)
3019eac3
DE
21149 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21150 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21151 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21152 && (DW_BLOCK (attr)->size
21153 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 21154 {
891d2f0b 21155 unsigned int dummy;
4c2df51b 21156
3019eac3
DE
21157 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21158 SYMBOL_VALUE_ADDRESS (sym) =
21159 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
21160 else
21161 SYMBOL_VALUE_ADDRESS (sym) =
21162 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 21163 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
21164 fixup_symbol_section (sym, objfile);
21165 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
21166 SYMBOL_SECTION (sym));
4c2df51b
DJ
21167 return;
21168 }
21169
21170 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21171 expression evaluator, and use LOC_COMPUTED only when necessary
21172 (i.e. when the value of a register or memory location is
21173 referenced, or a thread-local block, etc.). Then again, it might
21174 not be worthwhile. I'm assuming that it isn't unless performance
21175 or memory numbers show me otherwise. */
21176
f1e6e072 21177 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 21178
f1e6e072 21179 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 21180 cu->has_loclist = 1;
4c2df51b
DJ
21181}
21182
c906108c
SS
21183/* Given a pointer to a DWARF information entry, figure out if we need
21184 to make a symbol table entry for it, and if so, create a new entry
21185 and return a pointer to it.
21186 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
21187 used the passed type.
21188 If SPACE is not NULL, use it to hold the new symbol. If it is
21189 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
21190
21191static struct symbol *
5e2db402
TT
21192new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21193 struct symbol *space)
c906108c 21194{
518817b3
SM
21195 struct dwarf2_per_objfile *dwarf2_per_objfile
21196 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21197 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 21198 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 21199 struct symbol *sym = NULL;
15d034d0 21200 const char *name;
c906108c
SS
21201 struct attribute *attr = NULL;
21202 struct attribute *attr2 = NULL;
e142c38c 21203 CORE_ADDR baseaddr;
e37fd15a
SW
21204 struct pending **list_to_add = NULL;
21205
edb3359d 21206 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
21207
21208 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 21209
94af9270 21210 name = dwarf2_name (die, cu);
c906108c
SS
21211 if (name)
21212 {
94af9270 21213 const char *linkagename;
34eaf542 21214 int suppress_add = 0;
94af9270 21215
34eaf542
TT
21216 if (space)
21217 sym = space;
21218 else
e623cf5d 21219 sym = allocate_symbol (objfile);
c906108c 21220 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
21221
21222 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 21223 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
21224 linkagename = dwarf2_physname (name, die, cu);
21225 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 21226
f55ee35c
JK
21227 /* Fortran does not have mangling standard and the mangling does differ
21228 between gfortran, iFort etc. */
21229 if (cu->language == language_fortran
b250c185 21230 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 21231 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 21232 dwarf2_full_name (name, die, cu),
29df156d 21233 NULL);
f55ee35c 21234
c906108c 21235 /* Default assumptions.
c5aa993b 21236 Use the passed type or decode it from the die. */
176620f1 21237 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 21238 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
21239 if (type != NULL)
21240 SYMBOL_TYPE (sym) = type;
21241 else
e7c27a73 21242 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
21243 attr = dwarf2_attr (die,
21244 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21245 cu);
c906108c
SS
21246 if (attr)
21247 {
21248 SYMBOL_LINE (sym) = DW_UNSND (attr);
21249 }
cb1df416 21250
edb3359d
DJ
21251 attr = dwarf2_attr (die,
21252 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21253 cu);
cb1df416
DJ
21254 if (attr)
21255 {
ecfb656c 21256 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 21257 struct file_entry *fe;
9a619af0 21258
ecfb656c
PA
21259 if (cu->line_header != NULL)
21260 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
21261 else
21262 fe = NULL;
21263
21264 if (fe == NULL)
b98664d3 21265 complaint (_("file index out of range"));
8c43009f
PA
21266 else
21267 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
21268 }
21269
c906108c
SS
21270 switch (die->tag)
21271 {
21272 case DW_TAG_label:
e142c38c 21273 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c 21274 if (attr)
3e29f34a
MR
21275 {
21276 CORE_ADDR addr;
21277
21278 addr = attr_value_as_address (attr);
21279 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21280 SYMBOL_VALUE_ADDRESS (sym) = addr;
21281 }
0f5238ed
TT
21282 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21283 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 21284 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
380618d6 21285 dw2_add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
21286 break;
21287 case DW_TAG_subprogram:
21288 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21289 finish_block. */
f1e6e072 21290 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 21291 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
21292 if ((attr2 && (DW_UNSND (attr2) != 0))
21293 || cu->language == language_ada)
c906108c 21294 {
2cfa0c8d
JB
21295 /* Subprograms marked external are stored as a global symbol.
21296 Ada subprograms, whether marked external or not, are always
21297 stored as a global symbol, because we want to be able to
21298 access them globally. For instance, we want to be able
21299 to break on a nested subprogram without having to
21300 specify the context. */
804d2729 21301 list_to_add = cu->builder->get_global_symbols ();
c906108c
SS
21302 }
21303 else
21304 {
e37fd15a 21305 list_to_add = cu->list_in_scope;
c906108c
SS
21306 }
21307 break;
edb3359d
DJ
21308 case DW_TAG_inlined_subroutine:
21309 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21310 finish_block. */
f1e6e072 21311 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 21312 SYMBOL_INLINED (sym) = 1;
481860b3 21313 list_to_add = cu->list_in_scope;
edb3359d 21314 break;
34eaf542
TT
21315 case DW_TAG_template_value_param:
21316 suppress_add = 1;
21317 /* Fall through. */
72929c62 21318 case DW_TAG_constant:
c906108c 21319 case DW_TAG_variable:
254e6b9e 21320 case DW_TAG_member:
0963b4bd
MS
21321 /* Compilation with minimal debug info may result in
21322 variables with missing type entries. Change the
21323 misleading `void' type to something sensible. */
c906108c 21324 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 21325 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 21326
e142c38c 21327 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
21328 /* In the case of DW_TAG_member, we should only be called for
21329 static const members. */
21330 if (die->tag == DW_TAG_member)
21331 {
3863f96c
DE
21332 /* dwarf2_add_field uses die_is_declaration,
21333 so we do the same. */
254e6b9e
DE
21334 gdb_assert (die_is_declaration (die, cu));
21335 gdb_assert (attr);
21336 }
c906108c
SS
21337 if (attr)
21338 {
e7c27a73 21339 dwarf2_const_value (attr, sym, cu);
e142c38c 21340 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 21341 if (!suppress_add)
34eaf542
TT
21342 {
21343 if (attr2 && (DW_UNSND (attr2) != 0))
804d2729 21344 list_to_add = cu->builder->get_global_symbols ();
34eaf542 21345 else
e37fd15a 21346 list_to_add = cu->list_in_scope;
34eaf542 21347 }
c906108c
SS
21348 break;
21349 }
e142c38c 21350 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
21351 if (attr)
21352 {
e7c27a73 21353 var_decode_location (attr, sym, cu);
e142c38c 21354 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
21355
21356 /* Fortran explicitly imports any global symbols to the local
21357 scope by DW_TAG_common_block. */
21358 if (cu->language == language_fortran && die->parent
21359 && die->parent->tag == DW_TAG_common_block)
21360 attr2 = NULL;
21361
caac4577
JG
21362 if (SYMBOL_CLASS (sym) == LOC_STATIC
21363 && SYMBOL_VALUE_ADDRESS (sym) == 0
21364 && !dwarf2_per_objfile->has_section_at_zero)
21365 {
21366 /* When a static variable is eliminated by the linker,
21367 the corresponding debug information is not stripped
21368 out, but the variable address is set to null;
21369 do not add such variables into symbol table. */
21370 }
21371 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 21372 {
f55ee35c
JK
21373 /* Workaround gfortran PR debug/40040 - it uses
21374 DW_AT_location for variables in -fPIC libraries which may
21375 get overriden by other libraries/executable and get
21376 a different address. Resolve it by the minimal symbol
21377 which may come from inferior's executable using copy
21378 relocation. Make this workaround only for gfortran as for
21379 other compilers GDB cannot guess the minimal symbol
21380 Fortran mangling kind. */
21381 if (cu->language == language_fortran && die->parent
21382 && die->parent->tag == DW_TAG_module
21383 && cu->producer
28586665 21384 && startswith (cu->producer, "GNU Fortran"))
f1e6e072 21385 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 21386
1c809c68
TT
21387 /* A variable with DW_AT_external is never static,
21388 but it may be block-scoped. */
804d2729
TT
21389 list_to_add
21390 = (cu->list_in_scope == cu->builder->get_file_symbols ()
21391 ? cu->builder->get_global_symbols ()
21392 : cu->list_in_scope);
1c809c68 21393 }
c906108c 21394 else
e37fd15a 21395 list_to_add = cu->list_in_scope;
c906108c
SS
21396 }
21397 else
21398 {
21399 /* We do not know the address of this symbol.
c5aa993b
JM
21400 If it is an external symbol and we have type information
21401 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21402 The address of the variable will then be determined from
21403 the minimal symbol table whenever the variable is
21404 referenced. */
e142c38c 21405 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
21406
21407 /* Fortran explicitly imports any global symbols to the local
21408 scope by DW_TAG_common_block. */
21409 if (cu->language == language_fortran && die->parent
21410 && die->parent->tag == DW_TAG_common_block)
21411 {
21412 /* SYMBOL_CLASS doesn't matter here because
21413 read_common_block is going to reset it. */
21414 if (!suppress_add)
21415 list_to_add = cu->list_in_scope;
21416 }
21417 else if (attr2 && (DW_UNSND (attr2) != 0)
21418 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 21419 {
0fe7935b
DJ
21420 /* A variable with DW_AT_external is never static, but it
21421 may be block-scoped. */
804d2729
TT
21422 list_to_add
21423 = (cu->list_in_scope == cu->builder->get_file_symbols ()
21424 ? cu->builder->get_global_symbols ()
21425 : cu->list_in_scope);
0fe7935b 21426
f1e6e072 21427 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 21428 }
442ddf59
JK
21429 else if (!die_is_declaration (die, cu))
21430 {
21431 /* Use the default LOC_OPTIMIZED_OUT class. */
21432 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
21433 if (!suppress_add)
21434 list_to_add = cu->list_in_scope;
442ddf59 21435 }
c906108c
SS
21436 }
21437 break;
21438 case DW_TAG_formal_parameter:
a60f3166
TT
21439 {
21440 /* If we are inside a function, mark this as an argument. If
21441 not, we might be looking at an argument to an inlined function
21442 when we do not have enough information to show inlined frames;
21443 pretend it's a local variable in that case so that the user can
21444 still see it. */
804d2729
TT
21445 struct context_stack *curr
21446 = cu->builder->get_current_context_stack ();
a60f3166
TT
21447 if (curr != nullptr && curr->name != nullptr)
21448 SYMBOL_IS_ARGUMENT (sym) = 1;
21449 attr = dwarf2_attr (die, DW_AT_location, cu);
21450 if (attr)
21451 {
21452 var_decode_location (attr, sym, cu);
21453 }
21454 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21455 if (attr)
21456 {
21457 dwarf2_const_value (attr, sym, cu);
21458 }
f346a30d 21459
a60f3166
TT
21460 list_to_add = cu->list_in_scope;
21461 }
c906108c
SS
21462 break;
21463 case DW_TAG_unspecified_parameters:
21464 /* From varargs functions; gdb doesn't seem to have any
21465 interest in this information, so just ignore it for now.
21466 (FIXME?) */
21467 break;
34eaf542
TT
21468 case DW_TAG_template_type_param:
21469 suppress_add = 1;
21470 /* Fall through. */
c906108c 21471 case DW_TAG_class_type:
680b30c7 21472 case DW_TAG_interface_type:
c906108c
SS
21473 case DW_TAG_structure_type:
21474 case DW_TAG_union_type:
72019c9c 21475 case DW_TAG_set_type:
c906108c 21476 case DW_TAG_enumeration_type:
f1e6e072 21477 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21478 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 21479
63d06c5c 21480 {
9c37b5ae 21481 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
21482 really ever be static objects: otherwise, if you try
21483 to, say, break of a class's method and you're in a file
21484 which doesn't mention that class, it won't work unless
21485 the check for all static symbols in lookup_symbol_aux
21486 saves you. See the OtherFileClass tests in
21487 gdb.c++/namespace.exp. */
21488
e37fd15a 21489 if (!suppress_add)
34eaf542 21490 {
804d2729
TT
21491 list_to_add
21492 = (cu->list_in_scope == cu->builder->get_file_symbols ()
21493 && cu->language == language_cplus
21494 ? cu->builder->get_global_symbols ()
21495 : cu->list_in_scope);
63d06c5c 21496
64382290 21497 /* The semantics of C++ state that "struct foo {
9c37b5ae 21498 ... }" also defines a typedef for "foo". */
64382290 21499 if (cu->language == language_cplus
45280282 21500 || cu->language == language_ada
c44af4eb
TT
21501 || cu->language == language_d
21502 || cu->language == language_rust)
64382290
TT
21503 {
21504 /* The symbol's name is already allocated along
21505 with this objfile, so we don't need to
21506 duplicate it for the type. */
21507 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21508 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21509 }
63d06c5c
DC
21510 }
21511 }
c906108c
SS
21512 break;
21513 case DW_TAG_typedef:
f1e6e072 21514 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 21515 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21516 list_to_add = cu->list_in_scope;
63d06c5c 21517 break;
c906108c 21518 case DW_TAG_base_type:
a02abb62 21519 case DW_TAG_subrange_type:
f1e6e072 21520 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21521 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21522 list_to_add = cu->list_in_scope;
c906108c
SS
21523 break;
21524 case DW_TAG_enumerator:
e142c38c 21525 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
21526 if (attr)
21527 {
e7c27a73 21528 dwarf2_const_value (attr, sym, cu);
c906108c 21529 }
63d06c5c
DC
21530 {
21531 /* NOTE: carlton/2003-11-10: See comment above in the
21532 DW_TAG_class_type, etc. block. */
21533
804d2729
TT
21534 list_to_add
21535 = (cu->list_in_scope == cu->builder->get_file_symbols ()
21536 && cu->language == language_cplus
21537 ? cu->builder->get_global_symbols ()
21538 : cu->list_in_scope);
63d06c5c 21539 }
c906108c 21540 break;
74921315 21541 case DW_TAG_imported_declaration:
5c4e30ca 21542 case DW_TAG_namespace:
f1e6e072 21543 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
804d2729 21544 list_to_add = cu->builder->get_global_symbols ();
5c4e30ca 21545 break;
530e8392
KB
21546 case DW_TAG_module:
21547 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21548 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
804d2729 21549 list_to_add = cu->builder->get_global_symbols ();
530e8392 21550 break;
4357ac6c 21551 case DW_TAG_common_block:
f1e6e072 21552 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 21553 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
380618d6 21554 dw2_add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 21555 break;
c906108c
SS
21556 default:
21557 /* Not a tag we recognize. Hopefully we aren't processing
21558 trash data, but since we must specifically ignore things
21559 we don't recognize, there is nothing else we should do at
0963b4bd 21560 this point. */
b98664d3 21561 complaint (_("unsupported tag: '%s'"),
4d3c2250 21562 dwarf_tag_name (die->tag));
c906108c
SS
21563 break;
21564 }
df8a16a1 21565
e37fd15a
SW
21566 if (suppress_add)
21567 {
21568 sym->hash_next = objfile->template_symbols;
21569 objfile->template_symbols = sym;
21570 list_to_add = NULL;
21571 }
21572
21573 if (list_to_add != NULL)
380618d6 21574 dw2_add_symbol_to_list (sym, list_to_add);
e37fd15a 21575
df8a16a1
DJ
21576 /* For the benefit of old versions of GCC, check for anonymous
21577 namespaces based on the demangled name. */
4d4ec4e5 21578 if (!cu->processing_has_namespace_info
94af9270 21579 && cu->language == language_cplus)
804d2729 21580 cp_scan_for_anonymous_namespaces (cu->builder.get (), sym, objfile);
c906108c
SS
21581 }
21582 return (sym);
21583}
21584
98bfdba5
PA
21585/* Given an attr with a DW_FORM_dataN value in host byte order,
21586 zero-extend it as appropriate for the symbol's type. The DWARF
21587 standard (v4) is not entirely clear about the meaning of using
21588 DW_FORM_dataN for a constant with a signed type, where the type is
21589 wider than the data. The conclusion of a discussion on the DWARF
21590 list was that this is unspecified. We choose to always zero-extend
21591 because that is the interpretation long in use by GCC. */
c906108c 21592
98bfdba5 21593static gdb_byte *
ff39bb5e 21594dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 21595 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 21596{
518817b3 21597 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e17a4113
UW
21598 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21599 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
21600 LONGEST l = DW_UNSND (attr);
21601
21602 if (bits < sizeof (*value) * 8)
21603 {
21604 l &= ((LONGEST) 1 << bits) - 1;
21605 *value = l;
21606 }
21607 else if (bits == sizeof (*value) * 8)
21608 *value = l;
21609 else
21610 {
224c3ddb 21611 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
21612 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21613 return bytes;
21614 }
21615
21616 return NULL;
21617}
21618
21619/* Read a constant value from an attribute. Either set *VALUE, or if
21620 the value does not fit in *VALUE, set *BYTES - either already
21621 allocated on the objfile obstack, or newly allocated on OBSTACK,
21622 or, set *BATON, if we translated the constant to a location
21623 expression. */
21624
21625static void
ff39bb5e 21626dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
21627 const char *name, struct obstack *obstack,
21628 struct dwarf2_cu *cu,
d521ce57 21629 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
21630 struct dwarf2_locexpr_baton **baton)
21631{
518817b3 21632 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
98bfdba5 21633 struct comp_unit_head *cu_header = &cu->header;
c906108c 21634 struct dwarf_block *blk;
98bfdba5
PA
21635 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21636 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21637
21638 *value = 0;
21639 *bytes = NULL;
21640 *baton = NULL;
c906108c
SS
21641
21642 switch (attr->form)
21643 {
21644 case DW_FORM_addr:
3019eac3 21645 case DW_FORM_GNU_addr_index:
ac56253d 21646 {
ac56253d
TT
21647 gdb_byte *data;
21648
98bfdba5
PA
21649 if (TYPE_LENGTH (type) != cu_header->addr_size)
21650 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 21651 cu_header->addr_size,
98bfdba5 21652 TYPE_LENGTH (type));
ac56253d
TT
21653 /* Symbols of this form are reasonably rare, so we just
21654 piggyback on the existing location code rather than writing
21655 a new implementation of symbol_computed_ops. */
8d749320 21656 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
21657 (*baton)->per_cu = cu->per_cu;
21658 gdb_assert ((*baton)->per_cu);
ac56253d 21659
98bfdba5 21660 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 21661 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 21662 (*baton)->data = data;
ac56253d
TT
21663
21664 data[0] = DW_OP_addr;
21665 store_unsigned_integer (&data[1], cu_header->addr_size,
21666 byte_order, DW_ADDR (attr));
21667 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 21668 }
c906108c 21669 break;
4ac36638 21670 case DW_FORM_string:
93b5768b 21671 case DW_FORM_strp:
3019eac3 21672 case DW_FORM_GNU_str_index:
36586728 21673 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
21674 /* DW_STRING is already allocated on the objfile obstack, point
21675 directly to it. */
d521ce57 21676 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 21677 break;
c906108c
SS
21678 case DW_FORM_block1:
21679 case DW_FORM_block2:
21680 case DW_FORM_block4:
21681 case DW_FORM_block:
2dc7f7b3 21682 case DW_FORM_exprloc:
0224619f 21683 case DW_FORM_data16:
c906108c 21684 blk = DW_BLOCK (attr);
98bfdba5
PA
21685 if (TYPE_LENGTH (type) != blk->size)
21686 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21687 TYPE_LENGTH (type));
21688 *bytes = blk->data;
c906108c 21689 break;
2df3850c
JM
21690
21691 /* The DW_AT_const_value attributes are supposed to carry the
21692 symbol's value "represented as it would be on the target
21693 architecture." By the time we get here, it's already been
21694 converted to host endianness, so we just need to sign- or
21695 zero-extend it as appropriate. */
21696 case DW_FORM_data1:
3aef2284 21697 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 21698 break;
c906108c 21699 case DW_FORM_data2:
3aef2284 21700 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 21701 break;
c906108c 21702 case DW_FORM_data4:
3aef2284 21703 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 21704 break;
c906108c 21705 case DW_FORM_data8:
3aef2284 21706 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
21707 break;
21708
c906108c 21709 case DW_FORM_sdata:
663c44ac 21710 case DW_FORM_implicit_const:
98bfdba5 21711 *value = DW_SND (attr);
2df3850c
JM
21712 break;
21713
c906108c 21714 case DW_FORM_udata:
98bfdba5 21715 *value = DW_UNSND (attr);
c906108c 21716 break;
2df3850c 21717
c906108c 21718 default:
b98664d3 21719 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 21720 dwarf_form_name (attr->form));
98bfdba5 21721 *value = 0;
c906108c
SS
21722 break;
21723 }
21724}
21725
2df3850c 21726
98bfdba5
PA
21727/* Copy constant value from an attribute to a symbol. */
21728
2df3850c 21729static void
ff39bb5e 21730dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 21731 struct dwarf2_cu *cu)
2df3850c 21732{
518817b3 21733 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12df843f 21734 LONGEST value;
d521ce57 21735 const gdb_byte *bytes;
98bfdba5 21736 struct dwarf2_locexpr_baton *baton;
2df3850c 21737
98bfdba5
PA
21738 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21739 SYMBOL_PRINT_NAME (sym),
21740 &objfile->objfile_obstack, cu,
21741 &value, &bytes, &baton);
2df3850c 21742
98bfdba5
PA
21743 if (baton != NULL)
21744 {
98bfdba5 21745 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 21746 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
21747 }
21748 else if (bytes != NULL)
21749 {
21750 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 21751 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
21752 }
21753 else
21754 {
21755 SYMBOL_VALUE (sym) = value;
f1e6e072 21756 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 21757 }
2df3850c
JM
21758}
21759
c906108c
SS
21760/* Return the type of the die in question using its DW_AT_type attribute. */
21761
21762static struct type *
e7c27a73 21763die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21764{
c906108c 21765 struct attribute *type_attr;
c906108c 21766
e142c38c 21767 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
21768 if (!type_attr)
21769 {
518817b3 21770 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 21771 /* A missing DW_AT_type represents a void type. */
518817b3 21772 return objfile_type (objfile)->builtin_void;
c906108c 21773 }
348e048f 21774
673bfd45 21775 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21776}
21777
b4ba55a1
JB
21778/* True iff CU's producer generates GNAT Ada auxiliary information
21779 that allows to find parallel types through that information instead
21780 of having to do expensive parallel lookups by type name. */
21781
21782static int
21783need_gnat_info (struct dwarf2_cu *cu)
21784{
de4cb04a
JB
21785 /* Assume that the Ada compiler was GNAT, which always produces
21786 the auxiliary information. */
21787 return (cu->language == language_ada);
b4ba55a1
JB
21788}
21789
b4ba55a1
JB
21790/* Return the auxiliary type of the die in question using its
21791 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21792 attribute is not present. */
21793
21794static struct type *
21795die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21796{
b4ba55a1 21797 struct attribute *type_attr;
b4ba55a1
JB
21798
21799 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21800 if (!type_attr)
21801 return NULL;
21802
673bfd45 21803 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
21804}
21805
21806/* If DIE has a descriptive_type attribute, then set the TYPE's
21807 descriptive type accordingly. */
21808
21809static void
21810set_descriptive_type (struct type *type, struct die_info *die,
21811 struct dwarf2_cu *cu)
21812{
21813 struct type *descriptive_type = die_descriptive_type (die, cu);
21814
21815 if (descriptive_type)
21816 {
21817 ALLOCATE_GNAT_AUX_TYPE (type);
21818 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21819 }
21820}
21821
c906108c
SS
21822/* Return the containing type of the die in question using its
21823 DW_AT_containing_type attribute. */
21824
21825static struct type *
e7c27a73 21826die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21827{
c906108c 21828 struct attribute *type_attr;
518817b3 21829 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 21830
e142c38c 21831 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
21832 if (!type_attr)
21833 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 21834 "[in module %s]"), objfile_name (objfile));
33ac96f0 21835
673bfd45 21836 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21837}
21838
ac9ec31b
DE
21839/* Return an error marker type to use for the ill formed type in DIE/CU. */
21840
21841static struct type *
21842build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21843{
518817b3
SM
21844 struct dwarf2_per_objfile *dwarf2_per_objfile
21845 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
21846 struct objfile *objfile = dwarf2_per_objfile->objfile;
21847 char *message, *saved;
21848
9d8780f0 21849 message = xstrprintf (_("<unknown type in %s, CU %s, DIE %s>"),
4262abfb 21850 objfile_name (objfile),
9d8780f0
SM
21851 sect_offset_str (cu->header.sect_off),
21852 sect_offset_str (die->sect_off));
224c3ddb
SM
21853 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
21854 message, strlen (message));
ac9ec31b
DE
21855 xfree (message);
21856
19f392bc 21857 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
21858}
21859
673bfd45 21860/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
21861 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21862 DW_AT_containing_type.
673bfd45
DE
21863 If there is no type substitute an error marker. */
21864
c906108c 21865static struct type *
ff39bb5e 21866lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 21867 struct dwarf2_cu *cu)
c906108c 21868{
518817b3
SM
21869 struct dwarf2_per_objfile *dwarf2_per_objfile
21870 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21871 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
21872 struct type *this_type;
21873
ac9ec31b
DE
21874 gdb_assert (attr->name == DW_AT_type
21875 || attr->name == DW_AT_GNAT_descriptive_type
21876 || attr->name == DW_AT_containing_type);
21877
673bfd45
DE
21878 /* First see if we have it cached. */
21879
36586728
TT
21880 if (attr->form == DW_FORM_GNU_ref_alt)
21881 {
21882 struct dwarf2_per_cu_data *per_cu;
9c541725 21883 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 21884
ed2dc618
SM
21885 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21886 dwarf2_per_objfile);
9c541725 21887 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 21888 }
7771576e 21889 else if (attr_form_is_ref (attr))
673bfd45 21890 {
9c541725 21891 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 21892
9c541725 21893 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 21894 }
55f1336d 21895 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 21896 {
ac9ec31b 21897 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 21898
ac9ec31b 21899 return get_signatured_type (die, signature, cu);
673bfd45
DE
21900 }
21901 else
21902 {
b98664d3 21903 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
21904 " at %s [in module %s]"),
21905 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 21906 objfile_name (objfile));
ac9ec31b 21907 return build_error_marker_type (cu, die);
673bfd45
DE
21908 }
21909
21910 /* If not cached we need to read it in. */
21911
21912 if (this_type == NULL)
21913 {
ac9ec31b 21914 struct die_info *type_die = NULL;
673bfd45
DE
21915 struct dwarf2_cu *type_cu = cu;
21916
7771576e 21917 if (attr_form_is_ref (attr))
ac9ec31b
DE
21918 type_die = follow_die_ref (die, attr, &type_cu);
21919 if (type_die == NULL)
21920 return build_error_marker_type (cu, die);
21921 /* If we find the type now, it's probably because the type came
3019eac3
DE
21922 from an inter-CU reference and the type's CU got expanded before
21923 ours. */
ac9ec31b 21924 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
21925 }
21926
21927 /* If we still don't have a type use an error marker. */
21928
21929 if (this_type == NULL)
ac9ec31b 21930 return build_error_marker_type (cu, die);
673bfd45 21931
f792889a 21932 return this_type;
c906108c
SS
21933}
21934
673bfd45
DE
21935/* Return the type in DIE, CU.
21936 Returns NULL for invalid types.
21937
02142a6c 21938 This first does a lookup in die_type_hash,
673bfd45
DE
21939 and only reads the die in if necessary.
21940
21941 NOTE: This can be called when reading in partial or full symbols. */
21942
f792889a 21943static struct type *
e7c27a73 21944read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21945{
f792889a
DJ
21946 struct type *this_type;
21947
21948 this_type = get_die_type (die, cu);
21949 if (this_type)
21950 return this_type;
21951
673bfd45
DE
21952 return read_type_die_1 (die, cu);
21953}
21954
21955/* Read the type in DIE, CU.
21956 Returns NULL for invalid types. */
21957
21958static struct type *
21959read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21960{
21961 struct type *this_type = NULL;
21962
c906108c
SS
21963 switch (die->tag)
21964 {
21965 case DW_TAG_class_type:
680b30c7 21966 case DW_TAG_interface_type:
c906108c
SS
21967 case DW_TAG_structure_type:
21968 case DW_TAG_union_type:
f792889a 21969 this_type = read_structure_type (die, cu);
c906108c
SS
21970 break;
21971 case DW_TAG_enumeration_type:
f792889a 21972 this_type = read_enumeration_type (die, cu);
c906108c
SS
21973 break;
21974 case DW_TAG_subprogram:
21975 case DW_TAG_subroutine_type:
edb3359d 21976 case DW_TAG_inlined_subroutine:
f792889a 21977 this_type = read_subroutine_type (die, cu);
c906108c
SS
21978 break;
21979 case DW_TAG_array_type:
f792889a 21980 this_type = read_array_type (die, cu);
c906108c 21981 break;
72019c9c 21982 case DW_TAG_set_type:
f792889a 21983 this_type = read_set_type (die, cu);
72019c9c 21984 break;
c906108c 21985 case DW_TAG_pointer_type:
f792889a 21986 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
21987 break;
21988 case DW_TAG_ptr_to_member_type:
f792889a 21989 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
21990 break;
21991 case DW_TAG_reference_type:
4297a3f0
AV
21992 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21993 break;
21994 case DW_TAG_rvalue_reference_type:
21995 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
21996 break;
21997 case DW_TAG_const_type:
f792889a 21998 this_type = read_tag_const_type (die, cu);
c906108c
SS
21999 break;
22000 case DW_TAG_volatile_type:
f792889a 22001 this_type = read_tag_volatile_type (die, cu);
c906108c 22002 break;
06d66ee9
TT
22003 case DW_TAG_restrict_type:
22004 this_type = read_tag_restrict_type (die, cu);
22005 break;
c906108c 22006 case DW_TAG_string_type:
f792889a 22007 this_type = read_tag_string_type (die, cu);
c906108c
SS
22008 break;
22009 case DW_TAG_typedef:
f792889a 22010 this_type = read_typedef (die, cu);
c906108c 22011 break;
a02abb62 22012 case DW_TAG_subrange_type:
f792889a 22013 this_type = read_subrange_type (die, cu);
a02abb62 22014 break;
c906108c 22015 case DW_TAG_base_type:
f792889a 22016 this_type = read_base_type (die, cu);
c906108c 22017 break;
81a17f79 22018 case DW_TAG_unspecified_type:
f792889a 22019 this_type = read_unspecified_type (die, cu);
81a17f79 22020 break;
0114d602
DJ
22021 case DW_TAG_namespace:
22022 this_type = read_namespace_type (die, cu);
22023 break;
f55ee35c
JK
22024 case DW_TAG_module:
22025 this_type = read_module_type (die, cu);
22026 break;
a2c2acaf
MW
22027 case DW_TAG_atomic_type:
22028 this_type = read_tag_atomic_type (die, cu);
22029 break;
c906108c 22030 default:
b98664d3 22031 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 22032 dwarf_tag_name (die->tag));
c906108c
SS
22033 break;
22034 }
63d06c5c 22035
f792889a 22036 return this_type;
63d06c5c
DC
22037}
22038
abc72ce4
DE
22039/* See if we can figure out if the class lives in a namespace. We do
22040 this by looking for a member function; its demangled name will
22041 contain namespace info, if there is any.
22042 Return the computed name or NULL.
22043 Space for the result is allocated on the objfile's obstack.
22044 This is the full-die version of guess_partial_die_structure_name.
22045 In this case we know DIE has no useful parent. */
22046
22047static char *
22048guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22049{
22050 struct die_info *spec_die;
22051 struct dwarf2_cu *spec_cu;
22052 struct die_info *child;
518817b3 22053 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4
DE
22054
22055 spec_cu = cu;
22056 spec_die = die_specification (die, &spec_cu);
22057 if (spec_die != NULL)
22058 {
22059 die = spec_die;
22060 cu = spec_cu;
22061 }
22062
22063 for (child = die->child;
22064 child != NULL;
22065 child = child->sibling)
22066 {
22067 if (child->tag == DW_TAG_subprogram)
22068 {
73b9be8b 22069 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 22070
7d45c7c3 22071 if (linkage_name != NULL)
abc72ce4
DE
22072 {
22073 char *actual_name
22074 = language_class_name_from_physname (cu->language_defn,
7d45c7c3 22075 linkage_name);
abc72ce4
DE
22076 char *name = NULL;
22077
22078 if (actual_name != NULL)
22079 {
15d034d0 22080 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
22081
22082 if (die_name != NULL
22083 && strcmp (die_name, actual_name) != 0)
22084 {
22085 /* Strip off the class name from the full name.
22086 We want the prefix. */
22087 int die_name_len = strlen (die_name);
22088 int actual_name_len = strlen (actual_name);
22089
22090 /* Test for '::' as a sanity check. */
22091 if (actual_name_len > die_name_len + 2
3e43a32a
MS
22092 && actual_name[actual_name_len
22093 - die_name_len - 1] == ':')
224c3ddb 22094 name = (char *) obstack_copy0 (
e3b94546 22095 &objfile->per_bfd->storage_obstack,
224c3ddb 22096 actual_name, actual_name_len - die_name_len - 2);
abc72ce4
DE
22097 }
22098 }
22099 xfree (actual_name);
22100 return name;
22101 }
22102 }
22103 }
22104
22105 return NULL;
22106}
22107
96408a79
SA
22108/* GCC might emit a nameless typedef that has a linkage name. Determine the
22109 prefix part in such case. See
22110 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22111
a121b7c1 22112static const char *
96408a79
SA
22113anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22114{
22115 struct attribute *attr;
e6a959d6 22116 const char *base;
96408a79
SA
22117
22118 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22119 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22120 return NULL;
22121
7d45c7c3 22122 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
22123 return NULL;
22124
73b9be8b 22125 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
22126 if (attr == NULL || DW_STRING (attr) == NULL)
22127 return NULL;
22128
22129 /* dwarf2_name had to be already called. */
22130 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22131
22132 /* Strip the base name, keep any leading namespaces/classes. */
22133 base = strrchr (DW_STRING (attr), ':');
22134 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22135 return "";
22136
518817b3 22137 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e3b94546 22138 return (char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb
SM
22139 DW_STRING (attr),
22140 &base[-1] - DW_STRING (attr));
96408a79
SA
22141}
22142
fdde2d81 22143/* Return the name of the namespace/class that DIE is defined within,
0114d602 22144 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 22145
0114d602
DJ
22146 For example, if we're within the method foo() in the following
22147 code:
22148
22149 namespace N {
22150 class C {
22151 void foo () {
22152 }
22153 };
22154 }
22155
22156 then determine_prefix on foo's die will return "N::C". */
fdde2d81 22157
0d5cff50 22158static const char *
e142c38c 22159determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 22160{
518817b3
SM
22161 struct dwarf2_per_objfile *dwarf2_per_objfile
22162 = cu->per_cu->dwarf2_per_objfile;
0114d602
DJ
22163 struct die_info *parent, *spec_die;
22164 struct dwarf2_cu *spec_cu;
22165 struct type *parent_type;
a121b7c1 22166 const char *retval;
63d06c5c 22167
9c37b5ae 22168 if (cu->language != language_cplus
c44af4eb
TT
22169 && cu->language != language_fortran && cu->language != language_d
22170 && cu->language != language_rust)
0114d602
DJ
22171 return "";
22172
96408a79
SA
22173 retval = anonymous_struct_prefix (die, cu);
22174 if (retval)
22175 return retval;
22176
0114d602
DJ
22177 /* We have to be careful in the presence of DW_AT_specification.
22178 For example, with GCC 3.4, given the code
22179
22180 namespace N {
22181 void foo() {
22182 // Definition of N::foo.
22183 }
22184 }
22185
22186 then we'll have a tree of DIEs like this:
22187
22188 1: DW_TAG_compile_unit
22189 2: DW_TAG_namespace // N
22190 3: DW_TAG_subprogram // declaration of N::foo
22191 4: DW_TAG_subprogram // definition of N::foo
22192 DW_AT_specification // refers to die #3
22193
22194 Thus, when processing die #4, we have to pretend that we're in
22195 the context of its DW_AT_specification, namely the contex of die
22196 #3. */
22197 spec_cu = cu;
22198 spec_die = die_specification (die, &spec_cu);
22199 if (spec_die == NULL)
22200 parent = die->parent;
22201 else
63d06c5c 22202 {
0114d602
DJ
22203 parent = spec_die->parent;
22204 cu = spec_cu;
63d06c5c 22205 }
0114d602
DJ
22206
22207 if (parent == NULL)
22208 return "";
98bfdba5
PA
22209 else if (parent->building_fullname)
22210 {
22211 const char *name;
22212 const char *parent_name;
22213
22214 /* It has been seen on RealView 2.2 built binaries,
22215 DW_TAG_template_type_param types actually _defined_ as
22216 children of the parent class:
22217
22218 enum E {};
22219 template class <class Enum> Class{};
22220 Class<enum E> class_e;
22221
22222 1: DW_TAG_class_type (Class)
22223 2: DW_TAG_enumeration_type (E)
22224 3: DW_TAG_enumerator (enum1:0)
22225 3: DW_TAG_enumerator (enum2:1)
22226 ...
22227 2: DW_TAG_template_type_param
22228 DW_AT_type DW_FORM_ref_udata (E)
22229
22230 Besides being broken debug info, it can put GDB into an
22231 infinite loop. Consider:
22232
22233 When we're building the full name for Class<E>, we'll start
22234 at Class, and go look over its template type parameters,
22235 finding E. We'll then try to build the full name of E, and
22236 reach here. We're now trying to build the full name of E,
22237 and look over the parent DIE for containing scope. In the
22238 broken case, if we followed the parent DIE of E, we'd again
22239 find Class, and once again go look at its template type
22240 arguments, etc., etc. Simply don't consider such parent die
22241 as source-level parent of this die (it can't be, the language
22242 doesn't allow it), and break the loop here. */
22243 name = dwarf2_name (die, cu);
22244 parent_name = dwarf2_name (parent, cu);
b98664d3 22245 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
22246 name ? name : "<unknown>",
22247 parent_name ? parent_name : "<unknown>");
22248 return "";
22249 }
63d06c5c 22250 else
0114d602
DJ
22251 switch (parent->tag)
22252 {
63d06c5c 22253 case DW_TAG_namespace:
0114d602 22254 parent_type = read_type_die (parent, cu);
acebe513
UW
22255 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22256 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22257 Work around this problem here. */
22258 if (cu->language == language_cplus
e86ca25f 22259 && strcmp (TYPE_NAME (parent_type), "::") == 0)
acebe513 22260 return "";
0114d602 22261 /* We give a name to even anonymous namespaces. */
e86ca25f 22262 return TYPE_NAME (parent_type);
63d06c5c 22263 case DW_TAG_class_type:
680b30c7 22264 case DW_TAG_interface_type:
63d06c5c 22265 case DW_TAG_structure_type:
0114d602 22266 case DW_TAG_union_type:
f55ee35c 22267 case DW_TAG_module:
0114d602 22268 parent_type = read_type_die (parent, cu);
e86ca25f
TT
22269 if (TYPE_NAME (parent_type) != NULL)
22270 return TYPE_NAME (parent_type);
0114d602
DJ
22271 else
22272 /* An anonymous structure is only allowed non-static data
22273 members; no typedefs, no member functions, et cetera.
22274 So it does not need a prefix. */
22275 return "";
abc72ce4 22276 case DW_TAG_compile_unit:
95554aad 22277 case DW_TAG_partial_unit:
abc72ce4
DE
22278 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22279 if (cu->language == language_cplus
8b70b953 22280 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
22281 && die->child != NULL
22282 && (die->tag == DW_TAG_class_type
22283 || die->tag == DW_TAG_structure_type
22284 || die->tag == DW_TAG_union_type))
22285 {
22286 char *name = guess_full_die_structure_name (die, cu);
22287 if (name != NULL)
22288 return name;
22289 }
22290 return "";
3d567982
TT
22291 case DW_TAG_enumeration_type:
22292 parent_type = read_type_die (parent, cu);
22293 if (TYPE_DECLARED_CLASS (parent_type))
22294 {
e86ca25f
TT
22295 if (TYPE_NAME (parent_type) != NULL)
22296 return TYPE_NAME (parent_type);
3d567982
TT
22297 return "";
22298 }
22299 /* Fall through. */
63d06c5c 22300 default:
8176b9b8 22301 return determine_prefix (parent, cu);
63d06c5c 22302 }
63d06c5c
DC
22303}
22304
3e43a32a
MS
22305/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22306 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22307 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22308 an obconcat, otherwise allocate storage for the result. The CU argument is
22309 used to determine the language and hence, the appropriate separator. */
987504bb 22310
f55ee35c 22311#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
22312
22313static char *
f55ee35c
JK
22314typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22315 int physname, struct dwarf2_cu *cu)
63d06c5c 22316{
f55ee35c 22317 const char *lead = "";
5c315b68 22318 const char *sep;
63d06c5c 22319
3e43a32a
MS
22320 if (suffix == NULL || suffix[0] == '\0'
22321 || prefix == NULL || prefix[0] == '\0')
987504bb 22322 sep = "";
45280282
IB
22323 else if (cu->language == language_d)
22324 {
22325 /* For D, the 'main' function could be defined in any module, but it
22326 should never be prefixed. */
22327 if (strcmp (suffix, "D main") == 0)
22328 {
22329 prefix = "";
22330 sep = "";
22331 }
22332 else
22333 sep = ".";
22334 }
f55ee35c
JK
22335 else if (cu->language == language_fortran && physname)
22336 {
22337 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22338 DW_AT_MIPS_linkage_name is preferred and used instead. */
22339
22340 lead = "__";
22341 sep = "_MOD_";
22342 }
987504bb
JJ
22343 else
22344 sep = "::";
63d06c5c 22345
6dd47d34
DE
22346 if (prefix == NULL)
22347 prefix = "";
22348 if (suffix == NULL)
22349 suffix = "";
22350
987504bb
JJ
22351 if (obs == NULL)
22352 {
3e43a32a 22353 char *retval
224c3ddb
SM
22354 = ((char *)
22355 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 22356
f55ee35c
JK
22357 strcpy (retval, lead);
22358 strcat (retval, prefix);
6dd47d34
DE
22359 strcat (retval, sep);
22360 strcat (retval, suffix);
63d06c5c
DC
22361 return retval;
22362 }
987504bb
JJ
22363 else
22364 {
22365 /* We have an obstack. */
f55ee35c 22366 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 22367 }
63d06c5c
DC
22368}
22369
c906108c
SS
22370/* Return sibling of die, NULL if no sibling. */
22371
f9aca02d 22372static struct die_info *
fba45db2 22373sibling_die (struct die_info *die)
c906108c 22374{
639d11d3 22375 return die->sibling;
c906108c
SS
22376}
22377
71c25dea
TT
22378/* Get name of a die, return NULL if not found. */
22379
15d034d0
TT
22380static const char *
22381dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
22382 struct obstack *obstack)
22383{
22384 if (name && cu->language == language_cplus)
22385 {
2f408ecb 22386 std::string canon_name = cp_canonicalize_string (name);
71c25dea 22387
2f408ecb 22388 if (!canon_name.empty ())
71c25dea 22389 {
2f408ecb
PA
22390 if (canon_name != name)
22391 name = (const char *) obstack_copy0 (obstack,
22392 canon_name.c_str (),
22393 canon_name.length ());
71c25dea
TT
22394 }
22395 }
22396
22397 return name;
c906108c
SS
22398}
22399
96553a0c
DE
22400/* Get name of a die, return NULL if not found.
22401 Anonymous namespaces are converted to their magic string. */
9219021c 22402
15d034d0 22403static const char *
e142c38c 22404dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
22405{
22406 struct attribute *attr;
518817b3 22407 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9219021c 22408
e142c38c 22409 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 22410 if ((!attr || !DW_STRING (attr))
96553a0c 22411 && die->tag != DW_TAG_namespace
53832f31
TT
22412 && die->tag != DW_TAG_class_type
22413 && die->tag != DW_TAG_interface_type
22414 && die->tag != DW_TAG_structure_type
22415 && die->tag != DW_TAG_union_type)
71c25dea
TT
22416 return NULL;
22417
22418 switch (die->tag)
22419 {
22420 case DW_TAG_compile_unit:
95554aad 22421 case DW_TAG_partial_unit:
71c25dea
TT
22422 /* Compilation units have a DW_AT_name that is a filename, not
22423 a source language identifier. */
22424 case DW_TAG_enumeration_type:
22425 case DW_TAG_enumerator:
22426 /* These tags always have simple identifiers already; no need
22427 to canonicalize them. */
22428 return DW_STRING (attr);
907af001 22429
96553a0c
DE
22430 case DW_TAG_namespace:
22431 if (attr != NULL && DW_STRING (attr) != NULL)
22432 return DW_STRING (attr);
22433 return CP_ANONYMOUS_NAMESPACE_STR;
22434
907af001
UW
22435 case DW_TAG_class_type:
22436 case DW_TAG_interface_type:
22437 case DW_TAG_structure_type:
22438 case DW_TAG_union_type:
22439 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22440 structures or unions. These were of the form "._%d" in GCC 4.1,
22441 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22442 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 22443 if (attr && DW_STRING (attr)
61012eef
GB
22444 && (startswith (DW_STRING (attr), "._")
22445 || startswith (DW_STRING (attr), "<anonymous")))
907af001 22446 return NULL;
53832f31
TT
22447
22448 /* GCC might emit a nameless typedef that has a linkage name. See
22449 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22450 if (!attr || DW_STRING (attr) == NULL)
22451 {
df5c6c50 22452 char *demangled = NULL;
53832f31 22453
73b9be8b 22454 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
22455 if (attr == NULL || DW_STRING (attr) == NULL)
22456 return NULL;
22457
df5c6c50
JK
22458 /* Avoid demangling DW_STRING (attr) the second time on a second
22459 call for the same DIE. */
22460 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 22461 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
22462
22463 if (demangled)
22464 {
e6a959d6 22465 const char *base;
96408a79 22466
53832f31 22467 /* FIXME: we already did this for the partial symbol... */
34a68019 22468 DW_STRING (attr)
224c3ddb 22469 = ((const char *)
e3b94546 22470 obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb 22471 demangled, strlen (demangled)));
53832f31
TT
22472 DW_STRING_IS_CANONICAL (attr) = 1;
22473 xfree (demangled);
96408a79
SA
22474
22475 /* Strip any leading namespaces/classes, keep only the base name.
22476 DW_AT_name for named DIEs does not contain the prefixes. */
22477 base = strrchr (DW_STRING (attr), ':');
22478 if (base && base > DW_STRING (attr) && base[-1] == ':')
22479 return &base[1];
22480 else
22481 return DW_STRING (attr);
53832f31
TT
22482 }
22483 }
907af001
UW
22484 break;
22485
71c25dea 22486 default:
907af001
UW
22487 break;
22488 }
22489
22490 if (!DW_STRING_IS_CANONICAL (attr))
22491 {
22492 DW_STRING (attr)
22493 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
e3b94546 22494 &objfile->per_bfd->storage_obstack);
907af001 22495 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 22496 }
907af001 22497 return DW_STRING (attr);
9219021c
DC
22498}
22499
22500/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
22501 is none. *EXT_CU is the CU containing DIE on input, and the CU
22502 containing the return value on output. */
9219021c
DC
22503
22504static struct die_info *
f2f0e013 22505dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
22506{
22507 struct attribute *attr;
9219021c 22508
f2f0e013 22509 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
22510 if (attr == NULL)
22511 return NULL;
22512
f2f0e013 22513 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
22514}
22515
c906108c
SS
22516/* Convert a DIE tag into its string name. */
22517
f39c6ffd 22518static const char *
aa1ee363 22519dwarf_tag_name (unsigned tag)
c906108c 22520{
f39c6ffd
TT
22521 const char *name = get_DW_TAG_name (tag);
22522
22523 if (name == NULL)
22524 return "DW_TAG_<unknown>";
22525
22526 return name;
c906108c
SS
22527}
22528
22529/* Convert a DWARF attribute code into its string name. */
22530
f39c6ffd 22531static const char *
aa1ee363 22532dwarf_attr_name (unsigned attr)
c906108c 22533{
f39c6ffd
TT
22534 const char *name;
22535
c764a876 22536#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
22537 if (attr == DW_AT_MIPS_fde)
22538 return "DW_AT_MIPS_fde";
22539#else
22540 if (attr == DW_AT_HP_block_index)
22541 return "DW_AT_HP_block_index";
c764a876 22542#endif
f39c6ffd
TT
22543
22544 name = get_DW_AT_name (attr);
22545
22546 if (name == NULL)
22547 return "DW_AT_<unknown>";
22548
22549 return name;
c906108c
SS
22550}
22551
22552/* Convert a DWARF value form code into its string name. */
22553
f39c6ffd 22554static const char *
aa1ee363 22555dwarf_form_name (unsigned form)
c906108c 22556{
f39c6ffd
TT
22557 const char *name = get_DW_FORM_name (form);
22558
22559 if (name == NULL)
22560 return "DW_FORM_<unknown>";
22561
22562 return name;
c906108c
SS
22563}
22564
a121b7c1 22565static const char *
fba45db2 22566dwarf_bool_name (unsigned mybool)
c906108c
SS
22567{
22568 if (mybool)
22569 return "TRUE";
22570 else
22571 return "FALSE";
22572}
22573
22574/* Convert a DWARF type code into its string name. */
22575
f39c6ffd 22576static const char *
aa1ee363 22577dwarf_type_encoding_name (unsigned enc)
c906108c 22578{
f39c6ffd 22579 const char *name = get_DW_ATE_name (enc);
c906108c 22580
f39c6ffd
TT
22581 if (name == NULL)
22582 return "DW_ATE_<unknown>";
c906108c 22583
f39c6ffd 22584 return name;
c906108c 22585}
c906108c 22586
f9aca02d 22587static void
d97bc12b 22588dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
22589{
22590 unsigned int i;
22591
d97bc12b 22592 print_spaces (indent, f);
9d8780f0 22593 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 22594 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 22595 sect_offset_str (die->sect_off));
d97bc12b
DE
22596
22597 if (die->parent != NULL)
22598 {
22599 print_spaces (indent, f);
9d8780f0
SM
22600 fprintf_unfiltered (f, " parent at offset: %s\n",
22601 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
22602 }
22603
22604 print_spaces (indent, f);
22605 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 22606 dwarf_bool_name (die->child != NULL));
c906108c 22607
d97bc12b
DE
22608 print_spaces (indent, f);
22609 fprintf_unfiltered (f, " attributes:\n");
22610
c906108c
SS
22611 for (i = 0; i < die->num_attrs; ++i)
22612 {
d97bc12b
DE
22613 print_spaces (indent, f);
22614 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
22615 dwarf_attr_name (die->attrs[i].name),
22616 dwarf_form_name (die->attrs[i].form));
d97bc12b 22617
c906108c
SS
22618 switch (die->attrs[i].form)
22619 {
c906108c 22620 case DW_FORM_addr:
3019eac3 22621 case DW_FORM_GNU_addr_index:
d97bc12b 22622 fprintf_unfiltered (f, "address: ");
5af949e3 22623 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
22624 break;
22625 case DW_FORM_block2:
22626 case DW_FORM_block4:
22627 case DW_FORM_block:
22628 case DW_FORM_block1:
56eb65bd
SP
22629 fprintf_unfiltered (f, "block: size %s",
22630 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 22631 break;
2dc7f7b3 22632 case DW_FORM_exprloc:
56eb65bd
SP
22633 fprintf_unfiltered (f, "expression: size %s",
22634 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 22635 break;
0224619f
JK
22636 case DW_FORM_data16:
22637 fprintf_unfiltered (f, "constant of 16 bytes");
22638 break;
4568ecf9
DE
22639 case DW_FORM_ref_addr:
22640 fprintf_unfiltered (f, "ref address: ");
22641 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22642 break;
36586728
TT
22643 case DW_FORM_GNU_ref_alt:
22644 fprintf_unfiltered (f, "alt ref address: ");
22645 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22646 break;
10b3939b
DJ
22647 case DW_FORM_ref1:
22648 case DW_FORM_ref2:
22649 case DW_FORM_ref4:
4568ecf9
DE
22650 case DW_FORM_ref8:
22651 case DW_FORM_ref_udata:
d97bc12b 22652 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 22653 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 22654 break;
c906108c
SS
22655 case DW_FORM_data1:
22656 case DW_FORM_data2:
22657 case DW_FORM_data4:
ce5d95e1 22658 case DW_FORM_data8:
c906108c
SS
22659 case DW_FORM_udata:
22660 case DW_FORM_sdata:
43bbcdc2
PH
22661 fprintf_unfiltered (f, "constant: %s",
22662 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 22663 break;
2dc7f7b3
TT
22664 case DW_FORM_sec_offset:
22665 fprintf_unfiltered (f, "section offset: %s",
22666 pulongest (DW_UNSND (&die->attrs[i])));
22667 break;
55f1336d 22668 case DW_FORM_ref_sig8:
ac9ec31b
DE
22669 fprintf_unfiltered (f, "signature: %s",
22670 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 22671 break;
c906108c 22672 case DW_FORM_string:
4bdf3d34 22673 case DW_FORM_strp:
43988095 22674 case DW_FORM_line_strp:
3019eac3 22675 case DW_FORM_GNU_str_index:
36586728 22676 case DW_FORM_GNU_strp_alt:
8285870a 22677 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 22678 DW_STRING (&die->attrs[i])
8285870a
JK
22679 ? DW_STRING (&die->attrs[i]) : "",
22680 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
22681 break;
22682 case DW_FORM_flag:
22683 if (DW_UNSND (&die->attrs[i]))
d97bc12b 22684 fprintf_unfiltered (f, "flag: TRUE");
c906108c 22685 else
d97bc12b 22686 fprintf_unfiltered (f, "flag: FALSE");
c906108c 22687 break;
2dc7f7b3
TT
22688 case DW_FORM_flag_present:
22689 fprintf_unfiltered (f, "flag: TRUE");
22690 break;
a8329558 22691 case DW_FORM_indirect:
0963b4bd
MS
22692 /* The reader will have reduced the indirect form to
22693 the "base form" so this form should not occur. */
3e43a32a
MS
22694 fprintf_unfiltered (f,
22695 "unexpected attribute form: DW_FORM_indirect");
a8329558 22696 break;
663c44ac
JK
22697 case DW_FORM_implicit_const:
22698 fprintf_unfiltered (f, "constant: %s",
22699 plongest (DW_SND (&die->attrs[i])));
22700 break;
c906108c 22701 default:
d97bc12b 22702 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 22703 die->attrs[i].form);
d97bc12b 22704 break;
c906108c 22705 }
d97bc12b 22706 fprintf_unfiltered (f, "\n");
c906108c
SS
22707 }
22708}
22709
f9aca02d 22710static void
d97bc12b 22711dump_die_for_error (struct die_info *die)
c906108c 22712{
d97bc12b
DE
22713 dump_die_shallow (gdb_stderr, 0, die);
22714}
22715
22716static void
22717dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22718{
22719 int indent = level * 4;
22720
22721 gdb_assert (die != NULL);
22722
22723 if (level >= max_level)
22724 return;
22725
22726 dump_die_shallow (f, indent, die);
22727
22728 if (die->child != NULL)
c906108c 22729 {
d97bc12b
DE
22730 print_spaces (indent, f);
22731 fprintf_unfiltered (f, " Children:");
22732 if (level + 1 < max_level)
22733 {
22734 fprintf_unfiltered (f, "\n");
22735 dump_die_1 (f, level + 1, max_level, die->child);
22736 }
22737 else
22738 {
3e43a32a
MS
22739 fprintf_unfiltered (f,
22740 " [not printed, max nesting level reached]\n");
d97bc12b
DE
22741 }
22742 }
22743
22744 if (die->sibling != NULL && level > 0)
22745 {
22746 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
22747 }
22748}
22749
d97bc12b
DE
22750/* This is called from the pdie macro in gdbinit.in.
22751 It's not static so gcc will keep a copy callable from gdb. */
22752
22753void
22754dump_die (struct die_info *die, int max_level)
22755{
22756 dump_die_1 (gdb_stdlog, 0, max_level, die);
22757}
22758
f9aca02d 22759static void
51545339 22760store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22761{
51545339 22762 void **slot;
c906108c 22763
9c541725
PA
22764 slot = htab_find_slot_with_hash (cu->die_hash, die,
22765 to_underlying (die->sect_off),
b64f50a1 22766 INSERT);
51545339
DJ
22767
22768 *slot = die;
c906108c
SS
22769}
22770
b64f50a1
JK
22771/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22772 required kind. */
22773
22774static sect_offset
ff39bb5e 22775dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 22776{
7771576e 22777 if (attr_form_is_ref (attr))
9c541725 22778 return (sect_offset) DW_UNSND (attr);
93311388 22779
b98664d3 22780 complaint (_("unsupported die ref attribute form: '%s'"),
93311388 22781 dwarf_form_name (attr->form));
9c541725 22782 return {};
c906108c
SS
22783}
22784
43bbcdc2
PH
22785/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22786 * the value held by the attribute is not constant. */
a02abb62 22787
43bbcdc2 22788static LONGEST
ff39bb5e 22789dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 22790{
663c44ac 22791 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
22792 return DW_SND (attr);
22793 else if (attr->form == DW_FORM_udata
22794 || attr->form == DW_FORM_data1
22795 || attr->form == DW_FORM_data2
22796 || attr->form == DW_FORM_data4
22797 || attr->form == DW_FORM_data8)
22798 return DW_UNSND (attr);
22799 else
22800 {
0224619f 22801 /* For DW_FORM_data16 see attr_form_is_constant. */
b98664d3 22802 complaint (_("Attribute value is not a constant (%s)"),
a02abb62
JB
22803 dwarf_form_name (attr->form));
22804 return default_value;
22805 }
22806}
22807
348e048f
DE
22808/* Follow reference or signature attribute ATTR of SRC_DIE.
22809 On entry *REF_CU is the CU of SRC_DIE.
22810 On exit *REF_CU is the CU of the result. */
22811
22812static struct die_info *
ff39bb5e 22813follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
22814 struct dwarf2_cu **ref_cu)
22815{
22816 struct die_info *die;
22817
7771576e 22818 if (attr_form_is_ref (attr))
348e048f 22819 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 22820 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
22821 die = follow_die_sig (src_die, attr, ref_cu);
22822 else
22823 {
22824 dump_die_for_error (src_die);
22825 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
518817b3 22826 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
348e048f
DE
22827 }
22828
22829 return die;
03dd20cc
DJ
22830}
22831
5c631832 22832/* Follow reference OFFSET.
673bfd45
DE
22833 On entry *REF_CU is the CU of the source die referencing OFFSET.
22834 On exit *REF_CU is the CU of the result.
22835 Returns NULL if OFFSET is invalid. */
f504f079 22836
f9aca02d 22837static struct die_info *
9c541725 22838follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 22839 struct dwarf2_cu **ref_cu)
c906108c 22840{
10b3939b 22841 struct die_info temp_die;
f2f0e013 22842 struct dwarf2_cu *target_cu, *cu = *ref_cu;
518817b3
SM
22843 struct dwarf2_per_objfile *dwarf2_per_objfile
22844 = cu->per_cu->dwarf2_per_objfile;
10b3939b 22845
348e048f
DE
22846 gdb_assert (cu->per_cu != NULL);
22847
98bfdba5
PA
22848 target_cu = cu;
22849
3019eac3 22850 if (cu->per_cu->is_debug_types)
348e048f
DE
22851 {
22852 /* .debug_types CUs cannot reference anything outside their CU.
22853 If they need to, they have to reference a signatured type via
55f1336d 22854 DW_FORM_ref_sig8. */
9c541725 22855 if (!offset_in_cu_p (&cu->header, sect_off))
5c631832 22856 return NULL;
348e048f 22857 }
36586728 22858 else if (offset_in_dwz != cu->per_cu->is_dwz
9c541725 22859 || !offset_in_cu_p (&cu->header, sect_off))
10b3939b
DJ
22860 {
22861 struct dwarf2_per_cu_data *per_cu;
9a619af0 22862
9c541725 22863 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 22864 dwarf2_per_objfile);
03dd20cc
DJ
22865
22866 /* If necessary, add it to the queue and load its DIEs. */
95554aad 22867 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 22868 load_full_comp_unit (per_cu, false, cu->language);
03dd20cc 22869
10b3939b
DJ
22870 target_cu = per_cu->cu;
22871 }
98bfdba5
PA
22872 else if (cu->dies == NULL)
22873 {
22874 /* We're loading full DIEs during partial symbol reading. */
22875 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
58f0c718 22876 load_full_comp_unit (cu->per_cu, false, language_minimal);
98bfdba5 22877 }
c906108c 22878
f2f0e013 22879 *ref_cu = target_cu;
9c541725 22880 temp_die.sect_off = sect_off;
9a3c8263 22881 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
22882 &temp_die,
22883 to_underlying (sect_off));
5c631832 22884}
10b3939b 22885
5c631832
JK
22886/* Follow reference attribute ATTR of SRC_DIE.
22887 On entry *REF_CU is the CU of SRC_DIE.
22888 On exit *REF_CU is the CU of the result. */
22889
22890static struct die_info *
ff39bb5e 22891follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
22892 struct dwarf2_cu **ref_cu)
22893{
9c541725 22894 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
22895 struct dwarf2_cu *cu = *ref_cu;
22896 struct die_info *die;
22897
9c541725 22898 die = follow_die_offset (sect_off,
36586728
TT
22899 (attr->form == DW_FORM_GNU_ref_alt
22900 || cu->per_cu->is_dwz),
22901 ref_cu);
5c631832 22902 if (!die)
9d8780f0
SM
22903 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22904 "at %s [in module %s]"),
22905 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
518817b3 22906 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
348e048f 22907
5c631832
JK
22908 return die;
22909}
22910
9c541725 22911/* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
d83e736b 22912 Returned value is intended for DW_OP_call*. Returned
e3b94546
SM
22913 dwarf2_locexpr_baton->data has lifetime of
22914 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
5c631832
JK
22915
22916struct dwarf2_locexpr_baton
9c541725 22917dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
8b9737bf
TT
22918 struct dwarf2_per_cu_data *per_cu,
22919 CORE_ADDR (*get_frame_pc) (void *baton),
22920 void *baton)
5c631832 22921{
918dd910 22922 struct dwarf2_cu *cu;
5c631832
JK
22923 struct die_info *die;
22924 struct attribute *attr;
22925 struct dwarf2_locexpr_baton retval;
12359b5e
SM
22926 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22927 struct objfile *objfile = dwarf2_per_objfile->objfile;
8cf6f0b1 22928
918dd910 22929 if (per_cu->cu == NULL)
58f0c718 22930 load_cu (per_cu, false);
918dd910 22931 cu = per_cu->cu;
cc12ce38
DE
22932 if (cu == NULL)
22933 {
22934 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22935 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22936 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22937 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22938 }
918dd910 22939
9c541725 22940 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 22941 if (!die)
9d8780f0
SM
22942 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22943 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22944
22945 attr = dwarf2_attr (die, DW_AT_location, cu);
22946 if (!attr)
22947 {
e103e986
JK
22948 /* DWARF: "If there is no such attribute, then there is no effect.".
22949 DATA is ignored if SIZE is 0. */
5c631832 22950
e103e986 22951 retval.data = NULL;
5c631832
JK
22952 retval.size = 0;
22953 }
8cf6f0b1
TT
22954 else if (attr_form_is_section_offset (attr))
22955 {
22956 struct dwarf2_loclist_baton loclist_baton;
22957 CORE_ADDR pc = (*get_frame_pc) (baton);
22958 size_t size;
22959
22960 fill_in_loclist_baton (cu, &loclist_baton, attr);
22961
22962 retval.data = dwarf2_find_location_expression (&loclist_baton,
22963 &size, pc);
22964 retval.size = size;
22965 }
5c631832
JK
22966 else
22967 {
22968 if (!attr_form_is_block (attr))
9d8780f0 22969 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 22970 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 22971 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22972
22973 retval.data = DW_BLOCK (attr)->data;
22974 retval.size = DW_BLOCK (attr)->size;
22975 }
22976 retval.per_cu = cu->per_cu;
918dd910 22977
ed2dc618 22978 age_cached_comp_units (dwarf2_per_objfile);
918dd910 22979
5c631832 22980 return retval;
348e048f
DE
22981}
22982
8b9737bf
TT
22983/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
22984 offset. */
22985
22986struct dwarf2_locexpr_baton
22987dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22988 struct dwarf2_per_cu_data *per_cu,
22989 CORE_ADDR (*get_frame_pc) (void *baton),
22990 void *baton)
22991{
9c541725 22992 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 22993
9c541725 22994 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
22995}
22996
b6807d98
TT
22997/* Write a constant of a given type as target-ordered bytes into
22998 OBSTACK. */
22999
23000static const gdb_byte *
23001write_constant_as_bytes (struct obstack *obstack,
23002 enum bfd_endian byte_order,
23003 struct type *type,
23004 ULONGEST value,
23005 LONGEST *len)
23006{
23007 gdb_byte *result;
23008
23009 *len = TYPE_LENGTH (type);
224c3ddb 23010 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23011 store_unsigned_integer (result, *len, byte_order, value);
23012
23013 return result;
23014}
23015
23016/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
23017 pointer to the constant bytes and set LEN to the length of the
23018 data. If memory is needed, allocate it on OBSTACK. If the DIE
23019 does not have a DW_AT_const_value, return NULL. */
23020
23021const gdb_byte *
9c541725 23022dwarf2_fetch_constant_bytes (sect_offset sect_off,
b6807d98
TT
23023 struct dwarf2_per_cu_data *per_cu,
23024 struct obstack *obstack,
23025 LONGEST *len)
23026{
23027 struct dwarf2_cu *cu;
23028 struct die_info *die;
23029 struct attribute *attr;
23030 const gdb_byte *result = NULL;
23031 struct type *type;
23032 LONGEST value;
23033 enum bfd_endian byte_order;
e3b94546 23034 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 23035
b6807d98 23036 if (per_cu->cu == NULL)
58f0c718 23037 load_cu (per_cu, false);
b6807d98 23038 cu = per_cu->cu;
cc12ce38
DE
23039 if (cu == NULL)
23040 {
23041 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23042 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23043 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23044 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23045 }
b6807d98 23046
9c541725 23047 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 23048 if (!die)
9d8780f0
SM
23049 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23050 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
23051
23052 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23053 if (attr == NULL)
23054 return NULL;
23055
e3b94546 23056 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
23057 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23058
23059 switch (attr->form)
23060 {
23061 case DW_FORM_addr:
23062 case DW_FORM_GNU_addr_index:
23063 {
23064 gdb_byte *tem;
23065
23066 *len = cu->header.addr_size;
224c3ddb 23067 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23068 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
23069 result = tem;
23070 }
23071 break;
23072 case DW_FORM_string:
23073 case DW_FORM_strp:
23074 case DW_FORM_GNU_str_index:
23075 case DW_FORM_GNU_strp_alt:
23076 /* DW_STRING is already allocated on the objfile obstack, point
23077 directly to it. */
23078 result = (const gdb_byte *) DW_STRING (attr);
23079 *len = strlen (DW_STRING (attr));
23080 break;
23081 case DW_FORM_block1:
23082 case DW_FORM_block2:
23083 case DW_FORM_block4:
23084 case DW_FORM_block:
23085 case DW_FORM_exprloc:
0224619f 23086 case DW_FORM_data16:
b6807d98
TT
23087 result = DW_BLOCK (attr)->data;
23088 *len = DW_BLOCK (attr)->size;
23089 break;
23090
23091 /* The DW_AT_const_value attributes are supposed to carry the
23092 symbol's value "represented as it would be on the target
23093 architecture." By the time we get here, it's already been
23094 converted to host endianness, so we just need to sign- or
23095 zero-extend it as appropriate. */
23096 case DW_FORM_data1:
23097 type = die_type (die, cu);
23098 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23099 if (result == NULL)
23100 result = write_constant_as_bytes (obstack, byte_order,
23101 type, value, len);
23102 break;
23103 case DW_FORM_data2:
23104 type = die_type (die, cu);
23105 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23106 if (result == NULL)
23107 result = write_constant_as_bytes (obstack, byte_order,
23108 type, value, len);
23109 break;
23110 case DW_FORM_data4:
23111 type = die_type (die, cu);
23112 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23113 if (result == NULL)
23114 result = write_constant_as_bytes (obstack, byte_order,
23115 type, value, len);
23116 break;
23117 case DW_FORM_data8:
23118 type = die_type (die, cu);
23119 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23120 if (result == NULL)
23121 result = write_constant_as_bytes (obstack, byte_order,
23122 type, value, len);
23123 break;
23124
23125 case DW_FORM_sdata:
663c44ac 23126 case DW_FORM_implicit_const:
b6807d98
TT
23127 type = die_type (die, cu);
23128 result = write_constant_as_bytes (obstack, byte_order,
23129 type, DW_SND (attr), len);
23130 break;
23131
23132 case DW_FORM_udata:
23133 type = die_type (die, cu);
23134 result = write_constant_as_bytes (obstack, byte_order,
23135 type, DW_UNSND (attr), len);
23136 break;
23137
23138 default:
b98664d3 23139 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
23140 dwarf_form_name (attr->form));
23141 break;
23142 }
23143
23144 return result;
23145}
23146
7942e96e
AA
23147/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23148 valid type for this die is found. */
23149
23150struct type *
9c541725 23151dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
7942e96e
AA
23152 struct dwarf2_per_cu_data *per_cu)
23153{
23154 struct dwarf2_cu *cu;
23155 struct die_info *die;
23156
7942e96e 23157 if (per_cu->cu == NULL)
58f0c718 23158 load_cu (per_cu, false);
7942e96e
AA
23159 cu = per_cu->cu;
23160 if (!cu)
23161 return NULL;
23162
9c541725 23163 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
23164 if (!die)
23165 return NULL;
23166
23167 return die_type (die, cu);
23168}
23169
8a9b8146
TT
23170/* Return the type of the DIE at DIE_OFFSET in the CU named by
23171 PER_CU. */
23172
23173struct type *
b64f50a1 23174dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
23175 struct dwarf2_per_cu_data *per_cu)
23176{
9c541725 23177 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 23178 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
23179}
23180
ac9ec31b 23181/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 23182 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
23183 On exit *REF_CU is the CU of the result.
23184 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
23185
23186static struct die_info *
ac9ec31b
DE
23187follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23188 struct dwarf2_cu **ref_cu)
348e048f 23189{
348e048f 23190 struct die_info temp_die;
348e048f
DE
23191 struct dwarf2_cu *sig_cu;
23192 struct die_info *die;
23193
ac9ec31b
DE
23194 /* While it might be nice to assert sig_type->type == NULL here,
23195 we can get here for DW_AT_imported_declaration where we need
23196 the DIE not the type. */
348e048f
DE
23197
23198 /* If necessary, add it to the queue and load its DIEs. */
23199
95554aad 23200 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 23201 read_signatured_type (sig_type);
348e048f 23202
348e048f 23203 sig_cu = sig_type->per_cu.cu;
69d751e3 23204 gdb_assert (sig_cu != NULL);
9c541725
PA
23205 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23206 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 23207 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 23208 to_underlying (temp_die.sect_off));
348e048f
DE
23209 if (die)
23210 {
ed2dc618 23211 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 23212 = (*ref_cu)->per_cu->dwarf2_per_objfile;
ed2dc618 23213
796a7ff8
DE
23214 /* For .gdb_index version 7 keep track of included TUs.
23215 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23216 if (dwarf2_per_objfile->index_table != NULL
23217 && dwarf2_per_objfile->index_table->version <= 7)
23218 {
23219 VEC_safe_push (dwarf2_per_cu_ptr,
23220 (*ref_cu)->per_cu->imported_symtabs,
23221 sig_cu->per_cu);
23222 }
23223
348e048f
DE
23224 *ref_cu = sig_cu;
23225 return die;
23226 }
23227
ac9ec31b
DE
23228 return NULL;
23229}
23230
23231/* Follow signatured type referenced by ATTR in SRC_DIE.
23232 On entry *REF_CU is the CU of SRC_DIE.
23233 On exit *REF_CU is the CU of the result.
23234 The result is the DIE of the type.
23235 If the referenced type cannot be found an error is thrown. */
23236
23237static struct die_info *
ff39bb5e 23238follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
23239 struct dwarf2_cu **ref_cu)
23240{
23241 ULONGEST signature = DW_SIGNATURE (attr);
23242 struct signatured_type *sig_type;
23243 struct die_info *die;
23244
23245 gdb_assert (attr->form == DW_FORM_ref_sig8);
23246
a2ce51a0 23247 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
23248 /* sig_type will be NULL if the signatured type is missing from
23249 the debug info. */
23250 if (sig_type == NULL)
23251 {
23252 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23253 " from DIE at %s [in module %s]"),
23254 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23255 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23256 }
23257
23258 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23259 if (die == NULL)
23260 {
23261 dump_die_for_error (src_die);
23262 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23263 " from DIE at %s [in module %s]"),
23264 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23265 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23266 }
23267
23268 return die;
23269}
23270
23271/* Get the type specified by SIGNATURE referenced in DIE/CU,
23272 reading in and processing the type unit if necessary. */
23273
23274static struct type *
23275get_signatured_type (struct die_info *die, ULONGEST signature,
23276 struct dwarf2_cu *cu)
23277{
518817b3
SM
23278 struct dwarf2_per_objfile *dwarf2_per_objfile
23279 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
23280 struct signatured_type *sig_type;
23281 struct dwarf2_cu *type_cu;
23282 struct die_info *type_die;
23283 struct type *type;
23284
a2ce51a0 23285 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
23286 /* sig_type will be NULL if the signatured type is missing from
23287 the debug info. */
23288 if (sig_type == NULL)
23289 {
b98664d3 23290 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23291 " from DIE at %s [in module %s]"),
23292 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23293 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23294 return build_error_marker_type (cu, die);
23295 }
23296
23297 /* If we already know the type we're done. */
23298 if (sig_type->type != NULL)
23299 return sig_type->type;
23300
23301 type_cu = cu;
23302 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23303 if (type_die != NULL)
23304 {
23305 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23306 is created. This is important, for example, because for c++ classes
23307 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23308 type = read_type_die (type_die, type_cu);
23309 if (type == NULL)
23310 {
b98664d3 23311 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
23312 " referenced from DIE at %s [in module %s]"),
23313 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23314 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23315 type = build_error_marker_type (cu, die);
23316 }
23317 }
23318 else
23319 {
b98664d3 23320 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23321 " from DIE at %s [in module %s]"),
23322 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23323 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23324 type = build_error_marker_type (cu, die);
23325 }
23326 sig_type->type = type;
23327
23328 return type;
23329}
23330
23331/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23332 reading in and processing the type unit if necessary. */
23333
23334static struct type *
ff39bb5e 23335get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 23336 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
23337{
23338 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 23339 if (attr_form_is_ref (attr))
ac9ec31b
DE
23340 {
23341 struct dwarf2_cu *type_cu = cu;
23342 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23343
23344 return read_type_die (type_die, type_cu);
23345 }
23346 else if (attr->form == DW_FORM_ref_sig8)
23347 {
23348 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23349 }
23350 else
23351 {
518817b3
SM
23352 struct dwarf2_per_objfile *dwarf2_per_objfile
23353 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 23354
b98664d3 23355 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
23356 " at %s [in module %s]"),
23357 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 23358 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23359 return build_error_marker_type (cu, die);
23360 }
348e048f
DE
23361}
23362
e5fe5e75 23363/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
23364
23365static void
e5fe5e75 23366load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 23367{
52dc124a 23368 struct signatured_type *sig_type;
348e048f 23369
f4dc4d17
DE
23370 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23371 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23372
6721b2ec
DE
23373 /* We have the per_cu, but we need the signatured_type.
23374 Fortunately this is an easy translation. */
23375 gdb_assert (per_cu->is_debug_types);
23376 sig_type = (struct signatured_type *) per_cu;
348e048f 23377
6721b2ec 23378 gdb_assert (per_cu->cu == NULL);
348e048f 23379
52dc124a 23380 read_signatured_type (sig_type);
348e048f 23381
6721b2ec 23382 gdb_assert (per_cu->cu != NULL);
348e048f
DE
23383}
23384
dee91e82
DE
23385/* die_reader_func for read_signatured_type.
23386 This is identical to load_full_comp_unit_reader,
23387 but is kept separate for now. */
348e048f
DE
23388
23389static void
dee91e82 23390read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 23391 const gdb_byte *info_ptr,
dee91e82
DE
23392 struct die_info *comp_unit_die,
23393 int has_children,
23394 void *data)
348e048f 23395{
dee91e82 23396 struct dwarf2_cu *cu = reader->cu;
348e048f 23397
dee91e82
DE
23398 gdb_assert (cu->die_hash == NULL);
23399 cu->die_hash =
23400 htab_create_alloc_ex (cu->header.length / 12,
23401 die_hash,
23402 die_eq,
23403 NULL,
23404 &cu->comp_unit_obstack,
23405 hashtab_obstack_allocate,
23406 dummy_obstack_deallocate);
348e048f 23407
dee91e82
DE
23408 if (has_children)
23409 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23410 &info_ptr, comp_unit_die);
23411 cu->dies = comp_unit_die;
23412 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
23413
23414 /* We try not to read any attributes in this function, because not
9cdd5dbd 23415 all CUs needed for references have been loaded yet, and symbol
348e048f 23416 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
23417 or we won't be able to build types correctly.
23418 Similarly, if we do not read the producer, we can not apply
23419 producer-specific interpretation. */
95554aad 23420 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 23421}
348e048f 23422
3019eac3
DE
23423/* Read in a signatured type and build its CU and DIEs.
23424 If the type is a stub for the real type in a DWO file,
23425 read in the real type from the DWO file as well. */
dee91e82
DE
23426
23427static void
23428read_signatured_type (struct signatured_type *sig_type)
23429{
23430 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 23431
3019eac3 23432 gdb_assert (per_cu->is_debug_types);
dee91e82 23433 gdb_assert (per_cu->cu == NULL);
348e048f 23434
58f0c718 23435 init_cutu_and_read_dies (per_cu, NULL, 0, 1, false,
f4dc4d17 23436 read_signatured_type_reader, NULL);
7ee85ab1 23437 sig_type->per_cu.tu_read = 1;
c906108c
SS
23438}
23439
c906108c
SS
23440/* Decode simple location descriptions.
23441 Given a pointer to a dwarf block that defines a location, compute
23442 the location and return the value.
23443
4cecd739
DJ
23444 NOTE drow/2003-11-18: This function is called in two situations
23445 now: for the address of static or global variables (partial symbols
23446 only) and for offsets into structures which are expected to be
23447 (more or less) constant. The partial symbol case should go away,
23448 and only the constant case should remain. That will let this
23449 function complain more accurately. A few special modes are allowed
23450 without complaint for global variables (for instance, global
23451 register values and thread-local values).
c906108c
SS
23452
23453 A location description containing no operations indicates that the
4cecd739 23454 object is optimized out. The return value is 0 for that case.
6b992462
DJ
23455 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23456 callers will only want a very basic result and this can become a
21ae7a4d
JK
23457 complaint.
23458
23459 Note that stack[0] is unused except as a default error return. */
c906108c
SS
23460
23461static CORE_ADDR
e7c27a73 23462decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 23463{
518817b3 23464 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
56eb65bd
SP
23465 size_t i;
23466 size_t size = blk->size;
d521ce57 23467 const gdb_byte *data = blk->data;
21ae7a4d
JK
23468 CORE_ADDR stack[64];
23469 int stacki;
23470 unsigned int bytes_read, unsnd;
23471 gdb_byte op;
c906108c 23472
21ae7a4d
JK
23473 i = 0;
23474 stacki = 0;
23475 stack[stacki] = 0;
23476 stack[++stacki] = 0;
23477
23478 while (i < size)
23479 {
23480 op = data[i++];
23481 switch (op)
23482 {
23483 case DW_OP_lit0:
23484 case DW_OP_lit1:
23485 case DW_OP_lit2:
23486 case DW_OP_lit3:
23487 case DW_OP_lit4:
23488 case DW_OP_lit5:
23489 case DW_OP_lit6:
23490 case DW_OP_lit7:
23491 case DW_OP_lit8:
23492 case DW_OP_lit9:
23493 case DW_OP_lit10:
23494 case DW_OP_lit11:
23495 case DW_OP_lit12:
23496 case DW_OP_lit13:
23497 case DW_OP_lit14:
23498 case DW_OP_lit15:
23499 case DW_OP_lit16:
23500 case DW_OP_lit17:
23501 case DW_OP_lit18:
23502 case DW_OP_lit19:
23503 case DW_OP_lit20:
23504 case DW_OP_lit21:
23505 case DW_OP_lit22:
23506 case DW_OP_lit23:
23507 case DW_OP_lit24:
23508 case DW_OP_lit25:
23509 case DW_OP_lit26:
23510 case DW_OP_lit27:
23511 case DW_OP_lit28:
23512 case DW_OP_lit29:
23513 case DW_OP_lit30:
23514 case DW_OP_lit31:
23515 stack[++stacki] = op - DW_OP_lit0;
23516 break;
f1bea926 23517
21ae7a4d
JK
23518 case DW_OP_reg0:
23519 case DW_OP_reg1:
23520 case DW_OP_reg2:
23521 case DW_OP_reg3:
23522 case DW_OP_reg4:
23523 case DW_OP_reg5:
23524 case DW_OP_reg6:
23525 case DW_OP_reg7:
23526 case DW_OP_reg8:
23527 case DW_OP_reg9:
23528 case DW_OP_reg10:
23529 case DW_OP_reg11:
23530 case DW_OP_reg12:
23531 case DW_OP_reg13:
23532 case DW_OP_reg14:
23533 case DW_OP_reg15:
23534 case DW_OP_reg16:
23535 case DW_OP_reg17:
23536 case DW_OP_reg18:
23537 case DW_OP_reg19:
23538 case DW_OP_reg20:
23539 case DW_OP_reg21:
23540 case DW_OP_reg22:
23541 case DW_OP_reg23:
23542 case DW_OP_reg24:
23543 case DW_OP_reg25:
23544 case DW_OP_reg26:
23545 case DW_OP_reg27:
23546 case DW_OP_reg28:
23547 case DW_OP_reg29:
23548 case DW_OP_reg30:
23549 case DW_OP_reg31:
23550 stack[++stacki] = op - DW_OP_reg0;
23551 if (i < size)
23552 dwarf2_complex_location_expr_complaint ();
23553 break;
c906108c 23554
21ae7a4d
JK
23555 case DW_OP_regx:
23556 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23557 i += bytes_read;
23558 stack[++stacki] = unsnd;
23559 if (i < size)
23560 dwarf2_complex_location_expr_complaint ();
23561 break;
c906108c 23562
21ae7a4d
JK
23563 case DW_OP_addr:
23564 stack[++stacki] = read_address (objfile->obfd, &data[i],
23565 cu, &bytes_read);
23566 i += bytes_read;
23567 break;
d53d4ac5 23568
21ae7a4d
JK
23569 case DW_OP_const1u:
23570 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23571 i += 1;
23572 break;
23573
23574 case DW_OP_const1s:
23575 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23576 i += 1;
23577 break;
23578
23579 case DW_OP_const2u:
23580 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23581 i += 2;
23582 break;
23583
23584 case DW_OP_const2s:
23585 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23586 i += 2;
23587 break;
d53d4ac5 23588
21ae7a4d
JK
23589 case DW_OP_const4u:
23590 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23591 i += 4;
23592 break;
23593
23594 case DW_OP_const4s:
23595 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23596 i += 4;
23597 break;
23598
585861ea
JK
23599 case DW_OP_const8u:
23600 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23601 i += 8;
23602 break;
23603
21ae7a4d
JK
23604 case DW_OP_constu:
23605 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23606 &bytes_read);
23607 i += bytes_read;
23608 break;
23609
23610 case DW_OP_consts:
23611 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23612 i += bytes_read;
23613 break;
23614
23615 case DW_OP_dup:
23616 stack[stacki + 1] = stack[stacki];
23617 stacki++;
23618 break;
23619
23620 case DW_OP_plus:
23621 stack[stacki - 1] += stack[stacki];
23622 stacki--;
23623 break;
23624
23625 case DW_OP_plus_uconst:
23626 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23627 &bytes_read);
23628 i += bytes_read;
23629 break;
23630
23631 case DW_OP_minus:
23632 stack[stacki - 1] -= stack[stacki];
23633 stacki--;
23634 break;
23635
23636 case DW_OP_deref:
23637 /* If we're not the last op, then we definitely can't encode
23638 this using GDB's address_class enum. This is valid for partial
23639 global symbols, although the variable's address will be bogus
23640 in the psymtab. */
23641 if (i < size)
23642 dwarf2_complex_location_expr_complaint ();
23643 break;
23644
23645 case DW_OP_GNU_push_tls_address:
4aa4e28b 23646 case DW_OP_form_tls_address:
21ae7a4d
JK
23647 /* The top of the stack has the offset from the beginning
23648 of the thread control block at which the variable is located. */
23649 /* Nothing should follow this operator, so the top of stack would
23650 be returned. */
23651 /* This is valid for partial global symbols, but the variable's
585861ea
JK
23652 address will be bogus in the psymtab. Make it always at least
23653 non-zero to not look as a variable garbage collected by linker
23654 which have DW_OP_addr 0. */
21ae7a4d
JK
23655 if (i < size)
23656 dwarf2_complex_location_expr_complaint ();
585861ea 23657 stack[stacki]++;
21ae7a4d
JK
23658 break;
23659
23660 case DW_OP_GNU_uninit:
23661 break;
23662
3019eac3 23663 case DW_OP_GNU_addr_index:
49f6c839 23664 case DW_OP_GNU_const_index:
3019eac3
DE
23665 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23666 &bytes_read);
23667 i += bytes_read;
23668 break;
23669
21ae7a4d
JK
23670 default:
23671 {
f39c6ffd 23672 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
23673
23674 if (name)
b98664d3 23675 complaint (_("unsupported stack op: '%s'"),
21ae7a4d
JK
23676 name);
23677 else
b98664d3 23678 complaint (_("unsupported stack op: '%02x'"),
21ae7a4d
JK
23679 op);
23680 }
23681
23682 return (stack[stacki]);
d53d4ac5 23683 }
3c6e0cb3 23684
21ae7a4d
JK
23685 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23686 outside of the allocated space. Also enforce minimum>0. */
23687 if (stacki >= ARRAY_SIZE (stack) - 1)
23688 {
b98664d3 23689 complaint (_("location description stack overflow"));
21ae7a4d
JK
23690 return 0;
23691 }
23692
23693 if (stacki <= 0)
23694 {
b98664d3 23695 complaint (_("location description stack underflow"));
21ae7a4d
JK
23696 return 0;
23697 }
23698 }
23699 return (stack[stacki]);
c906108c
SS
23700}
23701
23702/* memory allocation interface */
23703
c906108c 23704static struct dwarf_block *
7b5a2f43 23705dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 23706{
8d749320 23707 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
23708}
23709
c906108c 23710static struct die_info *
b60c80d6 23711dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
23712{
23713 struct die_info *die;
b60c80d6
DJ
23714 size_t size = sizeof (struct die_info);
23715
23716 if (num_attrs > 1)
23717 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 23718
b60c80d6 23719 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
23720 memset (die, 0, sizeof (struct die_info));
23721 return (die);
23722}
2e276125
JB
23723
23724\f
23725/* Macro support. */
23726
233d95b5
JK
23727/* Return file name relative to the compilation directory of file number I in
23728 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 23729 responsible for freeing it. */
233d95b5 23730
2e276125 23731static char *
233d95b5 23732file_file_name (int file, struct line_header *lh)
2e276125 23733{
6a83a1e6
EZ
23734 /* Is the file number a valid index into the line header's file name
23735 table? Remember that file numbers start with one, not zero. */
fff8551c 23736 if (1 <= file && file <= lh->file_names.size ())
6a83a1e6 23737 {
8c43009f 23738 const file_entry &fe = lh->file_names[file - 1];
6e70227d 23739
8c43009f
PA
23740 if (!IS_ABSOLUTE_PATH (fe.name))
23741 {
23742 const char *dir = fe.include_dir (lh);
23743 if (dir != NULL)
23744 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
23745 }
23746 return xstrdup (fe.name);
6a83a1e6 23747 }
2e276125
JB
23748 else
23749 {
6a83a1e6
EZ
23750 /* The compiler produced a bogus file number. We can at least
23751 record the macro definitions made in the file, even if we
23752 won't be able to find the file by name. */
23753 char fake_name[80];
9a619af0 23754
8c042590
PM
23755 xsnprintf (fake_name, sizeof (fake_name),
23756 "<bad macro file number %d>", file);
2e276125 23757
b98664d3 23758 complaint (_("bad file number in macro information (%d)"),
6a83a1e6 23759 file);
2e276125 23760
6a83a1e6 23761 return xstrdup (fake_name);
2e276125
JB
23762 }
23763}
23764
233d95b5
JK
23765/* Return the full name of file number I in *LH's file name table.
23766 Use COMP_DIR as the name of the current directory of the
23767 compilation. The result is allocated using xmalloc; the caller is
23768 responsible for freeing it. */
23769static char *
23770file_full_name (int file, struct line_header *lh, const char *comp_dir)
23771{
23772 /* Is the file number a valid index into the line header's file name
23773 table? Remember that file numbers start with one, not zero. */
fff8551c 23774 if (1 <= file && file <= lh->file_names.size ())
233d95b5
JK
23775 {
23776 char *relative = file_file_name (file, lh);
23777
23778 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
23779 return relative;
b36cec19
PA
23780 return reconcat (relative, comp_dir, SLASH_STRING,
23781 relative, (char *) NULL);
233d95b5
JK
23782 }
23783 else
23784 return file_file_name (file, lh);
23785}
23786
2e276125
JB
23787
23788static struct macro_source_file *
804d2729
TT
23789macro_start_file (struct dwarf2_cu *cu,
23790 int file, int line,
2e276125 23791 struct macro_source_file *current_file,
43f3e411 23792 struct line_header *lh)
2e276125 23793{
233d95b5
JK
23794 /* File name relative to the compilation directory of this source file. */
23795 char *file_name = file_file_name (file, lh);
2e276125 23796
2e276125 23797 if (! current_file)
abc9d0dc 23798 {
fc474241
DE
23799 /* Note: We don't create a macro table for this compilation unit
23800 at all until we actually get a filename. */
804d2729 23801 struct macro_table *macro_table = cu->builder->get_macro_table ();
fc474241 23802
abc9d0dc
TT
23803 /* If we have no current file, then this must be the start_file
23804 directive for the compilation unit's main source file. */
fc474241
DE
23805 current_file = macro_set_main (macro_table, file_name);
23806 macro_define_special (macro_table);
abc9d0dc 23807 }
2e276125 23808 else
233d95b5 23809 current_file = macro_include (current_file, line, file_name);
2e276125 23810
233d95b5 23811 xfree (file_name);
6e70227d 23812
2e276125
JB
23813 return current_file;
23814}
23815
2e276125
JB
23816static const char *
23817consume_improper_spaces (const char *p, const char *body)
23818{
23819 if (*p == ' ')
23820 {
b98664d3 23821 complaint (_("macro definition contains spaces "
3e43a32a 23822 "in formal argument list:\n`%s'"),
4d3c2250 23823 body);
2e276125
JB
23824
23825 while (*p == ' ')
23826 p++;
23827 }
23828
23829 return p;
23830}
23831
23832
23833static void
23834parse_macro_definition (struct macro_source_file *file, int line,
23835 const char *body)
23836{
23837 const char *p;
23838
23839 /* The body string takes one of two forms. For object-like macro
23840 definitions, it should be:
23841
23842 <macro name> " " <definition>
23843
23844 For function-like macro definitions, it should be:
23845
23846 <macro name> "() " <definition>
23847 or
23848 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23849
23850 Spaces may appear only where explicitly indicated, and in the
23851 <definition>.
23852
23853 The Dwarf 2 spec says that an object-like macro's name is always
23854 followed by a space, but versions of GCC around March 2002 omit
6e70227d 23855 the space when the macro's definition is the empty string.
2e276125
JB
23856
23857 The Dwarf 2 spec says that there should be no spaces between the
23858 formal arguments in a function-like macro's formal argument list,
23859 but versions of GCC around March 2002 include spaces after the
23860 commas. */
23861
23862
23863 /* Find the extent of the macro name. The macro name is terminated
23864 by either a space or null character (for an object-like macro) or
23865 an opening paren (for a function-like macro). */
23866 for (p = body; *p; p++)
23867 if (*p == ' ' || *p == '(')
23868 break;
23869
23870 if (*p == ' ' || *p == '\0')
23871 {
23872 /* It's an object-like macro. */
23873 int name_len = p - body;
3f8a7804 23874 char *name = savestring (body, name_len);
2e276125
JB
23875 const char *replacement;
23876
23877 if (*p == ' ')
23878 replacement = body + name_len + 1;
23879 else
23880 {
4d3c2250 23881 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23882 replacement = body + name_len;
23883 }
6e70227d 23884
2e276125
JB
23885 macro_define_object (file, line, name, replacement);
23886
23887 xfree (name);
23888 }
23889 else if (*p == '(')
23890 {
23891 /* It's a function-like macro. */
3f8a7804 23892 char *name = savestring (body, p - body);
2e276125
JB
23893 int argc = 0;
23894 int argv_size = 1;
8d749320 23895 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
23896
23897 p++;
23898
23899 p = consume_improper_spaces (p, body);
23900
23901 /* Parse the formal argument list. */
23902 while (*p && *p != ')')
23903 {
23904 /* Find the extent of the current argument name. */
23905 const char *arg_start = p;
23906
23907 while (*p && *p != ',' && *p != ')' && *p != ' ')
23908 p++;
23909
23910 if (! *p || p == arg_start)
4d3c2250 23911 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23912 else
23913 {
23914 /* Make sure argv has room for the new argument. */
23915 if (argc >= argv_size)
23916 {
23917 argv_size *= 2;
224c3ddb 23918 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
23919 }
23920
3f8a7804 23921 argv[argc++] = savestring (arg_start, p - arg_start);
2e276125
JB
23922 }
23923
23924 p = consume_improper_spaces (p, body);
23925
23926 /* Consume the comma, if present. */
23927 if (*p == ',')
23928 {
23929 p++;
23930
23931 p = consume_improper_spaces (p, body);
23932 }
23933 }
23934
23935 if (*p == ')')
23936 {
23937 p++;
23938
23939 if (*p == ' ')
23940 /* Perfectly formed definition, no complaints. */
23941 macro_define_function (file, line, name,
6e70227d 23942 argc, (const char **) argv,
2e276125
JB
23943 p + 1);
23944 else if (*p == '\0')
23945 {
23946 /* Complain, but do define it. */
4d3c2250 23947 dwarf2_macro_malformed_definition_complaint (body);
2e276125 23948 macro_define_function (file, line, name,
6e70227d 23949 argc, (const char **) argv,
2e276125
JB
23950 p);
23951 }
23952 else
23953 /* Just complain. */
4d3c2250 23954 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23955 }
23956 else
23957 /* Just complain. */
4d3c2250 23958 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23959
23960 xfree (name);
23961 {
23962 int i;
23963
23964 for (i = 0; i < argc; i++)
23965 xfree (argv[i]);
23966 }
23967 xfree (argv);
23968 }
23969 else
4d3c2250 23970 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23971}
23972
cf2c3c16
TT
23973/* Skip some bytes from BYTES according to the form given in FORM.
23974 Returns the new pointer. */
2e276125 23975
d521ce57
TT
23976static const gdb_byte *
23977skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
23978 enum dwarf_form form,
23979 unsigned int offset_size,
23980 struct dwarf2_section_info *section)
2e276125 23981{
cf2c3c16 23982 unsigned int bytes_read;
2e276125 23983
cf2c3c16 23984 switch (form)
2e276125 23985 {
cf2c3c16
TT
23986 case DW_FORM_data1:
23987 case DW_FORM_flag:
23988 ++bytes;
23989 break;
23990
23991 case DW_FORM_data2:
23992 bytes += 2;
23993 break;
23994
23995 case DW_FORM_data4:
23996 bytes += 4;
23997 break;
23998
23999 case DW_FORM_data8:
24000 bytes += 8;
24001 break;
24002
0224619f
JK
24003 case DW_FORM_data16:
24004 bytes += 16;
24005 break;
24006
cf2c3c16
TT
24007 case DW_FORM_string:
24008 read_direct_string (abfd, bytes, &bytes_read);
24009 bytes += bytes_read;
24010 break;
24011
24012 case DW_FORM_sec_offset:
24013 case DW_FORM_strp:
36586728 24014 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
24015 bytes += offset_size;
24016 break;
24017
24018 case DW_FORM_block:
24019 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
24020 bytes += bytes_read;
24021 break;
24022
24023 case DW_FORM_block1:
24024 bytes += 1 + read_1_byte (abfd, bytes);
24025 break;
24026 case DW_FORM_block2:
24027 bytes += 2 + read_2_bytes (abfd, bytes);
24028 break;
24029 case DW_FORM_block4:
24030 bytes += 4 + read_4_bytes (abfd, bytes);
24031 break;
24032
24033 case DW_FORM_sdata:
24034 case DW_FORM_udata:
3019eac3
DE
24035 case DW_FORM_GNU_addr_index:
24036 case DW_FORM_GNU_str_index:
d521ce57 24037 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
24038 if (bytes == NULL)
24039 {
24040 dwarf2_section_buffer_overflow_complaint (section);
24041 return NULL;
24042 }
cf2c3c16
TT
24043 break;
24044
663c44ac
JK
24045 case DW_FORM_implicit_const:
24046 break;
24047
cf2c3c16
TT
24048 default:
24049 {
b98664d3 24050 complaint (_("invalid form 0x%x in `%s'"),
a32a8923 24051 form, get_section_name (section));
cf2c3c16
TT
24052 return NULL;
24053 }
2e276125
JB
24054 }
24055
cf2c3c16
TT
24056 return bytes;
24057}
757a13d0 24058
cf2c3c16
TT
24059/* A helper for dwarf_decode_macros that handles skipping an unknown
24060 opcode. Returns an updated pointer to the macro data buffer; or,
24061 on error, issues a complaint and returns NULL. */
757a13d0 24062
d521ce57 24063static const gdb_byte *
cf2c3c16 24064skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
24065 const gdb_byte **opcode_definitions,
24066 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
24067 bfd *abfd,
24068 unsigned int offset_size,
24069 struct dwarf2_section_info *section)
24070{
24071 unsigned int bytes_read, i;
24072 unsigned long arg;
d521ce57 24073 const gdb_byte *defn;
2e276125 24074
cf2c3c16 24075 if (opcode_definitions[opcode] == NULL)
2e276125 24076 {
b98664d3 24077 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
cf2c3c16
TT
24078 opcode);
24079 return NULL;
24080 }
2e276125 24081
cf2c3c16
TT
24082 defn = opcode_definitions[opcode];
24083 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
24084 defn += bytes_read;
2e276125 24085
cf2c3c16
TT
24086 for (i = 0; i < arg; ++i)
24087 {
aead7601
SM
24088 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24089 (enum dwarf_form) defn[i], offset_size,
f664829e 24090 section);
cf2c3c16
TT
24091 if (mac_ptr == NULL)
24092 {
24093 /* skip_form_bytes already issued the complaint. */
24094 return NULL;
24095 }
24096 }
757a13d0 24097
cf2c3c16
TT
24098 return mac_ptr;
24099}
757a13d0 24100
cf2c3c16
TT
24101/* A helper function which parses the header of a macro section.
24102 If the macro section is the extended (for now called "GNU") type,
24103 then this updates *OFFSET_SIZE. Returns a pointer to just after
24104 the header, or issues a complaint and returns NULL on error. */
757a13d0 24105
d521ce57
TT
24106static const gdb_byte *
24107dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 24108 bfd *abfd,
d521ce57 24109 const gdb_byte *mac_ptr,
cf2c3c16
TT
24110 unsigned int *offset_size,
24111 int section_is_gnu)
24112{
24113 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 24114
cf2c3c16
TT
24115 if (section_is_gnu)
24116 {
24117 unsigned int version, flags;
757a13d0 24118
cf2c3c16 24119 version = read_2_bytes (abfd, mac_ptr);
0af92d60 24120 if (version != 4 && version != 5)
cf2c3c16 24121 {
b98664d3 24122 complaint (_("unrecognized version `%d' in .debug_macro section"),
cf2c3c16
TT
24123 version);
24124 return NULL;
24125 }
24126 mac_ptr += 2;
757a13d0 24127
cf2c3c16
TT
24128 flags = read_1_byte (abfd, mac_ptr);
24129 ++mac_ptr;
24130 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 24131
cf2c3c16
TT
24132 if ((flags & 2) != 0)
24133 /* We don't need the line table offset. */
24134 mac_ptr += *offset_size;
757a13d0 24135
cf2c3c16
TT
24136 /* Vendor opcode descriptions. */
24137 if ((flags & 4) != 0)
24138 {
24139 unsigned int i, count;
757a13d0 24140
cf2c3c16
TT
24141 count = read_1_byte (abfd, mac_ptr);
24142 ++mac_ptr;
24143 for (i = 0; i < count; ++i)
24144 {
24145 unsigned int opcode, bytes_read;
24146 unsigned long arg;
24147
24148 opcode = read_1_byte (abfd, mac_ptr);
24149 ++mac_ptr;
24150 opcode_definitions[opcode] = mac_ptr;
24151 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24152 mac_ptr += bytes_read;
24153 mac_ptr += arg;
24154 }
757a13d0 24155 }
cf2c3c16 24156 }
757a13d0 24157
cf2c3c16
TT
24158 return mac_ptr;
24159}
757a13d0 24160
cf2c3c16 24161/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 24162 including DW_MACRO_import. */
cf2c3c16
TT
24163
24164static void
804d2729 24165dwarf_decode_macro_bytes (struct dwarf2_cu *cu,
ed2dc618 24166 bfd *abfd,
d521ce57 24167 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 24168 struct macro_source_file *current_file,
43f3e411 24169 struct line_header *lh,
cf2c3c16 24170 struct dwarf2_section_info *section,
36586728 24171 int section_is_gnu, int section_is_dwz,
cf2c3c16 24172 unsigned int offset_size,
8fc3fc34 24173 htab_t include_hash)
cf2c3c16 24174{
804d2729
TT
24175 struct dwarf2_per_objfile *dwarf2_per_objfile
24176 = cu->per_cu->dwarf2_per_objfile;
4d663531 24177 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
24178 enum dwarf_macro_record_type macinfo_type;
24179 int at_commandline;
d521ce57 24180 const gdb_byte *opcode_definitions[256];
757a13d0 24181
cf2c3c16
TT
24182 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24183 &offset_size, section_is_gnu);
24184 if (mac_ptr == NULL)
24185 {
24186 /* We already issued a complaint. */
24187 return;
24188 }
757a13d0
JK
24189
24190 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24191 GDB is still reading the definitions from command line. First
24192 DW_MACINFO_start_file will need to be ignored as it was already executed
24193 to create CURRENT_FILE for the main source holding also the command line
24194 definitions. On first met DW_MACINFO_start_file this flag is reset to
24195 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24196
24197 at_commandline = 1;
24198
24199 do
24200 {
24201 /* Do we at least have room for a macinfo type byte? */
24202 if (mac_ptr >= mac_end)
24203 {
f664829e 24204 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
24205 break;
24206 }
24207
aead7601 24208 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
24209 mac_ptr++;
24210
cf2c3c16
TT
24211 /* Note that we rely on the fact that the corresponding GNU and
24212 DWARF constants are the same. */
132448f8
SM
24213 DIAGNOSTIC_PUSH
24214 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
757a13d0
JK
24215 switch (macinfo_type)
24216 {
24217 /* A zero macinfo type indicates the end of the macro
24218 information. */
24219 case 0:
24220 break;
2e276125 24221
0af92d60
JK
24222 case DW_MACRO_define:
24223 case DW_MACRO_undef:
24224 case DW_MACRO_define_strp:
24225 case DW_MACRO_undef_strp:
24226 case DW_MACRO_define_sup:
24227 case DW_MACRO_undef_sup:
2e276125 24228 {
891d2f0b 24229 unsigned int bytes_read;
2e276125 24230 int line;
d521ce57 24231 const char *body;
cf2c3c16 24232 int is_define;
2e276125 24233
cf2c3c16
TT
24234 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24235 mac_ptr += bytes_read;
24236
0af92d60
JK
24237 if (macinfo_type == DW_MACRO_define
24238 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
24239 {
24240 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24241 mac_ptr += bytes_read;
24242 }
24243 else
24244 {
24245 LONGEST str_offset;
24246
24247 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24248 mac_ptr += offset_size;
2e276125 24249
0af92d60
JK
24250 if (macinfo_type == DW_MACRO_define_sup
24251 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 24252 || section_is_dwz)
36586728 24253 {
ed2dc618
SM
24254 struct dwz_file *dwz
24255 = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728 24256
ed2dc618
SM
24257 body = read_indirect_string_from_dwz (objfile,
24258 dwz, str_offset);
36586728
TT
24259 }
24260 else
ed2dc618
SM
24261 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24262 abfd, str_offset);
cf2c3c16
TT
24263 }
24264
0af92d60
JK
24265 is_define = (macinfo_type == DW_MACRO_define
24266 || macinfo_type == DW_MACRO_define_strp
24267 || macinfo_type == DW_MACRO_define_sup);
2e276125 24268 if (! current_file)
757a13d0
JK
24269 {
24270 /* DWARF violation as no main source is present. */
b98664d3 24271 complaint (_("debug info with no main source gives macro %s "
757a13d0 24272 "on line %d: %s"),
cf2c3c16
TT
24273 is_define ? _("definition") : _("undefinition"),
24274 line, body);
757a13d0
JK
24275 break;
24276 }
3e43a32a
MS
24277 if ((line == 0 && !at_commandline)
24278 || (line != 0 && at_commandline))
b98664d3 24279 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
757a13d0 24280 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 24281 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
24282 line == 0 ? _("zero") : _("non-zero"), line, body);
24283
cf2c3c16 24284 if (is_define)
757a13d0 24285 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
24286 else
24287 {
0af92d60
JK
24288 gdb_assert (macinfo_type == DW_MACRO_undef
24289 || macinfo_type == DW_MACRO_undef_strp
24290 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
24291 macro_undef (current_file, line, body);
24292 }
2e276125
JB
24293 }
24294 break;
24295
0af92d60 24296 case DW_MACRO_start_file:
2e276125 24297 {
891d2f0b 24298 unsigned int bytes_read;
2e276125
JB
24299 int line, file;
24300
24301 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24302 mac_ptr += bytes_read;
24303 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24304 mac_ptr += bytes_read;
24305
3e43a32a
MS
24306 if ((line == 0 && !at_commandline)
24307 || (line != 0 && at_commandline))
b98664d3 24308 complaint (_("debug info gives source %d included "
757a13d0
JK
24309 "from %s at %s line %d"),
24310 file, at_commandline ? _("command-line") : _("file"),
24311 line == 0 ? _("zero") : _("non-zero"), line);
24312
24313 if (at_commandline)
24314 {
0af92d60 24315 /* This DW_MACRO_start_file was executed in the
cf2c3c16 24316 pass one. */
757a13d0
JK
24317 at_commandline = 0;
24318 }
24319 else
804d2729
TT
24320 current_file = macro_start_file (cu, file, line, current_file,
24321 lh);
2e276125
JB
24322 }
24323 break;
24324
0af92d60 24325 case DW_MACRO_end_file:
2e276125 24326 if (! current_file)
b98664d3 24327 complaint (_("macro debug info has an unmatched "
3e43a32a 24328 "`close_file' directive"));
2e276125
JB
24329 else
24330 {
24331 current_file = current_file->included_by;
24332 if (! current_file)
24333 {
cf2c3c16 24334 enum dwarf_macro_record_type next_type;
2e276125
JB
24335
24336 /* GCC circa March 2002 doesn't produce the zero
24337 type byte marking the end of the compilation
24338 unit. Complain if it's not there, but exit no
24339 matter what. */
24340
24341 /* Do we at least have room for a macinfo type byte? */
24342 if (mac_ptr >= mac_end)
24343 {
f664829e 24344 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
24345 return;
24346 }
24347
24348 /* We don't increment mac_ptr here, so this is just
24349 a look-ahead. */
aead7601
SM
24350 next_type
24351 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24352 mac_ptr);
2e276125 24353 if (next_type != 0)
b98664d3 24354 complaint (_("no terminating 0-type entry for "
3e43a32a 24355 "macros in `.debug_macinfo' section"));
2e276125
JB
24356
24357 return;
24358 }
24359 }
24360 break;
24361
0af92d60
JK
24362 case DW_MACRO_import:
24363 case DW_MACRO_import_sup:
cf2c3c16
TT
24364 {
24365 LONGEST offset;
8fc3fc34 24366 void **slot;
a036ba48
TT
24367 bfd *include_bfd = abfd;
24368 struct dwarf2_section_info *include_section = section;
d521ce57 24369 const gdb_byte *include_mac_end = mac_end;
a036ba48 24370 int is_dwz = section_is_dwz;
d521ce57 24371 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
24372
24373 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24374 mac_ptr += offset_size;
24375
0af92d60 24376 if (macinfo_type == DW_MACRO_import_sup)
a036ba48 24377 {
ed2dc618 24378 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
a036ba48 24379
4d663531 24380 dwarf2_read_section (objfile, &dwz->macro);
a036ba48 24381
a036ba48 24382 include_section = &dwz->macro;
a32a8923 24383 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
24384 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24385 is_dwz = 1;
24386 }
24387
24388 new_mac_ptr = include_section->buffer + offset;
24389 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24390
8fc3fc34
TT
24391 if (*slot != NULL)
24392 {
24393 /* This has actually happened; see
24394 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
b98664d3 24395 complaint (_("recursive DW_MACRO_import in "
8fc3fc34
TT
24396 ".debug_macro section"));
24397 }
24398 else
24399 {
d521ce57 24400 *slot = (void *) new_mac_ptr;
36586728 24401
804d2729 24402 dwarf_decode_macro_bytes (cu, include_bfd, new_mac_ptr,
43f3e411 24403 include_mac_end, current_file, lh,
36586728 24404 section, section_is_gnu, is_dwz,
4d663531 24405 offset_size, include_hash);
8fc3fc34 24406
d521ce57 24407 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 24408 }
cf2c3c16
TT
24409 }
24410 break;
24411
2e276125 24412 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
24413 if (!section_is_gnu)
24414 {
24415 unsigned int bytes_read;
2e276125 24416
ac298888
TT
24417 /* This reads the constant, but since we don't recognize
24418 any vendor extensions, we ignore it. */
24419 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
24420 mac_ptr += bytes_read;
24421 read_direct_string (abfd, mac_ptr, &bytes_read);
24422 mac_ptr += bytes_read;
2e276125 24423
cf2c3c16
TT
24424 /* We don't recognize any vendor extensions. */
24425 break;
24426 }
24427 /* FALLTHROUGH */
24428
24429 default:
24430 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 24431 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
24432 section);
24433 if (mac_ptr == NULL)
24434 return;
24435 break;
2e276125 24436 }
132448f8 24437 DIAGNOSTIC_POP
757a13d0 24438 } while (macinfo_type != 0);
2e276125 24439}
8e19ed76 24440
cf2c3c16 24441static void
09262596 24442dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 24443 int section_is_gnu)
cf2c3c16 24444{
518817b3
SM
24445 struct dwarf2_per_objfile *dwarf2_per_objfile
24446 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 24447 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
24448 struct line_header *lh = cu->line_header;
24449 bfd *abfd;
d521ce57 24450 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
24451 struct macro_source_file *current_file = 0;
24452 enum dwarf_macro_record_type macinfo_type;
24453 unsigned int offset_size = cu->header.offset_size;
d521ce57 24454 const gdb_byte *opcode_definitions[256];
8fc3fc34 24455 void **slot;
09262596
DE
24456 struct dwarf2_section_info *section;
24457 const char *section_name;
24458
24459 if (cu->dwo_unit != NULL)
24460 {
24461 if (section_is_gnu)
24462 {
24463 section = &cu->dwo_unit->dwo_file->sections.macro;
24464 section_name = ".debug_macro.dwo";
24465 }
24466 else
24467 {
24468 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24469 section_name = ".debug_macinfo.dwo";
24470 }
24471 }
24472 else
24473 {
24474 if (section_is_gnu)
24475 {
24476 section = &dwarf2_per_objfile->macro;
24477 section_name = ".debug_macro";
24478 }
24479 else
24480 {
24481 section = &dwarf2_per_objfile->macinfo;
24482 section_name = ".debug_macinfo";
24483 }
24484 }
cf2c3c16 24485
bb5ed363 24486 dwarf2_read_section (objfile, section);
cf2c3c16
TT
24487 if (section->buffer == NULL)
24488 {
b98664d3 24489 complaint (_("missing %s section"), section_name);
cf2c3c16
TT
24490 return;
24491 }
a32a8923 24492 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
24493
24494 /* First pass: Find the name of the base filename.
24495 This filename is needed in order to process all macros whose definition
24496 (or undefinition) comes from the command line. These macros are defined
24497 before the first DW_MACINFO_start_file entry, and yet still need to be
24498 associated to the base file.
24499
24500 To determine the base file name, we scan the macro definitions until we
24501 reach the first DW_MACINFO_start_file entry. We then initialize
24502 CURRENT_FILE accordingly so that any macro definition found before the
24503 first DW_MACINFO_start_file can still be associated to the base file. */
24504
24505 mac_ptr = section->buffer + offset;
24506 mac_end = section->buffer + section->size;
24507
24508 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24509 &offset_size, section_is_gnu);
24510 if (mac_ptr == NULL)
24511 {
24512 /* We already issued a complaint. */
24513 return;
24514 }
24515
24516 do
24517 {
24518 /* Do we at least have room for a macinfo type byte? */
24519 if (mac_ptr >= mac_end)
24520 {
24521 /* Complaint is printed during the second pass as GDB will probably
24522 stop the first pass earlier upon finding
24523 DW_MACINFO_start_file. */
24524 break;
24525 }
24526
aead7601 24527 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
24528 mac_ptr++;
24529
24530 /* Note that we rely on the fact that the corresponding GNU and
24531 DWARF constants are the same. */
132448f8
SM
24532 DIAGNOSTIC_PUSH
24533 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
cf2c3c16
TT
24534 switch (macinfo_type)
24535 {
24536 /* A zero macinfo type indicates the end of the macro
24537 information. */
24538 case 0:
24539 break;
24540
0af92d60
JK
24541 case DW_MACRO_define:
24542 case DW_MACRO_undef:
cf2c3c16
TT
24543 /* Only skip the data by MAC_PTR. */
24544 {
24545 unsigned int bytes_read;
24546
24547 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24548 mac_ptr += bytes_read;
24549 read_direct_string (abfd, mac_ptr, &bytes_read);
24550 mac_ptr += bytes_read;
24551 }
24552 break;
24553
0af92d60 24554 case DW_MACRO_start_file:
cf2c3c16
TT
24555 {
24556 unsigned int bytes_read;
24557 int line, file;
24558
24559 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24560 mac_ptr += bytes_read;
24561 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24562 mac_ptr += bytes_read;
24563
804d2729 24564 current_file = macro_start_file (cu, file, line, current_file, lh);
cf2c3c16
TT
24565 }
24566 break;
24567
0af92d60 24568 case DW_MACRO_end_file:
cf2c3c16
TT
24569 /* No data to skip by MAC_PTR. */
24570 break;
24571
0af92d60
JK
24572 case DW_MACRO_define_strp:
24573 case DW_MACRO_undef_strp:
24574 case DW_MACRO_define_sup:
24575 case DW_MACRO_undef_sup:
cf2c3c16
TT
24576 {
24577 unsigned int bytes_read;
24578
24579 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24580 mac_ptr += bytes_read;
24581 mac_ptr += offset_size;
24582 }
24583 break;
24584
0af92d60
JK
24585 case DW_MACRO_import:
24586 case DW_MACRO_import_sup:
cf2c3c16 24587 /* Note that, according to the spec, a transparent include
0af92d60 24588 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
24589 skip this opcode. */
24590 mac_ptr += offset_size;
24591 break;
24592
24593 case DW_MACINFO_vendor_ext:
24594 /* Only skip the data by MAC_PTR. */
24595 if (!section_is_gnu)
24596 {
24597 unsigned int bytes_read;
24598
24599 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24600 mac_ptr += bytes_read;
24601 read_direct_string (abfd, mac_ptr, &bytes_read);
24602 mac_ptr += bytes_read;
24603 }
24604 /* FALLTHROUGH */
24605
24606 default:
24607 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 24608 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
24609 section);
24610 if (mac_ptr == NULL)
24611 return;
24612 break;
24613 }
132448f8 24614 DIAGNOSTIC_POP
cf2c3c16
TT
24615 } while (macinfo_type != 0 && current_file == NULL);
24616
24617 /* Second pass: Process all entries.
24618
24619 Use the AT_COMMAND_LINE flag to determine whether we are still processing
24620 command-line macro definitions/undefinitions. This flag is unset when we
24621 reach the first DW_MACINFO_start_file entry. */
24622
fc4007c9
TT
24623 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
24624 htab_eq_pointer,
24625 NULL, xcalloc, xfree));
8fc3fc34 24626 mac_ptr = section->buffer + offset;
fc4007c9 24627 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 24628 *slot = (void *) mac_ptr;
804d2729 24629 dwarf_decode_macro_bytes (cu, abfd, mac_ptr, mac_end,
43f3e411 24630 current_file, lh, section,
fc4007c9
TT
24631 section_is_gnu, 0, offset_size,
24632 include_hash.get ());
cf2c3c16
TT
24633}
24634
8e19ed76 24635/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 24636 if so return true else false. */
380bca97 24637
8e19ed76 24638static int
6e5a29e1 24639attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
24640{
24641 return (attr == NULL ? 0 :
24642 attr->form == DW_FORM_block1
24643 || attr->form == DW_FORM_block2
24644 || attr->form == DW_FORM_block4
2dc7f7b3
TT
24645 || attr->form == DW_FORM_block
24646 || attr->form == DW_FORM_exprloc);
8e19ed76 24647}
4c2df51b 24648
c6a0999f
JB
24649/* Return non-zero if ATTR's value is a section offset --- classes
24650 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
24651 You may use DW_UNSND (attr) to retrieve such offsets.
24652
24653 Section 7.5.4, "Attribute Encodings", explains that no attribute
24654 may have a value that belongs to more than one of these classes; it
24655 would be ambiguous if we did, because we use the same forms for all
24656 of them. */
380bca97 24657
3690dd37 24658static int
6e5a29e1 24659attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
24660{
24661 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
24662 || attr->form == DW_FORM_data8
24663 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
24664}
24665
3690dd37
JB
24666/* Return non-zero if ATTR's value falls in the 'constant' class, or
24667 zero otherwise. When this function returns true, you can apply
24668 dwarf2_get_attr_constant_value to it.
24669
24670 However, note that for some attributes you must check
24671 attr_form_is_section_offset before using this test. DW_FORM_data4
24672 and DW_FORM_data8 are members of both the constant class, and of
24673 the classes that contain offsets into other debug sections
24674 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
24675 that, if an attribute's can be either a constant or one of the
24676 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
0224619f
JK
24677 taken as section offsets, not constants.
24678
24679 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
24680 cannot handle that. */
380bca97 24681
3690dd37 24682static int
6e5a29e1 24683attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
24684{
24685 switch (attr->form)
24686 {
24687 case DW_FORM_sdata:
24688 case DW_FORM_udata:
24689 case DW_FORM_data1:
24690 case DW_FORM_data2:
24691 case DW_FORM_data4:
24692 case DW_FORM_data8:
663c44ac 24693 case DW_FORM_implicit_const:
3690dd37
JB
24694 return 1;
24695 default:
24696 return 0;
24697 }
24698}
24699
7771576e
SA
24700
24701/* DW_ADDR is always stored already as sect_offset; despite for the forms
24702 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
24703
24704static int
6e5a29e1 24705attr_form_is_ref (const struct attribute *attr)
7771576e
SA
24706{
24707 switch (attr->form)
24708 {
24709 case DW_FORM_ref_addr:
24710 case DW_FORM_ref1:
24711 case DW_FORM_ref2:
24712 case DW_FORM_ref4:
24713 case DW_FORM_ref8:
24714 case DW_FORM_ref_udata:
24715 case DW_FORM_GNU_ref_alt:
24716 return 1;
24717 default:
24718 return 0;
24719 }
24720}
24721
3019eac3
DE
24722/* Return the .debug_loc section to use for CU.
24723 For DWO files use .debug_loc.dwo. */
24724
24725static struct dwarf2_section_info *
24726cu_debug_loc_section (struct dwarf2_cu *cu)
24727{
518817b3
SM
24728 struct dwarf2_per_objfile *dwarf2_per_objfile
24729 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 24730
3019eac3 24731 if (cu->dwo_unit)
43988095
JK
24732 {
24733 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24734
24735 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24736 }
24737 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
24738 : &dwarf2_per_objfile->loc);
3019eac3
DE
24739}
24740
8cf6f0b1
TT
24741/* A helper function that fills in a dwarf2_loclist_baton. */
24742
24743static void
24744fill_in_loclist_baton (struct dwarf2_cu *cu,
24745 struct dwarf2_loclist_baton *baton,
ff39bb5e 24746 const struct attribute *attr)
8cf6f0b1 24747{
518817b3
SM
24748 struct dwarf2_per_objfile *dwarf2_per_objfile
24749 = cu->per_cu->dwarf2_per_objfile;
3019eac3
DE
24750 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24751
24752 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
24753
24754 baton->per_cu = cu->per_cu;
24755 gdb_assert (baton->per_cu);
24756 /* We don't know how long the location list is, but make sure we
24757 don't run off the edge of the section. */
3019eac3
DE
24758 baton->size = section->size - DW_UNSND (attr);
24759 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 24760 baton->base_address = cu->base_address;
f664829e 24761 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
24762}
24763
4c2df51b 24764static void
ff39bb5e 24765dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 24766 struct dwarf2_cu *cu, int is_block)
4c2df51b 24767{
518817b3
SM
24768 struct dwarf2_per_objfile *dwarf2_per_objfile
24769 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 24770 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 24771 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 24772
3690dd37 24773 if (attr_form_is_section_offset (attr)
3019eac3 24774 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
24775 the section. If so, fall through to the complaint in the
24776 other branch. */
3019eac3 24777 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 24778 {
0d53c4c4 24779 struct dwarf2_loclist_baton *baton;
4c2df51b 24780
8d749320 24781 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 24782
8cf6f0b1 24783 fill_in_loclist_baton (cu, baton, attr);
be391dca 24784
d00adf39 24785 if (cu->base_known == 0)
b98664d3 24786 complaint (_("Location list used without "
3e43a32a 24787 "specifying the CU base address."));
4c2df51b 24788
f1e6e072
TT
24789 SYMBOL_ACLASS_INDEX (sym) = (is_block
24790 ? dwarf2_loclist_block_index
24791 : dwarf2_loclist_index);
0d53c4c4
DJ
24792 SYMBOL_LOCATION_BATON (sym) = baton;
24793 }
24794 else
24795 {
24796 struct dwarf2_locexpr_baton *baton;
24797
8d749320 24798 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
24799 baton->per_cu = cu->per_cu;
24800 gdb_assert (baton->per_cu);
0d53c4c4
DJ
24801
24802 if (attr_form_is_block (attr))
24803 {
24804 /* Note that we're just copying the block's data pointer
24805 here, not the actual data. We're still pointing into the
6502dd73
DJ
24806 info_buffer for SYM's objfile; right now we never release
24807 that buffer, but when we do clean up properly this may
24808 need to change. */
0d53c4c4
DJ
24809 baton->size = DW_BLOCK (attr)->size;
24810 baton->data = DW_BLOCK (attr)->data;
24811 }
24812 else
24813 {
24814 dwarf2_invalid_attrib_class_complaint ("location description",
24815 SYMBOL_NATURAL_NAME (sym));
24816 baton->size = 0;
0d53c4c4 24817 }
6e70227d 24818
f1e6e072
TT
24819 SYMBOL_ACLASS_INDEX (sym) = (is_block
24820 ? dwarf2_locexpr_block_index
24821 : dwarf2_locexpr_index);
0d53c4c4
DJ
24822 SYMBOL_LOCATION_BATON (sym) = baton;
24823 }
4c2df51b 24824}
6502dd73 24825
9aa1f1e3
TT
24826/* Return the OBJFILE associated with the compilation unit CU. If CU
24827 came from a separate debuginfo file, then the master objfile is
24828 returned. */
ae0d2f24
UW
24829
24830struct objfile *
24831dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
24832{
e3b94546 24833 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
ae0d2f24
UW
24834
24835 /* Return the master objfile, so that we can report and look up the
24836 correct file containing this variable. */
24837 if (objfile->separate_debug_objfile_backlink)
24838 objfile = objfile->separate_debug_objfile_backlink;
24839
24840 return objfile;
24841}
24842
96408a79
SA
24843/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24844 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24845 CU_HEADERP first. */
24846
24847static const struct comp_unit_head *
24848per_cu_header_read_in (struct comp_unit_head *cu_headerp,
24849 struct dwarf2_per_cu_data *per_cu)
24850{
d521ce57 24851 const gdb_byte *info_ptr;
96408a79
SA
24852
24853 if (per_cu->cu)
24854 return &per_cu->cu->header;
24855
9c541725 24856 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
24857
24858 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
24859 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24860 rcuh_kind::COMPILE);
96408a79
SA
24861
24862 return cu_headerp;
24863}
24864
ae0d2f24
UW
24865/* Return the address size given in the compilation unit header for CU. */
24866
98714339 24867int
ae0d2f24
UW
24868dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
24869{
96408a79
SA
24870 struct comp_unit_head cu_header_local;
24871 const struct comp_unit_head *cu_headerp;
c471e790 24872
96408a79
SA
24873 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24874
24875 return cu_headerp->addr_size;
ae0d2f24
UW
24876}
24877
9eae7c52
TT
24878/* Return the offset size given in the compilation unit header for CU. */
24879
24880int
24881dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
24882{
96408a79
SA
24883 struct comp_unit_head cu_header_local;
24884 const struct comp_unit_head *cu_headerp;
9c6c53f7 24885
96408a79
SA
24886 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24887
24888 return cu_headerp->offset_size;
24889}
24890
24891/* See its dwarf2loc.h declaration. */
24892
24893int
24894dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
24895{
24896 struct comp_unit_head cu_header_local;
24897 const struct comp_unit_head *cu_headerp;
24898
24899 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24900
24901 if (cu_headerp->version == 2)
24902 return cu_headerp->addr_size;
24903 else
24904 return cu_headerp->offset_size;
181cebd4
JK
24905}
24906
9aa1f1e3
TT
24907/* Return the text offset of the CU. The returned offset comes from
24908 this CU's objfile. If this objfile came from a separate debuginfo
24909 file, then the offset may be different from the corresponding
24910 offset in the parent objfile. */
24911
24912CORE_ADDR
24913dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
24914{
e3b94546 24915 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
9aa1f1e3
TT
24916
24917 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
24918}
24919
43988095
JK
24920/* Return DWARF version number of PER_CU. */
24921
24922short
24923dwarf2_version (struct dwarf2_per_cu_data *per_cu)
24924{
24925 return per_cu->dwarf_version;
24926}
24927
348e048f
DE
24928/* Locate the .debug_info compilation unit from CU's objfile which contains
24929 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
24930
24931static struct dwarf2_per_cu_data *
9c541725 24932dwarf2_find_containing_comp_unit (sect_offset sect_off,
36586728 24933 unsigned int offset_in_dwz,
ed2dc618 24934 struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
24935{
24936 struct dwarf2_per_cu_data *this_cu;
24937 int low, high;
36586728 24938 const sect_offset *cu_off;
ae038cb0 24939
ae038cb0 24940 low = 0;
b76e467d 24941 high = dwarf2_per_objfile->all_comp_units.size () - 1;
ae038cb0
DJ
24942 while (high > low)
24943 {
36586728 24944 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 24945 int mid = low + (high - low) / 2;
9a619af0 24946
36586728 24947 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
9c541725 24948 cu_off = &mid_cu->sect_off;
36586728 24949 if (mid_cu->is_dwz > offset_in_dwz
9c541725 24950 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
ae038cb0
DJ
24951 high = mid;
24952 else
24953 low = mid + 1;
24954 }
24955 gdb_assert (low == high);
36586728 24956 this_cu = dwarf2_per_objfile->all_comp_units[low];
9c541725
PA
24957 cu_off = &this_cu->sect_off;
24958 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
ae038cb0 24959 {
36586728 24960 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 24961 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
24962 "offset %s [in module %s]"),
24963 sect_offset_str (sect_off),
ed2dc618 24964 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 24965
9c541725
PA
24966 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
24967 <= sect_off);
ae038cb0
DJ
24968 return dwarf2_per_objfile->all_comp_units[low-1];
24969 }
24970 else
24971 {
24972 this_cu = dwarf2_per_objfile->all_comp_units[low];
b76e467d 24973 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
9c541725 24974 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 24975 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 24976 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
24977 return this_cu;
24978 }
24979}
24980
23745b47 24981/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 24982
fcd3b13d
SM
24983dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
24984 : per_cu (per_cu_),
24985 mark (0),
24986 has_loclist (0),
24987 checked_producer (0),
24988 producer_is_gxx_lt_4_6 (0),
24989 producer_is_gcc_lt_4_3 (0),
24990 producer_is_icc_lt_14 (0),
24991 processing_has_namespace_info (0)
93311388 24992{
fcd3b13d
SM
24993 per_cu->cu = this;
24994}
24995
24996/* Destroy a dwarf2_cu. */
24997
24998dwarf2_cu::~dwarf2_cu ()
24999{
25000 per_cu->cu = NULL;
9816fde3
JK
25001}
25002
25003/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25004
25005static void
95554aad
TT
25006prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25007 enum language pretend_language)
9816fde3
JK
25008{
25009 struct attribute *attr;
25010
25011 /* Set the language we're debugging. */
25012 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
25013 if (attr)
25014 set_cu_language (DW_UNSND (attr), cu);
25015 else
9cded63f 25016 {
95554aad 25017 cu->language = pretend_language;
9cded63f
TT
25018 cu->language_defn = language_def (cu->language);
25019 }
dee91e82 25020
7d45c7c3 25021 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
25022}
25023
ae038cb0
DJ
25024/* Increase the age counter on each cached compilation unit, and free
25025 any that are too old. */
25026
25027static void
ed2dc618 25028age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
25029{
25030 struct dwarf2_per_cu_data *per_cu, **last_chain;
25031
25032 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
25033 per_cu = dwarf2_per_objfile->read_in_chain;
25034 while (per_cu != NULL)
25035 {
25036 per_cu->cu->last_used ++;
b4f54984 25037 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
25038 dwarf2_mark (per_cu->cu);
25039 per_cu = per_cu->cu->read_in_chain;
25040 }
25041
25042 per_cu = dwarf2_per_objfile->read_in_chain;
25043 last_chain = &dwarf2_per_objfile->read_in_chain;
25044 while (per_cu != NULL)
25045 {
25046 struct dwarf2_per_cu_data *next_cu;
25047
25048 next_cu = per_cu->cu->read_in_chain;
25049
25050 if (!per_cu->cu->mark)
25051 {
fcd3b13d 25052 delete per_cu->cu;
ae038cb0
DJ
25053 *last_chain = next_cu;
25054 }
25055 else
25056 last_chain = &per_cu->cu->read_in_chain;
25057
25058 per_cu = next_cu;
25059 }
25060}
25061
25062/* Remove a single compilation unit from the cache. */
25063
25064static void
dee91e82 25065free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
25066{
25067 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
25068 struct dwarf2_per_objfile *dwarf2_per_objfile
25069 = target_per_cu->dwarf2_per_objfile;
ae038cb0
DJ
25070
25071 per_cu = dwarf2_per_objfile->read_in_chain;
25072 last_chain = &dwarf2_per_objfile->read_in_chain;
25073 while (per_cu != NULL)
25074 {
25075 struct dwarf2_per_cu_data *next_cu;
25076
25077 next_cu = per_cu->cu->read_in_chain;
25078
dee91e82 25079 if (per_cu == target_per_cu)
ae038cb0 25080 {
fcd3b13d 25081 delete per_cu->cu;
dee91e82 25082 per_cu->cu = NULL;
ae038cb0
DJ
25083 *last_chain = next_cu;
25084 break;
25085 }
25086 else
25087 last_chain = &per_cu->cu->read_in_chain;
25088
25089 per_cu = next_cu;
25090 }
25091}
25092
d95d3aef 25093/* Cleanup function for the dwarf2_per_objfile data. */
fe3e1990 25094
d95d3aef
TT
25095static void
25096dwarf2_free_objfile (struct objfile *objfile, void *datum)
fe3e1990 25097{
ed2dc618 25098 struct dwarf2_per_objfile *dwarf2_per_objfile
d95d3aef 25099 = static_cast<struct dwarf2_per_objfile *> (datum);
fe3e1990 25100
fd90ace4 25101 delete dwarf2_per_objfile;
fe3e1990
DJ
25102}
25103
dee91e82
DE
25104/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25105 We store these in a hash table separate from the DIEs, and preserve them
25106 when the DIEs are flushed out of cache.
25107
25108 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 25109 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
25110 or the type may come from a DWO file. Furthermore, while it's more logical
25111 to use per_cu->section+offset, with Fission the section with the data is in
25112 the DWO file but we don't know that section at the point we need it.
25113 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25114 because we can enter the lookup routine, get_die_type_at_offset, from
25115 outside this file, and thus won't necessarily have PER_CU->cu.
25116 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 25117
dee91e82 25118struct dwarf2_per_cu_offset_and_type
1c379e20 25119{
dee91e82 25120 const struct dwarf2_per_cu_data *per_cu;
9c541725 25121 sect_offset sect_off;
1c379e20
DJ
25122 struct type *type;
25123};
25124
dee91e82 25125/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25126
25127static hashval_t
dee91e82 25128per_cu_offset_and_type_hash (const void *item)
1c379e20 25129{
9a3c8263
SM
25130 const struct dwarf2_per_cu_offset_and_type *ofs
25131 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 25132
9c541725 25133 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
25134}
25135
dee91e82 25136/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25137
25138static int
dee91e82 25139per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 25140{
9a3c8263
SM
25141 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25142 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25143 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25144 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 25145
dee91e82 25146 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 25147 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
25148}
25149
25150/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
25151 table if necessary. For convenience, return TYPE.
25152
25153 The DIEs reading must have careful ordering to:
25154 * Not cause infite loops trying to read in DIEs as a prerequisite for
25155 reading current DIE.
25156 * Not trying to dereference contents of still incompletely read in types
25157 while reading in other DIEs.
25158 * Enable referencing still incompletely read in types just by a pointer to
25159 the type without accessing its fields.
25160
25161 Therefore caller should follow these rules:
25162 * Try to fetch any prerequisite types we may need to build this DIE type
25163 before building the type and calling set_die_type.
e71ec853 25164 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
25165 possible before fetching more types to complete the current type.
25166 * Make the type as complete as possible before fetching more types. */
1c379e20 25167
f792889a 25168static struct type *
1c379e20
DJ
25169set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25170{
518817b3
SM
25171 struct dwarf2_per_objfile *dwarf2_per_objfile
25172 = cu->per_cu->dwarf2_per_objfile;
dee91e82 25173 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 25174 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
25175 struct attribute *attr;
25176 struct dynamic_prop prop;
1c379e20 25177
b4ba55a1
JB
25178 /* For Ada types, make sure that the gnat-specific data is always
25179 initialized (if not already set). There are a few types where
25180 we should not be doing so, because the type-specific area is
25181 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25182 where the type-specific area is used to store the floatformat).
25183 But this is not a problem, because the gnat-specific information
25184 is actually not needed for these types. */
25185 if (need_gnat_info (cu)
25186 && TYPE_CODE (type) != TYPE_CODE_FUNC
25187 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
25188 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25189 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25190 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
25191 && !HAVE_GNAT_AUX_INFO (type))
25192 INIT_GNAT_SPECIFIC (type);
25193
3f2f83dd
KB
25194 /* Read DW_AT_allocated and set in type. */
25195 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25196 if (attr_form_is_block (attr))
25197 {
25198 if (attr_to_dynamic_prop (attr, die, cu, &prop))
50a82047 25199 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
3f2f83dd
KB
25200 }
25201 else if (attr != NULL)
25202 {
b98664d3 25203 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 25204 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25205 sect_offset_str (die->sect_off));
3f2f83dd
KB
25206 }
25207
25208 /* Read DW_AT_associated and set in type. */
25209 attr = dwarf2_attr (die, DW_AT_associated, cu);
25210 if (attr_form_is_block (attr))
25211 {
25212 if (attr_to_dynamic_prop (attr, die, cu, &prop))
50a82047 25213 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
3f2f83dd
KB
25214 }
25215 else if (attr != NULL)
25216 {
b98664d3 25217 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 25218 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25219 sect_offset_str (die->sect_off));
3f2f83dd
KB
25220 }
25221
3cdcd0ce
JB
25222 /* Read DW_AT_data_location and set in type. */
25223 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25224 if (attr_to_dynamic_prop (attr, die, cu, &prop))
50a82047 25225 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
3cdcd0ce 25226
dee91e82 25227 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25228 {
dee91e82
DE
25229 dwarf2_per_objfile->die_type_hash =
25230 htab_create_alloc_ex (127,
25231 per_cu_offset_and_type_hash,
25232 per_cu_offset_and_type_eq,
25233 NULL,
25234 &objfile->objfile_obstack,
25235 hashtab_obstack_allocate,
25236 dummy_obstack_deallocate);
f792889a 25237 }
1c379e20 25238
dee91e82 25239 ofs.per_cu = cu->per_cu;
9c541725 25240 ofs.sect_off = die->sect_off;
1c379e20 25241 ofs.type = type;
dee91e82
DE
25242 slot = (struct dwarf2_per_cu_offset_and_type **)
25243 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57 25244 if (*slot)
b98664d3 25245 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 25246 sect_offset_str (die->sect_off));
8d749320
SM
25247 *slot = XOBNEW (&objfile->objfile_obstack,
25248 struct dwarf2_per_cu_offset_and_type);
1c379e20 25249 **slot = ofs;
f792889a 25250 return type;
1c379e20
DJ
25251}
25252
9c541725 25253/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 25254 or return NULL if the die does not have a saved type. */
1c379e20
DJ
25255
25256static struct type *
9c541725 25257get_die_type_at_offset (sect_offset sect_off,
673bfd45 25258 struct dwarf2_per_cu_data *per_cu)
1c379e20 25259{
dee91e82 25260 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 25261 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 25262
dee91e82 25263 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25264 return NULL;
1c379e20 25265
dee91e82 25266 ofs.per_cu = per_cu;
9c541725 25267 ofs.sect_off = sect_off;
9a3c8263
SM
25268 slot = ((struct dwarf2_per_cu_offset_and_type *)
25269 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
25270 if (slot)
25271 return slot->type;
25272 else
25273 return NULL;
25274}
25275
02142a6c 25276/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
25277 or return NULL if DIE does not have a saved type. */
25278
25279static struct type *
25280get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25281{
9c541725 25282 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
25283}
25284
10b3939b
DJ
25285/* Add a dependence relationship from CU to REF_PER_CU. */
25286
25287static void
25288dwarf2_add_dependence (struct dwarf2_cu *cu,
25289 struct dwarf2_per_cu_data *ref_per_cu)
25290{
25291 void **slot;
25292
25293 if (cu->dependencies == NULL)
25294 cu->dependencies
25295 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25296 NULL, &cu->comp_unit_obstack,
25297 hashtab_obstack_allocate,
25298 dummy_obstack_deallocate);
25299
25300 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25301 if (*slot == NULL)
25302 *slot = ref_per_cu;
25303}
1c379e20 25304
f504f079
DE
25305/* Subroutine of dwarf2_mark to pass to htab_traverse.
25306 Set the mark field in every compilation unit in the
ae038cb0
DJ
25307 cache that we must keep because we are keeping CU. */
25308
10b3939b
DJ
25309static int
25310dwarf2_mark_helper (void **slot, void *data)
25311{
25312 struct dwarf2_per_cu_data *per_cu;
25313
25314 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
25315
25316 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25317 reading of the chain. As such dependencies remain valid it is not much
25318 useful to track and undo them during QUIT cleanups. */
25319 if (per_cu->cu == NULL)
25320 return 1;
25321
10b3939b
DJ
25322 if (per_cu->cu->mark)
25323 return 1;
25324 per_cu->cu->mark = 1;
25325
25326 if (per_cu->cu->dependencies != NULL)
25327 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25328
25329 return 1;
25330}
25331
f504f079
DE
25332/* Set the mark field in CU and in every other compilation unit in the
25333 cache that we must keep because we are keeping CU. */
25334
ae038cb0
DJ
25335static void
25336dwarf2_mark (struct dwarf2_cu *cu)
25337{
25338 if (cu->mark)
25339 return;
25340 cu->mark = 1;
10b3939b
DJ
25341 if (cu->dependencies != NULL)
25342 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
25343}
25344
25345static void
25346dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25347{
25348 while (per_cu)
25349 {
25350 per_cu->cu->mark = 0;
25351 per_cu = per_cu->cu->read_in_chain;
25352 }
72bf9492
DJ
25353}
25354
72bf9492
DJ
25355/* Trivial hash function for partial_die_info: the hash value of a DIE
25356 is its offset in .debug_info for this objfile. */
25357
25358static hashval_t
25359partial_die_hash (const void *item)
25360{
9a3c8263
SM
25361 const struct partial_die_info *part_die
25362 = (const struct partial_die_info *) item;
9a619af0 25363
9c541725 25364 return to_underlying (part_die->sect_off);
72bf9492
DJ
25365}
25366
25367/* Trivial comparison function for partial_die_info structures: two DIEs
25368 are equal if they have the same offset. */
25369
25370static int
25371partial_die_eq (const void *item_lhs, const void *item_rhs)
25372{
9a3c8263
SM
25373 const struct partial_die_info *part_die_lhs
25374 = (const struct partial_die_info *) item_lhs;
25375 const struct partial_die_info *part_die_rhs
25376 = (const struct partial_die_info *) item_rhs;
9a619af0 25377
9c541725 25378 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
25379}
25380
3c3bb058
AB
25381struct cmd_list_element *set_dwarf_cmdlist;
25382struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
25383
25384static void
981a3fb3 25385set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 25386{
b4f54984 25387 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 25388 gdb_stdout);
ae038cb0
DJ
25389}
25390
25391static void
981a3fb3 25392show_dwarf_cmd (const char *args, int from_tty)
6e70227d 25393{
b4f54984 25394 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
25395}
25396
cd4fb1b2 25397int dwarf_always_disassemble;
437afbb8 25398
437afbb8 25399static void
cd4fb1b2
SM
25400show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
25401 struct cmd_list_element *c, const char *value)
9291a0cd 25402{
cd4fb1b2
SM
25403 fprintf_filtered (file,
25404 _("Whether to always disassemble "
25405 "DWARF expressions is %s.\n"),
25406 value);
9291a0cd
TT
25407}
25408
9291a0cd 25409static void
cd4fb1b2
SM
25410show_check_physname (struct ui_file *file, int from_tty,
25411 struct cmd_list_element *c, const char *value)
9291a0cd 25412{
cd4fb1b2
SM
25413 fprintf_filtered (file,
25414 _("Whether to check \"physname\" is %s.\n"),
25415 value);
9291a0cd
TT
25416}
25417
cd4fb1b2
SM
25418void
25419_initialize_dwarf2_read (void)
9291a0cd 25420{
d95d3aef
TT
25421 dwarf2_objfile_data_key
25422 = register_objfile_data_with_cleanup (nullptr, dwarf2_free_objfile);
156942c7 25423
cd4fb1b2
SM
25424 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
25425Set DWARF specific variables.\n\
25426Configure DWARF variables such as the cache size"),
25427 &set_dwarf_cmdlist, "maintenance set dwarf ",
25428 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 25429
cd4fb1b2
SM
25430 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
25431Show DWARF specific variables\n\
25432Show DWARF variables such as the cache size"),
25433 &show_dwarf_cmdlist, "maintenance show dwarf ",
25434 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 25435
cd4fb1b2
SM
25436 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25437 &dwarf_max_cache_age, _("\
25438Set the upper bound on the age of cached DWARF compilation units."), _("\
25439Show the upper bound on the age of cached DWARF compilation units."), _("\
25440A higher limit means that cached compilation units will be stored\n\
25441in memory longer, and more total memory will be used. Zero disables\n\
25442caching, which can slow down startup."),
25443 NULL,
25444 show_dwarf_max_cache_age,
25445 &set_dwarf_cmdlist,
25446 &show_dwarf_cmdlist);
156942c7 25447
cd4fb1b2
SM
25448 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
25449 &dwarf_always_disassemble, _("\
25450Set whether `info address' always disassembles DWARF expressions."), _("\
25451Show whether `info address' always disassembles DWARF expressions."), _("\
25452When enabled, DWARF expressions are always printed in an assembly-like\n\
25453syntax. When disabled, expressions will be printed in a more\n\
25454conversational style, when possible."),
25455 NULL,
25456 show_dwarf_always_disassemble,
25457 &set_dwarf_cmdlist,
25458 &show_dwarf_cmdlist);
9291a0cd 25459
cd4fb1b2
SM
25460 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25461Set debugging of the DWARF reader."), _("\
25462Show debugging of the DWARF reader."), _("\
25463When enabled (non-zero), debugging messages are printed during DWARF\n\
25464reading and symtab expansion. A value of 1 (one) provides basic\n\
25465information. A value greater than 1 provides more verbose information."),
25466 NULL,
25467 NULL,
25468 &setdebuglist, &showdebuglist);
9291a0cd 25469
cd4fb1b2
SM
25470 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25471Set debugging of the DWARF DIE reader."), _("\
25472Show debugging of the DWARF DIE reader."), _("\
25473When enabled (non-zero), DIEs are dumped after they are read in.\n\
25474The value is the maximum depth to print."),
25475 NULL,
25476 NULL,
25477 &setdebuglist, &showdebuglist);
9291a0cd 25478
cd4fb1b2
SM
25479 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25480Set debugging of the dwarf line reader."), _("\
25481Show debugging of the dwarf line reader."), _("\
25482When enabled (non-zero), line number entries are dumped as they are read in.\n\
25483A value of 1 (one) provides basic information.\n\
25484A value greater than 1 provides more verbose information."),
25485 NULL,
25486 NULL,
25487 &setdebuglist, &showdebuglist);
437afbb8 25488
cd4fb1b2
SM
25489 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25490Set cross-checking of \"physname\" code against demangler."), _("\
25491Show cross-checking of \"physname\" code against demangler."), _("\
25492When enabled, GDB's internal \"physname\" code is checked against\n\
25493the demangler."),
25494 NULL, show_check_physname,
25495 &setdebuglist, &showdebuglist);
900e11f9 25496
e615022a
DE
25497 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25498 no_class, &use_deprecated_index_sections, _("\
25499Set whether to use deprecated gdb_index sections."), _("\
25500Show whether to use deprecated gdb_index sections."), _("\
25501When enabled, deprecated .gdb_index sections are used anyway.\n\
25502Normally they are ignored either because of a missing feature or\n\
25503performance issue.\n\
25504Warning: This option must be enabled before gdb reads the file."),
25505 NULL,
25506 NULL,
25507 &setlist, &showlist);
25508
f1e6e072
TT
25509 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25510 &dwarf2_locexpr_funcs);
25511 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25512 &dwarf2_loclist_funcs);
25513
25514 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25515 &dwarf2_block_frame_base_locexpr_funcs);
25516 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25517 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
25518
25519#if GDB_SELF_TEST
25520 selftests::register_test ("dw2_expand_symtabs_matching",
25521 selftests::dw2_expand_symtabs_matching::run_test);
25522#endif
6502dd73 25523}
This page took 4.957178 seconds and 4 git commands to generate.