]> git.ipfire.org Git - thirdparty/binutils-gdb.git/blame - gdb/dwarf2read.c
Remove TYPE_TAG_NAME
[thirdparty/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"
c906108c
SS
40#include "buildsym.h"
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
e142c38c
DJ
438 /* The generic symbol table building routines have separate lists for
439 file scope symbols and all all other scopes (local scopes). So
440 we need to select the right one to pass to add_symbol_to_list().
441 We do it by keeping a pointer to the correct list in list_in_scope.
442
443 FIXME: The original dwarf code just treated the file scope as the
444 first local scope, and all other local scopes as nested local
445 scopes, and worked fine. Check to see if we really need to
446 distinguish these in buildsym.c. */
fcd3b13d 447 struct pending **list_in_scope = nullptr;
e142c38c 448
b64f50a1
JK
449 /* Hash table holding all the loaded partial DIEs
450 with partial_die->offset.SECT_OFF as hash. */
fcd3b13d 451 htab_t partial_dies = nullptr;
72bf9492
DJ
452
453 /* Storage for things with the same lifetime as this read-in compilation
454 unit, including partial DIEs. */
fcd3b13d 455 auto_obstack comp_unit_obstack;
72bf9492 456
ae038cb0
DJ
457 /* When multiple dwarf2_cu structures are living in memory, this field
458 chains them all together, so that they can be released efficiently.
459 We will probably also want a generation counter so that most-recently-used
460 compilation units are cached... */
fcd3b13d 461 struct dwarf2_per_cu_data *read_in_chain = nullptr;
ae038cb0 462
69d751e3 463 /* Backlink to our per_cu entry. */
ae038cb0
DJ
464 struct dwarf2_per_cu_data *per_cu;
465
466 /* How many compilation units ago was this CU last referenced? */
fcd3b13d 467 int last_used = 0;
ae038cb0 468
b64f50a1
JK
469 /* A hash table of DIE cu_offset for following references with
470 die_info->offset.sect_off as hash. */
fcd3b13d 471 htab_t die_hash = nullptr;
10b3939b
DJ
472
473 /* Full DIEs if read in. */
fcd3b13d 474 struct die_info *dies = nullptr;
10b3939b
DJ
475
476 /* A set of pointers to dwarf2_per_cu_data objects for compilation
477 units referenced by this one. Only set during full symbol processing;
478 partial symbol tables do not have dependencies. */
fcd3b13d 479 htab_t dependencies = nullptr;
10b3939b 480
cb1df416 481 /* Header data from the line table, during full symbol processing. */
fcd3b13d 482 struct line_header *line_header = nullptr;
4c8aa72d
PA
483 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
484 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
485 this is the DW_TAG_compile_unit die for this CU. We'll hold on
486 to the line header as long as this DIE is being processed. See
487 process_die_scope. */
fcd3b13d 488 die_info *line_header_die_owner = nullptr;
cb1df416 489
3da10d80
KS
490 /* A list of methods which need to have physnames computed
491 after all type information has been read. */
c89b44cd 492 std::vector<delayed_method_info> method_list;
3da10d80 493
96408a79 494 /* To be copied to symtab->call_site_htab. */
fcd3b13d 495 htab_t call_site_htab = nullptr;
96408a79 496
034e5797
DE
497 /* Non-NULL if this CU came from a DWO file.
498 There is an invariant here that is important to remember:
499 Except for attributes copied from the top level DIE in the "main"
500 (or "stub") file in preparation for reading the DWO file
501 (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
502 Either there isn't a DWO file (in which case this is NULL and the point
503 is moot), or there is and either we're not going to read it (in which
504 case this is NULL) or there is and we are reading it (in which case this
505 is non-NULL). */
fcd3b13d 506 struct dwo_unit *dwo_unit = nullptr;
3019eac3
DE
507
508 /* The DW_AT_addr_base attribute if present, zero otherwise
509 (zero is a valid value though).
1dbab08b 510 Note this value comes from the Fission stub CU/TU's DIE. */
fcd3b13d 511 ULONGEST addr_base = 0;
3019eac3 512
2e3cf129
DE
513 /* The DW_AT_ranges_base attribute if present, zero otherwise
514 (zero is a valid value though).
1dbab08b 515 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 516 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
517 be used without needing to know whether DWO files are in use or not.
518 N.B. This does not apply to DW_AT_ranges appearing in
519 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
520 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
521 DW_AT_ranges_base *would* have to be applied, and we'd have to care
522 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
fcd3b13d 523 ULONGEST ranges_base = 0;
2e3cf129 524
c9317f21
TT
525 /* When reading debug info generated by older versions of rustc, we
526 have to rewrite some union types to be struct types with a
527 variant part. This rewriting must be done after the CU is fully
528 read in, because otherwise at the point of rewriting some struct
529 type might not have been fully processed. So, we keep a list of
530 all such types here and process them after expansion. */
531 std::vector<struct type *> rust_unions;
532
ae038cb0
DJ
533 /* Mark used when releasing cached dies. */
534 unsigned int mark : 1;
535
8be455d7
JK
536 /* This CU references .debug_loc. See the symtab->locations_valid field.
537 This test is imperfect as there may exist optimized debug code not using
538 any location list and still facing inlining issues if handled as
539 unoptimized code. For a future better test see GCC PR other/32998. */
8be455d7 540 unsigned int has_loclist : 1;
ba919b58 541
1b80a9fa
JK
542 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is set
543 if all the producer_is_* fields are valid. This information is cached
544 because profiling CU expansion showed excessive time spent in
545 producer_is_gxx_lt_4_6. */
ba919b58
TT
546 unsigned int checked_producer : 1;
547 unsigned int producer_is_gxx_lt_4_6 : 1;
1b80a9fa 548 unsigned int producer_is_gcc_lt_4_3 : 1;
5230b05a 549 unsigned int producer_is_icc_lt_14 : 1;
4d4ec4e5
TT
550
551 /* When set, the file that we're processing is known to have
552 debugging info for C++ namespaces. GCC 3.3.x did not produce
553 this information, but later versions do. */
554
555 unsigned int processing_has_namespace_info : 1;
d590ff25
YQ
556
557 struct partial_die_info *find_partial_die (sect_offset sect_off);
e7c27a73
DJ
558};
559
094b34ac
DE
560/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
561 This includes type_unit_group and quick_file_names. */
562
563struct stmt_list_hash
564{
565 /* The DWO unit this table is from or NULL if there is none. */
566 struct dwo_unit *dwo_unit;
567
568 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 569 sect_offset line_sect_off;
094b34ac
DE
570};
571
f4dc4d17
DE
572/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
573 an object of this type. */
574
575struct type_unit_group
576{
0186c6a7 577 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
578 To simplify things we create an artificial CU that "includes" all the
579 type units using this stmt_list so that the rest of the code still has
580 a "per_cu" handle on the symtab.
581 This PER_CU is recognized by having no section. */
8a0459fd 582#define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
094b34ac
DE
583 struct dwarf2_per_cu_data per_cu;
584
0186c6a7
DE
585 /* The TUs that share this DW_AT_stmt_list entry.
586 This is added to while parsing type units to build partial symtabs,
587 and is deleted afterwards and not used again. */
588 VEC (sig_type_ptr) *tus;
f4dc4d17 589
43f3e411 590 /* The compunit symtab.
094b34ac 591 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
592 so we create an essentially anonymous symtab as the compunit symtab. */
593 struct compunit_symtab *compunit_symtab;
f4dc4d17 594
094b34ac
DE
595 /* The data used to construct the hash key. */
596 struct stmt_list_hash hash;
f4dc4d17
DE
597
598 /* The number of symtabs from the line header.
599 The value here must match line_header.num_file_names. */
600 unsigned int num_symtabs;
601
602 /* The symbol tables for this TU (obtained from the files listed in
603 DW_AT_stmt_list).
604 WARNING: The order of entries here must match the order of entries
605 in the line header. After the first TU using this type_unit_group, the
606 line header for the subsequent TUs is recreated from this. This is done
607 because we need to use the same symtabs for each TU using the same
608 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
609 there's no guarantee the line header doesn't have duplicate entries. */
610 struct symtab **symtabs;
611};
612
73869dc2 613/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
614
615struct dwo_sections
616{
617 struct dwarf2_section_info abbrev;
3019eac3
DE
618 struct dwarf2_section_info line;
619 struct dwarf2_section_info loc;
43988095 620 struct dwarf2_section_info loclists;
09262596
DE
621 struct dwarf2_section_info macinfo;
622 struct dwarf2_section_info macro;
3019eac3
DE
623 struct dwarf2_section_info str;
624 struct dwarf2_section_info str_offsets;
80626a55
DE
625 /* In the case of a virtual DWO file, these two are unused. */
626 struct dwarf2_section_info info;
3019eac3
DE
627 VEC (dwarf2_section_info_def) *types;
628};
629
c88ee1f0 630/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
631
632struct dwo_unit
633{
634 /* Backlink to the containing struct dwo_file. */
635 struct dwo_file *dwo_file;
636
637 /* The "id" that distinguishes this CU/TU.
638 .debug_info calls this "dwo_id", .debug_types calls this "signature".
639 Since signatures came first, we stick with it for consistency. */
640 ULONGEST signature;
641
642 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 643 struct dwarf2_section_info *section;
3019eac3 644
9c541725
PA
645 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
646 sect_offset sect_off;
3019eac3
DE
647 unsigned int length;
648
649 /* For types, offset in the type's DIE of the type defined by this TU. */
650 cu_offset type_offset_in_tu;
651};
652
73869dc2
DE
653/* include/dwarf2.h defines the DWP section codes.
654 It defines a max value but it doesn't define a min value, which we
655 use for error checking, so provide one. */
656
657enum dwp_v2_section_ids
658{
659 DW_SECT_MIN = 1
660};
661
80626a55 662/* Data for one DWO file.
57d63ce2
DE
663
664 This includes virtual DWO files (a virtual DWO file is a DWO file as it
665 appears in a DWP file). DWP files don't really have DWO files per se -
666 comdat folding of types "loses" the DWO file they came from, and from
667 a high level view DWP files appear to contain a mass of random types.
668 However, to maintain consistency with the non-DWP case we pretend DWP
669 files contain virtual DWO files, and we assign each TU with one virtual
670 DWO file (generally based on the line and abbrev section offsets -
671 a heuristic that seems to work in practice). */
3019eac3
DE
672
673struct dwo_file
674{
0ac5b59e 675 /* The DW_AT_GNU_dwo_name attribute.
80626a55
DE
676 For virtual DWO files the name is constructed from the section offsets
677 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
678 from related CU+TUs. */
0ac5b59e
DE
679 const char *dwo_name;
680
681 /* The DW_AT_comp_dir attribute. */
682 const char *comp_dir;
3019eac3 683
80626a55
DE
684 /* The bfd, when the file is open. Otherwise this is NULL.
685 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
686 bfd *dbfd;
3019eac3 687
73869dc2
DE
688 /* The sections that make up this DWO file.
689 Remember that for virtual DWO files in DWP V2, these are virtual
690 sections (for lack of a better name). */
3019eac3
DE
691 struct dwo_sections sections;
692
33c5cd75
DB
693 /* The CUs in the file.
694 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
695 an extension to handle LLVM's Link Time Optimization output (where
696 multiple source files may be compiled into a single object/dwo pair). */
697 htab_t cus;
3019eac3
DE
698
699 /* Table of TUs in the file.
700 Each element is a struct dwo_unit. */
701 htab_t tus;
702};
703
80626a55
DE
704/* These sections are what may appear in a DWP file. */
705
706struct dwp_sections
707{
73869dc2 708 /* These are used by both DWP version 1 and 2. */
80626a55
DE
709 struct dwarf2_section_info str;
710 struct dwarf2_section_info cu_index;
711 struct dwarf2_section_info tu_index;
73869dc2
DE
712
713 /* These are only used by DWP version 2 files.
714 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
715 sections are referenced by section number, and are not recorded here.
716 In DWP version 2 there is at most one copy of all these sections, each
717 section being (effectively) comprised of the concatenation of all of the
718 individual sections that exist in the version 1 format.
719 To keep the code simple we treat each of these concatenated pieces as a
720 section itself (a virtual section?). */
721 struct dwarf2_section_info abbrev;
722 struct dwarf2_section_info info;
723 struct dwarf2_section_info line;
724 struct dwarf2_section_info loc;
725 struct dwarf2_section_info macinfo;
726 struct dwarf2_section_info macro;
727 struct dwarf2_section_info str_offsets;
728 struct dwarf2_section_info types;
80626a55
DE
729};
730
73869dc2
DE
731/* These sections are what may appear in a virtual DWO file in DWP version 1.
732 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 733
73869dc2 734struct virtual_v1_dwo_sections
80626a55
DE
735{
736 struct dwarf2_section_info abbrev;
737 struct dwarf2_section_info line;
738 struct dwarf2_section_info loc;
739 struct dwarf2_section_info macinfo;
740 struct dwarf2_section_info macro;
741 struct dwarf2_section_info str_offsets;
742 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 743 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
744 struct dwarf2_section_info info_or_types;
745};
746
73869dc2
DE
747/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
748 In version 2, the sections of the DWO files are concatenated together
749 and stored in one section of that name. Thus each ELF section contains
750 several "virtual" sections. */
751
752struct virtual_v2_dwo_sections
753{
754 bfd_size_type abbrev_offset;
755 bfd_size_type abbrev_size;
756
757 bfd_size_type line_offset;
758 bfd_size_type line_size;
759
760 bfd_size_type loc_offset;
761 bfd_size_type loc_size;
762
763 bfd_size_type macinfo_offset;
764 bfd_size_type macinfo_size;
765
766 bfd_size_type macro_offset;
767 bfd_size_type macro_size;
768
769 bfd_size_type str_offsets_offset;
770 bfd_size_type str_offsets_size;
771
772 /* Each DWP hash table entry records one CU or one TU.
773 That is recorded here, and copied to dwo_unit.section. */
774 bfd_size_type info_or_types_offset;
775 bfd_size_type info_or_types_size;
776};
777
80626a55
DE
778/* Contents of DWP hash tables. */
779
780struct dwp_hash_table
781{
73869dc2 782 uint32_t version, nr_columns;
80626a55 783 uint32_t nr_units, nr_slots;
73869dc2
DE
784 const gdb_byte *hash_table, *unit_table;
785 union
786 {
787 struct
788 {
789 const gdb_byte *indices;
790 } v1;
791 struct
792 {
793 /* This is indexed by column number and gives the id of the section
794 in that column. */
795#define MAX_NR_V2_DWO_SECTIONS \
796 (1 /* .debug_info or .debug_types */ \
797 + 1 /* .debug_abbrev */ \
798 + 1 /* .debug_line */ \
799 + 1 /* .debug_loc */ \
800 + 1 /* .debug_str_offsets */ \
801 + 1 /* .debug_macro or .debug_macinfo */)
802 int section_ids[MAX_NR_V2_DWO_SECTIONS];
803 const gdb_byte *offsets;
804 const gdb_byte *sizes;
805 } v2;
806 } section_pool;
80626a55
DE
807};
808
809/* Data for one DWP file. */
810
811struct dwp_file
812{
400174b1
TT
813 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
814 : name (name_),
815 dbfd (std::move (abfd))
816 {
817 }
818
80626a55
DE
819 /* Name of the file. */
820 const char *name;
821
73869dc2 822 /* File format version. */
400174b1 823 int version = 0;
73869dc2 824
93417882 825 /* The bfd. */
400174b1 826 gdb_bfd_ref_ptr dbfd;
80626a55
DE
827
828 /* Section info for this file. */
400174b1 829 struct dwp_sections sections {};
80626a55 830
57d63ce2 831 /* Table of CUs in the file. */
400174b1 832 const struct dwp_hash_table *cus = nullptr;
80626a55
DE
833
834 /* Table of TUs in the file. */
400174b1 835 const struct dwp_hash_table *tus = nullptr;
80626a55 836
19ac8c2e 837 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
400174b1
TT
838 htab_t loaded_cus {};
839 htab_t loaded_tus {};
80626a55 840
73869dc2
DE
841 /* Table to map ELF section numbers to their sections.
842 This is only needed for the DWP V1 file format. */
400174b1
TT
843 unsigned int num_sections = 0;
844 asection **elf_sections = nullptr;
80626a55
DE
845};
846
36586728
TT
847/* This represents a '.dwz' file. */
848
849struct dwz_file
850{
7ff8cb8c
TT
851 dwz_file (gdb_bfd_ref_ptr &&bfd)
852 : dwz_bfd (std::move (bfd))
853 {
854 }
855
36586728 856 /* A dwz file can only contain a few sections. */
7ff8cb8c
TT
857 struct dwarf2_section_info abbrev {};
858 struct dwarf2_section_info info {};
859 struct dwarf2_section_info str {};
860 struct dwarf2_section_info line {};
861 struct dwarf2_section_info macro {};
862 struct dwarf2_section_info gdb_index {};
863 struct dwarf2_section_info debug_names {};
36586728
TT
864
865 /* The dwz's BFD. */
7ff8cb8c 866 gdb_bfd_ref_ptr dwz_bfd;
36586728
TT
867};
868
0963b4bd
MS
869/* Struct used to pass misc. parameters to read_die_and_children, et
870 al. which are used for both .debug_info and .debug_types dies.
871 All parameters here are unchanging for the life of the call. This
dee91e82 872 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
873
874struct die_reader_specs
875{
a32a8923 876 /* The bfd of die_section. */
93311388
DE
877 bfd* abfd;
878
879 /* The CU of the DIE we are parsing. */
880 struct dwarf2_cu *cu;
881
80626a55 882 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
883 struct dwo_file *dwo_file;
884
dee91e82 885 /* The section the die comes from.
3019eac3 886 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
887 struct dwarf2_section_info *die_section;
888
889 /* die_section->buffer. */
d521ce57 890 const gdb_byte *buffer;
f664829e
DE
891
892 /* The end of the buffer. */
893 const gdb_byte *buffer_end;
a2ce51a0
DE
894
895 /* The value of the DW_AT_comp_dir attribute. */
896 const char *comp_dir;
685af9cd
TT
897
898 /* The abbreviation table to use when reading the DIEs. */
899 struct abbrev_table *abbrev_table;
93311388
DE
900};
901
fd820528 902/* Type of function passed to init_cutu_and_read_dies, et.al. */
dee91e82 903typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
d521ce57 904 const gdb_byte *info_ptr,
dee91e82
DE
905 struct die_info *comp_unit_die,
906 int has_children,
907 void *data);
908
ecfb656c
PA
909/* A 1-based directory index. This is a strong typedef to prevent
910 accidentally using a directory index as a 0-based index into an
911 array/vector. */
912enum class dir_index : unsigned int {};
913
914/* Likewise, a 1-based file name index. */
915enum class file_name_index : unsigned int {};
916
52059ffd
TT
917struct file_entry
918{
fff8551c
PA
919 file_entry () = default;
920
ecfb656c 921 file_entry (const char *name_, dir_index d_index_,
fff8551c
PA
922 unsigned int mod_time_, unsigned int length_)
923 : name (name_),
ecfb656c 924 d_index (d_index_),
fff8551c
PA
925 mod_time (mod_time_),
926 length (length_)
927 {}
928
ecfb656c
PA
929 /* Return the include directory at D_INDEX stored in LH. Returns
930 NULL if D_INDEX is out of bounds. */
8c43009f
PA
931 const char *include_dir (const line_header *lh) const;
932
fff8551c
PA
933 /* The file name. Note this is an observing pointer. The memory is
934 owned by debug_line_buffer. */
935 const char *name {};
936
8c43009f 937 /* The directory index (1-based). */
ecfb656c 938 dir_index d_index {};
fff8551c
PA
939
940 unsigned int mod_time {};
941
942 unsigned int length {};
943
944 /* True if referenced by the Line Number Program. */
945 bool included_p {};
946
83769d0b 947 /* The associated symbol table, if any. */
fff8551c 948 struct symtab *symtab {};
52059ffd
TT
949};
950
debd256d
JB
951/* The line number information for a compilation unit (found in the
952 .debug_line section) begins with a "statement program header",
953 which contains the following information. */
954struct line_header
955{
fff8551c
PA
956 line_header ()
957 : offset_in_dwz {}
958 {}
959
960 /* Add an entry to the include directory table. */
961 void add_include_dir (const char *include_dir);
962
963 /* Add an entry to the file name table. */
ecfb656c 964 void add_file_name (const char *name, dir_index d_index,
fff8551c
PA
965 unsigned int mod_time, unsigned int length);
966
ecfb656c 967 /* Return the include dir at INDEX (1-based). Returns NULL if INDEX
8c43009f 968 is out of bounds. */
ecfb656c 969 const char *include_dir_at (dir_index index) const
8c43009f 970 {
ecfb656c
PA
971 /* Convert directory index number (1-based) to vector index
972 (0-based). */
973 size_t vec_index = to_underlying (index) - 1;
974
975 if (vec_index >= include_dirs.size ())
8c43009f 976 return NULL;
ecfb656c 977 return include_dirs[vec_index];
8c43009f
PA
978 }
979
ecfb656c 980 /* Return the file name at INDEX (1-based). Returns NULL if INDEX
8c43009f 981 is out of bounds. */
ecfb656c 982 file_entry *file_name_at (file_name_index index)
8c43009f 983 {
ecfb656c
PA
984 /* Convert file name index number (1-based) to vector index
985 (0-based). */
986 size_t vec_index = to_underlying (index) - 1;
987
988 if (vec_index >= file_names.size ())
fff8551c 989 return NULL;
ecfb656c 990 return &file_names[vec_index];
fff8551c
PA
991 }
992
993 /* Const version of the above. */
994 const file_entry *file_name_at (unsigned int index) const
995 {
996 if (index >= file_names.size ())
8c43009f
PA
997 return NULL;
998 return &file_names[index];
999 }
1000
527f3840 1001 /* Offset of line number information in .debug_line section. */
9c541725 1002 sect_offset sect_off {};
527f3840
JK
1003
1004 /* OFFSET is for struct dwz_file associated with dwarf2_per_objfile. */
fff8551c
PA
1005 unsigned offset_in_dwz : 1; /* Can't initialize bitfields in-class. */
1006
1007 unsigned int total_length {};
1008 unsigned short version {};
1009 unsigned int header_length {};
1010 unsigned char minimum_instruction_length {};
1011 unsigned char maximum_ops_per_instruction {};
1012 unsigned char default_is_stmt {};
1013 int line_base {};
1014 unsigned char line_range {};
1015 unsigned char opcode_base {};
debd256d
JB
1016
1017 /* standard_opcode_lengths[i] is the number of operands for the
1018 standard opcode whose value is i. This means that
1019 standard_opcode_lengths[0] is unused, and the last meaningful
1020 element is standard_opcode_lengths[opcode_base - 1]. */
fff8551c 1021 std::unique_ptr<unsigned char[]> standard_opcode_lengths;
debd256d 1022
fff8551c
PA
1023 /* The include_directories table. Note these are observing
1024 pointers. The memory is owned by debug_line_buffer. */
1025 std::vector<const char *> include_dirs;
debd256d 1026
fff8551c
PA
1027 /* The file_names table. */
1028 std::vector<file_entry> file_names;
debd256d
JB
1029
1030 /* The start and end of the statement program following this
6502dd73 1031 header. These point into dwarf2_per_objfile->line_buffer. */
fff8551c 1032 const gdb_byte *statement_program_start {}, *statement_program_end {};
debd256d 1033};
c906108c 1034
fff8551c
PA
1035typedef std::unique_ptr<line_header> line_header_up;
1036
8c43009f
PA
1037const char *
1038file_entry::include_dir (const line_header *lh) const
1039{
ecfb656c 1040 return lh->include_dir_at (d_index);
8c43009f
PA
1041}
1042
c906108c 1043/* When we construct a partial symbol table entry we only
0963b4bd 1044 need this much information. */
6f06d47b 1045struct partial_die_info : public allocate_on_obstack
c906108c 1046 {
6f06d47b
YQ
1047 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1048
1049 /* Disable assign but still keep copy ctor, which is needed
1050 load_partial_dies. */
1051 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1052
52356b79
YQ
1053 /* Adjust the partial die before generating a symbol for it. This
1054 function may set the is_external flag or change the DIE's
1055 name. */
1056 void fixup (struct dwarf2_cu *cu);
1057
48fbe735
YQ
1058 /* Read a minimal amount of information into the minimal die
1059 structure. */
1060 const gdb_byte *read (const struct die_reader_specs *reader,
1061 const struct abbrev_info &abbrev,
1062 const gdb_byte *info_ptr);
1063
72bf9492 1064 /* Offset of this DIE. */
6f06d47b 1065 const sect_offset sect_off;
72bf9492
DJ
1066
1067 /* DWARF-2 tag for this DIE. */
6f06d47b 1068 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 1069
72bf9492 1070 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
1071 const unsigned int has_children : 1;
1072
72bf9492
DJ
1073 unsigned int is_external : 1;
1074 unsigned int is_declaration : 1;
1075 unsigned int has_type : 1;
1076 unsigned int has_specification : 1;
1077 unsigned int has_pc_info : 1;
481860b3 1078 unsigned int may_be_inlined : 1;
72bf9492 1079
0c1b455e
TT
1080 /* This DIE has been marked DW_AT_main_subprogram. */
1081 unsigned int main_subprogram : 1;
1082
72bf9492
DJ
1083 /* Flag set if the SCOPE field of this structure has been
1084 computed. */
1085 unsigned int scope_set : 1;
1086
fa4028e9
JB
1087 /* Flag set if the DIE has a byte_size attribute. */
1088 unsigned int has_byte_size : 1;
1089
ff908ebf
AW
1090 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1091 unsigned int has_const_value : 1;
1092
98bfdba5
PA
1093 /* Flag set if any of the DIE's children are template arguments. */
1094 unsigned int has_template_arguments : 1;
1095
52356b79 1096 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
1097 unsigned int fixup_called : 1;
1098
36586728
TT
1099 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1100 unsigned int is_dwz : 1;
1101
1102 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1103 unsigned int spec_is_dwz : 1;
1104
72bf9492 1105 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1106 sometimes a default name for unnamed DIEs. */
6f06d47b 1107 const char *name = nullptr;
72bf9492 1108
abc72ce4 1109 /* The linkage name, if present. */
6f06d47b 1110 const char *linkage_name = nullptr;
abc72ce4 1111
72bf9492
DJ
1112 /* The scope to prepend to our children. This is generally
1113 allocated on the comp_unit_obstack, so will disappear
1114 when this compilation unit leaves the cache. */
6f06d47b 1115 const char *scope = nullptr;
72bf9492 1116
95554aad
TT
1117 /* Some data associated with the partial DIE. The tag determines
1118 which field is live. */
1119 union
1120 {
1121 /* The location description associated with this DIE, if any. */
1122 struct dwarf_block *locdesc;
1123 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1124 sect_offset sect_off;
6f06d47b 1125 } d {};
72bf9492
DJ
1126
1127 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
1128 CORE_ADDR lowpc = 0;
1129 CORE_ADDR highpc = 0;
72bf9492 1130
93311388 1131 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1132 DW_AT_sibling, if any. */
48fbe735
YQ
1133 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1134 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1135 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1136
1137 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1138 DW_AT_specification (or DW_AT_abstract_origin or
1139 DW_AT_extension). */
6f06d47b 1140 sect_offset spec_offset {};
72bf9492
DJ
1141
1142 /* Pointers to this DIE's parent, first child, and next sibling,
1143 if any. */
6f06d47b
YQ
1144 struct partial_die_info *die_parent = nullptr;
1145 struct partial_die_info *die_child = nullptr;
1146 struct partial_die_info *die_sibling = nullptr;
1147
1148 friend struct partial_die_info *
1149 dwarf2_cu::find_partial_die (sect_offset sect_off);
1150
1151 private:
1152 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1153 partial_die_info (sect_offset sect_off)
1154 : partial_die_info (sect_off, DW_TAG_padding, 0)
1155 {
1156 }
1157
1158 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1159 int has_children_)
1160 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1161 {
1162 is_external = 0;
1163 is_declaration = 0;
1164 has_type = 0;
1165 has_specification = 0;
1166 has_pc_info = 0;
1167 may_be_inlined = 0;
1168 main_subprogram = 0;
1169 scope_set = 0;
1170 has_byte_size = 0;
1171 has_const_value = 0;
1172 has_template_arguments = 0;
1173 fixup_called = 0;
1174 is_dwz = 0;
1175 spec_is_dwz = 0;
1176 }
c906108c
SS
1177 };
1178
0963b4bd 1179/* This data structure holds the information of an abbrev. */
c906108c
SS
1180struct abbrev_info
1181 {
1182 unsigned int number; /* number identifying abbrev */
1183 enum dwarf_tag tag; /* dwarf tag */
f3dd6933
DJ
1184 unsigned short has_children; /* boolean */
1185 unsigned short num_attrs; /* number of attributes */
c906108c
SS
1186 struct attr_abbrev *attrs; /* an array of attribute descriptions */
1187 struct abbrev_info *next; /* next in chain */
1188 };
1189
1190struct attr_abbrev
1191 {
9d25dd43
DE
1192 ENUM_BITFIELD(dwarf_attribute) name : 16;
1193 ENUM_BITFIELD(dwarf_form) form : 16;
43988095
JK
1194
1195 /* It is valid only if FORM is DW_FORM_implicit_const. */
1196 LONGEST implicit_const;
c906108c
SS
1197 };
1198
433df2d4
DE
1199/* Size of abbrev_table.abbrev_hash_table. */
1200#define ABBREV_HASH_SIZE 121
1201
1202/* Top level data structure to contain an abbreviation table. */
1203
1204struct abbrev_table
1205{
685af9cd
TT
1206 explicit abbrev_table (sect_offset off)
1207 : sect_off (off)
1208 {
4a17f768 1209 m_abbrevs =
685af9cd 1210 XOBNEWVEC (&abbrev_obstack, struct abbrev_info *, ABBREV_HASH_SIZE);
4a17f768 1211 memset (m_abbrevs, 0, ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
685af9cd
TT
1212 }
1213
1214 DISABLE_COPY_AND_ASSIGN (abbrev_table);
1215
1216 /* Allocate space for a struct abbrev_info object in
1217 ABBREV_TABLE. */
1218 struct abbrev_info *alloc_abbrev ();
1219
1220 /* Add an abbreviation to the table. */
1221 void add_abbrev (unsigned int abbrev_number, struct abbrev_info *abbrev);
1222
1223 /* Look up an abbrev in the table.
1224 Returns NULL if the abbrev is not found. */
1225
1226 struct abbrev_info *lookup_abbrev (unsigned int abbrev_number);
1227
1228
f4dc4d17
DE
1229 /* Where the abbrev table came from.
1230 This is used as a sanity check when the table is used. */
685af9cd 1231 const sect_offset sect_off;
433df2d4
DE
1232
1233 /* Storage for the abbrev table. */
685af9cd 1234 auto_obstack abbrev_obstack;
433df2d4 1235
4a17f768
YQ
1236private:
1237
433df2d4
DE
1238 /* Hash table of abbrevs.
1239 This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
1240 It could be statically allocated, but the previous code didn't so we
1241 don't either. */
4a17f768 1242 struct abbrev_info **m_abbrevs;
433df2d4
DE
1243};
1244
685af9cd
TT
1245typedef std::unique_ptr<struct abbrev_table> abbrev_table_up;
1246
0963b4bd 1247/* Attributes have a name and a value. */
b60c80d6
DJ
1248struct attribute
1249 {
9d25dd43 1250 ENUM_BITFIELD(dwarf_attribute) name : 16;
8285870a
JK
1251 ENUM_BITFIELD(dwarf_form) form : 15;
1252
1253 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
1254 field should be in u.str (existing only for DW_STRING) but it is kept
1255 here for better struct attribute alignment. */
1256 unsigned int string_is_canonical : 1;
1257
b60c80d6
DJ
1258 union
1259 {
15d034d0 1260 const char *str;
b60c80d6 1261 struct dwarf_block *blk;
43bbcdc2
PH
1262 ULONGEST unsnd;
1263 LONGEST snd;
b60c80d6 1264 CORE_ADDR addr;
ac9ec31b 1265 ULONGEST signature;
b60c80d6
DJ
1266 }
1267 u;
1268 };
1269
0963b4bd 1270/* This data structure holds a complete die structure. */
c906108c
SS
1271struct die_info
1272 {
76815b17
DE
1273 /* DWARF-2 tag for this DIE. */
1274 ENUM_BITFIELD(dwarf_tag) tag : 16;
1275
1276 /* Number of attributes */
98bfdba5
PA
1277 unsigned char num_attrs;
1278
1279 /* True if we're presently building the full type name for the
1280 type derived from this DIE. */
1281 unsigned char building_fullname : 1;
76815b17 1282
adde2bff
DE
1283 /* True if this die is in process. PR 16581. */
1284 unsigned char in_process : 1;
1285
76815b17
DE
1286 /* Abbrev number */
1287 unsigned int abbrev;
1288
93311388 1289 /* Offset in .debug_info or .debug_types section. */
9c541725 1290 sect_offset sect_off;
78ba4af6
JB
1291
1292 /* The dies in a compilation unit form an n-ary tree. PARENT
1293 points to this die's parent; CHILD points to the first child of
1294 this node; and all the children of a given node are chained
4950bc1c 1295 together via their SIBLING fields. */
639d11d3
DC
1296 struct die_info *child; /* Its first child, if any. */
1297 struct die_info *sibling; /* Its next sibling, if any. */
1298 struct die_info *parent; /* Its parent, if any. */
c906108c 1299
b60c80d6
DJ
1300 /* An array of attributes, with NUM_ATTRS elements. There may be
1301 zero, but it's not common and zero-sized arrays are not
1302 sufficiently portable C. */
1303 struct attribute attrs[1];
c906108c
SS
1304 };
1305
0963b4bd 1306/* Get at parts of an attribute structure. */
c906108c
SS
1307
1308#define DW_STRING(attr) ((attr)->u.str)
8285870a 1309#define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
c906108c
SS
1310#define DW_UNSND(attr) ((attr)->u.unsnd)
1311#define DW_BLOCK(attr) ((attr)->u.blk)
1312#define DW_SND(attr) ((attr)->u.snd)
1313#define DW_ADDR(attr) ((attr)->u.addr)
ac9ec31b 1314#define DW_SIGNATURE(attr) ((attr)->u.signature)
c906108c 1315
0963b4bd 1316/* Blocks are a bunch of untyped bytes. */
c906108c
SS
1317struct dwarf_block
1318 {
56eb65bd 1319 size_t size;
1d6edc3c
JK
1320
1321 /* Valid only if SIZE is not zero. */
d521ce57 1322 const gdb_byte *data;
c906108c
SS
1323 };
1324
c906108c
SS
1325#ifndef ATTR_ALLOC_CHUNK
1326#define ATTR_ALLOC_CHUNK 4
1327#endif
1328
c906108c
SS
1329/* Allocate fields for structs, unions and enums in this size. */
1330#ifndef DW_FIELD_ALLOC_CHUNK
1331#define DW_FIELD_ALLOC_CHUNK 4
1332#endif
1333
c906108c
SS
1334/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1335 but this would require a corresponding change in unpack_field_as_long
1336 and friends. */
1337static int bits_per_byte = 8;
1338
2ddeaf8a
TT
1339/* When reading a variant or variant part, we track a bit more
1340 information about the field, and store it in an object of this
1341 type. */
1342
1343struct variant_field
1344{
1345 /* If we see a DW_TAG_variant, then this will be the discriminant
1346 value. */
1347 ULONGEST discriminant_value;
1348 /* If we see a DW_TAG_variant, then this will be set if this is the
1349 default branch. */
1350 bool default_branch;
1351 /* While reading a DW_TAG_variant_part, this will be set if this
1352 field is the discriminant. */
1353 bool is_discriminant;
1354};
1355
52059ffd
TT
1356struct nextfield
1357{
be2daae6
TT
1358 int accessibility = 0;
1359 int virtuality = 0;
2ddeaf8a 1360 /* Extra information to describe a variant or variant part. */
be2daae6
TT
1361 struct variant_field variant {};
1362 struct field field {};
52059ffd
TT
1363};
1364
1365struct fnfieldlist
1366{
be2daae6
TT
1367 const char *name = nullptr;
1368 std::vector<struct fn_field> fnfields;
52059ffd
TT
1369};
1370
c906108c
SS
1371/* The routines that read and process dies for a C struct or C++ class
1372 pass lists of data member fields and lists of member function fields
1373 in an instance of a field_info structure, as defined below. */
1374struct field_info
c5aa993b 1375 {
0963b4bd 1376 /* List of data member and baseclasses fields. */
be2daae6
TT
1377 std::vector<struct nextfield> fields;
1378 std::vector<struct nextfield> baseclasses;
c906108c 1379
7d0ccb61 1380 /* Number of fields (including baseclasses). */
be2daae6 1381 int nfields = 0;
c906108c 1382
c5aa993b 1383 /* Set if the accesibility of one of the fields is not public. */
be2daae6 1384 int non_public_fields = 0;
c906108c 1385
c5aa993b
JM
1386 /* Member function fieldlist array, contains name of possibly overloaded
1387 member function, number of overloaded member functions and a pointer
1388 to the head of the member function field chain. */
be2daae6 1389 std::vector<struct fnfieldlist> fnfieldlists;
98751a41
JK
1390
1391 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1392 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
be2daae6 1393 std::vector<struct decl_field> typedef_field_list;
883fd55a
KS
1394
1395 /* Nested types defined by this class and the number of elements in this
1396 list. */
be2daae6 1397 std::vector<struct decl_field> nested_types_list;
c5aa993b 1398 };
c906108c 1399
10b3939b
DJ
1400/* One item on the queue of compilation units to read in full symbols
1401 for. */
1402struct dwarf2_queue_item
1403{
1404 struct dwarf2_per_cu_data *per_cu;
95554aad 1405 enum language pretend_language;
10b3939b
DJ
1406 struct dwarf2_queue_item *next;
1407};
1408
1409/* The current queue. */
1410static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
1411
ae038cb0
DJ
1412/* Loaded secondary compilation units are kept in memory until they
1413 have not been referenced for the processing of this many
1414 compilation units. Set this to zero to disable caching. Cache
1415 sizes of up to at least twenty will improve startup time for
1416 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1417static int dwarf_max_cache_age = 5;
920d2a44 1418static void
b4f54984
DE
1419show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1420 struct cmd_list_element *c, const char *value)
920d2a44 1421{
3e43a32a 1422 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1423 "DWARF compilation units is %s.\n"),
920d2a44
AC
1424 value);
1425}
4390d890 1426\f
c906108c
SS
1427/* local function prototypes */
1428
a32a8923
DE
1429static const char *get_section_name (const struct dwarf2_section_info *);
1430
1431static const char *get_section_file_name (const struct dwarf2_section_info *);
1432
918dd910
JK
1433static void dwarf2_find_base_address (struct die_info *die,
1434 struct dwarf2_cu *cu);
1435
0018ea6f
DE
1436static struct partial_symtab *create_partial_symtab
1437 (struct dwarf2_per_cu_data *per_cu, const char *name);
1438
f1902523
JK
1439static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1440 const gdb_byte *info_ptr,
1441 struct die_info *type_unit_die,
1442 int has_children, void *data);
1443
ed2dc618
SM
1444static void dwarf2_build_psymtabs_hard
1445 (struct dwarf2_per_objfile *dwarf2_per_objfile);
c906108c 1446
72bf9492
DJ
1447static void scan_partial_symbols (struct partial_die_info *,
1448 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1449 int, struct dwarf2_cu *);
c906108c 1450
72bf9492
DJ
1451static void add_partial_symbol (struct partial_die_info *,
1452 struct dwarf2_cu *);
63d06c5c 1453
72bf9492
DJ
1454static void add_partial_namespace (struct partial_die_info *pdi,
1455 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1456 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1457
5d7cb8df 1458static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1459 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1460 struct dwarf2_cu *cu);
1461
72bf9492
DJ
1462static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1463 struct dwarf2_cu *cu);
91c24f0a 1464
bc30ff58
JB
1465static void add_partial_subprogram (struct partial_die_info *pdi,
1466 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1467 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1468
257e7a09
YQ
1469static void dwarf2_read_symtab (struct partial_symtab *,
1470 struct objfile *);
c906108c 1471
a14ed312 1472static void psymtab_to_symtab_1 (struct partial_symtab *);
c906108c 1473
685af9cd 1474static abbrev_table_up abbrev_table_read_table
ed2dc618
SM
1475 (struct dwarf2_per_objfile *dwarf2_per_objfile, struct dwarf2_section_info *,
1476 sect_offset);
433df2d4 1477
d521ce57 1478static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1479
dee91e82 1480static struct partial_die_info *load_partial_dies
d521ce57 1481 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1482
36586728 1483static struct partial_die_info *find_partial_die (sect_offset, int,
10b3939b 1484 struct dwarf2_cu *);
72bf9492 1485
d521ce57
TT
1486static const gdb_byte *read_attribute (const struct die_reader_specs *,
1487 struct attribute *, struct attr_abbrev *,
1488 const gdb_byte *);
a8329558 1489
a1855c1d 1490static unsigned int read_1_byte (bfd *, const gdb_byte *);
c906108c 1491
a1855c1d 1492static int read_1_signed_byte (bfd *, const gdb_byte *);
c906108c 1493
a1855c1d 1494static unsigned int read_2_bytes (bfd *, const gdb_byte *);
c906108c 1495
a1855c1d 1496static unsigned int read_4_bytes (bfd *, const gdb_byte *);
c906108c 1497
a1855c1d 1498static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
c906108c 1499
d521ce57 1500static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
891d2f0b 1501 unsigned int *);
c906108c 1502
d521ce57 1503static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
c764a876
DE
1504
1505static LONGEST read_checked_initial_length_and_offset
d521ce57 1506 (bfd *, const gdb_byte *, const struct comp_unit_head *,
c764a876 1507 unsigned int *, unsigned int *);
613e1657 1508
d521ce57
TT
1509static LONGEST read_offset (bfd *, const gdb_byte *,
1510 const struct comp_unit_head *,
c764a876
DE
1511 unsigned int *);
1512
d521ce57 1513static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
613e1657 1514
ed2dc618
SM
1515static sect_offset read_abbrev_offset
1516 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1517 struct dwarf2_section_info *, sect_offset);
f4dc4d17 1518
d521ce57 1519static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
c906108c 1520
d521ce57 1521static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
c906108c 1522
ed2dc618
SM
1523static const char *read_indirect_string
1524 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1525 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1526
ed2dc618
SM
1527static const char *read_indirect_line_string
1528 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1529 const struct comp_unit_head *, unsigned int *);
36586728 1530
ed2dc618
SM
1531static const char *read_indirect_string_at_offset
1532 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
1533 LONGEST str_offset);
927aa2e7 1534
ed2dc618
SM
1535static const char *read_indirect_string_from_dwz
1536 (struct objfile *objfile, struct dwz_file *, LONGEST);
c906108c 1537
d521ce57 1538static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
c906108c 1539
d521ce57
TT
1540static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1541 const gdb_byte *,
3019eac3
DE
1542 unsigned int *);
1543
d521ce57 1544static const char *read_str_index (const struct die_reader_specs *reader,
342587c4 1545 ULONGEST str_index);
3019eac3 1546
e142c38c 1547static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1548
e142c38c
DJ
1549static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1550 struct dwarf2_cu *);
c906108c 1551
348e048f 1552static struct attribute *dwarf2_attr_no_follow (struct die_info *,
45e58e77 1553 unsigned int);
348e048f 1554
7d45c7c3
KB
1555static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1556 struct dwarf2_cu *cu);
1557
05cf31d1
JB
1558static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1559 struct dwarf2_cu *cu);
1560
e142c38c 1561static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1562
e142c38c 1563static struct die_info *die_specification (struct die_info *die,
f2f0e013 1564 struct dwarf2_cu **);
63d06c5c 1565
9c541725 1566static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1567 struct dwarf2_cu *cu);
debd256d 1568
f3f5162e 1569static void dwarf_decode_lines (struct line_header *, const char *,
c3b7b696 1570 struct dwarf2_cu *, struct partial_symtab *,
527f3840 1571 CORE_ADDR, int decode_mapping);
c906108c 1572
4d663531 1573static void dwarf2_start_subfile (const char *, const char *);
c906108c 1574
43f3e411
DE
1575static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
1576 const char *, const char *,
1577 CORE_ADDR);
f4dc4d17 1578
a14ed312 1579static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1580 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1581
ff39bb5e 1582static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1583 struct dwarf2_cu *);
c906108c 1584
ff39bb5e 1585static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1586 struct type *type,
1587 const char *name,
1588 struct obstack *obstack,
12df843f 1589 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1590 const gdb_byte **bytes,
98bfdba5 1591 struct dwarf2_locexpr_baton **baton);
2df3850c 1592
e7c27a73 1593static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1594
b4ba55a1
JB
1595static int need_gnat_info (struct dwarf2_cu *);
1596
3e43a32a
MS
1597static struct type *die_descriptive_type (struct die_info *,
1598 struct dwarf2_cu *);
b4ba55a1
JB
1599
1600static void set_descriptive_type (struct type *, struct die_info *,
1601 struct dwarf2_cu *);
1602
e7c27a73
DJ
1603static struct type *die_containing_type (struct die_info *,
1604 struct dwarf2_cu *);
c906108c 1605
ff39bb5e 1606static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1607 struct dwarf2_cu *);
c906108c 1608
f792889a 1609static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1610
673bfd45
DE
1611static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1612
0d5cff50 1613static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1614
6e70227d 1615static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1616 const char *suffix, int physname,
1617 struct dwarf2_cu *cu);
63d06c5c 1618
e7c27a73 1619static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1620
348e048f
DE
1621static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1622
e7c27a73 1623static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1624
e7c27a73 1625static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1626
96408a79
SA
1627static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1628
71a3c369
TT
1629static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1630
ff013f42
JK
1631static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1632 struct dwarf2_cu *, struct partial_symtab *);
1633
3a2b436a 1634/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1635 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1636enum pc_bounds_kind
1637{
e385593e 1638 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1639 PC_BOUNDS_NOT_PRESENT,
1640
e385593e
JK
1641 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1642 were present but they do not form a valid range of PC addresses. */
1643 PC_BOUNDS_INVALID,
1644
3a2b436a
JK
1645 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1646 PC_BOUNDS_RANGES,
1647
1648 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1649 PC_BOUNDS_HIGH_LOW,
1650};
1651
1652static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1653 CORE_ADDR *, CORE_ADDR *,
1654 struct dwarf2_cu *,
1655 struct partial_symtab *);
c906108c 1656
fae299cd
DC
1657static void get_scope_pc_bounds (struct die_info *,
1658 CORE_ADDR *, CORE_ADDR *,
1659 struct dwarf2_cu *);
1660
801e3a5b
JB
1661static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1662 CORE_ADDR, struct dwarf2_cu *);
1663
a14ed312 1664static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1665 struct dwarf2_cu *);
c906108c 1666
a14ed312 1667static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1668 struct type *, struct dwarf2_cu *);
c906108c 1669
a14ed312 1670static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1671 struct die_info *, struct type *,
e7c27a73 1672 struct dwarf2_cu *);
c906108c 1673
a14ed312 1674static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1675 struct type *,
1676 struct dwarf2_cu *);
c906108c 1677
134d01f1 1678static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1679
e7c27a73 1680static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1681
e7c27a73 1682static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1683
5d7cb8df
JK
1684static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1685
22cee43f
PMR
1686static struct using_direct **using_directives (enum language);
1687
27aa8d6a
SW
1688static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1689
74921315
KS
1690static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1691
f55ee35c
JK
1692static struct type *read_module_type (struct die_info *die,
1693 struct dwarf2_cu *cu);
1694
38d518c9 1695static const char *namespace_name (struct die_info *die,
e142c38c 1696 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1697
134d01f1 1698static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1699
e7c27a73 1700static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1701
6e70227d 1702static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1703 struct dwarf2_cu *);
1704
bf6af496 1705static struct die_info *read_die_and_siblings_1
d521ce57 1706 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1707 struct die_info *);
639d11d3 1708
dee91e82 1709static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1710 const gdb_byte *info_ptr,
1711 const gdb_byte **new_info_ptr,
639d11d3
DC
1712 struct die_info *parent);
1713
d521ce57
TT
1714static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1715 struct die_info **, const gdb_byte *,
1716 int *, int);
3019eac3 1717
d521ce57
TT
1718static const gdb_byte *read_full_die (const struct die_reader_specs *,
1719 struct die_info **, const gdb_byte *,
1720 int *);
93311388 1721
e7c27a73 1722static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1723
15d034d0
TT
1724static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
1725 struct obstack *);
71c25dea 1726
15d034d0 1727static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1728
15d034d0 1729static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1730 struct die_info *die,
1731 struct dwarf2_cu *cu);
1732
ca69b9e6
DE
1733static const char *dwarf2_physname (const char *name, struct die_info *die,
1734 struct dwarf2_cu *cu);
1735
e142c38c 1736static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1737 struct dwarf2_cu **);
9219021c 1738
f39c6ffd 1739static const char *dwarf_tag_name (unsigned int);
c906108c 1740
f39c6ffd 1741static const char *dwarf_attr_name (unsigned int);
c906108c 1742
f39c6ffd 1743static const char *dwarf_form_name (unsigned int);
c906108c 1744
a121b7c1 1745static const char *dwarf_bool_name (unsigned int);
c906108c 1746
f39c6ffd 1747static const char *dwarf_type_encoding_name (unsigned int);
c906108c 1748
f9aca02d 1749static struct die_info *sibling_die (struct die_info *);
c906108c 1750
d97bc12b
DE
1751static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1752
1753static void dump_die_for_error (struct die_info *);
1754
1755static void dump_die_1 (struct ui_file *, int level, int max_level,
1756 struct die_info *);
c906108c 1757
d97bc12b 1758/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1759
51545339 1760static void store_in_ref_table (struct die_info *,
10b3939b 1761 struct dwarf2_cu *);
c906108c 1762
ff39bb5e 1763static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
c906108c 1764
ff39bb5e 1765static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
a02abb62 1766
348e048f 1767static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1768 const struct attribute *,
348e048f
DE
1769 struct dwarf2_cu **);
1770
10b3939b 1771static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1772 const struct attribute *,
f2f0e013 1773 struct dwarf2_cu **);
c906108c 1774
348e048f 1775static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1776 const struct attribute *,
348e048f
DE
1777 struct dwarf2_cu **);
1778
ac9ec31b
DE
1779static struct type *get_signatured_type (struct die_info *, ULONGEST,
1780 struct dwarf2_cu *);
1781
1782static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1783 const struct attribute *,
ac9ec31b
DE
1784 struct dwarf2_cu *);
1785
e5fe5e75 1786static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1787
52dc124a 1788static void read_signatured_type (struct signatured_type *);
348e048f 1789
63e43d3a
PMR
1790static int attr_to_dynamic_prop (const struct attribute *attr,
1791 struct die_info *die, struct dwarf2_cu *cu,
1792 struct dynamic_prop *prop);
1793
c906108c
SS
1794/* memory allocation interface */
1795
7b5a2f43 1796static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1797
b60c80d6 1798static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1799
43f3e411 1800static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1801
6e5a29e1 1802static int attr_form_is_block (const struct attribute *);
8e19ed76 1803
6e5a29e1 1804static int attr_form_is_section_offset (const struct attribute *);
3690dd37 1805
6e5a29e1 1806static int attr_form_is_constant (const struct attribute *);
3690dd37 1807
6e5a29e1 1808static int attr_form_is_ref (const struct attribute *);
7771576e 1809
8cf6f0b1
TT
1810static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1811 struct dwarf2_loclist_baton *baton,
ff39bb5e 1812 const struct attribute *attr);
8cf6f0b1 1813
ff39bb5e 1814static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1815 struct symbol *sym,
f1e6e072
TT
1816 struct dwarf2_cu *cu,
1817 int is_block);
4c2df51b 1818
d521ce57
TT
1819static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1820 const gdb_byte *info_ptr,
1821 struct abbrev_info *abbrev);
4bb7a0a7 1822
72bf9492
DJ
1823static hashval_t partial_die_hash (const void *item);
1824
1825static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1826
ae038cb0 1827static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618
SM
1828 (sect_offset sect_off, unsigned int offset_in_dwz,
1829 struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1830
9816fde3 1831static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1832 struct die_info *comp_unit_die,
1833 enum language pretend_language);
93311388 1834
ed2dc618 1835static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1836
dee91e82 1837static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1838
f792889a
DJ
1839static struct type *set_die_type (struct die_info *, struct type *,
1840 struct dwarf2_cu *);
1c379e20 1841
ed2dc618 1842static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1843
ed2dc618 1844static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 1845
58f0c718 1846static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
95554aad 1847 enum language);
10b3939b 1848
95554aad
TT
1849static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1850 enum language);
10b3939b 1851
f4dc4d17
DE
1852static void process_full_type_unit (struct dwarf2_per_cu_data *,
1853 enum language);
1854
10b3939b
DJ
1855static void dwarf2_add_dependence (struct dwarf2_cu *,
1856 struct dwarf2_per_cu_data *);
1857
ae038cb0
DJ
1858static void dwarf2_mark (struct dwarf2_cu *);
1859
1860static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1861
b64f50a1 1862static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1863 struct dwarf2_per_cu_data *);
673bfd45 1864
f792889a 1865static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1866
95554aad
TT
1867static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1868 enum language pretend_language);
1869
ed2dc618 1870static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 1871
b303c6f6
AB
1872/* Class, the destructor of which frees all allocated queue entries. This
1873 will only have work to do if an error was thrown while processing the
1874 dwarf. If no error was thrown then the queue entries should have all
1875 been processed, and freed, as we went along. */
1876
1877class dwarf2_queue_guard
1878{
1879public:
1880 dwarf2_queue_guard () = default;
1881
1882 /* Free any entries remaining on the queue. There should only be
1883 entries left if we hit an error while processing the dwarf. */
1884 ~dwarf2_queue_guard ()
1885 {
1886 struct dwarf2_queue_item *item, *last;
1887
1888 item = dwarf2_queue;
1889 while (item)
1890 {
1891 /* Anything still marked queued is likely to be in an
1892 inconsistent state, so discard it. */
1893 if (item->per_cu->queued)
1894 {
1895 if (item->per_cu->cu != NULL)
1896 free_one_cached_comp_unit (item->per_cu);
1897 item->per_cu->queued = 0;
1898 }
1899
1900 last = item;
1901 item = item->next;
1902 xfree (last);
1903 }
1904
1905 dwarf2_queue = dwarf2_queue_tail = NULL;
1906 }
1907};
1908
d721ba37
PA
1909/* The return type of find_file_and_directory. Note, the enclosed
1910 string pointers are only valid while this object is valid. */
1911
1912struct file_and_directory
1913{
1914 /* The filename. This is never NULL. */
1915 const char *name;
1916
1917 /* The compilation directory. NULL if not known. If we needed to
1918 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1919 points directly to the DW_AT_comp_dir string attribute owned by
1920 the obstack that owns the DIE. */
1921 const char *comp_dir;
1922
1923 /* If we needed to build a new string for comp_dir, this is what
1924 owns the storage. */
1925 std::string comp_dir_storage;
1926};
1927
1928static file_and_directory find_file_and_directory (struct die_info *die,
1929 struct dwarf2_cu *cu);
9291a0cd
TT
1930
1931static char *file_full_name (int file, struct line_header *lh,
1932 const char *comp_dir);
1933
43988095
JK
1934/* Expected enum dwarf_unit_type for read_comp_unit_head. */
1935enum class rcuh_kind { COMPILE, TYPE };
1936
d521ce57 1937static const gdb_byte *read_and_check_comp_unit_head
ed2dc618
SM
1938 (struct dwarf2_per_objfile* dwarf2_per_objfile,
1939 struct comp_unit_head *header,
36586728 1940 struct dwarf2_section_info *section,
d521ce57 1941 struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
43988095 1942 rcuh_kind section_kind);
36586728 1943
fd820528 1944static void init_cutu_and_read_dies
f4dc4d17 1945 (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
58f0c718 1946 int use_existing_cu, int keep, bool skip_partial,
3019eac3
DE
1947 die_reader_func_ftype *die_reader_func, void *data);
1948
dee91e82
DE
1949static void init_cutu_and_read_dies_simple
1950 (struct dwarf2_per_cu_data *this_cu,
1951 die_reader_func_ftype *die_reader_func, void *data);
9291a0cd 1952
673bfd45 1953static htab_t allocate_signatured_type_table (struct objfile *objfile);
1fd400ff 1954
3019eac3
DE
1955static htab_t allocate_dwo_unit_table (struct objfile *objfile);
1956
57d63ce2 1957static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
1958 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1959 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 1960 ULONGEST signature, int is_debug_types);
a2ce51a0 1961
ed2dc618
SM
1962static struct dwp_file *get_dwp_file
1963 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 1964
3019eac3 1965static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1966 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1967
1968static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1969 (struct signatured_type *, const char *, const char *);
3019eac3 1970
89e63ee4
DE
1971static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1972
263db9a1 1973static void free_dwo_file (struct dwo_file *);
3019eac3 1974
263db9a1
TT
1975/* A unique_ptr helper to free a dwo_file. */
1976
1977struct dwo_file_deleter
ed2dc618 1978{
263db9a1
TT
1979 void operator() (struct dwo_file *df) const
1980 {
1981 free_dwo_file (df);
1982 }
ed2dc618
SM
1983};
1984
263db9a1
TT
1985/* A unique pointer to a dwo_file. */
1986
1987typedef std::unique_ptr<struct dwo_file, dwo_file_deleter> dwo_file_up;
1988
ed2dc618 1989static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 1990
1b80a9fa 1991static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1992
1993static void free_line_header_voidp (void *arg);
4390d890
DE
1994\f
1995/* Various complaints about symbol reading that don't abort the process. */
1996
1997static void
1998dwarf2_statement_list_fits_in_line_number_section_complaint (void)
1999{
b98664d3 2000 complaint (_("statement list doesn't fit in .debug_line section"));
4390d890
DE
2001}
2002
2003static void
2004dwarf2_debug_line_missing_file_complaint (void)
2005{
b98664d3 2006 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
2007}
2008
2009static void
2010dwarf2_debug_line_missing_end_sequence_complaint (void)
2011{
b98664d3 2012 complaint (_(".debug_line section has line "
4390d890
DE
2013 "program sequence without an end"));
2014}
2015
2016static void
2017dwarf2_complex_location_expr_complaint (void)
2018{
b98664d3 2019 complaint (_("location expression too complex"));
4390d890
DE
2020}
2021
2022static void
2023dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
2024 int arg3)
2025{
b98664d3 2026 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
2027 arg1, arg2, arg3);
2028}
2029
2030static void
2031dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
2032{
b98664d3 2033 complaint (_("debug info runs off end of %s section"
4390d890 2034 " [in module %s]"),
a32a8923
DE
2035 get_section_name (section),
2036 get_section_file_name (section));
4390d890 2037}
1b80a9fa 2038
4390d890
DE
2039static void
2040dwarf2_macro_malformed_definition_complaint (const char *arg1)
2041{
b98664d3 2042 complaint (_("macro debug info contains a "
4390d890
DE
2043 "malformed macro definition:\n`%s'"),
2044 arg1);
2045}
2046
2047static void
2048dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
2049{
b98664d3 2050 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
2051 arg1, arg2);
2052}
527f3840
JK
2053
2054/* Hash function for line_header_hash. */
2055
2056static hashval_t
2057line_header_hash (const struct line_header *ofs)
2058{
9c541725 2059 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
2060}
2061
2062/* Hash function for htab_create_alloc_ex for line_header_hash. */
2063
2064static hashval_t
2065line_header_hash_voidp (const void *item)
2066{
9a3c8263 2067 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
2068
2069 return line_header_hash (ofs);
2070}
2071
2072/* Equality function for line_header_hash. */
2073
2074static int
2075line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
2076{
9a3c8263
SM
2077 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
2078 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 2079
9c541725 2080 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
2081 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
2082}
2083
4390d890 2084\f
9291a0cd 2085
31aa7e4e
JB
2086/* Read the given attribute value as an address, taking the attribute's
2087 form into account. */
2088
2089static CORE_ADDR
2090attr_value_as_address (struct attribute *attr)
2091{
2092 CORE_ADDR addr;
2093
2094 if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
2095 {
2096 /* Aside from a few clearly defined exceptions, attributes that
2097 contain an address must always be in DW_FORM_addr form.
2098 Unfortunately, some compilers happen to be violating this
2099 requirement by encoding addresses using other forms, such
2100 as DW_FORM_data4 for example. For those broken compilers,
2101 we try to do our best, without any guarantee of success,
2102 to interpret the address correctly. It would also be nice
2103 to generate a complaint, but that would require us to maintain
2104 a list of legitimate cases where a non-address form is allowed,
2105 as well as update callers to pass in at least the CU's DWARF
2106 version. This is more overhead than what we're willing to
2107 expand for a pretty rare case. */
2108 addr = DW_UNSND (attr);
2109 }
2110 else
2111 addr = DW_ADDR (attr);
2112
2113 return addr;
2114}
2115
330cdd98
PA
2116/* See declaration. */
2117
2118dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
2119 const dwarf2_debug_sections *names)
2120 : objfile (objfile_)
2121{
2122 if (names == NULL)
2123 names = &dwarf2_elf_names;
2124
2125 bfd *obfd = objfile->obfd;
2126
2127 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
2128 locate_sections (obfd, sec, *names);
2129}
2130
fc8e7e75
SM
2131static void free_dwo_files (htab_t dwo_files, struct objfile *objfile);
2132
330cdd98
PA
2133dwarf2_per_objfile::~dwarf2_per_objfile ()
2134{
2135 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
2136 free_cached_comp_units ();
2137
2138 if (quick_file_names_table)
2139 htab_delete (quick_file_names_table);
2140
2141 if (line_header_hash)
2142 htab_delete (line_header_hash);
2143
b76e467d
SM
2144 for (dwarf2_per_cu_data *per_cu : all_comp_units)
2145 VEC_free (dwarf2_per_cu_ptr, per_cu->imported_symtabs);
fc8e7e75 2146
b2bdb8cf
SM
2147 for (signatured_type *sig_type : all_type_units)
2148 VEC_free (dwarf2_per_cu_ptr, sig_type->per_cu.imported_symtabs);
fc8e7e75
SM
2149
2150 VEC_free (dwarf2_section_info_def, types);
2151
2152 if (dwo_files != NULL)
2153 free_dwo_files (dwo_files, objfile);
fc8e7e75 2154
330cdd98
PA
2155 /* Everything else should be on the objfile obstack. */
2156}
2157
2158/* See declaration. */
2159
2160void
2161dwarf2_per_objfile::free_cached_comp_units ()
2162{
2163 dwarf2_per_cu_data *per_cu = read_in_chain;
2164 dwarf2_per_cu_data **last_chain = &read_in_chain;
2165 while (per_cu != NULL)
2166 {
2167 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
2168
fcd3b13d 2169 delete per_cu->cu;
330cdd98
PA
2170 *last_chain = next_cu;
2171 per_cu = next_cu;
2172 }
2173}
2174
11ed8cad
TT
2175/* A helper class that calls free_cached_comp_units on
2176 destruction. */
2177
2178class free_cached_comp_units
2179{
2180public:
2181
2182 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
2183 : m_per_objfile (per_objfile)
2184 {
2185 }
2186
2187 ~free_cached_comp_units ()
2188 {
2189 m_per_objfile->free_cached_comp_units ();
2190 }
2191
2192 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
2193
2194private:
2195
2196 dwarf2_per_objfile *m_per_objfile;
2197};
2198
c906108c 2199/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
2200 information and return true if we have enough to do something.
2201 NAMES points to the dwarf2 section names, or is NULL if the standard
2202 ELF names are used. */
c906108c
SS
2203
2204int
251d32d9
TG
2205dwarf2_has_info (struct objfile *objfile,
2206 const struct dwarf2_debug_sections *names)
c906108c 2207{
97cbe998
SDJ
2208 if (objfile->flags & OBJF_READNEVER)
2209 return 0;
2210
ed2dc618
SM
2211 struct dwarf2_per_objfile *dwarf2_per_objfile
2212 = get_dwarf2_per_objfile (objfile);
2213
2214 if (dwarf2_per_objfile == NULL)
be391dca
TT
2215 {
2216 /* Initialize per-objfile state. */
fd90ace4
YQ
2217 dwarf2_per_objfile
2218 = new (&objfile->objfile_obstack) struct dwarf2_per_objfile (objfile,
2219 names);
ed2dc618 2220 set_dwarf2_per_objfile (objfile, dwarf2_per_objfile);
be391dca 2221 }
73869dc2 2222 return (!dwarf2_per_objfile->info.is_virtual
049412e3 2223 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 2224 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 2225 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
2226}
2227
2228/* Return the containing section of virtual section SECTION. */
2229
2230static struct dwarf2_section_info *
2231get_containing_section (const struct dwarf2_section_info *section)
2232{
2233 gdb_assert (section->is_virtual);
2234 return section->s.containing_section;
c906108c
SS
2235}
2236
a32a8923
DE
2237/* Return the bfd owner of SECTION. */
2238
2239static struct bfd *
2240get_section_bfd_owner (const struct dwarf2_section_info *section)
2241{
73869dc2
DE
2242 if (section->is_virtual)
2243 {
2244 section = get_containing_section (section);
2245 gdb_assert (!section->is_virtual);
2246 }
049412e3 2247 return section->s.section->owner;
a32a8923
DE
2248}
2249
2250/* Return the bfd section of SECTION.
2251 Returns NULL if the section is not present. */
2252
2253static asection *
2254get_section_bfd_section (const struct dwarf2_section_info *section)
2255{
73869dc2
DE
2256 if (section->is_virtual)
2257 {
2258 section = get_containing_section (section);
2259 gdb_assert (!section->is_virtual);
2260 }
049412e3 2261 return section->s.section;
a32a8923
DE
2262}
2263
2264/* Return the name of SECTION. */
2265
2266static const char *
2267get_section_name (const struct dwarf2_section_info *section)
2268{
2269 asection *sectp = get_section_bfd_section (section);
2270
2271 gdb_assert (sectp != NULL);
2272 return bfd_section_name (get_section_bfd_owner (section), sectp);
2273}
2274
2275/* Return the name of the file SECTION is in. */
2276
2277static const char *
2278get_section_file_name (const struct dwarf2_section_info *section)
2279{
2280 bfd *abfd = get_section_bfd_owner (section);
2281
2282 return bfd_get_filename (abfd);
2283}
2284
2285/* Return the id of SECTION.
2286 Returns 0 if SECTION doesn't exist. */
2287
2288static int
2289get_section_id (const struct dwarf2_section_info *section)
2290{
2291 asection *sectp = get_section_bfd_section (section);
2292
2293 if (sectp == NULL)
2294 return 0;
2295 return sectp->id;
2296}
2297
2298/* Return the flags of SECTION.
73869dc2 2299 SECTION (or containing section if this is a virtual section) must exist. */
a32a8923
DE
2300
2301static int
2302get_section_flags (const struct dwarf2_section_info *section)
2303{
2304 asection *sectp = get_section_bfd_section (section);
2305
2306 gdb_assert (sectp != NULL);
2307 return bfd_get_section_flags (sectp->owner, sectp);
2308}
2309
251d32d9
TG
2310/* When loading sections, we look either for uncompressed section or for
2311 compressed section names. */
233a11ab
CS
2312
2313static int
251d32d9
TG
2314section_is_p (const char *section_name,
2315 const struct dwarf2_section_names *names)
233a11ab 2316{
251d32d9
TG
2317 if (names->normal != NULL
2318 && strcmp (section_name, names->normal) == 0)
2319 return 1;
2320 if (names->compressed != NULL
2321 && strcmp (section_name, names->compressed) == 0)
2322 return 1;
2323 return 0;
233a11ab
CS
2324}
2325
330cdd98 2326/* See declaration. */
c906108c 2327
330cdd98
PA
2328void
2329dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
2330 const dwarf2_debug_sections &names)
c906108c 2331{
dc7650b8 2332 flagword aflag = bfd_get_section_flags (abfd, sectp);
251d32d9 2333
dc7650b8
JK
2334 if ((aflag & SEC_HAS_CONTENTS) == 0)
2335 {
2336 }
330cdd98 2337 else if (section_is_p (sectp->name, &names.info))
c906108c 2338 {
330cdd98
PA
2339 this->info.s.section = sectp;
2340 this->info.size = bfd_get_section_size (sectp);
c906108c 2341 }
330cdd98 2342 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 2343 {
330cdd98
PA
2344 this->abbrev.s.section = sectp;
2345 this->abbrev.size = bfd_get_section_size (sectp);
c906108c 2346 }
330cdd98 2347 else if (section_is_p (sectp->name, &names.line))
c906108c 2348 {
330cdd98
PA
2349 this->line.s.section = sectp;
2350 this->line.size = bfd_get_section_size (sectp);
c906108c 2351 }
330cdd98 2352 else if (section_is_p (sectp->name, &names.loc))
c906108c 2353 {
330cdd98
PA
2354 this->loc.s.section = sectp;
2355 this->loc.size = bfd_get_section_size (sectp);
c906108c 2356 }
330cdd98 2357 else if (section_is_p (sectp->name, &names.loclists))
43988095 2358 {
330cdd98
PA
2359 this->loclists.s.section = sectp;
2360 this->loclists.size = bfd_get_section_size (sectp);
43988095 2361 }
330cdd98 2362 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 2363 {
330cdd98
PA
2364 this->macinfo.s.section = sectp;
2365 this->macinfo.size = bfd_get_section_size (sectp);
c906108c 2366 }
330cdd98 2367 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 2368 {
330cdd98
PA
2369 this->macro.s.section = sectp;
2370 this->macro.size = bfd_get_section_size (sectp);
cf2c3c16 2371 }
330cdd98 2372 else if (section_is_p (sectp->name, &names.str))
c906108c 2373 {
330cdd98
PA
2374 this->str.s.section = sectp;
2375 this->str.size = bfd_get_section_size (sectp);
c906108c 2376 }
330cdd98 2377 else if (section_is_p (sectp->name, &names.line_str))
43988095 2378 {
330cdd98
PA
2379 this->line_str.s.section = sectp;
2380 this->line_str.size = bfd_get_section_size (sectp);
43988095 2381 }
330cdd98 2382 else if (section_is_p (sectp->name, &names.addr))
3019eac3 2383 {
330cdd98
PA
2384 this->addr.s.section = sectp;
2385 this->addr.size = bfd_get_section_size (sectp);
3019eac3 2386 }
330cdd98 2387 else if (section_is_p (sectp->name, &names.frame))
b6af0555 2388 {
330cdd98
PA
2389 this->frame.s.section = sectp;
2390 this->frame.size = bfd_get_section_size (sectp);
b6af0555 2391 }
330cdd98 2392 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2393 {
330cdd98
PA
2394 this->eh_frame.s.section = sectp;
2395 this->eh_frame.size = bfd_get_section_size (sectp);
b6af0555 2396 }
330cdd98 2397 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2398 {
330cdd98
PA
2399 this->ranges.s.section = sectp;
2400 this->ranges.size = bfd_get_section_size (sectp);
af34e669 2401 }
330cdd98 2402 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2403 {
330cdd98
PA
2404 this->rnglists.s.section = sectp;
2405 this->rnglists.size = bfd_get_section_size (sectp);
43988095 2406 }
330cdd98 2407 else if (section_is_p (sectp->name, &names.types))
348e048f 2408 {
8b70b953
TT
2409 struct dwarf2_section_info type_section;
2410
2411 memset (&type_section, 0, sizeof (type_section));
049412e3 2412 type_section.s.section = sectp;
8b70b953
TT
2413 type_section.size = bfd_get_section_size (sectp);
2414
330cdd98 2415 VEC_safe_push (dwarf2_section_info_def, this->types,
8b70b953 2416 &type_section);
348e048f 2417 }
330cdd98 2418 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2419 {
330cdd98
PA
2420 this->gdb_index.s.section = sectp;
2421 this->gdb_index.size = bfd_get_section_size (sectp);
9291a0cd 2422 }
927aa2e7
JK
2423 else if (section_is_p (sectp->name, &names.debug_names))
2424 {
2425 this->debug_names.s.section = sectp;
2426 this->debug_names.size = bfd_get_section_size (sectp);
2427 }
2428 else if (section_is_p (sectp->name, &names.debug_aranges))
2429 {
2430 this->debug_aranges.s.section = sectp;
2431 this->debug_aranges.size = bfd_get_section_size (sectp);
2432 }
dce234bc 2433
b4e1fd61 2434 if ((bfd_get_section_flags (abfd, sectp) & (SEC_LOAD | SEC_ALLOC))
72dca2f5 2435 && bfd_section_vma (abfd, sectp) == 0)
330cdd98 2436 this->has_section_at_zero = true;
c906108c
SS
2437}
2438
fceca515
DE
2439/* A helper function that decides whether a section is empty,
2440 or not present. */
9e0ac564
TT
2441
2442static int
19ac8c2e 2443dwarf2_section_empty_p (const struct dwarf2_section_info *section)
9e0ac564 2444{
73869dc2
DE
2445 if (section->is_virtual)
2446 return section->size == 0;
049412e3 2447 return section->s.section == NULL || section->size == 0;
9e0ac564
TT
2448}
2449
cd4fb1b2 2450/* See dwarf2read.h. */
c906108c 2451
cd4fb1b2
SM
2452void
2453dwarf2_read_section (struct objfile *objfile, dwarf2_section_info *info)
c906108c 2454{
a32a8923 2455 asection *sectp;
3019eac3 2456 bfd *abfd;
dce234bc 2457 gdb_byte *buf, *retbuf;
c906108c 2458
be391dca
TT
2459 if (info->readin)
2460 return;
dce234bc 2461 info->buffer = NULL;
be391dca 2462 info->readin = 1;
188dd5d6 2463
9e0ac564 2464 if (dwarf2_section_empty_p (info))
dce234bc 2465 return;
c906108c 2466
a32a8923 2467 sectp = get_section_bfd_section (info);
3019eac3 2468
73869dc2
DE
2469 /* If this is a virtual section we need to read in the real one first. */
2470 if (info->is_virtual)
2471 {
2472 struct dwarf2_section_info *containing_section =
2473 get_containing_section (info);
2474
2475 gdb_assert (sectp != NULL);
2476 if ((sectp->flags & SEC_RELOC) != 0)
2477 {
2478 error (_("Dwarf Error: DWP format V2 with relocations is not"
2479 " supported in section %s [in module %s]"),
2480 get_section_name (info), get_section_file_name (info));
2481 }
2482 dwarf2_read_section (objfile, containing_section);
2483 /* Other code should have already caught virtual sections that don't
2484 fit. */
2485 gdb_assert (info->virtual_offset + info->size
2486 <= containing_section->size);
2487 /* If the real section is empty or there was a problem reading the
2488 section we shouldn't get here. */
2489 gdb_assert (containing_section->buffer != NULL);
2490 info->buffer = containing_section->buffer + info->virtual_offset;
2491 return;
2492 }
2493
4bf44c1c
TT
2494 /* If the section has relocations, we must read it ourselves.
2495 Otherwise we attach it to the BFD. */
2496 if ((sectp->flags & SEC_RELOC) == 0)
dce234bc 2497 {
d521ce57 2498 info->buffer = gdb_bfd_map_section (sectp, &info->size);
4bf44c1c 2499 return;
dce234bc 2500 }
dce234bc 2501
224c3ddb 2502 buf = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, info->size);
4bf44c1c 2503 info->buffer = buf;
dce234bc
PP
2504
2505 /* When debugging .o files, we may need to apply relocations; see
2506 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
2507 We never compress sections in .o files, so we only need to
2508 try this when the section is not compressed. */
ac8035ab 2509 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
dce234bc
PP
2510 if (retbuf != NULL)
2511 {
2512 info->buffer = retbuf;
2513 return;
2514 }
2515
a32a8923
DE
2516 abfd = get_section_bfd_owner (info);
2517 gdb_assert (abfd != NULL);
2518
dce234bc
PP
2519 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
2520 || bfd_bread (buf, info->size, abfd) != info->size)
19ac8c2e
DE
2521 {
2522 error (_("Dwarf Error: Can't read DWARF data"
2523 " in section %s [in module %s]"),
2524 bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
2525 }
dce234bc
PP
2526}
2527
9e0ac564
TT
2528/* A helper function that returns the size of a section in a safe way.
2529 If you are positive that the section has been read before using the
2530 size, then it is safe to refer to the dwarf2_section_info object's
2531 "size" field directly. In other cases, you must call this
2532 function, because for compressed sections the size field is not set
2533 correctly until the section has been read. */
2534
2535static bfd_size_type
2536dwarf2_section_size (struct objfile *objfile,
2537 struct dwarf2_section_info *info)
2538{
2539 if (!info->readin)
2540 dwarf2_read_section (objfile, info);
2541 return info->size;
2542}
2543
dce234bc 2544/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2545 SECTION_NAME. */
af34e669 2546
dce234bc 2547void
3017a003
TG
2548dwarf2_get_section_info (struct objfile *objfile,
2549 enum dwarf2_section_enum sect,
d521ce57 2550 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2551 bfd_size_type *sizep)
2552{
2553 struct dwarf2_per_objfile *data
9a3c8263
SM
2554 = (struct dwarf2_per_objfile *) objfile_data (objfile,
2555 dwarf2_objfile_data_key);
dce234bc 2556 struct dwarf2_section_info *info;
a3b2a86b
TT
2557
2558 /* We may see an objfile without any DWARF, in which case we just
2559 return nothing. */
2560 if (data == NULL)
2561 {
2562 *sectp = NULL;
2563 *bufp = NULL;
2564 *sizep = 0;
2565 return;
2566 }
3017a003
TG
2567 switch (sect)
2568 {
2569 case DWARF2_DEBUG_FRAME:
2570 info = &data->frame;
2571 break;
2572 case DWARF2_EH_FRAME:
2573 info = &data->eh_frame;
2574 break;
2575 default:
2576 gdb_assert_not_reached ("unexpected section");
2577 }
dce234bc 2578
9e0ac564 2579 dwarf2_read_section (objfile, info);
dce234bc 2580
a32a8923 2581 *sectp = get_section_bfd_section (info);
dce234bc
PP
2582 *bufp = info->buffer;
2583 *sizep = info->size;
2584}
2585
36586728
TT
2586/* A helper function to find the sections for a .dwz file. */
2587
2588static void
2589locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2590{
9a3c8263 2591 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2592
2593 /* Note that we only support the standard ELF names, because .dwz
2594 is ELF-only (at the time of writing). */
2595 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2596 {
049412e3 2597 dwz_file->abbrev.s.section = sectp;
36586728
TT
2598 dwz_file->abbrev.size = bfd_get_section_size (sectp);
2599 }
2600 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2601 {
049412e3 2602 dwz_file->info.s.section = sectp;
36586728
TT
2603 dwz_file->info.size = bfd_get_section_size (sectp);
2604 }
2605 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2606 {
049412e3 2607 dwz_file->str.s.section = sectp;
36586728
TT
2608 dwz_file->str.size = bfd_get_section_size (sectp);
2609 }
2610 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2611 {
049412e3 2612 dwz_file->line.s.section = sectp;
36586728
TT
2613 dwz_file->line.size = bfd_get_section_size (sectp);
2614 }
2615 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2616 {
049412e3 2617 dwz_file->macro.s.section = sectp;
36586728
TT
2618 dwz_file->macro.size = bfd_get_section_size (sectp);
2619 }
2ec9a5e0
TT
2620 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2621 {
049412e3 2622 dwz_file->gdb_index.s.section = sectp;
2ec9a5e0
TT
2623 dwz_file->gdb_index.size = bfd_get_section_size (sectp);
2624 }
927aa2e7
JK
2625 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2626 {
2627 dwz_file->debug_names.s.section = sectp;
2628 dwz_file->debug_names.size = bfd_get_section_size (sectp);
2629 }
36586728
TT
2630}
2631
4db1a1dc
TT
2632/* Open the separate '.dwz' debug file, if needed. Return NULL if
2633 there is no .gnu_debugaltlink section in the file. Error if there
2634 is such a section but the file cannot be found. */
36586728
TT
2635
2636static struct dwz_file *
ed2dc618 2637dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 2638{
36586728 2639 const char *filename;
acd13123 2640 bfd_size_type buildid_len_arg;
dc294be5
TT
2641 size_t buildid_len;
2642 bfd_byte *buildid;
36586728
TT
2643
2644 if (dwarf2_per_objfile->dwz_file != NULL)
7ff8cb8c 2645 return dwarf2_per_objfile->dwz_file.get ();
36586728 2646
4db1a1dc 2647 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2648 gdb::unique_xmalloc_ptr<char> data
2649 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2650 &buildid_len_arg, &buildid));
4db1a1dc
TT
2651 if (data == NULL)
2652 {
2653 if (bfd_get_error () == bfd_error_no_error)
2654 return NULL;
2655 error (_("could not read '.gnu_debugaltlink' section: %s"),
2656 bfd_errmsg (bfd_get_error ()));
2657 }
791afaa2
TT
2658
2659 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2660
acd13123
TT
2661 buildid_len = (size_t) buildid_len_arg;
2662
791afaa2 2663 filename = data.get ();
d721ba37
PA
2664
2665 std::string abs_storage;
36586728
TT
2666 if (!IS_ABSOLUTE_PATH (filename))
2667 {
14278e1f
TT
2668 gdb::unique_xmalloc_ptr<char> abs
2669 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2670
14278e1f 2671 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2672 filename = abs_storage.c_str ();
36586728
TT
2673 }
2674
dc294be5
TT
2675 /* First try the file name given in the section. If that doesn't
2676 work, try to use the build-id instead. */
192b62ce 2677 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2678 if (dwz_bfd != NULL)
36586728 2679 {
192b62ce
TT
2680 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2681 dwz_bfd.release ();
36586728
TT
2682 }
2683
dc294be5
TT
2684 if (dwz_bfd == NULL)
2685 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2686
2687 if (dwz_bfd == NULL)
2688 error (_("could not find '.gnu_debugaltlink' file for %s"),
2689 objfile_name (dwarf2_per_objfile->objfile));
2690
7ff8cb8c
TT
2691 std::unique_ptr<struct dwz_file> result
2692 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2693
7ff8cb8c
TT
2694 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2695 result.get ());
36586728 2696
7ff8cb8c
TT
2697 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2698 result->dwz_bfd.get ());
2699 dwarf2_per_objfile->dwz_file = std::move (result);
2700 return dwarf2_per_objfile->dwz_file.get ();
36586728 2701}
9291a0cd 2702\f
7b9f3c50
DE
2703/* DWARF quick_symbols_functions support. */
2704
2705/* TUs can share .debug_line entries, and there can be a lot more TUs than
2706 unique line tables, so we maintain a separate table of all .debug_line
2707 derived entries to support the sharing.
2708 All the quick functions need is the list of file names. We discard the
2709 line_header when we're done and don't need to record it here. */
2710struct quick_file_names
2711{
094b34ac
DE
2712 /* The data used to construct the hash key. */
2713 struct stmt_list_hash hash;
7b9f3c50
DE
2714
2715 /* The number of entries in file_names, real_names. */
2716 unsigned int num_file_names;
2717
2718 /* The file names from the line table, after being run through
2719 file_full_name. */
2720 const char **file_names;
2721
2722 /* The file names from the line table after being run through
2723 gdb_realpath. These are computed lazily. */
2724 const char **real_names;
2725};
2726
2727/* When using the index (and thus not using psymtabs), each CU has an
2728 object of this type. This is used to hold information needed by
2729 the various "quick" methods. */
2730struct dwarf2_per_cu_quick_data
2731{
2732 /* The file table. This can be NULL if there was no file table
2733 or it's currently not read in.
2734 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2735 struct quick_file_names *file_names;
2736
2737 /* The corresponding symbol table. This is NULL if symbols for this
2738 CU have not yet been read. */
43f3e411 2739 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2740
2741 /* A temporary mark bit used when iterating over all CUs in
2742 expand_symtabs_matching. */
2743 unsigned int mark : 1;
2744
2745 /* True if we've tried to read the file table and found there isn't one.
2746 There will be no point in trying to read it again next time. */
2747 unsigned int no_file_data : 1;
2748};
2749
094b34ac
DE
2750/* Utility hash function for a stmt_list_hash. */
2751
2752static hashval_t
2753hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2754{
2755 hashval_t v = 0;
2756
2757 if (stmt_list_hash->dwo_unit != NULL)
2758 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2759 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2760 return v;
2761}
2762
2763/* Utility equality function for a stmt_list_hash. */
2764
2765static int
2766eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2767 const struct stmt_list_hash *rhs)
2768{
2769 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2770 return 0;
2771 if (lhs->dwo_unit != NULL
2772 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2773 return 0;
2774
9c541725 2775 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2776}
2777
7b9f3c50
DE
2778/* Hash function for a quick_file_names. */
2779
2780static hashval_t
2781hash_file_name_entry (const void *e)
2782{
9a3c8263
SM
2783 const struct quick_file_names *file_data
2784 = (const struct quick_file_names *) e;
7b9f3c50 2785
094b34ac 2786 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2787}
2788
2789/* Equality function for a quick_file_names. */
2790
2791static int
2792eq_file_name_entry (const void *a, const void *b)
2793{
9a3c8263
SM
2794 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2795 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2796
094b34ac 2797 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2798}
2799
2800/* Delete function for a quick_file_names. */
2801
2802static void
2803delete_file_name_entry (void *e)
2804{
9a3c8263 2805 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2806 int i;
2807
2808 for (i = 0; i < file_data->num_file_names; ++i)
2809 {
2810 xfree ((void*) file_data->file_names[i]);
2811 if (file_data->real_names)
2812 xfree ((void*) file_data->real_names[i]);
2813 }
2814
2815 /* The space for the struct itself lives on objfile_obstack,
2816 so we don't free it here. */
2817}
2818
2819/* Create a quick_file_names hash table. */
2820
2821static htab_t
2822create_quick_file_names_table (unsigned int nr_initial_entries)
2823{
2824 return htab_create_alloc (nr_initial_entries,
2825 hash_file_name_entry, eq_file_name_entry,
2826 delete_file_name_entry, xcalloc, xfree);
2827}
9291a0cd 2828
918dd910
JK
2829/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2830 have to be created afterwards. You should call age_cached_comp_units after
2831 processing PER_CU->CU. dw2_setup must have been already called. */
2832
2833static void
58f0c718 2834load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
918dd910 2835{
3019eac3 2836 if (per_cu->is_debug_types)
e5fe5e75 2837 load_full_type_unit (per_cu);
918dd910 2838 else
58f0c718 2839 load_full_comp_unit (per_cu, skip_partial, language_minimal);
918dd910 2840
cc12ce38
DE
2841 if (per_cu->cu == NULL)
2842 return; /* Dummy CU. */
2dc860c0
DE
2843
2844 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2845}
2846
a0f42c21 2847/* Read in the symbols for PER_CU. */
2fdf6df6 2848
9291a0cd 2849static void
58f0c718 2850dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2851{
ed2dc618 2852 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9291a0cd 2853
f4dc4d17
DE
2854 /* Skip type_unit_groups, reading the type units they contain
2855 is handled elsewhere. */
2856 if (IS_TYPE_UNIT_GROUP (per_cu))
2857 return;
2858
b303c6f6
AB
2859 /* The destructor of dwarf2_queue_guard frees any entries left on
2860 the queue. After this point we're guaranteed to leave this function
2861 with the dwarf queue empty. */
2862 dwarf2_queue_guard q_guard;
9291a0cd 2863
95554aad 2864 if (dwarf2_per_objfile->using_index
43f3e411 2865 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2866 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2867 {
2868 queue_comp_unit (per_cu, language_minimal);
58f0c718 2869 load_cu (per_cu, skip_partial);
89e63ee4
DE
2870
2871 /* If we just loaded a CU from a DWO, and we're working with an index
2872 that may badly handle TUs, load all the TUs in that DWO as well.
2873 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2874 if (!per_cu->is_debug_types
cc12ce38 2875 && per_cu->cu != NULL
89e63ee4
DE
2876 && per_cu->cu->dwo_unit != NULL
2877 && dwarf2_per_objfile->index_table != NULL
2878 && dwarf2_per_objfile->index_table->version <= 7
2879 /* DWP files aren't supported yet. */
ed2dc618 2880 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 2881 queue_and_load_all_dwo_tus (per_cu);
95554aad 2882 }
9291a0cd 2883
ed2dc618 2884 process_queue (dwarf2_per_objfile);
9291a0cd
TT
2885
2886 /* Age the cache, releasing compilation units that have not
2887 been used recently. */
ed2dc618 2888 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
2889}
2890
2891/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2892 the objfile from which this CU came. Returns the resulting symbol
2893 table. */
2fdf6df6 2894
43f3e411 2895static struct compunit_symtab *
58f0c718 2896dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2897{
ed2dc618
SM
2898 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2899
95554aad 2900 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2901 if (!per_cu->v.quick->compunit_symtab)
9291a0cd 2902 {
11ed8cad 2903 free_cached_comp_units freer (dwarf2_per_objfile);
c83dd867 2904 scoped_restore decrementer = increment_reading_symtab ();
58f0c718 2905 dw2_do_instantiate_symtab (per_cu, skip_partial);
ed2dc618 2906 process_cu_includes (dwarf2_per_objfile);
9291a0cd 2907 }
f194fefb 2908
43f3e411 2909 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2910}
2911
ff4c9fec 2912/* See declaration. */
f4dc4d17 2913
ff4c9fec
SM
2914dwarf2_per_cu_data *
2915dwarf2_per_objfile::get_cutu (int index)
2916{
b76e467d 2917 if (index >= this->all_comp_units.size ())
ff4c9fec 2918 {
b76e467d 2919 index -= this->all_comp_units.size ();
b2bdb8cf 2920 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2921 return &this->all_type_units[index]->per_cu;
2922 }
f4dc4d17 2923
ff4c9fec
SM
2924 return this->all_comp_units[index];
2925}
f4dc4d17 2926
ff4c9fec 2927/* See declaration. */
2fdf6df6 2928
ff4c9fec
SM
2929dwarf2_per_cu_data *
2930dwarf2_per_objfile::get_cu (int index)
1fd400ff 2931{
b76e467d 2932 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2933
ff4c9fec 2934 return this->all_comp_units[index];
f4dc4d17
DE
2935}
2936
ff4c9fec 2937/* See declaration. */
f4dc4d17 2938
ff4c9fec
SM
2939signatured_type *
2940dwarf2_per_objfile::get_tu (int index)
f4dc4d17 2941{
b2bdb8cf 2942 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2943
ff4c9fec 2944 return this->all_type_units[index];
1fd400ff
TT
2945}
2946
4b514bc8
JK
2947/* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2948 objfile_obstack, and constructed with the specified field
2949 values. */
2950
2951static dwarf2_per_cu_data *
ed2dc618 2952create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
2953 struct dwarf2_section_info *section,
2954 int is_dwz,
2955 sect_offset sect_off, ULONGEST length)
2956{
ed2dc618 2957 struct objfile *objfile = dwarf2_per_objfile->objfile;
4b514bc8
JK
2958 dwarf2_per_cu_data *the_cu
2959 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2960 struct dwarf2_per_cu_data);
2961 the_cu->sect_off = sect_off;
2962 the_cu->length = length;
e3b94546 2963 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
4b514bc8
JK
2964 the_cu->section = section;
2965 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2966 struct dwarf2_per_cu_quick_data);
2967 the_cu->is_dwz = is_dwz;
2968 return the_cu;
2969}
2970
2ec9a5e0
TT
2971/* A helper for create_cus_from_index that handles a given list of
2972 CUs. */
2fdf6df6 2973
74a0d9f6 2974static void
12359b5e 2975create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2976 const gdb_byte *cu_list, offset_type n_elements,
2977 struct dwarf2_section_info *section,
b76e467d 2978 int is_dwz)
9291a0cd 2979{
12359b5e 2980 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2981 {
74a0d9f6 2982 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2983
2984 sect_offset sect_off
2985 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2986 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2987 cu_list += 2 * 8;
2988
b76e467d 2989 dwarf2_per_cu_data *per_cu
ed2dc618
SM
2990 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2991 sect_off, length);
b76e467d 2992 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
9291a0cd 2993 }
9291a0cd
TT
2994}
2995
2ec9a5e0 2996/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2997 the CU objects for this objfile. */
2ec9a5e0 2998
74a0d9f6 2999static void
12359b5e 3000create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
3001 const gdb_byte *cu_list, offset_type cu_list_elements,
3002 const gdb_byte *dwz_list, offset_type dwz_elements)
3003{
b76e467d
SM
3004 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
3005 dwarf2_per_objfile->all_comp_units.reserve
3006 ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 3007
12359b5e 3008 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
b76e467d 3009 &dwarf2_per_objfile->info, 0);
2ec9a5e0
TT
3010
3011 if (dwz_elements == 0)
74a0d9f6 3012 return;
2ec9a5e0 3013
12359b5e
SM
3014 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
3015 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
b76e467d 3016 &dwz->info, 1);
2ec9a5e0
TT
3017}
3018
1fd400ff 3019/* Create the signatured type hash table from the index. */
673bfd45 3020
74a0d9f6 3021static void
12359b5e
SM
3022create_signatured_type_table_from_index
3023 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3024 struct dwarf2_section_info *section,
3025 const gdb_byte *bytes,
3026 offset_type elements)
1fd400ff 3027{
12359b5e 3028 struct objfile *objfile = dwarf2_per_objfile->objfile;
1fd400ff 3029
b2bdb8cf
SM
3030 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3031 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
1fd400ff 3032
12359b5e 3033 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
1fd400ff 3034
12359b5e 3035 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 3036 {
52dc124a 3037 struct signatured_type *sig_type;
9c541725 3038 ULONGEST signature;
1fd400ff 3039 void **slot;
9c541725 3040 cu_offset type_offset_in_tu;
1fd400ff 3041
74a0d9f6 3042 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
3043 sect_offset sect_off
3044 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
3045 type_offset_in_tu
3046 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
3047 BFD_ENDIAN_LITTLE);
1fd400ff
TT
3048 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
3049 bytes += 3 * 8;
3050
52dc124a 3051 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 3052 struct signatured_type);
52dc124a 3053 sig_type->signature = signature;
9c541725 3054 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 3055 sig_type->per_cu.is_debug_types = 1;
8a0459fd 3056 sig_type->per_cu.section = section;
9c541725 3057 sig_type->per_cu.sect_off = sect_off;
e3b94546 3058 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
52dc124a 3059 sig_type->per_cu.v.quick
1fd400ff
TT
3060 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3061 struct dwarf2_per_cu_quick_data);
3062
52dc124a
DE
3063 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3064 *slot = sig_type;
1fd400ff 3065
b2bdb8cf 3066 dwarf2_per_objfile->all_type_units.push_back (sig_type);
1fd400ff
TT
3067 }
3068
673bfd45 3069 dwarf2_per_objfile->signatured_types = sig_types_hash;
1fd400ff
TT
3070}
3071
927aa2e7
JK
3072/* Create the signatured type hash table from .debug_names. */
3073
3074static void
3075create_signatured_type_table_from_debug_names
ed2dc618 3076 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3077 const mapped_debug_names &map,
3078 struct dwarf2_section_info *section,
3079 struct dwarf2_section_info *abbrev_section)
3080{
ed2dc618
SM
3081 struct objfile *objfile = dwarf2_per_objfile->objfile;
3082
927aa2e7
JK
3083 dwarf2_read_section (objfile, section);
3084 dwarf2_read_section (objfile, abbrev_section);
3085
b2bdb8cf
SM
3086 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
3087 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
927aa2e7
JK
3088
3089 htab_t sig_types_hash = allocate_signatured_type_table (objfile);
3090
3091 for (uint32_t i = 0; i < map.tu_count; ++i)
3092 {
3093 struct signatured_type *sig_type;
927aa2e7 3094 void **slot;
927aa2e7
JK
3095
3096 sect_offset sect_off
3097 = (sect_offset) (extract_unsigned_integer
3098 (map.tu_table_reordered + i * map.offset_size,
3099 map.offset_size,
3100 map.dwarf5_byte_order));
3101
3102 comp_unit_head cu_header;
ed2dc618
SM
3103 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
3104 abbrev_section,
927aa2e7
JK
3105 section->buffer + to_underlying (sect_off),
3106 rcuh_kind::TYPE);
3107
3108 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3109 struct signatured_type);
3110 sig_type->signature = cu_header.signature;
3111 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
3112 sig_type->per_cu.is_debug_types = 1;
3113 sig_type->per_cu.section = section;
3114 sig_type->per_cu.sect_off = sect_off;
e3b94546 3115 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
927aa2e7
JK
3116 sig_type->per_cu.v.quick
3117 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
3118 struct dwarf2_per_cu_quick_data);
3119
3120 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
3121 *slot = sig_type;
3122
b2bdb8cf 3123 dwarf2_per_objfile->all_type_units.push_back (sig_type);
927aa2e7
JK
3124 }
3125
3126 dwarf2_per_objfile->signatured_types = sig_types_hash;
3127}
3128
9291a0cd
TT
3129/* Read the address map data from the mapped index, and use it to
3130 populate the objfile's psymtabs_addrmap. */
2fdf6df6 3131
9291a0cd 3132static void
ed2dc618
SM
3133create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
3134 struct mapped_index *index)
9291a0cd 3135{
ed2dc618 3136 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 3137 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 3138 const gdb_byte *iter, *end;
9291a0cd 3139 struct addrmap *mutable_map;
9291a0cd
TT
3140 CORE_ADDR baseaddr;
3141
8268c778
PA
3142 auto_obstack temp_obstack;
3143
9291a0cd
TT
3144 mutable_map = addrmap_create_mutable (&temp_obstack);
3145
f00a2de2
PA
3146 iter = index->address_table.data ();
3147 end = iter + index->address_table.size ();
9291a0cd
TT
3148
3149 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3150
3151 while (iter < end)
3152 {
3153 ULONGEST hi, lo, cu_index;
3154 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3155 iter += 8;
3156 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
3157 iter += 8;
3158 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
3159 iter += 4;
f652bce2 3160
24a55014 3161 if (lo > hi)
f652bce2 3162 {
b98664d3 3163 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 3164 hex_string (lo), hex_string (hi));
24a55014 3165 continue;
f652bce2 3166 }
24a55014 3167
b76e467d 3168 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
f652bce2 3169 {
b98664d3 3170 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 3171 (unsigned) cu_index);
24a55014 3172 continue;
f652bce2 3173 }
24a55014 3174
3e29f34a
MR
3175 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
3176 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
ed2dc618 3177 addrmap_set_empty (mutable_map, lo, hi - 1,
ff4c9fec 3178 dwarf2_per_objfile->get_cu (cu_index));
9291a0cd
TT
3179 }
3180
3181 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3182 &objfile->objfile_obstack);
9291a0cd
TT
3183}
3184
927aa2e7
JK
3185/* Read the address map data from DWARF-5 .debug_aranges, and use it to
3186 populate the objfile's psymtabs_addrmap. */
3187
3188static void
ed2dc618 3189create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
3190 struct dwarf2_section_info *section)
3191{
ed2dc618 3192 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
3193 bfd *abfd = objfile->obfd;
3194 struct gdbarch *gdbarch = get_objfile_arch (objfile);
3195 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
3196 SECT_OFF_TEXT (objfile));
3197
3198 auto_obstack temp_obstack;
3199 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
3200
3201 std::unordered_map<sect_offset,
3202 dwarf2_per_cu_data *,
3203 gdb::hash_enum<sect_offset>>
3204 debug_info_offset_to_per_cu;
b76e467d 3205 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 3206 {
927aa2e7
JK
3207 const auto insertpair
3208 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
3209 if (!insertpair.second)
3210 {
3211 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
3212 "debug_info_offset %s, ignoring .debug_aranges."),
3213 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
3214 return;
3215 }
3216 }
3217
3218 dwarf2_read_section (objfile, section);
3219
3220 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
3221
3222 const gdb_byte *addr = section->buffer;
3223
3224 while (addr < section->buffer + section->size)
3225 {
3226 const gdb_byte *const entry_addr = addr;
3227 unsigned int bytes_read;
3228
3229 const LONGEST entry_length = read_initial_length (abfd, addr,
3230 &bytes_read);
3231 addr += bytes_read;
3232
3233 const gdb_byte *const entry_end = addr + entry_length;
3234 const bool dwarf5_is_dwarf64 = bytes_read != 4;
3235 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
3236 if (addr + entry_length > section->buffer + section->size)
3237 {
3238 warning (_("Section .debug_aranges in %s entry at offset %zu "
3239 "length %s exceeds section length %s, "
3240 "ignoring .debug_aranges."),
3241 objfile_name (objfile), entry_addr - section->buffer,
3242 plongest (bytes_read + entry_length),
3243 pulongest (section->size));
3244 return;
3245 }
3246
3247 /* The version number. */
3248 const uint16_t version = read_2_bytes (abfd, addr);
3249 addr += 2;
3250 if (version != 2)
3251 {
3252 warning (_("Section .debug_aranges in %s entry at offset %zu "
3253 "has unsupported version %d, ignoring .debug_aranges."),
3254 objfile_name (objfile), entry_addr - section->buffer,
3255 version);
3256 return;
3257 }
3258
3259 const uint64_t debug_info_offset
3260 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
3261 addr += offset_size;
3262 const auto per_cu_it
3263 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
3264 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
3265 {
3266 warning (_("Section .debug_aranges in %s entry at offset %zu "
3267 "debug_info_offset %s does not exists, "
3268 "ignoring .debug_aranges."),
3269 objfile_name (objfile), entry_addr - section->buffer,
3270 pulongest (debug_info_offset));
3271 return;
3272 }
3273 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
3274
3275 const uint8_t address_size = *addr++;
3276 if (address_size < 1 || address_size > 8)
3277 {
3278 warning (_("Section .debug_aranges in %s entry at offset %zu "
3279 "address_size %u is invalid, ignoring .debug_aranges."),
3280 objfile_name (objfile), entry_addr - section->buffer,
3281 address_size);
3282 return;
3283 }
3284
3285 const uint8_t segment_selector_size = *addr++;
3286 if (segment_selector_size != 0)
3287 {
3288 warning (_("Section .debug_aranges in %s entry at offset %zu "
3289 "segment_selector_size %u is not supported, "
3290 "ignoring .debug_aranges."),
3291 objfile_name (objfile), entry_addr - section->buffer,
3292 segment_selector_size);
3293 return;
3294 }
3295
3296 /* Must pad to an alignment boundary that is twice the address
3297 size. It is undocumented by the DWARF standard but GCC does
3298 use it. */
3299 for (size_t padding = ((-(addr - section->buffer))
3300 & (2 * address_size - 1));
3301 padding > 0; padding--)
3302 if (*addr++ != 0)
3303 {
3304 warning (_("Section .debug_aranges in %s entry at offset %zu "
3305 "padding is not zero, ignoring .debug_aranges."),
3306 objfile_name (objfile), entry_addr - section->buffer);
3307 return;
3308 }
3309
3310 for (;;)
3311 {
3312 if (addr + 2 * address_size > entry_end)
3313 {
3314 warning (_("Section .debug_aranges in %s entry at offset %zu "
3315 "address list is not properly terminated, "
3316 "ignoring .debug_aranges."),
3317 objfile_name (objfile), entry_addr - section->buffer);
3318 return;
3319 }
3320 ULONGEST start = extract_unsigned_integer (addr, address_size,
3321 dwarf5_byte_order);
3322 addr += address_size;
3323 ULONGEST length = extract_unsigned_integer (addr, address_size,
3324 dwarf5_byte_order);
3325 addr += address_size;
3326 if (start == 0 && length == 0)
3327 break;
3328 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
3329 {
3330 /* Symbol was eliminated due to a COMDAT group. */
3331 continue;
3332 }
3333 ULONGEST end = start + length;
3334 start = gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr);
3335 end = gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr);
3336 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3337 }
3338 }
3339
3340 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
3341 &objfile->objfile_obstack);
3342}
3343
9291a0cd
TT
3344/* Find a slot in the mapped index INDEX for the object named NAME.
3345 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
3346 constant pool and return true. If NAME cannot be found, return
3347 false. */
2fdf6df6 3348
109483d9 3349static bool
9291a0cd
TT
3350find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3351 offset_type **vec_out)
3352{
0cf03b49 3353 offset_type hash;
9291a0cd 3354 offset_type slot, step;
559a7a62 3355 int (*cmp) (const char *, const char *);
9291a0cd 3356
791afaa2 3357 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 3358 if (current_language->la_language == language_cplus
45280282
IB
3359 || current_language->la_language == language_fortran
3360 || current_language->la_language == language_d)
0cf03b49
JK
3361 {
3362 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3363 not contain any. */
a8719064 3364
72998fb3 3365 if (strchr (name, '(') != NULL)
0cf03b49 3366 {
109483d9 3367 without_params = cp_remove_params (name);
0cf03b49 3368
72998fb3 3369 if (without_params != NULL)
791afaa2 3370 name = without_params.get ();
0cf03b49
JK
3371 }
3372 }
3373
559a7a62 3374 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3375 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3376 simulate our NAME being searched is also lowercased. */
3377 hash = mapped_index_string_hash ((index->version == 4
3378 && case_sensitivity == case_sensitive_off
3379 ? 5 : index->version),
3380 name);
3381
f00a2de2
PA
3382 slot = hash & (index->symbol_table.size () - 1);
3383 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 3384 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3385
3386 for (;;)
3387 {
9291a0cd 3388 const char *str;
f00a2de2
PA
3389
3390 const auto &bucket = index->symbol_table[slot];
3391 if (bucket.name == 0 && bucket.vec == 0)
109483d9 3392 return false;
9291a0cd 3393
f00a2de2 3394 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 3395 if (!cmp (name, str))
9291a0cd
TT
3396 {
3397 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 3398 + MAYBE_SWAP (bucket.vec));
109483d9 3399 return true;
9291a0cd
TT
3400 }
3401
f00a2de2 3402 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
3403 }
3404}
3405
2ec9a5e0
TT
3406/* A helper function that reads the .gdb_index from SECTION and fills
3407 in MAP. FILENAME is the name of the file containing the section;
d33bc52e 3408 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
3409 ok to use deprecated sections.
3410
3411 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3412 out parameters that are filled in with information about the CU and
3413 TU lists in the section.
3414
3415 Returns 1 if all went well, 0 otherwise. */
2fdf6df6 3416
d33bc52e 3417static bool
2ec9a5e0
TT
3418read_index_from_section (struct objfile *objfile,
3419 const char *filename,
d33bc52e 3420 bool deprecated_ok,
2ec9a5e0
TT
3421 struct dwarf2_section_info *section,
3422 struct mapped_index *map,
3423 const gdb_byte **cu_list,
3424 offset_type *cu_list_elements,
3425 const gdb_byte **types_list,
3426 offset_type *types_list_elements)
9291a0cd 3427{
948f8e3d 3428 const gdb_byte *addr;
2ec9a5e0 3429 offset_type version;
b3b272e1 3430 offset_type *metadata;
1fd400ff 3431 int i;
9291a0cd 3432
2ec9a5e0 3433 if (dwarf2_section_empty_p (section))
9291a0cd 3434 return 0;
82430852
JK
3435
3436 /* Older elfutils strip versions could keep the section in the main
3437 executable while splitting it for the separate debug info file. */
a32a8923 3438 if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
82430852
JK
3439 return 0;
3440
2ec9a5e0 3441 dwarf2_read_section (objfile, section);
9291a0cd 3442
2ec9a5e0 3443 addr = section->buffer;
9291a0cd 3444 /* Version check. */
1fd400ff 3445 version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3446 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3447 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3448 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3449 indices. */
831adc1f 3450 if (version < 4)
481860b3
GB
3451 {
3452 static int warning_printed = 0;
3453 if (!warning_printed)
3454 {
3455 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3456 filename);
481860b3
GB
3457 warning_printed = 1;
3458 }
3459 return 0;
3460 }
3461 /* Index version 4 uses a different hash function than index version
3462 5 and later.
3463
3464 Versions earlier than 6 did not emit psymbols for inlined
3465 functions. Using these files will cause GDB not to be able to
3466 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3467 indices unless the user has done
3468 "set use-deprecated-index-sections on". */
2ec9a5e0 3469 if (version < 6 && !deprecated_ok)
481860b3
GB
3470 {
3471 static int warning_printed = 0;
3472 if (!warning_printed)
3473 {
e615022a
DE
3474 warning (_("\
3475Skipping deprecated .gdb_index section in %s.\n\
3476Do \"set use-deprecated-index-sections on\" before the file is read\n\
3477to use the section anyway."),
2ec9a5e0 3478 filename);
481860b3
GB
3479 warning_printed = 1;
3480 }
3481 return 0;
3482 }
796a7ff8 3483 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3484 of the TU (for symbols coming from TUs),
3485 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3486 Plus gold-generated indices can have duplicate entries for global symbols,
3487 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3488 These are just performance bugs, and we can't distinguish gdb-generated
3489 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3490
481860b3 3491 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3492 longer backward compatible. */
796a7ff8 3493 if (version > 8)
594e8718 3494 return 0;
9291a0cd 3495
559a7a62 3496 map->version = version;
9291a0cd
TT
3497
3498 metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff
TT
3499
3500 i = 0;
2ec9a5e0
TT
3501 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3502 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3503 / 8);
1fd400ff
TT
3504 ++i;
3505
2ec9a5e0
TT
3506 *types_list = addr + MAYBE_SWAP (metadata[i]);
3507 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3508 - MAYBE_SWAP (metadata[i]))
3509 / 8);
987d643c 3510 ++i;
1fd400ff 3511
f00a2de2
PA
3512 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3513 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3514 map->address_table
3515 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3516 ++i;
3517
f00a2de2
PA
3518 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3519 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3520 map->symbol_table
3521 = gdb::array_view<mapped_index::symbol_table_slot>
3522 ((mapped_index::symbol_table_slot *) symbol_table,
3523 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3524
f00a2de2 3525 ++i;
f9d83a0b 3526 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3527
2ec9a5e0
TT
3528 return 1;
3529}
3530
927aa2e7 3531/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3532 elements of all the CUs and return 1. Otherwise, return 0. */
3533
3534static int
12359b5e 3535dwarf2_read_index (struct dwarf2_per_objfile *dwarf2_per_objfile)
2ec9a5e0 3536{
2ec9a5e0
TT
3537 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3538 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3539 struct dwz_file *dwz;
12359b5e 3540 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ec9a5e0 3541
3063847f 3542 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
4262abfb 3543 if (!read_index_from_section (objfile, objfile_name (objfile),
2ec9a5e0 3544 use_deprecated_index_sections,
3063847f 3545 &dwarf2_per_objfile->gdb_index, map.get (),
2ec9a5e0
TT
3546 &cu_list, &cu_list_elements,
3547 &types_list, &types_list_elements))
3548 return 0;
3549
0fefef59 3550 /* Don't use the index if it's empty. */
3063847f 3551 if (map->symbol_table.empty ())
0fefef59
DE
3552 return 0;
3553
2ec9a5e0
TT
3554 /* If there is a .dwz file, read it so we can get its CU list as
3555 well. */
ed2dc618 3556 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 3557 if (dwz != NULL)
2ec9a5e0 3558 {
2ec9a5e0
TT
3559 struct mapped_index dwz_map;
3560 const gdb_byte *dwz_types_ignore;
3561 offset_type dwz_types_elements_ignore;
3562
3563 if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
3564 1,
3565 &dwz->gdb_index, &dwz_map,
3566 &dwz_list, &dwz_list_elements,
3567 &dwz_types_ignore,
3568 &dwz_types_elements_ignore))
3569 {
3570 warning (_("could not read '.gdb_index' section from %s; skipping"),
3571 bfd_get_filename (dwz->dwz_bfd));
3572 return 0;
3573 }
3574 }
3575
12359b5e
SM
3576 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3577 dwz_list, dwz_list_elements);
1fd400ff 3578
8b70b953
TT
3579 if (types_list_elements)
3580 {
3581 struct dwarf2_section_info *section;
3582
3583 /* We can only handle a single .debug_types when we have an
3584 index. */
3585 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
3586 return 0;
3587
3588 section = VEC_index (dwarf2_section_info_def,
3589 dwarf2_per_objfile->types, 0);
3590
12359b5e
SM
3591 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3592 types_list, types_list_elements);
8b70b953 3593 }
9291a0cd 3594
3063847f 3595 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
9291a0cd 3596
3063847f 3597 dwarf2_per_objfile->index_table = std::move (map);
9291a0cd 3598 dwarf2_per_objfile->using_index = 1;
7b9f3c50 3599 dwarf2_per_objfile->quick_file_names_table =
b76e467d 3600 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd
TT
3601
3602 return 1;
3603}
3604
dee91e82 3605/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3606
dee91e82
DE
3607static void
3608dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3609 const gdb_byte *info_ptr,
dee91e82
DE
3610 struct die_info *comp_unit_die,
3611 int has_children,
3612 void *data)
9291a0cd 3613{
dee91e82 3614 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3615 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
518817b3
SM
3616 struct dwarf2_per_objfile *dwarf2_per_objfile
3617 = cu->per_cu->dwarf2_per_objfile;
dee91e82 3618 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3619 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3620 struct attribute *attr;
dee91e82 3621 int i;
7b9f3c50
DE
3622 void **slot;
3623 struct quick_file_names *qfn;
9291a0cd 3624
0186c6a7
DE
3625 gdb_assert (! this_cu->is_debug_types);
3626
07261596
TT
3627 /* Our callers never want to match partial units -- instead they
3628 will match the enclosing full CU. */
3629 if (comp_unit_die->tag == DW_TAG_partial_unit)
3630 {
3631 this_cu->v.quick->no_file_data = 1;
3632 return;
3633 }
3634
0186c6a7 3635 lh_cu = this_cu;
7b9f3c50 3636 slot = NULL;
dee91e82 3637
fff8551c 3638 line_header_up lh;
9c541725 3639 sect_offset line_offset {};
fff8551c 3640
dee91e82 3641 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
9291a0cd
TT
3642 if (attr)
3643 {
7b9f3c50
DE
3644 struct quick_file_names find_entry;
3645
9c541725 3646 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3647
3648 /* We may have already read in this line header (TU line header sharing).
3649 If we have we're done. */
094b34ac 3650 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3651 find_entry.hash.line_sect_off = line_offset;
7b9f3c50
DE
3652 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
3653 &find_entry, INSERT);
3654 if (*slot != NULL)
3655 {
9a3c8263 3656 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3657 return;
7b9f3c50
DE
3658 }
3659
3019eac3 3660 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3661 }
3662 if (lh == NULL)
3663 {
094b34ac 3664 lh_cu->v.quick->no_file_data = 1;
dee91e82 3665 return;
9291a0cd
TT
3666 }
3667
8d749320 3668 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3669 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3670 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3671 gdb_assert (slot != NULL);
3672 *slot = qfn;
9291a0cd 3673
d721ba37 3674 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3675
fff8551c 3676 qfn->num_file_names = lh->file_names.size ();
8d749320 3677 qfn->file_names =
fff8551c
PA
3678 XOBNEWVEC (&objfile->objfile_obstack, const char *, lh->file_names.size ());
3679 for (i = 0; i < lh->file_names.size (); ++i)
3680 qfn->file_names[i] = file_full_name (i + 1, lh.get (), fnd.comp_dir);
7b9f3c50 3681 qfn->real_names = NULL;
9291a0cd 3682
094b34ac 3683 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3684}
3685
3686/* A helper for the "quick" functions which attempts to read the line
3687 table for THIS_CU. */
3688
3689static struct quick_file_names *
e4a48d9d 3690dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3691{
0186c6a7
DE
3692 /* This should never be called for TUs. */
3693 gdb_assert (! this_cu->is_debug_types);
3694 /* Nor type unit groups. */
3695 gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
f4dc4d17 3696
dee91e82
DE
3697 if (this_cu->v.quick->file_names != NULL)
3698 return this_cu->v.quick->file_names;
3699 /* If we know there is no line data, no point in looking again. */
3700 if (this_cu->v.quick->no_file_data)
3701 return NULL;
3702
0186c6a7 3703 init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
dee91e82
DE
3704
3705 if (this_cu->v.quick->no_file_data)
3706 return NULL;
3707 return this_cu->v.quick->file_names;
9291a0cd
TT
3708}
3709
3710/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3711 real path for a given file name from the line table. */
2fdf6df6 3712
9291a0cd 3713static const char *
7b9f3c50
DE
3714dw2_get_real_path (struct objfile *objfile,
3715 struct quick_file_names *qfn, int index)
9291a0cd 3716{
7b9f3c50
DE
3717 if (qfn->real_names == NULL)
3718 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3719 qfn->num_file_names, const char *);
9291a0cd 3720
7b9f3c50 3721 if (qfn->real_names[index] == NULL)
14278e1f 3722 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3723
7b9f3c50 3724 return qfn->real_names[index];
9291a0cd
TT
3725}
3726
3727static struct symtab *
3728dw2_find_last_source_symtab (struct objfile *objfile)
3729{
ed2dc618
SM
3730 struct dwarf2_per_objfile *dwarf2_per_objfile
3731 = get_dwarf2_per_objfile (objfile);
b76e467d 3732 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
58f0c718 3733 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
ae2de4f8 3734
43f3e411
DE
3735 if (cust == NULL)
3736 return NULL;
ed2dc618 3737
43f3e411 3738 return compunit_primary_filetab (cust);
9291a0cd
TT
3739}
3740
7b9f3c50
DE
3741/* Traversal function for dw2_forget_cached_source_info. */
3742
3743static int
3744dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3745{
7b9f3c50 3746 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3747
7b9f3c50 3748 if (file_data->real_names)
9291a0cd 3749 {
7b9f3c50 3750 int i;
9291a0cd 3751
7b9f3c50 3752 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3753 {
7b9f3c50
DE
3754 xfree ((void*) file_data->real_names[i]);
3755 file_data->real_names[i] = NULL;
9291a0cd
TT
3756 }
3757 }
7b9f3c50
DE
3758
3759 return 1;
3760}
3761
3762static void
3763dw2_forget_cached_source_info (struct objfile *objfile)
3764{
ed2dc618
SM
3765 struct dwarf2_per_objfile *dwarf2_per_objfile
3766 = get_dwarf2_per_objfile (objfile);
7b9f3c50
DE
3767
3768 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
3769 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3770}
3771
f8eba3c6
TT
3772/* Helper function for dw2_map_symtabs_matching_filename that expands
3773 the symtabs and calls the iterator. */
3774
3775static int
3776dw2_map_expand_apply (struct objfile *objfile,
3777 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3778 const char *name, const char *real_path,
14bc53a8 3779 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3780{
43f3e411 3781 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3782
3783 /* Don't visit already-expanded CUs. */
43f3e411 3784 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3785 return 0;
3786
3787 /* This may expand more than one symtab, and we want to iterate over
3788 all of them. */
58f0c718 3789 dw2_instantiate_symtab (per_cu, false);
f8eba3c6 3790
14bc53a8
PA
3791 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3792 last_made, callback);
f8eba3c6
TT
3793}
3794
3795/* Implementation of the map_symtabs_matching_filename method. */
3796
14bc53a8
PA
3797static bool
3798dw2_map_symtabs_matching_filename
3799 (struct objfile *objfile, const char *name, const char *real_path,
3800 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3801{
c011a4f4 3802 const char *name_basename = lbasename (name);
ed2dc618
SM
3803 struct dwarf2_per_objfile *dwarf2_per_objfile
3804 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3805
848e3e78
DE
3806 /* The rule is CUs specify all the files, including those used by
3807 any TU, so there's no need to scan TUs here. */
f4dc4d17 3808
b76e467d 3809 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3810 {
3d7bb9d9 3811 /* We only need to look at symtabs not already expanded. */
43f3e411 3812 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3813 continue;
3814
b76e467d 3815 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3816 if (file_data == NULL)
9291a0cd
TT
3817 continue;
3818
b76e467d 3819 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3820 {
7b9f3c50 3821 const char *this_name = file_data->file_names[j];
da235a7c 3822 const char *this_real_name;
9291a0cd 3823
af529f8f 3824 if (compare_filenames_for_search (this_name, name))
9291a0cd 3825 {
f5b95b50 3826 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3827 callback))
3828 return true;
288e77a7 3829 continue;
4aac40c8 3830 }
9291a0cd 3831
c011a4f4
DE
3832 /* Before we invoke realpath, which can get expensive when many
3833 files are involved, do a quick comparison of the basenames. */
3834 if (! basenames_may_differ
3835 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3836 continue;
3837
da235a7c
JK
3838 this_real_name = dw2_get_real_path (objfile, file_data, j);
3839 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3840 {
da235a7c 3841 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3842 callback))
3843 return true;
288e77a7 3844 continue;
da235a7c 3845 }
9291a0cd 3846
da235a7c
JK
3847 if (real_path != NULL)
3848 {
af529f8f
JK
3849 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3850 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3851 if (this_real_name != NULL
af529f8f 3852 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3853 {
f5b95b50 3854 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3855 callback))
3856 return true;
288e77a7 3857 continue;
9291a0cd
TT
3858 }
3859 }
3860 }
3861 }
3862
14bc53a8 3863 return false;
9291a0cd
TT
3864}
3865
da51c347
DE
3866/* Struct used to manage iterating over all CUs looking for a symbol. */
3867
3868struct dw2_symtab_iterator
9291a0cd 3869{
ed2dc618
SM
3870 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3871 struct dwarf2_per_objfile *dwarf2_per_objfile;
da51c347
DE
3872 /* If non-zero, only look for symbols that match BLOCK_INDEX. */
3873 int want_specific_block;
3874 /* One of GLOBAL_BLOCK or STATIC_BLOCK.
3875 Unused if !WANT_SPECIFIC_BLOCK. */
3876 int block_index;
3877 /* The kind of symbol we're looking for. */
3878 domain_enum domain;
3879 /* The list of CUs from the index entry of the symbol,
3880 or NULL if not found. */
3881 offset_type *vec;
3882 /* The next element in VEC to look at. */
3883 int next;
3884 /* The number of elements in VEC, or zero if there is no match. */
3885 int length;
8943b874
DE
3886 /* Have we seen a global version of the symbol?
3887 If so we can ignore all further global instances.
3888 This is to work around gold/15646, inefficient gold-generated
3889 indices. */
3890 int global_seen;
da51c347 3891};
9291a0cd 3892
da51c347
DE
3893/* Initialize the index symtab iterator ITER.
3894 If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
3895 in block BLOCK_INDEX. Otherwise BLOCK_INDEX is ignored. */
2fdf6df6 3896
9291a0cd 3897static void
da51c347 3898dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3899 struct dwarf2_per_objfile *dwarf2_per_objfile,
da51c347
DE
3900 int want_specific_block,
3901 int block_index,
3902 domain_enum domain,
3903 const char *name)
3904{
ed2dc618 3905 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3906 iter->want_specific_block = want_specific_block;
3907 iter->block_index = block_index;
3908 iter->domain = domain;
3909 iter->next = 0;
8943b874 3910 iter->global_seen = 0;
da51c347 3911
3063847f 3912 mapped_index *index = dwarf2_per_objfile->index_table.get ();
ed2dc618
SM
3913
3914 /* index is NULL if OBJF_READNOW. */
3915 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3916 iter->length = MAYBE_SWAP (*iter->vec);
3917 else
3918 {
3919 iter->vec = NULL;
3920 iter->length = 0;
3921 }
3922}
3923
3924/* Return the next matching CU or NULL if there are no more. */
3925
3926static struct dwarf2_per_cu_data *
3927dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3928{
ed2dc618
SM
3929 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3930
da51c347
DE
3931 for ( ; iter->next < iter->length; ++iter->next)
3932 {
3933 offset_type cu_index_and_attrs =
3934 MAYBE_SWAP (iter->vec[iter->next + 1]);
3935 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3936 int want_static = iter->block_index != GLOBAL_BLOCK;
3937 /* This value is only valid for index versions >= 7. */
3938 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3939 gdb_index_symbol_kind symbol_kind =
3940 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3941 /* Only check the symbol attributes if they're present.
3942 Indices prior to version 7 don't record them,
3943 and indices >= 7 may elide them for certain symbols
3944 (gold does this). */
3945 int attrs_valid =
ed2dc618 3946 (dwarf2_per_objfile->index_table->version >= 7
da51c347
DE
3947 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3948
3190f0c6 3949 /* Don't crash on bad data. */
b76e467d 3950 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3951 + dwarf2_per_objfile->all_type_units.size ()))
3190f0c6 3952 {
b98664d3 3953 complaint (_(".gdb_index entry has bad CU index"
4262abfb
JK
3954 " [in module %s]"),
3955 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3956 continue;
3957 }
3958
ff4c9fec 3959 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3190f0c6 3960
da51c347 3961 /* Skip if already read in. */
43f3e411 3962 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
3963 continue;
3964
8943b874
DE
3965 /* Check static vs global. */
3966 if (attrs_valid)
3967 {
3968 if (iter->want_specific_block
3969 && want_static != is_static)
3970 continue;
3971 /* Work around gold/15646. */
3972 if (!is_static && iter->global_seen)
3973 continue;
3974 if (!is_static)
3975 iter->global_seen = 1;
3976 }
da51c347
DE
3977
3978 /* Only check the symbol's kind if it has one. */
3979 if (attrs_valid)
3980 {
3981 switch (iter->domain)
3982 {
3983 case VAR_DOMAIN:
3984 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3985 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3986 /* Some types are also in VAR_DOMAIN. */
3987 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3988 continue;
3989 break;
3990 case STRUCT_DOMAIN:
3991 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3992 continue;
3993 break;
3994 case LABEL_DOMAIN:
3995 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3996 continue;
3997 break;
3998 default:
3999 break;
4000 }
4001 }
4002
4003 ++iter->next;
4004 return per_cu;
4005 }
4006
4007 return NULL;
4008}
4009
43f3e411 4010static struct compunit_symtab *
da51c347
DE
4011dw2_lookup_symbol (struct objfile *objfile, int block_index,
4012 const char *name, domain_enum domain)
9291a0cd 4013{
43f3e411 4014 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
4015 struct dwarf2_per_objfile *dwarf2_per_objfile
4016 = get_dwarf2_per_objfile (objfile);
9291a0cd 4017
b5ec771e
PA
4018 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
4019
ed2dc618
SM
4020 struct dw2_symtab_iterator iter;
4021 struct dwarf2_per_cu_data *per_cu;
da51c347 4022
ed2dc618 4023 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 1, block_index, domain, name);
9291a0cd 4024
ed2dc618
SM
4025 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
4026 {
4027 struct symbol *sym, *with_opaque = NULL;
58f0c718 4028 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
ed2dc618
SM
4029 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
4030 struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 4031
ed2dc618
SM
4032 sym = block_find_symbol (block, name, domain,
4033 block_find_non_opaque_type_preferred,
4034 &with_opaque);
b2e2f908 4035
ed2dc618
SM
4036 /* Some caution must be observed with overloaded functions
4037 and methods, since the index will not contain any overload
4038 information (but NAME might contain it). */
da51c347 4039
ed2dc618
SM
4040 if (sym != NULL
4041 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
4042 return stab;
4043 if (with_opaque != NULL
4044 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
4045 stab_best = stab;
da51c347 4046
ed2dc618 4047 /* Keep looking through other CUs. */
9291a0cd 4048 }
9291a0cd 4049
da51c347 4050 return stab_best;
9291a0cd
TT
4051}
4052
4053static void
4054dw2_print_stats (struct objfile *objfile)
4055{
ed2dc618
SM
4056 struct dwarf2_per_objfile *dwarf2_per_objfile
4057 = get_dwarf2_per_objfile (objfile);
b76e467d 4058 int total = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4059 + dwarf2_per_objfile->all_type_units.size ());
ed2dc618 4060 int count = 0;
9291a0cd 4061
ed2dc618 4062 for (int i = 0; i < total; ++i)
9291a0cd 4063 {
ff4c9fec 4064 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 4065
43f3e411 4066 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4067 ++count;
4068 }
e4a48d9d 4069 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
4070 printf_filtered (_(" Number of unread CUs: %d\n"), count);
4071}
4072
779bd270
DE
4073/* This dumps minimal information about the index.
4074 It is called via "mt print objfiles".
4075 One use is to verify .gdb_index has been loaded by the
4076 gdb.dwarf2/gdb-index.exp testcase. */
4077
9291a0cd
TT
4078static void
4079dw2_dump (struct objfile *objfile)
4080{
ed2dc618
SM
4081 struct dwarf2_per_objfile *dwarf2_per_objfile
4082 = get_dwarf2_per_objfile (objfile);
4083
779bd270
DE
4084 gdb_assert (dwarf2_per_objfile->using_index);
4085 printf_filtered (".gdb_index:");
4086 if (dwarf2_per_objfile->index_table != NULL)
4087 {
4088 printf_filtered (" version %d\n",
4089 dwarf2_per_objfile->index_table->version);
4090 }
4091 else
4092 printf_filtered (" faked for \"readnow\"\n");
4093 printf_filtered ("\n");
9291a0cd
TT
4094}
4095
4096static void
3189cb12
DE
4097dw2_relocate (struct objfile *objfile,
4098 const struct section_offsets *new_offsets,
4099 const struct section_offsets *delta)
9291a0cd
TT
4100{
4101 /* There's nothing to relocate here. */
4102}
4103
4104static void
4105dw2_expand_symtabs_for_function (struct objfile *objfile,
4106 const char *func_name)
4107{
ed2dc618
SM
4108 struct dwarf2_per_objfile *dwarf2_per_objfile
4109 = get_dwarf2_per_objfile (objfile);
da51c347 4110
ed2dc618
SM
4111 struct dw2_symtab_iterator iter;
4112 struct dwarf2_per_cu_data *per_cu;
da51c347 4113
ed2dc618
SM
4114 /* Note: It doesn't matter what we pass for block_index here. */
4115 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, 0, GLOBAL_BLOCK, VAR_DOMAIN,
4116 func_name);
da51c347 4117
ed2dc618 4118 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
58f0c718 4119 dw2_instantiate_symtab (per_cu, false);
da51c347 4120
9291a0cd
TT
4121}
4122
4123static void
4124dw2_expand_all_symtabs (struct objfile *objfile)
4125{
ed2dc618
SM
4126 struct dwarf2_per_objfile *dwarf2_per_objfile
4127 = get_dwarf2_per_objfile (objfile);
b76e467d 4128 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4129 + dwarf2_per_objfile->all_type_units.size ());
9291a0cd 4130
ed2dc618 4131 for (int i = 0; i < total_units; ++i)
9291a0cd 4132 {
ff4c9fec 4133 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 4134
58f0c718
TT
4135 /* We don't want to directly expand a partial CU, because if we
4136 read it with the wrong language, then assertion failures can
4137 be triggered later on. See PR symtab/23010. So, tell
4138 dw2_instantiate_symtab to skip partial CUs -- any important
4139 partial CU will be read via DW_TAG_imported_unit anyway. */
4140 dw2_instantiate_symtab (per_cu, true);
9291a0cd
TT
4141 }
4142}
4143
4144static void
652a8996
JK
4145dw2_expand_symtabs_with_fullname (struct objfile *objfile,
4146 const char *fullname)
9291a0cd 4147{
ed2dc618
SM
4148 struct dwarf2_per_objfile *dwarf2_per_objfile
4149 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
4150
4151 /* We don't need to consider type units here.
4152 This is only called for examining code, e.g. expand_line_sal.
4153 There can be an order of magnitude (or more) more type units
4154 than comp units, and we avoid them if we can. */
4155
b76e467d 4156 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 4157 {
3d7bb9d9 4158 /* We only need to look at symtabs not already expanded. */
43f3e411 4159 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
4160 continue;
4161
b76e467d 4162 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 4163 if (file_data == NULL)
9291a0cd
TT
4164 continue;
4165
b76e467d 4166 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 4167 {
652a8996
JK
4168 const char *this_fullname = file_data->file_names[j];
4169
4170 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 4171 {
58f0c718 4172 dw2_instantiate_symtab (per_cu, false);
9291a0cd
TT
4173 break;
4174 }
4175 }
4176 }
4177}
4178
9291a0cd 4179static void
ade7ed9e 4180dw2_map_matching_symbols (struct objfile *objfile,
fe978cb0 4181 const char * name, domain_enum domain,
ade7ed9e 4182 int global,
40658b94
PH
4183 int (*callback) (struct block *,
4184 struct symbol *, void *),
b5ec771e 4185 void *data, symbol_name_match_type match,
2edb89d3 4186 symbol_compare_ftype *ordered_compare)
9291a0cd 4187{
40658b94 4188 /* Currently unimplemented; used for Ada. The function can be called if the
a9e6a4bb
JK
4189 current language is Ada for a non-Ada objfile using GNU index. As Ada
4190 does not look for non-Ada symbols this function should just return. */
9291a0cd
TT
4191}
4192
b5ec771e
PA
4193/* Symbol name matcher for .gdb_index names.
4194
4195 Symbol names in .gdb_index have a few particularities:
4196
4197 - There's no indication of which is the language of each symbol.
4198
4199 Since each language has its own symbol name matching algorithm,
4200 and we don't know which language is the right one, we must match
3f563c84
PA
4201 each symbol against all languages. This would be a potential
4202 performance problem if it were not mitigated by the
4203 mapped_index::name_components lookup table, which significantly
4204 reduces the number of times we need to call into this matcher,
4205 making it a non-issue.
b5ec771e
PA
4206
4207 - Symbol names in the index have no overload (parameter)
4208 information. I.e., in C++, "foo(int)" and "foo(long)" both
4209 appear as "foo" in the index, for example.
4210
4211 This means that the lookup names passed to the symbol name
4212 matcher functions must have no parameter information either
4213 because (e.g.) symbol search name "foo" does not match
4214 lookup-name "foo(int)" [while swapping search name for lookup
4215 name would match].
4216*/
4217class gdb_index_symbol_name_matcher
4218{
4219public:
4220 /* Prepares the vector of comparison functions for LOOKUP_NAME. */
4221 gdb_index_symbol_name_matcher (const lookup_name_info &lookup_name);
4222
4223 /* Walk all the matcher routines and match SYMBOL_NAME against them.
4224 Returns true if any matcher matches. */
4225 bool matches (const char *symbol_name);
4226
4227private:
4228 /* A reference to the lookup name we're matching against. */
4229 const lookup_name_info &m_lookup_name;
4230
4231 /* A vector holding all the different symbol name matchers, for all
4232 languages. */
4233 std::vector<symbol_name_matcher_ftype *> m_symbol_name_matcher_funcs;
4234};
4235
4236gdb_index_symbol_name_matcher::gdb_index_symbol_name_matcher
4237 (const lookup_name_info &lookup_name)
4238 : m_lookup_name (lookup_name)
4239{
4240 /* Prepare the vector of comparison functions upfront, to avoid
4241 doing the same work for each symbol. Care is taken to avoid
4242 matching with the same matcher more than once if/when multiple
4243 languages use the same matcher function. */
4244 auto &matchers = m_symbol_name_matcher_funcs;
4245 matchers.reserve (nr_languages);
4246
4247 matchers.push_back (default_symbol_name_matcher);
4248
4249 for (int i = 0; i < nr_languages; i++)
4250 {
4251 const language_defn *lang = language_def ((enum language) i);
c63d3e8d 4252 symbol_name_matcher_ftype *name_matcher
618daa93 4253 = get_symbol_name_matcher (lang, m_lookup_name);
c63d3e8d
PA
4254
4255 /* Don't insert the same comparison routine more than once.
4256 Note that we do this linear walk instead of a seemingly
4257 cheaper sorted insert, or use a std::set or something like
4258 that, because relative order of function addresses is not
4259 stable. This is not a problem in practice because the number
4260 of supported languages is low, and the cost here is tiny
4261 compared to the number of searches we'll do afterwards using
4262 this object. */
4263 if (name_matcher != default_symbol_name_matcher
4264 && (std::find (matchers.begin (), matchers.end (), name_matcher)
4265 == matchers.end ()))
4266 matchers.push_back (name_matcher);
b5ec771e
PA
4267 }
4268}
4269
4270bool
4271gdb_index_symbol_name_matcher::matches (const char *symbol_name)
4272{
4273 for (auto matches_name : m_symbol_name_matcher_funcs)
4274 if (matches_name (symbol_name, m_lookup_name, NULL))
4275 return true;
4276
4277 return false;
4278}
4279
e1ef7d7a
PA
4280/* Starting from a search name, return the string that finds the upper
4281 bound of all strings that start with SEARCH_NAME in a sorted name
4282 list. Returns the empty string to indicate that the upper bound is
4283 the end of the list. */
4284
4285static std::string
4286make_sort_after_prefix_name (const char *search_name)
4287{
4288 /* When looking to complete "func", we find the upper bound of all
4289 symbols that start with "func" by looking for where we'd insert
4290 the closest string that would follow "func" in lexicographical
4291 order. Usually, that's "func"-with-last-character-incremented,
4292 i.e. "fund". Mind non-ASCII characters, though. Usually those
4293 will be UTF-8 multi-byte sequences, but we can't be certain.
4294 Especially mind the 0xff character, which is a valid character in
4295 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
4296 rule out compilers allowing it in identifiers. Note that
4297 conveniently, strcmp/strcasecmp are specified to compare
4298 characters interpreted as unsigned char. So what we do is treat
4299 the whole string as a base 256 number composed of a sequence of
4300 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
4301 to 0, and carries 1 to the following more-significant position.
4302 If the very first character in SEARCH_NAME ends up incremented
4303 and carries/overflows, then the upper bound is the end of the
4304 list. The string after the empty string is also the empty
4305 string.
4306
4307 Some examples of this operation:
4308
4309 SEARCH_NAME => "+1" RESULT
4310
4311 "abc" => "abd"
4312 "ab\xff" => "ac"
4313 "\xff" "a" "\xff" => "\xff" "b"
4314 "\xff" => ""
4315 "\xff\xff" => ""
4316 "" => ""
4317
4318 Then, with these symbols for example:
4319
4320 func
4321 func1
4322 fund
4323
4324 completing "func" looks for symbols between "func" and
4325 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4326 which finds "func" and "func1", but not "fund".
4327
4328 And with:
4329
4330 funcÿ (Latin1 'ÿ' [0xff])
4331 funcÿ1
4332 fund
4333
4334 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4335 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4336
4337 And with:
4338
4339 ÿÿ (Latin1 'ÿ' [0xff])
4340 ÿÿ1
4341
4342 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4343 the end of the list.
4344 */
4345 std::string after = search_name;
4346 while (!after.empty () && (unsigned char) after.back () == 0xff)
4347 after.pop_back ();
4348 if (!after.empty ())
4349 after.back () = (unsigned char) after.back () + 1;
4350 return after;
4351}
4352
5c58de74 4353/* See declaration. */
61d96d7e 4354
5c58de74
PA
4355std::pair<std::vector<name_component>::const_iterator,
4356 std::vector<name_component>::const_iterator>
44ed8f3e 4357mapped_index_base::find_name_components_bounds
5c58de74 4358 (const lookup_name_info &lookup_name_without_params) const
3f563c84 4359{
5c58de74
PA
4360 auto *name_cmp
4361 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84
PA
4362
4363 const char *cplus
c62446b1 4364 = lookup_name_without_params.cplus ().lookup_name ().c_str ();
9291a0cd 4365
3f563c84
PA
4366 /* Comparison function object for lower_bound that matches against a
4367 given symbol name. */
4368 auto lookup_compare_lower = [&] (const name_component &elem,
4369 const char *name)
4370 {
5c58de74 4371 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4372 const char *elem_name = elem_qualified + elem.name_offset;
4373 return name_cmp (elem_name, name) < 0;
4374 };
4375
4376 /* Comparison function object for upper_bound that matches against a
4377 given symbol name. */
4378 auto lookup_compare_upper = [&] (const char *name,
4379 const name_component &elem)
4380 {
5c58de74 4381 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
4382 const char *elem_name = elem_qualified + elem.name_offset;
4383 return name_cmp (name, elem_name) < 0;
4384 };
4385
5c58de74
PA
4386 auto begin = this->name_components.begin ();
4387 auto end = this->name_components.end ();
3f563c84
PA
4388
4389 /* Find the lower bound. */
4390 auto lower = [&] ()
4391 {
5c58de74 4392 if (lookup_name_without_params.completion_mode () && cplus[0] == '\0')
3f563c84
PA
4393 return begin;
4394 else
4395 return std::lower_bound (begin, end, cplus, lookup_compare_lower);
4396 } ();
4397
4398 /* Find the upper bound. */
4399 auto upper = [&] ()
4400 {
5c58de74 4401 if (lookup_name_without_params.completion_mode ())
3f563c84 4402 {
e1ef7d7a
PA
4403 /* In completion mode, we want UPPER to point past all
4404 symbols names that have the same prefix. I.e., with
4405 these symbols, and completing "func":
4406
4407 function << lower bound
4408 function1
4409 other_function << upper bound
4410
4411 We find the upper bound by looking for the insertion
4412 point of "func"-with-last-character-incremented,
4413 i.e. "fund". */
4414 std::string after = make_sort_after_prefix_name (cplus);
4415 if (after.empty ())
3f563c84 4416 return end;
e6b2f5ef
PA
4417 return std::lower_bound (lower, end, after.c_str (),
4418 lookup_compare_lower);
3f563c84
PA
4419 }
4420 else
4421 return std::upper_bound (lower, end, cplus, lookup_compare_upper);
4422 } ();
4423
5c58de74
PA
4424 return {lower, upper};
4425}
4426
4427/* See declaration. */
4428
4429void
44ed8f3e 4430mapped_index_base::build_name_components ()
5c58de74
PA
4431{
4432 if (!this->name_components.empty ())
4433 return;
4434
4435 this->name_components_casing = case_sensitivity;
4436 auto *name_cmp
4437 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4438
4439 /* The code below only knows how to break apart components of C++
4440 symbol names (and other languages that use '::' as
4441 namespace/module separator). If we add support for wild matching
4442 to some language that uses some other operator (E.g., Ada, Go and
4443 D use '.'), then we'll need to try splitting the symbol name
4444 according to that language too. Note that Ada does support wild
4445 matching, but doesn't currently support .gdb_index. */
44ed8f3e
PA
4446 auto count = this->symbol_name_count ();
4447 for (offset_type idx = 0; idx < count; idx++)
5c58de74 4448 {
44ed8f3e 4449 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
4450 continue;
4451
4452 const char *name = this->symbol_name_at (idx);
4453
4454 /* Add each name component to the name component table. */
4455 unsigned int previous_len = 0;
4456 for (unsigned int current_len = cp_find_first_component (name);
4457 name[current_len] != '\0';
4458 current_len += cp_find_first_component (name + current_len))
4459 {
4460 gdb_assert (name[current_len] == ':');
4461 this->name_components.push_back ({previous_len, idx});
4462 /* Skip the '::'. */
4463 current_len += 2;
4464 previous_len = current_len;
4465 }
4466 this->name_components.push_back ({previous_len, idx});
4467 }
4468
4469 /* Sort name_components elements by name. */
4470 auto name_comp_compare = [&] (const name_component &left,
4471 const name_component &right)
4472 {
4473 const char *left_qualified = this->symbol_name_at (left.idx);
4474 const char *right_qualified = this->symbol_name_at (right.idx);
4475
4476 const char *left_name = left_qualified + left.name_offset;
4477 const char *right_name = right_qualified + right.name_offset;
4478
4479 return name_cmp (left_name, right_name) < 0;
4480 };
4481
4482 std::sort (this->name_components.begin (),
4483 this->name_components.end (),
4484 name_comp_compare);
4485}
4486
4487/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4488 mapped_index_base instead of the containing objfile. This is split
4489 to a separate function in order to be able to unit test the
4490 name_components matching using a mock mapped_index_base. For each
5c58de74 4491 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4492 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4493
4494static void
4495dw2_expand_symtabs_matching_symbol
44ed8f3e 4496 (mapped_index_base &index,
5c58de74
PA
4497 const lookup_name_info &lookup_name_in,
4498 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4499 enum search_domain kind,
4500 gdb::function_view<void (offset_type)> match_callback)
4501{
4502 lookup_name_info lookup_name_without_params
4503 = lookup_name_in.make_ignore_params ();
4504 gdb_index_symbol_name_matcher lookup_name_matcher
4505 (lookup_name_without_params);
4506
4507 /* Build the symbol name component sorted vector, if we haven't
4508 yet. */
4509 index.build_name_components ();
4510
4511 auto bounds = index.find_name_components_bounds (lookup_name_without_params);
4512
3f563c84
PA
4513 /* Now for each symbol name in range, check to see if we have a name
4514 match, and if so, call the MATCH_CALLBACK callback. */
4515
4516 /* The same symbol may appear more than once in the range though.
4517 E.g., if we're looking for symbols that complete "w", and we have
4518 a symbol named "w1::w2", we'll find the two name components for
4519 that same symbol in the range. To be sure we only call the
4520 callback once per symbol, we first collect the symbol name
4521 indexes that matched in a temporary vector and ignore
4522 duplicates. */
4523 std::vector<offset_type> matches;
5c58de74 4524 matches.reserve (std::distance (bounds.first, bounds.second));
3f563c84 4525
5c58de74 4526 for (; bounds.first != bounds.second; ++bounds.first)
3f563c84 4527 {
5c58de74 4528 const char *qualified = index.symbol_name_at (bounds.first->idx);
3f563c84
PA
4529
4530 if (!lookup_name_matcher.matches (qualified)
4531 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
9291a0cd
TT
4532 continue;
4533
5c58de74 4534 matches.push_back (bounds.first->idx);
3f563c84
PA
4535 }
4536
4537 std::sort (matches.begin (), matches.end ());
4538
4539 /* Finally call the callback, once per match. */
4540 ULONGEST prev = -1;
4541 for (offset_type idx : matches)
4542 {
4543 if (prev != idx)
4544 {
4545 match_callback (idx);
4546 prev = idx;
4547 }
4548 }
4549
4550 /* Above we use a type wider than idx's for 'prev', since 0 and
4551 (offset_type)-1 are both possible values. */
4552 static_assert (sizeof (prev) > sizeof (offset_type), "");
4553}
4554
c62446b1
PA
4555#if GDB_SELF_TEST
4556
4557namespace selftests { namespace dw2_expand_symtabs_matching {
4558
a3c5fafd
PA
4559/* A mock .gdb_index/.debug_names-like name index table, enough to
4560 exercise dw2_expand_symtabs_matching_symbol, which works with the
4561 mapped_index_base interface. Builds an index from the symbol list
4562 passed as parameter to the constructor. */
4563class mock_mapped_index : public mapped_index_base
c62446b1
PA
4564{
4565public:
a3c5fafd
PA
4566 mock_mapped_index (gdb::array_view<const char *> symbols)
4567 : m_symbol_table (symbols)
c62446b1
PA
4568 {}
4569
a3c5fafd 4570 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4571
a3c5fafd 4572 /* Return the number of names in the symbol table. */
632e107b 4573 size_t symbol_name_count () const override
c62446b1 4574 {
a3c5fafd 4575 return m_symbol_table.size ();
c62446b1
PA
4576 }
4577
a3c5fafd 4578 /* Get the name of the symbol at IDX in the symbol table. */
632e107b 4579 const char *symbol_name_at (offset_type idx) const override
a3c5fafd
PA
4580 {
4581 return m_symbol_table[idx];
4582 }
c62446b1 4583
a3c5fafd
PA
4584private:
4585 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4586};
4587
4588/* Convenience function that converts a NULL pointer to a "<null>"
4589 string, to pass to print routines. */
4590
4591static const char *
4592string_or_null (const char *str)
4593{
4594 return str != NULL ? str : "<null>";
4595}
4596
4597/* Check if a lookup_name_info built from
4598 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4599 index. EXPECTED_LIST is the list of expected matches, in expected
4600 matching order. If no match expected, then an empty list is
4601 specified. Returns true on success. On failure prints a warning
4602 indicating the file:line that failed, and returns false. */
4603
4604static bool
4605check_match (const char *file, int line,
4606 mock_mapped_index &mock_index,
4607 const char *name, symbol_name_match_type match_type,
4608 bool completion_mode,
4609 std::initializer_list<const char *> expected_list)
4610{
4611 lookup_name_info lookup_name (name, match_type, completion_mode);
4612
4613 bool matched = true;
4614
4615 auto mismatch = [&] (const char *expected_str,
4616 const char *got)
4617 {
4618 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4619 "expected=\"%s\", got=\"%s\"\n"),
4620 file, line,
4621 (match_type == symbol_name_match_type::FULL
4622 ? "FULL" : "WILD"),
4623 name, string_or_null (expected_str), string_or_null (got));
4624 matched = false;
4625 };
4626
4627 auto expected_it = expected_list.begin ();
4628 auto expected_end = expected_list.end ();
4629
a3c5fafd 4630 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4631 NULL, ALL_DOMAIN,
4632 [&] (offset_type idx)
4633 {
a3c5fafd 4634 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4635 const char *expected_str
4636 = expected_it == expected_end ? NULL : *expected_it++;
4637
4638 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4639 mismatch (expected_str, matched_name);
4640 });
4641
4642 const char *expected_str
4643 = expected_it == expected_end ? NULL : *expected_it++;
4644 if (expected_str != NULL)
4645 mismatch (expected_str, NULL);
4646
4647 return matched;
4648}
4649
4650/* The symbols added to the mock mapped_index for testing (in
4651 canonical form). */
4652static const char *test_symbols[] = {
4653 "function",
4654 "std::bar",
4655 "std::zfunction",
4656 "std::zfunction2",
4657 "w1::w2",
4658 "ns::foo<char*>",
4659 "ns::foo<int>",
4660 "ns::foo<long>",
a20714ff
PA
4661 "ns2::tmpl<int>::foo2",
4662 "(anonymous namespace)::A::B::C",
c62446b1 4663
e1ef7d7a
PA
4664 /* These are used to check that the increment-last-char in the
4665 matching algorithm for completion doesn't match "t1_fund" when
4666 completing "t1_func". */
4667 "t1_func",
4668 "t1_func1",
4669 "t1_fund",
4670 "t1_fund1",
4671
4672 /* A UTF-8 name with multi-byte sequences to make sure that
4673 cp-name-parser understands this as a single identifier ("função"
4674 is "function" in PT). */
4675 u8"u8função",
4676
4677 /* \377 (0xff) is Latin1 'ÿ'. */
4678 "yfunc\377",
4679
4680 /* \377 (0xff) is Latin1 'ÿ'. */
4681 "\377",
4682 "\377\377123",
4683
c62446b1
PA
4684 /* A name with all sorts of complications. Starts with "z" to make
4685 it easier for the completion tests below. */
4686#define Z_SYM_NAME \
4687 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4688 "::tuple<(anonymous namespace)::ui*, " \
4689 "std::default_delete<(anonymous namespace)::ui>, void>"
4690
4691 Z_SYM_NAME
4692};
4693
a3c5fafd
PA
4694/* Returns true if the mapped_index_base::find_name_component_bounds
4695 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4696 in completion mode. */
5c58de74
PA
4697
4698static bool
a3c5fafd 4699check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4700 const char *search_name,
4701 gdb::array_view<const char *> expected_syms)
4702{
4703 lookup_name_info lookup_name (search_name,
4704 symbol_name_match_type::FULL, true);
4705
4706 auto bounds = index.find_name_components_bounds (lookup_name);
4707
4708 size_t distance = std::distance (bounds.first, bounds.second);
4709 if (distance != expected_syms.size ())
4710 return false;
4711
4712 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4713 {
4714 auto nc_elem = bounds.first + exp_elem;
4715 const char *qualified = index.symbol_name_at (nc_elem->idx);
4716 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4717 return false;
4718 }
4719
4720 return true;
4721}
4722
4723/* Test the lower-level mapped_index::find_name_component_bounds
4724 method. */
4725
c62446b1 4726static void
5c58de74
PA
4727test_mapped_index_find_name_component_bounds ()
4728{
4729 mock_mapped_index mock_index (test_symbols);
4730
a3c5fafd 4731 mock_index.build_name_components ();
5c58de74
PA
4732
4733 /* Test the lower-level mapped_index::find_name_component_bounds
4734 method in completion mode. */
4735 {
4736 static const char *expected_syms[] = {
4737 "t1_func",
4738 "t1_func1",
5c58de74
PA
4739 };
4740
a3c5fafd 4741 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4742 "t1_func", expected_syms));
4743 }
4744
4745 /* Check that the increment-last-char in the name matching algorithm
4746 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4747 {
4748 static const char *expected_syms1[] = {
4749 "\377",
4750 "\377\377123",
4751 };
a3c5fafd 4752 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4753 "\377", expected_syms1));
4754
4755 static const char *expected_syms2[] = {
4756 "\377\377123",
4757 };
a3c5fafd 4758 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4759 "\377\377", expected_syms2));
4760 }
4761}
4762
4763/* Test dw2_expand_symtabs_matching_symbol. */
4764
4765static void
4766test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4767{
4768 mock_mapped_index mock_index (test_symbols);
4769
4770 /* We let all tests run until the end even if some fails, for debug
4771 convenience. */
4772 bool any_mismatch = false;
4773
4774 /* Create the expected symbols list (an initializer_list). Needed
4775 because lists have commas, and we need to pass them to CHECK,
4776 which is a macro. */
4777#define EXPECT(...) { __VA_ARGS__ }
4778
4779 /* Wrapper for check_match that passes down the current
4780 __FILE__/__LINE__. */
4781#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4782 any_mismatch |= !check_match (__FILE__, __LINE__, \
4783 mock_index, \
4784 NAME, MATCH_TYPE, COMPLETION_MODE, \
4785 EXPECTED_LIST)
4786
4787 /* Identity checks. */
4788 for (const char *sym : test_symbols)
4789 {
4790 /* Should be able to match all existing symbols. */
4791 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4792 EXPECT (sym));
4793
4794 /* Should be able to match all existing symbols with
4795 parameters. */
4796 std::string with_params = std::string (sym) + "(int)";
4797 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4798 EXPECT (sym));
4799
4800 /* Should be able to match all existing symbols with
4801 parameters and qualifiers. */
4802 with_params = std::string (sym) + " ( int ) const";
4803 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4804 EXPECT (sym));
4805
4806 /* This should really find sym, but cp-name-parser.y doesn't
4807 know about lvalue/rvalue qualifiers yet. */
4808 with_params = std::string (sym) + " ( int ) &&";
4809 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4810 {});
4811 }
4812
e1ef7d7a
PA
4813 /* Check that the name matching algorithm for completion doesn't get
4814 confused with Latin1 'ÿ' / 0xff. */
4815 {
4816 static const char str[] = "\377";
4817 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4818 EXPECT ("\377", "\377\377123"));
4819 }
4820
4821 /* Check that the increment-last-char in the matching algorithm for
4822 completion doesn't match "t1_fund" when completing "t1_func". */
4823 {
4824 static const char str[] = "t1_func";
4825 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4826 EXPECT ("t1_func", "t1_func1"));
4827 }
4828
c62446b1
PA
4829 /* Check that completion mode works at each prefix of the expected
4830 symbol name. */
4831 {
4832 static const char str[] = "function(int)";
4833 size_t len = strlen (str);
4834 std::string lookup;
4835
4836 for (size_t i = 1; i < len; i++)
4837 {
4838 lookup.assign (str, i);
4839 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4840 EXPECT ("function"));
4841 }
4842 }
4843
4844 /* While "w" is a prefix of both components, the match function
4845 should still only be called once. */
4846 {
4847 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4848 EXPECT ("w1::w2"));
a20714ff
PA
4849 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4850 EXPECT ("w1::w2"));
c62446b1
PA
4851 }
4852
4853 /* Same, with a "complicated" symbol. */
4854 {
4855 static const char str[] = Z_SYM_NAME;
4856 size_t len = strlen (str);
4857 std::string lookup;
4858
4859 for (size_t i = 1; i < len; i++)
4860 {
4861 lookup.assign (str, i);
4862 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4863 EXPECT (Z_SYM_NAME));
4864 }
4865 }
4866
4867 /* In FULL mode, an incomplete symbol doesn't match. */
4868 {
4869 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4870 {});
4871 }
4872
4873 /* A complete symbol with parameters matches any overload, since the
4874 index has no overload info. */
4875 {
4876 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4877 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4878 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4879 EXPECT ("std::zfunction", "std::zfunction2"));
4880 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4881 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4882 }
4883
4884 /* Check that whitespace is ignored appropriately. A symbol with a
4885 template argument list. */
4886 {
4887 static const char expected[] = "ns::foo<int>";
4888 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4889 EXPECT (expected));
a20714ff
PA
4890 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4891 EXPECT (expected));
c62446b1
PA
4892 }
4893
4894 /* Check that whitespace is ignored appropriately. A symbol with a
4895 template argument list that includes a pointer. */
4896 {
4897 static const char expected[] = "ns::foo<char*>";
4898 /* Try both completion and non-completion modes. */
4899 static const bool completion_mode[2] = {false, true};
4900 for (size_t i = 0; i < 2; i++)
4901 {
4902 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4903 completion_mode[i], EXPECT (expected));
a20714ff
PA
4904 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4905 completion_mode[i], EXPECT (expected));
c62446b1
PA
4906
4907 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4908 completion_mode[i], EXPECT (expected));
a20714ff
PA
4909 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4910 completion_mode[i], EXPECT (expected));
c62446b1
PA
4911 }
4912 }
4913
4914 {
4915 /* Check method qualifiers are ignored. */
4916 static const char expected[] = "ns::foo<char*>";
4917 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4918 symbol_name_match_type::FULL, true, EXPECT (expected));
4919 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4920 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4921 CHECK_MATCH ("foo < char * > ( int ) const",
4922 symbol_name_match_type::WILD, true, EXPECT (expected));
4923 CHECK_MATCH ("foo < char * > ( int ) &&",
4924 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4925 }
4926
4927 /* Test lookup names that don't match anything. */
4928 {
a20714ff
PA
4929 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4930 {});
4931
c62446b1
PA
4932 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4933 {});
4934 }
4935
a20714ff
PA
4936 /* Some wild matching tests, exercising "(anonymous namespace)",
4937 which should not be confused with a parameter list. */
4938 {
4939 static const char *syms[] = {
4940 "A::B::C",
4941 "B::C",
4942 "C",
4943 "A :: B :: C ( int )",
4944 "B :: C ( int )",
4945 "C ( int )",
4946 };
4947
4948 for (const char *s : syms)
4949 {
4950 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4951 EXPECT ("(anonymous namespace)::A::B::C"));
4952 }
4953 }
4954
4955 {
4956 static const char expected[] = "ns2::tmpl<int>::foo2";
4957 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4958 EXPECT (expected));
4959 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4960 EXPECT (expected));
4961 }
4962
c62446b1
PA
4963 SELF_CHECK (!any_mismatch);
4964
4965#undef EXPECT
4966#undef CHECK_MATCH
4967}
4968
5c58de74
PA
4969static void
4970run_test ()
4971{
4972 test_mapped_index_find_name_component_bounds ();
4973 test_dw2_expand_symtabs_matching_symbol ();
4974}
4975
c62446b1
PA
4976}} // namespace selftests::dw2_expand_symtabs_matching
4977
4978#endif /* GDB_SELF_TEST */
4979
4b514bc8
JK
4980/* If FILE_MATCHER is NULL or if PER_CU has
4981 dwarf2_per_cu_quick_data::MARK set (see
4982 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4983 EXPANSION_NOTIFY on it. */
4984
4985static void
4986dw2_expand_symtabs_matching_one
4987 (struct dwarf2_per_cu_data *per_cu,
4988 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4989 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4990{
4991 if (file_matcher == NULL || per_cu->v.quick->mark)
4992 {
4993 bool symtab_was_null
4994 = (per_cu->v.quick->compunit_symtab == NULL);
4995
58f0c718 4996 dw2_instantiate_symtab (per_cu, false);
4b514bc8
JK
4997
4998 if (expansion_notify != NULL
4999 && symtab_was_null
5000 && per_cu->v.quick->compunit_symtab != NULL)
5001 expansion_notify (per_cu->v.quick->compunit_symtab);
5002 }
5003}
5004
3f563c84
PA
5005/* Helper for dw2_expand_matching symtabs. Called on each symbol
5006 matched, to expand corresponding CUs that were marked. IDX is the
5007 index of the symbol name that matched. */
5008
5009static void
5010dw2_expand_marked_cus
ed2dc618 5011 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
5012 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5013 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5014 search_domain kind)
5015{
3f563c84
PA
5016 offset_type *vec, vec_len, vec_idx;
5017 bool global_seen = false;
ed2dc618 5018 mapped_index &index = *dwarf2_per_objfile->index_table;
3f563c84 5019
61920122 5020 vec = (offset_type *) (index.constant_pool
f00a2de2 5021 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
5022 vec_len = MAYBE_SWAP (vec[0]);
5023 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
5024 {
61920122
PA
5025 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
5026 /* This value is only valid for index versions >= 7. */
5027 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
5028 gdb_index_symbol_kind symbol_kind =
5029 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
5030 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
5031 /* Only check the symbol attributes if they're present.
5032 Indices prior to version 7 don't record them,
5033 and indices >= 7 may elide them for certain symbols
5034 (gold does this). */
5035 int attrs_valid =
5036 (index.version >= 7
5037 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
5038
5039 /* Work around gold/15646. */
5040 if (attrs_valid)
9291a0cd 5041 {
61920122
PA
5042 if (!is_static && global_seen)
5043 continue;
5044 if (!is_static)
5045 global_seen = true;
5046 }
3190f0c6 5047
61920122
PA
5048 /* Only check the symbol's kind if it has one. */
5049 if (attrs_valid)
5050 {
5051 switch (kind)
8943b874 5052 {
61920122
PA
5053 case VARIABLES_DOMAIN:
5054 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
5055 continue;
5056 break;
5057 case FUNCTIONS_DOMAIN:
5058 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 5059 continue;
61920122
PA
5060 break;
5061 case TYPES_DOMAIN:
5062 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
5063 continue;
5064 break;
5065 default:
5066 break;
8943b874 5067 }
61920122 5068 }
8943b874 5069
61920122 5070 /* Don't crash on bad data. */
b76e467d 5071 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 5072 + dwarf2_per_objfile->all_type_units.size ()))
61920122 5073 {
b98664d3 5074 complaint (_(".gdb_index entry has bad CU index"
ed2dc618
SM
5075 " [in module %s]"),
5076 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
5077 continue;
5078 }
5079
ff4c9fec 5080 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4b514bc8
JK
5081 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5082 expansion_notify);
61920122
PA
5083 }
5084}
5085
4b514bc8
JK
5086/* If FILE_MATCHER is non-NULL, set all the
5087 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
5088 that match FILE_MATCHER. */
5089
61920122 5090static void
4b514bc8 5091dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
5092 (struct dwarf2_per_objfile *dwarf2_per_objfile,
5093 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 5094{
4b514bc8 5095 if (file_matcher == NULL)
61920122
PA
5096 return;
5097
4b514bc8
JK
5098 objfile *const objfile = dwarf2_per_objfile->objfile;
5099
5100 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
5101 htab_eq_pointer,
5102 NULL, xcalloc, xfree));
5103 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
5104 htab_eq_pointer,
5105 NULL, xcalloc, xfree));
61920122 5106
4b514bc8
JK
5107 /* The rule is CUs specify all the files, including those used by
5108 any TU, so there's no need to scan TUs here. */
61920122 5109
b76e467d 5110 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5111 {
927aa2e7
JK
5112 QUIT;
5113
5114 per_cu->v.quick->mark = 0;
5115
5116 /* We only need to look at symtabs not already expanded. */
5117 if (per_cu->v.quick->compunit_symtab)
5118 continue;
5119
b76e467d 5120 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
5121 if (file_data == NULL)
5122 continue;
5123
5124 if (htab_find (visited_not_found.get (), file_data) != NULL)
5125 continue;
5126 else if (htab_find (visited_found.get (), file_data) != NULL)
5127 {
5128 per_cu->v.quick->mark = 1;
5129 continue;
5130 }
5131
b76e467d 5132 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
5133 {
5134 const char *this_real_name;
5135
5136 if (file_matcher (file_data->file_names[j], false))
5137 {
5138 per_cu->v.quick->mark = 1;
5139 break;
5140 }
5141
5142 /* Before we invoke realpath, which can get expensive when many
5143 files are involved, do a quick comparison of the basenames. */
5144 if (!basenames_may_differ
5145 && !file_matcher (lbasename (file_data->file_names[j]),
5146 true))
5147 continue;
5148
5149 this_real_name = dw2_get_real_path (objfile, file_data, j);
5150 if (file_matcher (this_real_name, false))
5151 {
5152 per_cu->v.quick->mark = 1;
5153 break;
5154 }
5155 }
5156
b76e467d
SM
5157 void **slot = htab_find_slot (per_cu->v.quick->mark
5158 ? visited_found.get ()
5159 : visited_not_found.get (),
5160 file_data, INSERT);
927aa2e7
JK
5161 *slot = file_data;
5162 }
5163}
5164
5165static void
5166dw2_expand_symtabs_matching
5167 (struct objfile *objfile,
5168 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5169 const lookup_name_info &lookup_name,
5170 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5171 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5172 enum search_domain kind)
5173{
ed2dc618
SM
5174 struct dwarf2_per_objfile *dwarf2_per_objfile
5175 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5176
5177 /* index_table is NULL if OBJF_READNOW. */
5178 if (!dwarf2_per_objfile->index_table)
5179 return;
5180
ed2dc618 5181 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7
JK
5182
5183 mapped_index &index = *dwarf2_per_objfile->index_table;
5184
5185 dw2_expand_symtabs_matching_symbol (index, lookup_name,
5186 symbol_matcher,
5187 kind, [&] (offset_type idx)
5188 {
ed2dc618 5189 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7
JK
5190 expansion_notify, kind);
5191 });
5192}
5193
5194/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
5195 symtab. */
5196
5197static struct compunit_symtab *
5198recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
5199 CORE_ADDR pc)
5200{
5201 int i;
5202
5203 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
5204 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
5205 return cust;
5206
5207 if (cust->includes == NULL)
5208 return NULL;
5209
5210 for (i = 0; cust->includes[i]; ++i)
5211 {
5212 struct compunit_symtab *s = cust->includes[i];
5213
5214 s = recursively_find_pc_sect_compunit_symtab (s, pc);
5215 if (s != NULL)
5216 return s;
5217 }
5218
5219 return NULL;
5220}
5221
5222static struct compunit_symtab *
5223dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
5224 struct bound_minimal_symbol msymbol,
5225 CORE_ADDR pc,
5226 struct obj_section *section,
5227 int warn_if_readin)
5228{
5229 struct dwarf2_per_cu_data *data;
5230 struct compunit_symtab *result;
5231
927aa2e7
JK
5232 if (!objfile->psymtabs_addrmap)
5233 return NULL;
5234
5235 data = (struct dwarf2_per_cu_data *) addrmap_find (objfile->psymtabs_addrmap,
5236 pc);
5237 if (!data)
5238 return NULL;
5239
5240 if (warn_if_readin && data->v.quick->compunit_symtab)
5241 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
5242 paddress (get_objfile_arch (objfile), pc));
5243
5244 result
58f0c718
TT
5245 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
5246 false),
927aa2e7
JK
5247 pc);
5248 gdb_assert (result != NULL);
5249 return result;
5250}
5251
5252static void
5253dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5254 void *data, int need_fullname)
5255{
ed2dc618
SM
5256 struct dwarf2_per_objfile *dwarf2_per_objfile
5257 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
5258
5259 if (!dwarf2_per_objfile->filenames_cache)
5260 {
5261 dwarf2_per_objfile->filenames_cache.emplace ();
5262
5263 htab_up visited (htab_create_alloc (10,
5264 htab_hash_pointer, htab_eq_pointer,
5265 NULL, xcalloc, xfree));
5266
5267 /* The rule is CUs specify all the files, including those used
5268 by any TU, so there's no need to scan TUs here. We can
5269 ignore file names coming from already-expanded CUs. */
5270
b76e467d 5271 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5272 {
927aa2e7
JK
5273 if (per_cu->v.quick->compunit_symtab)
5274 {
5275 void **slot = htab_find_slot (visited.get (),
5276 per_cu->v.quick->file_names,
5277 INSERT);
5278
5279 *slot = per_cu->v.quick->file_names;
5280 }
5281 }
5282
b76e467d 5283 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 5284 {
927aa2e7
JK
5285 /* We only need to look at symtabs not already expanded. */
5286 if (per_cu->v.quick->compunit_symtab)
5287 continue;
5288
b76e467d 5289 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
5290 if (file_data == NULL)
5291 continue;
5292
b76e467d 5293 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
5294 if (*slot)
5295 {
5296 /* Already visited. */
5297 continue;
5298 }
5299 *slot = file_data;
5300
5301 for (int j = 0; j < file_data->num_file_names; ++j)
5302 {
5303 const char *filename = file_data->file_names[j];
5304 dwarf2_per_objfile->filenames_cache->seen (filename);
5305 }
5306 }
5307 }
5308
5309 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
5310 {
5311 gdb::unique_xmalloc_ptr<char> this_real_name;
5312
5313 if (need_fullname)
5314 this_real_name = gdb_realpath (filename);
5315 (*fun) (filename, this_real_name.get (), data);
5316 });
5317}
5318
5319static int
5320dw2_has_symbols (struct objfile *objfile)
5321{
5322 return 1;
5323}
5324
5325const struct quick_symbol_functions dwarf2_gdb_index_functions =
5326{
5327 dw2_has_symbols,
5328 dw2_find_last_source_symtab,
5329 dw2_forget_cached_source_info,
5330 dw2_map_symtabs_matching_filename,
5331 dw2_lookup_symbol,
5332 dw2_print_stats,
5333 dw2_dump,
5334 dw2_relocate,
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,
9291a0cd 6147 dw2_relocate,
927aa2e7 6148 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 6149 dw2_expand_all_symtabs,
652a8996 6150 dw2_expand_symtabs_with_fullname,
40658b94 6151 dw2_map_matching_symbols,
927aa2e7 6152 dw2_debug_names_expand_symtabs_matching,
43f3e411 6153 dw2_find_pc_sect_compunit_symtab,
71a3c369 6154 NULL,
9291a0cd
TT
6155 dw2_map_symbol_filenames
6156};
6157
3c0aa29a 6158/* See symfile.h. */
9291a0cd 6159
3c0aa29a
PA
6160bool
6161dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 6162{
ed2dc618
SM
6163 struct dwarf2_per_objfile *dwarf2_per_objfile
6164 = get_dwarf2_per_objfile (objfile);
6165
9291a0cd
TT
6166 /* If we're about to read full symbols, don't bother with the
6167 indices. In this case we also don't care if some other debug
6168 format is making psymtabs, because they are all about to be
6169 expanded anyway. */
6170 if ((objfile->flags & OBJF_READNOW))
6171 {
9291a0cd 6172 dwarf2_per_objfile->using_index = 1;
ed2dc618
SM
6173 create_all_comp_units (dwarf2_per_objfile);
6174 create_all_type_units (dwarf2_per_objfile);
b76e467d
SM
6175 dwarf2_per_objfile->quick_file_names_table
6176 = create_quick_file_names_table
6177 (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd 6178
b76e467d 6179 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 6180 + dwarf2_per_objfile->all_type_units.size ()); ++i)
9291a0cd 6181 {
ff4c9fec 6182 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 6183
e254ef6a
DE
6184 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6185 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
6186 }
6187
6188 /* Return 1 so that gdb sees the "quick" functions. However,
6189 these functions will be no-ops because we will have expanded
6190 all symtabs. */
3c0aa29a
PA
6191 *index_kind = dw_index_kind::GDB_INDEX;
6192 return true;
9291a0cd
TT
6193 }
6194
ed2dc618 6195 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
6196 {
6197 *index_kind = dw_index_kind::DEBUG_NAMES;
6198 return true;
6199 }
927aa2e7 6200
12359b5e 6201 if (dwarf2_read_index (dwarf2_per_objfile))
3c0aa29a
PA
6202 {
6203 *index_kind = dw_index_kind::GDB_INDEX;
6204 return true;
6205 }
9291a0cd 6206
3c0aa29a 6207 return false;
9291a0cd
TT
6208}
6209
6210\f
6211
dce234bc
PP
6212/* Build a partial symbol table. */
6213
6214void
f29dff0a 6215dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 6216{
ed2dc618
SM
6217 struct dwarf2_per_objfile *dwarf2_per_objfile
6218 = get_dwarf2_per_objfile (objfile);
c9bf0622 6219
af5bf4ad
SM
6220 if (objfile->global_psymbols.capacity () == 0
6221 && objfile->static_psymbols.capacity () == 0)
6222 init_psymbol_list (objfile, 1024);
c906108c 6223
492d29ea 6224 TRY
c9bf0622
TT
6225 {
6226 /* This isn't really ideal: all the data we allocate on the
6227 objfile's obstack is still uselessly kept around. However,
6228 freeing it seems unsafe. */
906768f9 6229 psymtab_discarder psymtabs (objfile);
ed2dc618 6230 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 6231 psymtabs.keep ();
c9bf0622 6232 }
492d29ea
PA
6233 CATCH (except, RETURN_MASK_ERROR)
6234 {
6235 exception_print (gdb_stderr, except);
6236 }
6237 END_CATCH
c906108c 6238}
c906108c 6239
1ce1cefd
DE
6240/* Return the total length of the CU described by HEADER. */
6241
6242static unsigned int
6243get_cu_length (const struct comp_unit_head *header)
6244{
6245 return header->initial_length_size + header->length;
6246}
6247
9c541725 6248/* Return TRUE if SECT_OFF is within CU_HEADER. */
45452591 6249
9c541725
PA
6250static inline bool
6251offset_in_cu_p (const comp_unit_head *cu_header, sect_offset sect_off)
45452591 6252{
9c541725
PA
6253 sect_offset bottom = cu_header->sect_off;
6254 sect_offset top = cu_header->sect_off + get_cu_length (cu_header);
9a619af0 6255
9c541725 6256 return sect_off >= bottom && sect_off < top;
45452591
DE
6257}
6258
3b80fe9b
DE
6259/* Find the base address of the compilation unit for range lists and
6260 location lists. It will normally be specified by DW_AT_low_pc.
6261 In DWARF-3 draft 4, the base address could be overridden by
6262 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6263 compilation units with discontinuous ranges. */
6264
6265static void
6266dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6267{
6268 struct attribute *attr;
6269
6270 cu->base_known = 0;
6271 cu->base_address = 0;
6272
6273 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
6274 if (attr)
6275 {
31aa7e4e 6276 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6277 cu->base_known = 1;
6278 }
6279 else
6280 {
6281 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6282 if (attr)
6283 {
31aa7e4e 6284 cu->base_address = attr_value_as_address (attr);
3b80fe9b
DE
6285 cu->base_known = 1;
6286 }
6287 }
6288}
6289
93311388 6290/* Read in the comp unit header information from the debug_info at info_ptr.
43988095 6291 Use rcuh_kind::COMPILE as the default type if not known by the caller.
93311388
DE
6292 NOTE: This leaves members offset, first_die_offset to be filled in
6293 by the caller. */
107d2387 6294
d521ce57 6295static const gdb_byte *
107d2387 6296read_comp_unit_head (struct comp_unit_head *cu_header,
43988095
JK
6297 const gdb_byte *info_ptr,
6298 struct dwarf2_section_info *section,
6299 rcuh_kind section_kind)
107d2387
AC
6300{
6301 int signed_addr;
891d2f0b 6302 unsigned int bytes_read;
43988095
JK
6303 const char *filename = get_section_file_name (section);
6304 bfd *abfd = get_section_bfd_owner (section);
c764a876
DE
6305
6306 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
6307 cu_header->initial_length_size = bytes_read;
6308 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
613e1657 6309 info_ptr += bytes_read;
107d2387
AC
6310 cu_header->version = read_2_bytes (abfd, info_ptr);
6311 info_ptr += 2;
43988095
JK
6312 if (cu_header->version < 5)
6313 switch (section_kind)
6314 {
6315 case rcuh_kind::COMPILE:
6316 cu_header->unit_type = DW_UT_compile;
6317 break;
6318 case rcuh_kind::TYPE:
6319 cu_header->unit_type = DW_UT_type;
6320 break;
6321 default:
6322 internal_error (__FILE__, __LINE__,
6323 _("read_comp_unit_head: invalid section_kind"));
6324 }
6325 else
6326 {
6327 cu_header->unit_type = static_cast<enum dwarf_unit_type>
6328 (read_1_byte (abfd, info_ptr));
6329 info_ptr += 1;
6330 switch (cu_header->unit_type)
6331 {
6332 case DW_UT_compile:
6333 if (section_kind != rcuh_kind::COMPILE)
6334 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6335 "(is DW_UT_compile, should be DW_UT_type) [in module %s]"),
6336 filename);
6337 break;
6338 case DW_UT_type:
6339 section_kind = rcuh_kind::TYPE;
6340 break;
6341 default:
6342 error (_("Dwarf Error: wrong unit_type in compilation unit header "
6343 "(is %d, should be %d or %d) [in module %s]"),
6344 cu_header->unit_type, DW_UT_compile, DW_UT_type, filename);
6345 }
6346
6347 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6348 info_ptr += 1;
6349 }
9c541725
PA
6350 cu_header->abbrev_sect_off = (sect_offset) read_offset (abfd, info_ptr,
6351 cu_header,
6352 &bytes_read);
613e1657 6353 info_ptr += bytes_read;
43988095
JK
6354 if (cu_header->version < 5)
6355 {
6356 cu_header->addr_size = read_1_byte (abfd, info_ptr);
6357 info_ptr += 1;
6358 }
107d2387
AC
6359 signed_addr = bfd_get_sign_extend_vma (abfd);
6360 if (signed_addr < 0)
8e65ff28 6361 internal_error (__FILE__, __LINE__,
e2e0b3e5 6362 _("read_comp_unit_head: dwarf from non elf file"));
107d2387 6363 cu_header->signed_addr_p = signed_addr;
c764a876 6364
43988095
JK
6365 if (section_kind == rcuh_kind::TYPE)
6366 {
6367 LONGEST type_offset;
6368
6369 cu_header->signature = read_8_bytes (abfd, info_ptr);
6370 info_ptr += 8;
6371
6372 type_offset = read_offset (abfd, info_ptr, cu_header, &bytes_read);
6373 info_ptr += bytes_read;
9c541725
PA
6374 cu_header->type_cu_offset_in_tu = (cu_offset) type_offset;
6375 if (to_underlying (cu_header->type_cu_offset_in_tu) != type_offset)
43988095
JK
6376 error (_("Dwarf Error: Too big type_offset in compilation unit "
6377 "header (is %s) [in module %s]"), plongest (type_offset),
6378 filename);
6379 }
6380
107d2387
AC
6381 return info_ptr;
6382}
6383
36586728
TT
6384/* Helper function that returns the proper abbrev section for
6385 THIS_CU. */
6386
6387static struct dwarf2_section_info *
6388get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6389{
6390 struct dwarf2_section_info *abbrev;
ed2dc618 6391 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
36586728
TT
6392
6393 if (this_cu->is_dwz)
ed2dc618 6394 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
36586728
TT
6395 else
6396 abbrev = &dwarf2_per_objfile->abbrev;
6397
6398 return abbrev;
6399}
6400
9ff913ba
DE
6401/* Subroutine of read_and_check_comp_unit_head and
6402 read_and_check_type_unit_head to simplify them.
6403 Perform various error checking on the header. */
6404
6405static void
ed2dc618
SM
6406error_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6407 struct comp_unit_head *header,
4bdcc0c1
DE
6408 struct dwarf2_section_info *section,
6409 struct dwarf2_section_info *abbrev_section)
9ff913ba 6410{
a32a8923 6411 const char *filename = get_section_file_name (section);
9ff913ba 6412
43988095 6413 if (header->version < 2 || header->version > 5)
9ff913ba 6414 error (_("Dwarf Error: wrong version in compilation unit header "
43988095 6415 "(is %d, should be 2, 3, 4 or 5) [in module %s]"), header->version,
9ff913ba
DE
6416 filename);
6417
9c541725 6418 if (to_underlying (header->abbrev_sect_off)
36586728 6419 >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
9d8780f0
SM
6420 error (_("Dwarf Error: bad offset (%s) in compilation unit header "
6421 "(offset %s + 6) [in module %s]"),
6422 sect_offset_str (header->abbrev_sect_off),
6423 sect_offset_str (header->sect_off),
9ff913ba
DE
6424 filename);
6425
9c541725 6426 /* Cast to ULONGEST to use 64-bit arithmetic when possible to
9ff913ba 6427 avoid potential 32-bit overflow. */
9c541725 6428 if (((ULONGEST) header->sect_off + get_cu_length (header))
9ff913ba 6429 > section->size)
9c541725 6430 error (_("Dwarf Error: bad length (0x%x) in compilation unit header "
9d8780f0
SM
6431 "(offset %s + 0) [in module %s]"),
6432 header->length, sect_offset_str (header->sect_off),
9ff913ba
DE
6433 filename);
6434}
6435
6436/* Read in a CU/TU header and perform some basic error checking.
6437 The contents of the header are stored in HEADER.
6438 The result is a pointer to the start of the first DIE. */
adabb602 6439
d521ce57 6440static const gdb_byte *
ed2dc618
SM
6441read_and_check_comp_unit_head (struct dwarf2_per_objfile *dwarf2_per_objfile,
6442 struct comp_unit_head *header,
9ff913ba 6443 struct dwarf2_section_info *section,
4bdcc0c1 6444 struct dwarf2_section_info *abbrev_section,
d521ce57 6445 const gdb_byte *info_ptr,
43988095 6446 rcuh_kind section_kind)
72bf9492 6447{
d521ce57 6448 const gdb_byte *beg_of_comp_unit = info_ptr;
72bf9492 6449
9c541725 6450 header->sect_off = (sect_offset) (beg_of_comp_unit - section->buffer);
adabb602 6451
43988095 6452 info_ptr = read_comp_unit_head (header, info_ptr, section, section_kind);
9ff913ba 6453
9c541725 6454 header->first_die_cu_offset = (cu_offset) (info_ptr - beg_of_comp_unit);
348e048f 6455
ed2dc618
SM
6456 error_check_comp_unit_head (dwarf2_per_objfile, header, section,
6457 abbrev_section);
9ff913ba
DE
6458
6459 return info_ptr;
348e048f
DE
6460}
6461
f4dc4d17
DE
6462/* Fetch the abbreviation table offset from a comp or type unit header. */
6463
6464static sect_offset
ed2dc618
SM
6465read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6466 struct dwarf2_section_info *section,
9c541725 6467 sect_offset sect_off)
f4dc4d17 6468{
a32a8923 6469 bfd *abfd = get_section_bfd_owner (section);
d521ce57 6470 const gdb_byte *info_ptr;
ac298888 6471 unsigned int initial_length_size, offset_size;
43988095 6472 uint16_t version;
f4dc4d17
DE
6473
6474 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
9c541725 6475 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6476 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6477 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6478 info_ptr += initial_length_size;
6479
6480 version = read_2_bytes (abfd, info_ptr);
6481 info_ptr += 2;
6482 if (version >= 5)
6483 {
6484 /* Skip unit type and address size. */
6485 info_ptr += 2;
6486 }
6487
9c541725 6488 return (sect_offset) read_offset_1 (abfd, info_ptr, offset_size);
f4dc4d17
DE
6489}
6490
aaa75496
JB
6491/* Allocate a new partial symtab for file named NAME and mark this new
6492 partial symtab as being an include of PST. */
6493
6494static void
d521ce57 6495dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
aaa75496
JB
6496 struct objfile *objfile)
6497{
6498 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
6499
fbd9ab74
JK
6500 if (!IS_ABSOLUTE_PATH (subpst->filename))
6501 {
6502 /* It shares objfile->objfile_obstack. */
6503 subpst->dirname = pst->dirname;
6504 }
6505
aaa75496
JB
6506 subpst->textlow = 0;
6507 subpst->texthigh = 0;
6508
8d749320
SM
6509 subpst->dependencies
6510 = XOBNEW (&objfile->objfile_obstack, struct partial_symtab *);
aaa75496
JB
6511 subpst->dependencies[0] = pst;
6512 subpst->number_of_dependencies = 1;
6513
6514 subpst->globals_offset = 0;
6515 subpst->n_global_syms = 0;
6516 subpst->statics_offset = 0;
6517 subpst->n_static_syms = 0;
43f3e411 6518 subpst->compunit_symtab = NULL;
aaa75496
JB
6519 subpst->read_symtab = pst->read_symtab;
6520 subpst->readin = 0;
6521
6522 /* No private part is necessary for include psymtabs. This property
6523 can be used to differentiate between such include psymtabs and
10b3939b 6524 the regular ones. */
58a9656e 6525 subpst->read_symtab_private = NULL;
aaa75496
JB
6526}
6527
6528/* Read the Line Number Program data and extract the list of files
6529 included by the source file represented by PST. Build an include
d85a05f0 6530 partial symtab for each of these included files. */
aaa75496
JB
6531
6532static void
6533dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82
DE
6534 struct die_info *die,
6535 struct partial_symtab *pst)
aaa75496 6536{
fff8551c 6537 line_header_up lh;
d85a05f0 6538 struct attribute *attr;
aaa75496 6539
d85a05f0
DJ
6540 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
6541 if (attr)
9c541725 6542 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6543 if (lh == NULL)
6544 return; /* No linetable, so no includes. */
6545
c6da4cef 6546 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
fff8551c 6547 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst, pst->textlow, 1);
aaa75496
JB
6548}
6549
348e048f 6550static hashval_t
52dc124a 6551hash_signatured_type (const void *item)
348e048f 6552{
9a3c8263
SM
6553 const struct signatured_type *sig_type
6554 = (const struct signatured_type *) item;
9a619af0 6555
348e048f 6556 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6557 return sig_type->signature;
348e048f
DE
6558}
6559
6560static int
52dc124a 6561eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6562{
9a3c8263
SM
6563 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6564 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6565
348e048f
DE
6566 return lhs->signature == rhs->signature;
6567}
6568
1fd400ff
TT
6569/* Allocate a hash table for signatured types. */
6570
6571static htab_t
673bfd45 6572allocate_signatured_type_table (struct objfile *objfile)
1fd400ff
TT
6573{
6574 return htab_create_alloc_ex (41,
52dc124a
DE
6575 hash_signatured_type,
6576 eq_signatured_type,
1fd400ff
TT
6577 NULL,
6578 &objfile->objfile_obstack,
6579 hashtab_obstack_allocate,
6580 dummy_obstack_deallocate);
6581}
6582
d467dd73 6583/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6584
6585static int
d467dd73 6586add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6587{
9a3c8263 6588 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6589 std::vector<signatured_type *> *all_type_units
6590 = (std::vector<signatured_type *> *) datum;
1fd400ff 6591
b2bdb8cf 6592 all_type_units->push_back (sigt);
1fd400ff
TT
6593
6594 return 1;
6595}
6596
78d4d2c5 6597/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6598 and fill them into TYPES_HTAB. It will process only type units,
6599 therefore DW_UT_type. */
c88ee1f0 6600
78d4d2c5 6601static void
ed2dc618
SM
6602create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6603 struct dwo_file *dwo_file,
43988095
JK
6604 dwarf2_section_info *section, htab_t &types_htab,
6605 rcuh_kind section_kind)
348e048f 6606{
3019eac3 6607 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6608 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6609 bfd *abfd;
6610 const gdb_byte *info_ptr, *end_ptr;
348e048f 6611
4bdcc0c1
DE
6612 abbrev_section = (dwo_file != NULL
6613 ? &dwo_file->sections.abbrev
6614 : &dwarf2_per_objfile->abbrev);
6615
b4f54984 6616 if (dwarf_read_debug)
43988095
JK
6617 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
6618 get_section_name (section),
a32a8923 6619 get_section_file_name (abbrev_section));
09406207 6620
78d4d2c5
JK
6621 dwarf2_read_section (objfile, section);
6622 info_ptr = section->buffer;
348e048f 6623
78d4d2c5
JK
6624 if (info_ptr == NULL)
6625 return;
348e048f 6626
78d4d2c5
JK
6627 /* We can't set abfd until now because the section may be empty or
6628 not present, in which case the bfd is unknown. */
6629 abfd = get_section_bfd_owner (section);
348e048f 6630
78d4d2c5
JK
6631 /* We don't use init_cutu_and_read_dies_simple, or some such, here
6632 because we don't need to read any dies: the signature is in the
6633 header. */
3019eac3 6634
78d4d2c5
JK
6635 end_ptr = info_ptr + section->size;
6636 while (info_ptr < end_ptr)
6637 {
78d4d2c5
JK
6638 struct signatured_type *sig_type;
6639 struct dwo_unit *dwo_tu;
6640 void **slot;
6641 const gdb_byte *ptr = info_ptr;
6642 struct comp_unit_head header;
6643 unsigned int length;
8b70b953 6644
9c541725 6645 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6646
a49dd8dd
JK
6647 /* Initialize it due to a false compiler warning. */
6648 header.signature = -1;
9c541725 6649 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6650
78d4d2c5
JK
6651 /* We need to read the type's signature in order to build the hash
6652 table, but we don't need anything else just yet. */
348e048f 6653
ed2dc618 6654 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6655 abbrev_section, ptr, section_kind);
348e048f 6656
78d4d2c5 6657 length = get_cu_length (&header);
6caca83c 6658
78d4d2c5
JK
6659 /* Skip dummy type units. */
6660 if (ptr >= info_ptr + length
43988095
JK
6661 || peek_abbrev_code (abfd, ptr) == 0
6662 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6663 {
6664 info_ptr += length;
6665 continue;
6666 }
dee91e82 6667
78d4d2c5
JK
6668 if (types_htab == NULL)
6669 {
6670 if (dwo_file)
6671 types_htab = allocate_dwo_unit_table (objfile);
6672 else
6673 types_htab = allocate_signatured_type_table (objfile);
6674 }
8b70b953 6675
78d4d2c5
JK
6676 if (dwo_file)
6677 {
6678 sig_type = NULL;
6679 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6680 struct dwo_unit);
6681 dwo_tu->dwo_file = dwo_file;
43988095 6682 dwo_tu->signature = header.signature;
9c541725 6683 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6684 dwo_tu->section = section;
9c541725 6685 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6686 dwo_tu->length = length;
6687 }
6688 else
6689 {
6690 /* N.B.: type_offset is not usable if this type uses a DWO file.
6691 The real type_offset is in the DWO file. */
6692 dwo_tu = NULL;
6693 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6694 struct signatured_type);
43988095 6695 sig_type->signature = header.signature;
9c541725 6696 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 6697 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
6698 sig_type->per_cu.is_debug_types = 1;
6699 sig_type->per_cu.section = section;
9c541725 6700 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6701 sig_type->per_cu.length = length;
6702 }
6703
6704 slot = htab_find_slot (types_htab,
6705 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6706 INSERT);
6707 gdb_assert (slot != NULL);
6708 if (*slot != NULL)
6709 {
9c541725 6710 sect_offset dup_sect_off;
0349ea22 6711
3019eac3
DE
6712 if (dwo_file)
6713 {
78d4d2c5
JK
6714 const struct dwo_unit *dup_tu
6715 = (const struct dwo_unit *) *slot;
6716
9c541725 6717 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6718 }
6719 else
6720 {
78d4d2c5
JK
6721 const struct signatured_type *dup_tu
6722 = (const struct signatured_type *) *slot;
6723
9c541725 6724 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6725 }
8b70b953 6726
b98664d3 6727 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6728 " the entry at offset %s, signature %s"),
6729 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6730 hex_string (header.signature));
78d4d2c5
JK
6731 }
6732 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6733
78d4d2c5 6734 if (dwarf_read_debug > 1)
9d8780f0
SM
6735 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6736 sect_offset_str (sect_off),
43988095 6737 hex_string (header.signature));
3019eac3 6738
78d4d2c5
JK
6739 info_ptr += length;
6740 }
6741}
3019eac3 6742
78d4d2c5
JK
6743/* Create the hash table of all entries in the .debug_types
6744 (or .debug_types.dwo) section(s).
6745 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6746 otherwise it is NULL.
b3c8eb43 6747
78d4d2c5 6748 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6749
78d4d2c5 6750 Note: This function processes DWO files only, not DWP files. */
348e048f 6751
78d4d2c5 6752static void
ed2dc618
SM
6753create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6754 struct dwo_file *dwo_file,
78d4d2c5
JK
6755 VEC (dwarf2_section_info_def) *types,
6756 htab_t &types_htab)
6757{
6758 int ix;
6759 struct dwarf2_section_info *section;
6760
6761 if (VEC_empty (dwarf2_section_info_def, types))
6762 return;
348e048f 6763
78d4d2c5
JK
6764 for (ix = 0;
6765 VEC_iterate (dwarf2_section_info_def, types, ix, section);
6766 ++ix)
ed2dc618
SM
6767 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, section,
6768 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6769}
6770
6771/* Create the hash table of all entries in the .debug_types section,
6772 and initialize all_type_units.
6773 The result is zero if there is an error (e.g. missing .debug_types section),
6774 otherwise non-zero. */
6775
6776static int
ed2dc618 6777create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6778{
78d4d2c5 6779 htab_t types_htab = NULL;
3019eac3 6780
ed2dc618
SM
6781 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6782 &dwarf2_per_objfile->info, types_htab,
43988095 6783 rcuh_kind::COMPILE);
ed2dc618
SM
6784 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6785 dwarf2_per_objfile->types, types_htab);
3019eac3
DE
6786 if (types_htab == NULL)
6787 {
6788 dwarf2_per_objfile->signatured_types = NULL;
6789 return 0;
6790 }
6791
348e048f
DE
6792 dwarf2_per_objfile->signatured_types = types_htab;
6793
b2bdb8cf
SM
6794 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
6795 dwarf2_per_objfile->all_type_units.reserve (htab_elements (types_htab));
6796
6797 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table,
6798 &dwarf2_per_objfile->all_type_units);
1fd400ff 6799
348e048f
DE
6800 return 1;
6801}
6802
6aa5f3a6
DE
6803/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6804 If SLOT is non-NULL, it is the entry to use in the hash table.
6805 Otherwise we find one. */
6806
6807static struct signatured_type *
ed2dc618
SM
6808add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6809 void **slot)
6aa5f3a6
DE
6810{
6811 struct objfile *objfile = dwarf2_per_objfile->objfile;
6aa5f3a6 6812
b2bdb8cf
SM
6813 if (dwarf2_per_objfile->all_type_units.size ()
6814 == dwarf2_per_objfile->all_type_units.capacity ())
6815 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6816
b2bdb8cf
SM
6817 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6818 struct signatured_type);
6819
6820 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6821 sig_type->signature = sig;
6822 sig_type->per_cu.is_debug_types = 1;
6823 if (dwarf2_per_objfile->using_index)
6824 {
6825 sig_type->per_cu.v.quick =
6826 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6827 struct dwarf2_per_cu_quick_data);
6828 }
6829
6830 if (slot == NULL)
6831 {
6832 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6833 sig_type, INSERT);
6834 }
6835 gdb_assert (*slot == NULL);
6836 *slot = sig_type;
6837 /* The rest of sig_type must be filled in by the caller. */
6838 return sig_type;
6839}
6840
a2ce51a0
DE
6841/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6842 Fill in SIG_ENTRY with DWO_ENTRY. */
6843
6844static void
ed2dc618 6845fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
6846 struct signatured_type *sig_entry,
6847 struct dwo_unit *dwo_entry)
6848{
7ee85ab1 6849 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
6850 gdb_assert (! sig_entry->per_cu.queued);
6851 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
6852 if (dwarf2_per_objfile->using_index)
6853 {
6854 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 6855 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
6856 }
6857 else
6858 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6859 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6860 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6861 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6862 gdb_assert (sig_entry->dwo_unit == NULL);
6863
6864 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6865 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6866 sig_entry->per_cu.length = dwo_entry->length;
6867 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 6868 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
a2ce51a0
DE
6869 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6870 sig_entry->dwo_unit = dwo_entry;
6871}
6872
6873/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6874 If we haven't read the TU yet, create the signatured_type data structure
6875 for a TU to be read in directly from a DWO file, bypassing the stub.
6876 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6877 using .gdb_index, then when reading a CU we want to stay in the DWO file
6878 containing that CU. Otherwise we could end up reading several other DWO
6879 files (due to comdat folding) to process the transitive closure of all the
6880 mentioned TUs, and that can be slow. The current DWO file will have every
6881 type signature that it needs.
a2ce51a0
DE
6882 We only do this for .gdb_index because in the psymtab case we already have
6883 to read all the DWOs to build the type unit groups. */
6884
6885static struct signatured_type *
6886lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6887{
518817b3
SM
6888 struct dwarf2_per_objfile *dwarf2_per_objfile
6889 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0
DE
6890 struct objfile *objfile = dwarf2_per_objfile->objfile;
6891 struct dwo_file *dwo_file;
6892 struct dwo_unit find_dwo_entry, *dwo_entry;
6893 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6894 void **slot;
a2ce51a0
DE
6895
6896 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6897
6aa5f3a6
DE
6898 /* If TU skeletons have been removed then we may not have read in any
6899 TUs yet. */
6900 if (dwarf2_per_objfile->signatured_types == NULL)
6901 {
6902 dwarf2_per_objfile->signatured_types
6903 = allocate_signatured_type_table (objfile);
6904 }
a2ce51a0
DE
6905
6906 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6907 Use the global signatured_types array to do our own comdat-folding
6908 of types. If this is the first time we're reading this TU, and
6909 the TU has an entry in .gdb_index, replace the recorded data from
6910 .gdb_index with this TU. */
a2ce51a0 6911
a2ce51a0 6912 find_sig_entry.signature = sig;
6aa5f3a6
DE
6913 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6914 &find_sig_entry, INSERT);
9a3c8263 6915 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6916
6917 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6918 read. Don't reassign the global entry to point to this DWO if that's
6919 the case. Also note that if the TU is already being read, it may not
6920 have come from a DWO, the program may be a mix of Fission-compiled
6921 code and non-Fission-compiled code. */
6922
6923 /* Have we already tried to read this TU?
6924 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6925 needn't exist in the global table yet). */
6926 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6927 return sig_entry;
6928
6aa5f3a6
DE
6929 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6930 dwo_unit of the TU itself. */
6931 dwo_file = cu->dwo_unit->dwo_file;
6932
a2ce51a0
DE
6933 /* Ok, this is the first time we're reading this TU. */
6934 if (dwo_file->tus == NULL)
6935 return NULL;
6936 find_dwo_entry.signature = sig;
9a3c8263 6937 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_entry);
a2ce51a0
DE
6938 if (dwo_entry == NULL)
6939 return NULL;
6940
6aa5f3a6
DE
6941 /* If the global table doesn't have an entry for this TU, add one. */
6942 if (sig_entry == NULL)
ed2dc618 6943 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 6944
ed2dc618 6945 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 6946 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6947 return sig_entry;
6948}
6949
a2ce51a0
DE
6950/* Subroutine of lookup_signatured_type.
6951 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6952 then try the DWP file. If the TU stub (skeleton) has been removed then
6953 it won't be in .gdb_index. */
a2ce51a0
DE
6954
6955static struct signatured_type *
6956lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6957{
518817b3
SM
6958 struct dwarf2_per_objfile *dwarf2_per_objfile
6959 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0 6960 struct objfile *objfile = dwarf2_per_objfile->objfile;
ed2dc618 6961 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
6962 struct dwo_unit *dwo_entry;
6963 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6964 void **slot;
a2ce51a0
DE
6965
6966 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6967 gdb_assert (dwp_file != NULL);
6968
6aa5f3a6
DE
6969 /* If TU skeletons have been removed then we may not have read in any
6970 TUs yet. */
6971 if (dwarf2_per_objfile->signatured_types == NULL)
a2ce51a0 6972 {
6aa5f3a6
DE
6973 dwarf2_per_objfile->signatured_types
6974 = allocate_signatured_type_table (objfile);
a2ce51a0
DE
6975 }
6976
6aa5f3a6
DE
6977 find_sig_entry.signature = sig;
6978 slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
6979 &find_sig_entry, INSERT);
9a3c8263 6980 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6981
6982 /* Have we already tried to read this TU?
6983 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6984 needn't exist in the global table yet). */
6985 if (sig_entry != NULL)
6986 return sig_entry;
6987
a2ce51a0
DE
6988 if (dwp_file->tus == NULL)
6989 return NULL;
ed2dc618 6990 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 6991 sig, 1 /* is_debug_types */);
a2ce51a0
DE
6992 if (dwo_entry == NULL)
6993 return NULL;
6994
ed2dc618
SM
6995 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6996 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 6997
a2ce51a0
DE
6998 return sig_entry;
6999}
7000
380bca97 7001/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
7002 Returns NULL if signature SIG is not present in the table.
7003 It is up to the caller to complain about this. */
348e048f
DE
7004
7005static struct signatured_type *
a2ce51a0 7006lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 7007{
518817b3
SM
7008 struct dwarf2_per_objfile *dwarf2_per_objfile
7009 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 7010
a2ce51a0
DE
7011 if (cu->dwo_unit
7012 && dwarf2_per_objfile->using_index)
7013 {
7014 /* We're in a DWO/DWP file, and we're using .gdb_index.
7015 These cases require special processing. */
ed2dc618 7016 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
7017 return lookup_dwo_signatured_type (cu, sig);
7018 else
7019 return lookup_dwp_signatured_type (cu, sig);
7020 }
7021 else
7022 {
7023 struct signatured_type find_entry, *entry;
348e048f 7024
a2ce51a0
DE
7025 if (dwarf2_per_objfile->signatured_types == NULL)
7026 return NULL;
7027 find_entry.signature = sig;
9a3c8263
SM
7028 entry = ((struct signatured_type *)
7029 htab_find (dwarf2_per_objfile->signatured_types, &find_entry));
a2ce51a0
DE
7030 return entry;
7031 }
348e048f 7032}
42e7ad6c
DE
7033\f
7034/* Low level DIE reading support. */
348e048f 7035
d85a05f0
DJ
7036/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
7037
7038static void
7039init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 7040 struct dwarf2_cu *cu,
3019eac3 7041 struct dwarf2_section_info *section,
685af9cd
TT
7042 struct dwo_file *dwo_file,
7043 struct abbrev_table *abbrev_table)
d85a05f0 7044{
fceca515 7045 gdb_assert (section->readin && section->buffer != NULL);
a32a8923 7046 reader->abfd = get_section_bfd_owner (section);
d85a05f0 7047 reader->cu = cu;
3019eac3 7048 reader->dwo_file = dwo_file;
dee91e82
DE
7049 reader->die_section = section;
7050 reader->buffer = section->buffer;
f664829e 7051 reader->buffer_end = section->buffer + section->size;
a2ce51a0 7052 reader->comp_dir = NULL;
685af9cd 7053 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
7054}
7055
b0c7bfa9
DE
7056/* Subroutine of init_cutu_and_read_dies to simplify it.
7057 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
7058 There's just a lot of work to do, and init_cutu_and_read_dies is big enough
7059 already.
7060
7061 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
7062 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
7063 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
7064 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
7065 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
7066 STUB_COMP_DIR may be non-NULL.
b0c7bfa9
DE
7067 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
7068 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
7069 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
7070 from the dwo. Since *RESULT_READER references this abbrev table, it must be
7071 kept around for at least as long as *RESULT_READER.
7072
b0c7bfa9
DE
7073 The result is non-zero if a valid (non-dummy) DIE was found. */
7074
7075static int
7076read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
7077 struct dwo_unit *dwo_unit,
b0c7bfa9 7078 struct die_info *stub_comp_unit_die,
a2ce51a0 7079 const char *stub_comp_dir,
b0c7bfa9 7080 struct die_reader_specs *result_reader,
d521ce57 7081 const gdb_byte **result_info_ptr,
b0c7bfa9 7082 struct die_info **result_comp_unit_die,
685af9cd
TT
7083 int *result_has_children,
7084 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 7085{
ed2dc618 7086 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
b0c7bfa9
DE
7087 struct objfile *objfile = dwarf2_per_objfile->objfile;
7088 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 7089 bfd *abfd;
d521ce57 7090 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
7091 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
7092 int i,num_extra_attrs;
7093 struct dwarf2_section_info *dwo_abbrev_section;
7094 struct attribute *attr;
7095 struct die_info *comp_unit_die;
7096
b0aeadb3
DE
7097 /* At most one of these may be provided. */
7098 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 7099
b0c7bfa9
DE
7100 /* These attributes aren't processed until later:
7101 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
7102 DW_AT_comp_dir is used now, to find the DWO file, but it is also
7103 referenced later. However, these attributes are found in the stub
7104 which we won't have later. In order to not impose this complication
7105 on the rest of the code, we read them here and copy them to the
7106 DWO CU/TU die. */
b0c7bfa9
DE
7107
7108 stmt_list = NULL;
7109 low_pc = NULL;
7110 high_pc = NULL;
7111 ranges = NULL;
7112 comp_dir = NULL;
7113
7114 if (stub_comp_unit_die != NULL)
7115 {
7116 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
7117 DWO file. */
7118 if (! this_cu->is_debug_types)
7119 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
7120 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
7121 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
7122 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
7123 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
7124
7125 /* There should be a DW_AT_addr_base attribute here (if needed).
7126 We need the value before we can process DW_FORM_GNU_addr_index. */
7127 cu->addr_base = 0;
7128 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
7129 if (attr)
7130 cu->addr_base = DW_UNSND (attr);
7131
7132 /* There should be a DW_AT_ranges_base attribute here (if needed).
7133 We need the value before we can process DW_AT_ranges. */
7134 cu->ranges_base = 0;
7135 attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
7136 if (attr)
7137 cu->ranges_base = DW_UNSND (attr);
7138 }
a2ce51a0
DE
7139 else if (stub_comp_dir != NULL)
7140 {
7141 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 7142 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
7143 comp_dir->name = DW_AT_comp_dir;
7144 comp_dir->form = DW_FORM_string;
7145 DW_STRING_IS_CANONICAL (comp_dir) = 0;
7146 DW_STRING (comp_dir) = stub_comp_dir;
7147 }
b0c7bfa9
DE
7148
7149 /* Set up for reading the DWO CU/TU. */
7150 cu->dwo_unit = dwo_unit;
685af9cd 7151 dwarf2_section_info *section = dwo_unit->section;
b0c7bfa9 7152 dwarf2_read_section (objfile, section);
a32a8923 7153 abfd = get_section_bfd_owner (section);
9c541725
PA
7154 begin_info_ptr = info_ptr = (section->buffer
7155 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 7156 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9
DE
7157
7158 if (this_cu->is_debug_types)
7159 {
b0c7bfa9
DE
7160 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
7161
ed2dc618
SM
7162 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7163 &cu->header, section,
b0c7bfa9 7164 dwo_abbrev_section,
43988095 7165 info_ptr, rcuh_kind::TYPE);
a2ce51a0 7166 /* This is not an assert because it can be caused by bad debug info. */
43988095 7167 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
7168 {
7169 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 7170 " TU at offset %s [in module %s]"),
a2ce51a0 7171 hex_string (sig_type->signature),
43988095 7172 hex_string (cu->header.signature),
9d8780f0 7173 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
7174 bfd_get_filename (abfd));
7175 }
9c541725 7176 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7177 /* For DWOs coming from DWP files, we don't know the CU length
7178 nor the type's offset in the TU until now. */
7179 dwo_unit->length = get_cu_length (&cu->header);
9c541725 7180 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
7181
7182 /* Establish the type offset that can be used to lookup the type.
7183 For DWO files, we don't know it until now. */
9c541725
PA
7184 sig_type->type_offset_in_section
7185 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
7186 }
7187 else
7188 {
ed2dc618
SM
7189 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7190 &cu->header, section,
b0c7bfa9 7191 dwo_abbrev_section,
43988095 7192 info_ptr, rcuh_kind::COMPILE);
9c541725 7193 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7194 /* For DWOs coming from DWP files, we don't know the CU length
7195 until now. */
7196 dwo_unit->length = get_cu_length (&cu->header);
7197 }
7198
685af9cd
TT
7199 *result_dwo_abbrev_table
7200 = abbrev_table_read_table (dwarf2_per_objfile, dwo_abbrev_section,
7201 cu->header.abbrev_sect_off);
7202 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7203 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
7204
7205 /* Read in the die, but leave space to copy over the attributes
7206 from the stub. This has the benefit of simplifying the rest of
7207 the code - all the work to maintain the illusion of a single
7208 DW_TAG_{compile,type}_unit DIE is done here. */
7209 num_extra_attrs = ((stmt_list != NULL)
7210 + (low_pc != NULL)
7211 + (high_pc != NULL)
7212 + (ranges != NULL)
7213 + (comp_dir != NULL));
7214 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
7215 result_has_children, num_extra_attrs);
7216
7217 /* Copy over the attributes from the stub to the DIE we just read in. */
7218 comp_unit_die = *result_comp_unit_die;
7219 i = comp_unit_die->num_attrs;
7220 if (stmt_list != NULL)
7221 comp_unit_die->attrs[i++] = *stmt_list;
7222 if (low_pc != NULL)
7223 comp_unit_die->attrs[i++] = *low_pc;
7224 if (high_pc != NULL)
7225 comp_unit_die->attrs[i++] = *high_pc;
7226 if (ranges != NULL)
7227 comp_unit_die->attrs[i++] = *ranges;
7228 if (comp_dir != NULL)
7229 comp_unit_die->attrs[i++] = *comp_dir;
7230 comp_unit_die->num_attrs += num_extra_attrs;
7231
b4f54984 7232 if (dwarf_die_debug)
bf6af496
DE
7233 {
7234 fprintf_unfiltered (gdb_stdlog,
7235 "Read die from %s@0x%x of %s:\n",
a32a8923 7236 get_section_name (section),
bf6af496
DE
7237 (unsigned) (begin_info_ptr - section->buffer),
7238 bfd_get_filename (abfd));
b4f54984 7239 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
7240 }
7241
a2ce51a0
DE
7242 /* Save the comp_dir attribute. If there is no DWP file then we'll read
7243 TUs by skipping the stub and going directly to the entry in the DWO file.
7244 However, skipping the stub means we won't get DW_AT_comp_dir, so we have
7245 to get it via circuitous means. Blech. */
7246 if (comp_dir != NULL)
7247 result_reader->comp_dir = DW_STRING (comp_dir);
7248
b0c7bfa9
DE
7249 /* Skip dummy compilation units. */
7250 if (info_ptr >= begin_info_ptr + dwo_unit->length
7251 || peek_abbrev_code (abfd, info_ptr) == 0)
7252 return 0;
7253
7254 *result_info_ptr = info_ptr;
7255 return 1;
7256}
7257
7258/* Subroutine of init_cutu_and_read_dies to simplify it.
7259 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 7260 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
7261
7262static struct dwo_unit *
7263lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
7264 struct die_info *comp_unit_die)
7265{
7266 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9
DE
7267 ULONGEST signature;
7268 struct dwo_unit *dwo_unit;
7269 const char *comp_dir, *dwo_name;
7270
a2ce51a0
DE
7271 gdb_assert (cu != NULL);
7272
b0c7bfa9 7273 /* Yeah, we look dwo_name up again, but it simplifies the code. */
7d45c7c3
KB
7274 dwo_name = dwarf2_string_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
7275 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
7276
7277 if (this_cu->is_debug_types)
7278 {
7279 struct signatured_type *sig_type;
7280
7281 /* Since this_cu is the first member of struct signatured_type,
7282 we can go from a pointer to one to a pointer to the other. */
7283 sig_type = (struct signatured_type *) this_cu;
7284 signature = sig_type->signature;
7285 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
7286 }
7287 else
7288 {
7289 struct attribute *attr;
7290
7291 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
7292 if (! attr)
7293 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7294 " [in module %s]"),
e3b94546 7295 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
b0c7bfa9
DE
7296 signature = DW_UNSND (attr);
7297 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
7298 signature);
7299 }
7300
b0c7bfa9
DE
7301 return dwo_unit;
7302}
7303
a2ce51a0 7304/* Subroutine of init_cutu_and_read_dies to simplify it.
6aa5f3a6 7305 See it for a description of the parameters.
fcd3b13d 7306 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0
DE
7307
7308static void
6aa5f3a6
DE
7309init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
7310 int use_existing_cu, int keep,
a2ce51a0
DE
7311 die_reader_func_ftype *die_reader_func,
7312 void *data)
7313{
fcd3b13d 7314 std::unique_ptr<dwarf2_cu> new_cu;
a2ce51a0 7315 struct signatured_type *sig_type;
a2ce51a0
DE
7316 struct die_reader_specs reader;
7317 const gdb_byte *info_ptr;
7318 struct die_info *comp_unit_die;
7319 int has_children;
ed2dc618 7320 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
a2ce51a0
DE
7321
7322 /* Verify we can do the following downcast, and that we have the
7323 data we need. */
7324 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7325 sig_type = (struct signatured_type *) this_cu;
7326 gdb_assert (sig_type->dwo_unit != NULL);
7327
6aa5f3a6
DE
7328 if (use_existing_cu && this_cu->cu != NULL)
7329 {
7330 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6
DE
7331 /* There's no need to do the rereading_dwo_cu handling that
7332 init_cutu_and_read_dies does since we don't read the stub. */
7333 }
7334 else
7335 {
7336 /* If !use_existing_cu, this_cu->cu must be NULL. */
7337 gdb_assert (this_cu->cu == NULL);
fcd3b13d 7338 new_cu.reset (new dwarf2_cu (this_cu));
6aa5f3a6
DE
7339 }
7340
7341 /* A future optimization, if needed, would be to use an existing
7342 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7343 could share abbrev tables. */
a2ce51a0 7344
685af9cd
TT
7345 /* The abbreviation table used by READER, this must live at least as long as
7346 READER. */
7347 abbrev_table_up dwo_abbrev_table;
7348
a2ce51a0 7349 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
a2ce51a0
DE
7350 NULL /* stub_comp_unit_die */,
7351 sig_type->dwo_unit->dwo_file->comp_dir,
7352 &reader, &info_ptr,
685af9cd
TT
7353 &comp_unit_die, &has_children,
7354 &dwo_abbrev_table) == 0)
a2ce51a0
DE
7355 {
7356 /* Dummy die. */
a2ce51a0
DE
7357 return;
7358 }
7359
7360 /* All the "real" work is done here. */
7361 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7362
6aa5f3a6 7363 /* This duplicates the code in init_cutu_and_read_dies,
a2ce51a0
DE
7364 but the alternative is making the latter more complex.
7365 This function is only for the special case of using DWO files directly:
7366 no point in overly complicating the general case just to handle this. */
fcd3b13d 7367 if (new_cu != NULL && keep)
a2ce51a0 7368 {
fcd3b13d
SM
7369 /* Link this CU into read_in_chain. */
7370 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7371 dwarf2_per_objfile->read_in_chain = this_cu;
7372 /* The chain owns it now. */
7373 new_cu.release ();
a2ce51a0 7374 }
a2ce51a0
DE
7375}
7376
fd820528 7377/* Initialize a CU (or TU) and read its DIEs.
3019eac3 7378 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 7379
f4dc4d17
DE
7380 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7381 Otherwise the table specified in the comp unit header is read in and used.
7382 This is an optimization for when we already have the abbrev table.
7383
dee91e82
DE
7384 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
7385 Otherwise, a new CU is allocated with xmalloc.
7386
7387 If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
7388 read_in_chain. Otherwise the dwarf2_cu data is freed at the end.
7389
7390 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
fd820528 7391 linker) then DIE_READER_FUNC will not get called. */
aaa75496 7392
70221824 7393static void
fd820528 7394init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
f4dc4d17 7395 struct abbrev_table *abbrev_table,
fd820528 7396 int use_existing_cu, int keep,
58f0c718 7397 bool skip_partial,
fd820528
DE
7398 die_reader_func_ftype *die_reader_func,
7399 void *data)
c906108c 7400{
ed2dc618 7401 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7402 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7403 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7404 bfd *abfd = get_section_bfd_owner (section);
dee91e82 7405 struct dwarf2_cu *cu;
d521ce57 7406 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7407 struct die_reader_specs reader;
d85a05f0 7408 struct die_info *comp_unit_die;
dee91e82 7409 int has_children;
d85a05f0 7410 struct attribute *attr;
dee91e82 7411 struct signatured_type *sig_type = NULL;
4bdcc0c1 7412 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7413 /* Non-zero if CU currently points to a DWO file and we need to
7414 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7415 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7416 int rereading_dwo_cu = 0;
c906108c 7417
b4f54984 7418 if (dwarf_die_debug)
9d8780f0 7419 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7420 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7421 sect_offset_str (this_cu->sect_off));
09406207 7422
dee91e82
DE
7423 if (use_existing_cu)
7424 gdb_assert (keep);
23745b47 7425
a2ce51a0
DE
7426 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7427 file (instead of going through the stub), short-circuit all of this. */
7428 if (this_cu->reading_dwo_directly)
7429 {
7430 /* Narrow down the scope of possibilities to have to understand. */
7431 gdb_assert (this_cu->is_debug_types);
7432 gdb_assert (abbrev_table == NULL);
6aa5f3a6
DE
7433 init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
7434 die_reader_func, data);
a2ce51a0
DE
7435 return;
7436 }
7437
dee91e82
DE
7438 /* This is cheap if the section is already read in. */
7439 dwarf2_read_section (objfile, section);
7440
9c541725 7441 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7442
7443 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 7444
fcd3b13d 7445 std::unique_ptr<dwarf2_cu> new_cu;
dee91e82
DE
7446 if (use_existing_cu && this_cu->cu != NULL)
7447 {
7448 cu = this_cu->cu;
42e7ad6c
DE
7449 /* If this CU is from a DWO file we need to start over, we need to
7450 refetch the attributes from the skeleton CU.
7451 This could be optimized by retrieving those attributes from when we
7452 were here the first time: the previous comp_unit_die was stored in
7453 comp_unit_obstack. But there's no data yet that we need this
7454 optimization. */
7455 if (cu->dwo_unit != NULL)
7456 rereading_dwo_cu = 1;
dee91e82
DE
7457 }
7458 else
7459 {
7460 /* If !use_existing_cu, this_cu->cu must be NULL. */
7461 gdb_assert (this_cu->cu == NULL);
fcd3b13d
SM
7462 new_cu.reset (new dwarf2_cu (this_cu));
7463 cu = new_cu.get ();
42e7ad6c 7464 }
dee91e82 7465
b0c7bfa9 7466 /* Get the header. */
9c541725 7467 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7468 {
7469 /* We already have the header, there's no need to read it in again. */
9c541725 7470 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7471 }
7472 else
7473 {
3019eac3 7474 if (this_cu->is_debug_types)
dee91e82 7475 {
ed2dc618
SM
7476 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7477 &cu->header, section,
4bdcc0c1 7478 abbrev_section, info_ptr,
43988095 7479 rcuh_kind::TYPE);
dee91e82 7480
42e7ad6c
DE
7481 /* Since per_cu is the first member of struct signatured_type,
7482 we can go from a pointer to one to a pointer to the other. */
7483 sig_type = (struct signatured_type *) this_cu;
43988095 7484 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7485 gdb_assert (sig_type->type_offset_in_tu
7486 == cu->header.type_cu_offset_in_tu);
7487 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7488
42e7ad6c
DE
7489 /* LENGTH has not been set yet for type units if we're
7490 using .gdb_index. */
1ce1cefd 7491 this_cu->length = get_cu_length (&cu->header);
3019eac3
DE
7492
7493 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7494 sig_type->type_offset_in_section =
7495 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7496
7497 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7498 }
7499 else
7500 {
ed2dc618
SM
7501 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7502 &cu->header, section,
4bdcc0c1 7503 abbrev_section,
43988095
JK
7504 info_ptr,
7505 rcuh_kind::COMPILE);
dee91e82 7506
9c541725 7507 gdb_assert (this_cu->sect_off == cu->header.sect_off);
1ce1cefd 7508 gdb_assert (this_cu->length == get_cu_length (&cu->header));
43988095 7509 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7510 }
7511 }
10b3939b 7512
6caca83c 7513 /* Skip dummy compilation units. */
dee91e82 7514 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7515 || peek_abbrev_code (abfd, info_ptr) == 0)
fcd3b13d 7516 return;
6caca83c 7517
433df2d4
DE
7518 /* If we don't have them yet, read the abbrevs for this compilation unit.
7519 And if we need to read them now, make sure they're freed when we're
685af9cd
TT
7520 done (own the table through ABBREV_TABLE_HOLDER). */
7521 abbrev_table_up abbrev_table_holder;
f4dc4d17 7522 if (abbrev_table != NULL)
685af9cd
TT
7523 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7524 else
f4dc4d17 7525 {
685af9cd
TT
7526 abbrev_table_holder
7527 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7528 cu->header.abbrev_sect_off);
7529 abbrev_table = abbrev_table_holder.get ();
42e7ad6c 7530 }
af703f96 7531
dee91e82 7532 /* Read the top level CU/TU die. */
685af9cd 7533 init_cu_die_reader (&reader, cu, section, NULL, abbrev_table);
dee91e82 7534 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
93311388 7535
58f0c718
TT
7536 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
7537 return;
7538
b0c7bfa9 7539 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7540 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7541 table from the DWO file and pass the ownership over to us. It will be
7542 referenced from READER, so we must make sure to free it after we're done
7543 with READER.
7544
b0c7bfa9
DE
7545 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7546 DWO CU, that this test will fail (the attribute will not be present). */
3019eac3 7547 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
685af9cd 7548 abbrev_table_up dwo_abbrev_table;
3019eac3
DE
7549 if (attr)
7550 {
3019eac3 7551 struct dwo_unit *dwo_unit;
b0c7bfa9 7552 struct die_info *dwo_comp_unit_die;
3019eac3
DE
7553
7554 if (has_children)
6a506a2d 7555 {
b98664d3 7556 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7557 " has children (offset %s) [in module %s]"),
7558 sect_offset_str (this_cu->sect_off),
7559 bfd_get_filename (abfd));
6a506a2d 7560 }
b0c7bfa9 7561 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
6a506a2d 7562 if (dwo_unit != NULL)
3019eac3 7563 {
6a506a2d 7564 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
a2ce51a0 7565 comp_unit_die, NULL,
6a506a2d 7566 &reader, &info_ptr,
685af9cd
TT
7567 &dwo_comp_unit_die, &has_children,
7568 &dwo_abbrev_table) == 0)
6a506a2d
DE
7569 {
7570 /* Dummy die. */
6a506a2d
DE
7571 return;
7572 }
7573 comp_unit_die = dwo_comp_unit_die;
7574 }
7575 else
7576 {
7577 /* Yikes, we couldn't find the rest of the DIE, we only have
7578 the stub. A complaint has already been logged. There's
7579 not much more we can do except pass on the stub DIE to
7580 die_reader_func. We don't want to throw an error on bad
7581 debug info. */
3019eac3
DE
7582 }
7583 }
7584
b0c7bfa9 7585 /* All of the above is setup for this call. Yikes. */
dee91e82
DE
7586 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
7587
b0c7bfa9 7588 /* Done, clean up. */
fcd3b13d 7589 if (new_cu != NULL && keep)
348e048f 7590 {
fcd3b13d
SM
7591 /* Link this CU into read_in_chain. */
7592 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7593 dwarf2_per_objfile->read_in_chain = this_cu;
7594 /* The chain owns it now. */
7595 new_cu.release ();
348e048f 7596 }
dee91e82
DE
7597}
7598
33e80786
DE
7599/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
7600 DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
7601 to have already done the lookup to find the DWO file).
dee91e82
DE
7602
7603 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7604 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7605
7606 We fill in THIS_CU->length.
7607
7608 WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
7609 linker) then DIE_READER_FUNC will not get called.
7610
7611 THIS_CU->cu is always freed when done.
3019eac3
DE
7612 This is done in order to not leave THIS_CU->cu in a state where we have
7613 to care whether it refers to the "main" CU or the DWO CU. */
dee91e82
DE
7614
7615static void
7616init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
3019eac3 7617 struct dwo_file *dwo_file,
dee91e82
DE
7618 die_reader_func_ftype *die_reader_func,
7619 void *data)
7620{
ed2dc618 7621 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7622 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7623 struct dwarf2_section_info *section = this_cu->section;
a32a8923 7624 bfd *abfd = get_section_bfd_owner (section);
33e80786 7625 struct dwarf2_section_info *abbrev_section;
d521ce57 7626 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7627 struct die_reader_specs reader;
dee91e82
DE
7628 struct die_info *comp_unit_die;
7629 int has_children;
7630
b4f54984 7631 if (dwarf_die_debug)
9d8780f0 7632 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7633 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7634 sect_offset_str (this_cu->sect_off));
09406207 7635
dee91e82
DE
7636 gdb_assert (this_cu->cu == NULL);
7637
33e80786
DE
7638 abbrev_section = (dwo_file != NULL
7639 ? &dwo_file->sections.abbrev
7640 : get_abbrev_section_for_cu (this_cu));
7641
dee91e82
DE
7642 /* This is cheap if the section is already read in. */
7643 dwarf2_read_section (objfile, section);
7644
fcd3b13d 7645 struct dwarf2_cu cu (this_cu);
dee91e82 7646
9c541725 7647 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618
SM
7648 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7649 &cu.header, section,
4bdcc0c1 7650 abbrev_section, info_ptr,
43988095
JK
7651 (this_cu->is_debug_types
7652 ? rcuh_kind::TYPE
7653 : rcuh_kind::COMPILE));
dee91e82 7654
1ce1cefd 7655 this_cu->length = get_cu_length (&cu.header);
dee91e82
DE
7656
7657 /* Skip dummy compilation units. */
7658 if (info_ptr >= begin_info_ptr + this_cu->length
7659 || peek_abbrev_code (abfd, info_ptr) == 0)
fcd3b13d 7660 return;
72bf9492 7661
685af9cd
TT
7662 abbrev_table_up abbrev_table
7663 = abbrev_table_read_table (dwarf2_per_objfile, abbrev_section,
7664 cu.header.abbrev_sect_off);
dee91e82 7665
685af9cd 7666 init_cu_die_reader (&reader, &cu, section, dwo_file, abbrev_table.get ());
dee91e82
DE
7667 info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
7668
7669 die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
dee91e82
DE
7670}
7671
3019eac3
DE
7672/* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
7673 does not lookup the specified DWO file.
7674 This cannot be used to read DWO files.
dee91e82
DE
7675
7676 THIS_CU->cu is always freed when done.
3019eac3
DE
7677 This is done in order to not leave THIS_CU->cu in a state where we have
7678 to care whether it refers to the "main" CU or the DWO CU.
7679 We can revisit this if the data shows there's a performance issue. */
dee91e82
DE
7680
7681static void
7682init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
7683 die_reader_func_ftype *die_reader_func,
7684 void *data)
7685{
33e80786 7686 init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
dee91e82 7687}
0018ea6f
DE
7688\f
7689/* Type Unit Groups.
dee91e82 7690
0018ea6f
DE
7691 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7692 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7693 so that all types coming from the same compilation (.o file) are grouped
7694 together. A future step could be to put the types in the same symtab as
7695 the CU the types ultimately came from. */
ff013f42 7696
f4dc4d17
DE
7697static hashval_t
7698hash_type_unit_group (const void *item)
7699{
9a3c8263
SM
7700 const struct type_unit_group *tu_group
7701 = (const struct type_unit_group *) item;
f4dc4d17 7702
094b34ac 7703 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7704}
348e048f
DE
7705
7706static int
f4dc4d17 7707eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7708{
9a3c8263
SM
7709 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7710 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7711
094b34ac 7712 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7713}
348e048f 7714
f4dc4d17
DE
7715/* Allocate a hash table for type unit groups. */
7716
7717static htab_t
ed2dc618 7718allocate_type_unit_groups_table (struct objfile *objfile)
f4dc4d17
DE
7719{
7720 return htab_create_alloc_ex (3,
7721 hash_type_unit_group,
7722 eq_type_unit_group,
7723 NULL,
ed2dc618 7724 &objfile->objfile_obstack,
f4dc4d17
DE
7725 hashtab_obstack_allocate,
7726 dummy_obstack_deallocate);
7727}
dee91e82 7728
f4dc4d17
DE
7729/* Type units that don't have DW_AT_stmt_list are grouped into their own
7730 partial symtabs. We combine several TUs per psymtab to not let the size
7731 of any one psymtab grow too big. */
7732#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7733#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7734
094b34ac 7735/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7736 Create the type_unit_group object used to hold one or more TUs. */
7737
7738static struct type_unit_group *
094b34ac 7739create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7740{
518817b3
SM
7741 struct dwarf2_per_objfile *dwarf2_per_objfile
7742 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17 7743 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 7744 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7745 struct type_unit_group *tu_group;
f4dc4d17
DE
7746
7747 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7748 struct type_unit_group);
094b34ac 7749 per_cu = &tu_group->per_cu;
518817b3 7750 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
f4dc4d17 7751
094b34ac
DE
7752 if (dwarf2_per_objfile->using_index)
7753 {
7754 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7755 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7756 }
7757 else
7758 {
9c541725 7759 unsigned int line_offset = to_underlying (line_offset_struct);
094b34ac
DE
7760 struct partial_symtab *pst;
7761 char *name;
7762
7763 /* Give the symtab a useful name for debug purposes. */
7764 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
7765 name = xstrprintf ("<type_units_%d>",
7766 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
7767 else
7768 name = xstrprintf ("<type_units_at_0x%x>", line_offset);
7769
7770 pst = create_partial_symtab (per_cu, name);
7771 pst->anonymous = 1;
f4dc4d17 7772
094b34ac
DE
7773 xfree (name);
7774 }
f4dc4d17 7775
094b34ac 7776 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7777 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7778
7779 return tu_group;
7780}
7781
094b34ac
DE
7782/* Look up the type_unit_group for type unit CU, and create it if necessary.
7783 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7784
7785static struct type_unit_group *
ff39bb5e 7786get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7787{
518817b3
SM
7788 struct dwarf2_per_objfile *dwarf2_per_objfile
7789 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7790 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7791 struct type_unit_group *tu_group;
7792 void **slot;
7793 unsigned int line_offset;
7794 struct type_unit_group type_unit_group_for_lookup;
7795
7796 if (dwarf2_per_objfile->type_unit_groups == NULL)
7797 {
7798 dwarf2_per_objfile->type_unit_groups =
ed2dc618 7799 allocate_type_unit_groups_table (dwarf2_per_objfile->objfile);
f4dc4d17
DE
7800 }
7801
7802 /* Do we need to create a new group, or can we use an existing one? */
7803
7804 if (stmt_list)
7805 {
7806 line_offset = DW_UNSND (stmt_list);
7807 ++tu_stats->nr_symtab_sharers;
7808 }
7809 else
7810 {
7811 /* Ugh, no stmt_list. Rare, but we have to handle it.
7812 We can do various things here like create one group per TU or
7813 spread them over multiple groups to split up the expansion work.
7814 To avoid worst case scenarios (too many groups or too large groups)
7815 we, umm, group them in bunches. */
7816 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7817 | (tu_stats->nr_stmt_less_type_units
7818 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7819 ++tu_stats->nr_stmt_less_type_units;
7820 }
7821
094b34ac 7822 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7823 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
f4dc4d17
DE
7824 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
7825 &type_unit_group_for_lookup, INSERT);
7826 if (*slot != NULL)
7827 {
9a3c8263 7828 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7829 gdb_assert (tu_group != NULL);
7830 }
7831 else
7832 {
9c541725 7833 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7834 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7835 *slot = tu_group;
7836 ++tu_stats->nr_symtabs;
7837 }
7838
7839 return tu_group;
7840}
0018ea6f
DE
7841\f
7842/* Partial symbol tables. */
7843
7844/* Create a psymtab named NAME and assign it to PER_CU.
7845
7846 The caller must fill in the following details:
7847 dirname, textlow, texthigh. */
7848
7849static struct partial_symtab *
7850create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7851{
e3b94546 7852 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
0018ea6f
DE
7853 struct partial_symtab *pst;
7854
18a94d75 7855 pst = start_psymtab_common (objfile, name, 0,
af5bf4ad
SM
7856 objfile->global_psymbols,
7857 objfile->static_psymbols);
0018ea6f
DE
7858
7859 pst->psymtabs_addrmap_supported = 1;
7860
7861 /* This is the glue that links PST into GDB's symbol API. */
7862 pst->read_symtab_private = per_cu;
7863 pst->read_symtab = dwarf2_read_symtab;
7864 per_cu->v.psymtab = pst;
7865
7866 return pst;
7867}
7868
b93601f3
TT
7869/* The DATA object passed to process_psymtab_comp_unit_reader has this
7870 type. */
7871
7872struct process_psymtab_comp_unit_data
7873{
7874 /* True if we are reading a DW_TAG_partial_unit. */
7875
7876 int want_partial_unit;
7877
7878 /* The "pretend" language that is used if the CU doesn't declare a
7879 language. */
7880
7881 enum language pretend_language;
7882};
7883
0018ea6f
DE
7884/* die_reader_func for process_psymtab_comp_unit. */
7885
7886static void
7887process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7888 const gdb_byte *info_ptr,
0018ea6f
DE
7889 struct die_info *comp_unit_die,
7890 int has_children,
7891 void *data)
7892{
7893 struct dwarf2_cu *cu = reader->cu;
518817b3 7894 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 7895 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 7896 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7897 CORE_ADDR baseaddr;
7898 CORE_ADDR best_lowpc = 0, best_highpc = 0;
7899 struct partial_symtab *pst;
3a2b436a 7900 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7901 const char *filename;
9a3c8263
SM
7902 struct process_psymtab_comp_unit_data *info
7903 = (struct process_psymtab_comp_unit_data *) data;
0018ea6f 7904
b93601f3 7905 if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
0018ea6f
DE
7906 return;
7907
7908 gdb_assert (! per_cu->is_debug_types);
7909
b93601f3 7910 prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
0018ea6f
DE
7911
7912 cu->list_in_scope = &file_symbols;
7913
7914 /* Allocate a new partial symbol table structure. */
7d45c7c3
KB
7915 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7916 if (filename == NULL)
0018ea6f 7917 filename = "";
0018ea6f
DE
7918
7919 pst = create_partial_symtab (per_cu, filename);
7920
7921 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7922 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f
DE
7923
7924 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
7925
7926 dwarf2_find_base_address (comp_unit_die, cu);
7927
7928 /* Possibly set the default values of LOWPC and HIGHPC from
7929 `DW_AT_ranges'. */
3a2b436a
JK
7930 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7931 &best_highpc, cu, pst);
7932 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
0018ea6f
DE
7933 /* Store the contiguous range if it is not empty; it can be empty for
7934 CUs with no code. */
7935 addrmap_set_empty (objfile->psymtabs_addrmap,
3e29f34a
MR
7936 gdbarch_adjust_dwarf2_addr (gdbarch,
7937 best_lowpc + baseaddr),
7938 gdbarch_adjust_dwarf2_addr (gdbarch,
7939 best_highpc + baseaddr) - 1,
7940 pst);
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 }
3e29f34a
MR
7971 pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
7972 pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);
0018ea6f 7973
8763cede 7974 end_psymtab_common (objfile, pst);
0018ea6f
DE
7975
7976 if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
7977 {
7978 int i;
7979 int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
7980 struct dwarf2_per_cu_data *iter;
7981
7982 /* Fill in 'dependencies' here; we fill in 'users' in a
7983 post-pass. */
7984 pst->number_of_dependencies = len;
8d749320
SM
7985 pst->dependencies =
7986 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
0018ea6f
DE
7987 for (i = 0;
7988 VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
7989 i, iter);
7990 ++i)
7991 pst->dependencies[i] = iter->v.psymtab;
7992
7993 VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
7994 }
7995
7996 /* Get the list of files included in the current compilation unit,
7997 and build a psymtab for each of them. */
7998 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7999
b4f54984 8000 if (dwarf_read_debug)
0018ea6f
DE
8001 {
8002 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8003
8004 fprintf_unfiltered (gdb_stdlog,
9d8780f0 8005 "Psymtab for %s unit @%s: %s - %s"
0018ea6f
DE
8006 ", %d global, %d static syms\n",
8007 per_cu->is_debug_types ? "type" : "comp",
9d8780f0 8008 sect_offset_str (per_cu->sect_off),
0018ea6f
DE
8009 paddress (gdbarch, pst->textlow),
8010 paddress (gdbarch, pst->texthigh),
8011 pst->n_global_syms, pst->n_static_syms);
8012 }
8013}
8014
8015/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8016 Process compilation unit THIS_CU for a psymtab. */
8017
8018static void
8019process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
b93601f3
TT
8020 int want_partial_unit,
8021 enum language pretend_language)
0018ea6f
DE
8022{
8023 /* If this compilation unit was already read in, free the
8024 cached copy in order to read it in again. This is
8025 necessary because we skipped some symbols when we first
8026 read in the compilation unit (see load_partial_dies).
8027 This problem could be avoided, but the benefit is unclear. */
8028 if (this_cu->cu != NULL)
8029 free_one_cached_comp_unit (this_cu);
8030
f1902523 8031 if (this_cu->is_debug_types)
58f0c718
TT
8032 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
8033 build_type_psymtabs_reader, NULL);
f1902523
JK
8034 else
8035 {
8036 process_psymtab_comp_unit_data info;
8037 info.want_partial_unit = want_partial_unit;
8038 info.pretend_language = pretend_language;
58f0c718 8039 init_cutu_and_read_dies (this_cu, NULL, 0, 0, false,
f1902523
JK
8040 process_psymtab_comp_unit_reader, &info);
8041 }
0018ea6f
DE
8042
8043 /* Age out any secondary CUs. */
ed2dc618 8044 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 8045}
f4dc4d17
DE
8046
8047/* Reader function for build_type_psymtabs. */
8048
8049static void
8050build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 8051 const gdb_byte *info_ptr,
f4dc4d17
DE
8052 struct die_info *type_unit_die,
8053 int has_children,
8054 void *data)
8055{
ed2dc618 8056 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 8057 = reader->cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
8058 struct objfile *objfile = dwarf2_per_objfile->objfile;
8059 struct dwarf2_cu *cu = reader->cu;
8060 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 8061 struct signatured_type *sig_type;
f4dc4d17
DE
8062 struct type_unit_group *tu_group;
8063 struct attribute *attr;
8064 struct partial_die_info *first_die;
8065 CORE_ADDR lowpc, highpc;
8066 struct partial_symtab *pst;
8067
8068 gdb_assert (data == NULL);
0186c6a7
DE
8069 gdb_assert (per_cu->is_debug_types);
8070 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
8071
8072 if (! has_children)
8073 return;
8074
8075 attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
094b34ac 8076 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 8077
0186c6a7 8078 VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
f4dc4d17
DE
8079
8080 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
8081 cu->list_in_scope = &file_symbols;
8082 pst = create_partial_symtab (per_cu, "");
8083 pst->anonymous = 1;
8084
8085 first_die = load_partial_dies (reader, info_ptr, 1);
8086
8087 lowpc = (CORE_ADDR) -1;
8088 highpc = (CORE_ADDR) 0;
8089 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
8090
8763cede 8091 end_psymtab_common (objfile, pst);
f4dc4d17
DE
8092}
8093
73051182
DE
8094/* Struct used to sort TUs by their abbreviation table offset. */
8095
8096struct tu_abbrev_offset
8097{
b2bdb8cf
SM
8098 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
8099 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
8100 {}
8101
8102 signatured_type *sig_type;
73051182
DE
8103 sect_offset abbrev_offset;
8104};
8105
484cf504 8106/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 8107
484cf504
TT
8108static bool
8109sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
8110 const struct tu_abbrev_offset &b)
73051182 8111{
484cf504 8112 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
8113}
8114
8115/* Efficiently read all the type units.
8116 This does the bulk of the work for build_type_psymtabs.
8117
8118 The efficiency is because we sort TUs by the abbrev table they use and
8119 only read each abbrev table once. In one program there are 200K TUs
8120 sharing 8K abbrev tables.
8121
8122 The main purpose of this function is to support building the
8123 dwarf2_per_objfile->type_unit_groups table.
8124 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
8125 can collapse the search space by grouping them by stmt_list.
8126 The savings can be significant, in the same program from above the 200K TUs
8127 share 8K stmt_list tables.
8128
8129 FUNC is expected to call get_type_unit_group, which will create the
8130 struct type_unit_group if necessary and add it to
8131 dwarf2_per_objfile->type_unit_groups. */
8132
8133static void
ed2dc618 8134build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 8135{
73051182 8136 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
685af9cd 8137 abbrev_table_up abbrev_table;
73051182 8138 sect_offset abbrev_offset;
73051182
DE
8139
8140 /* It's up to the caller to not call us multiple times. */
8141 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
8142
b2bdb8cf 8143 if (dwarf2_per_objfile->all_type_units.empty ())
73051182
DE
8144 return;
8145
8146 /* TUs typically share abbrev tables, and there can be way more TUs than
8147 abbrev tables. Sort by abbrev table to reduce the number of times we
8148 read each abbrev table in.
8149 Alternatives are to punt or to maintain a cache of abbrev tables.
8150 This is simpler and efficient enough for now.
8151
8152 Later we group TUs by their DW_AT_stmt_list value (as this defines the
8153 symtab to use). Typically TUs with the same abbrev offset have the same
8154 stmt_list value too so in practice this should work well.
8155
8156 The basic algorithm here is:
8157
8158 sort TUs by abbrev table
8159 for each TU with same abbrev table:
8160 read abbrev table if first user
8161 read TU top level DIE
8162 [IWBN if DWO skeletons had DW_AT_stmt_list]
8163 call FUNC */
8164
b4f54984 8165 if (dwarf_read_debug)
73051182
DE
8166 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
8167
8168 /* Sort in a separate table to maintain the order of all_type_units
8169 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf
SM
8170 std::vector<tu_abbrev_offset> sorted_by_abbrev;
8171 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
8172
8173 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
8174 sorted_by_abbrev.emplace_back
8175 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
8176 sig_type->per_cu.section,
8177 sig_type->per_cu.sect_off));
73051182 8178
484cf504
TT
8179 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
8180 sort_tu_by_abbrev_offset);
73051182 8181
9c541725 8182 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 8183
b2bdb8cf 8184 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 8185 {
73051182
DE
8186 /* Switch to the next abbrev table if necessary. */
8187 if (abbrev_table == NULL
b2bdb8cf 8188 || tu.abbrev_offset != abbrev_offset)
73051182 8189 {
b2bdb8cf 8190 abbrev_offset = tu.abbrev_offset;
73051182 8191 abbrev_table =
ed2dc618
SM
8192 abbrev_table_read_table (dwarf2_per_objfile,
8193 &dwarf2_per_objfile->abbrev,
73051182
DE
8194 abbrev_offset);
8195 ++tu_stats->nr_uniq_abbrev_tables;
8196 }
8197
b2bdb8cf 8198 init_cutu_and_read_dies (&tu.sig_type->per_cu, abbrev_table.get (),
58f0c718 8199 0, 0, false, build_type_psymtabs_reader, NULL);
73051182 8200 }
6aa5f3a6 8201}
73051182 8202
6aa5f3a6
DE
8203/* Print collected type unit statistics. */
8204
8205static void
ed2dc618 8206print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8207{
8208 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
8209
8210 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf
SM
8211 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
8212 dwarf2_per_objfile->all_type_units.size ());
6aa5f3a6
DE
8213 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
8214 tu_stats->nr_uniq_abbrev_tables);
8215 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
8216 tu_stats->nr_symtabs);
8217 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
8218 tu_stats->nr_symtab_sharers);
8219 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
8220 tu_stats->nr_stmt_less_type_units);
8221 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
8222 tu_stats->nr_all_type_units_reallocs);
73051182
DE
8223}
8224
f4dc4d17
DE
8225/* Traversal function for build_type_psymtabs. */
8226
8227static int
8228build_type_psymtab_dependencies (void **slot, void *info)
8229{
ed2dc618
SM
8230 struct dwarf2_per_objfile *dwarf2_per_objfile
8231 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
8232 struct objfile *objfile = dwarf2_per_objfile->objfile;
8233 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 8234 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
f4dc4d17 8235 struct partial_symtab *pst = per_cu->v.psymtab;
0186c6a7
DE
8236 int len = VEC_length (sig_type_ptr, tu_group->tus);
8237 struct signatured_type *iter;
f4dc4d17
DE
8238 int i;
8239
8240 gdb_assert (len > 0);
0186c6a7 8241 gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
f4dc4d17
DE
8242
8243 pst->number_of_dependencies = len;
8d749320
SM
8244 pst->dependencies =
8245 XOBNEWVEC (&objfile->objfile_obstack, struct partial_symtab *, len);
f4dc4d17 8246 for (i = 0;
0186c6a7 8247 VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
f4dc4d17
DE
8248 ++i)
8249 {
0186c6a7
DE
8250 gdb_assert (iter->per_cu.is_debug_types);
8251 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 8252 iter->type_unit_group = tu_group;
f4dc4d17
DE
8253 }
8254
0186c6a7 8255 VEC_free (sig_type_ptr, tu_group->tus);
348e048f
DE
8256
8257 return 1;
8258}
8259
8260/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8261 Build partial symbol tables for the .debug_types comp-units. */
8262
8263static void
ed2dc618 8264build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 8265{
ed2dc618 8266 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
8267 return;
8268
ed2dc618 8269 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 8270}
f4dc4d17 8271
6aa5f3a6
DE
8272/* Traversal function for process_skeletonless_type_unit.
8273 Read a TU in a DWO file and build partial symbols for it. */
8274
8275static int
8276process_skeletonless_type_unit (void **slot, void *info)
8277{
8278 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
8279 struct dwarf2_per_objfile *dwarf2_per_objfile
8280 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
8281 struct signatured_type find_entry, *entry;
8282
8283 /* If this TU doesn't exist in the global table, add it and read it in. */
8284
8285 if (dwarf2_per_objfile->signatured_types == NULL)
8286 {
8287 dwarf2_per_objfile->signatured_types
ed2dc618 8288 = allocate_signatured_type_table (dwarf2_per_objfile->objfile);
6aa5f3a6
DE
8289 }
8290
8291 find_entry.signature = dwo_unit->signature;
8292 slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
8293 INSERT);
8294 /* If we've already seen this type there's nothing to do. What's happening
8295 is we're doing our own version of comdat-folding here. */
8296 if (*slot != NULL)
8297 return 1;
8298
8299 /* This does the job that create_all_type_units would have done for
8300 this TU. */
ed2dc618
SM
8301 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
8302 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
8303 *slot = entry;
8304
8305 /* This does the job that build_type_psymtabs_1 would have done. */
58f0c718 8306 init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0, false,
6aa5f3a6
DE
8307 build_type_psymtabs_reader, NULL);
8308
8309 return 1;
8310}
8311
8312/* Traversal function for process_skeletonless_type_units. */
8313
8314static int
8315process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8316{
8317 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8318
8319 if (dwo_file->tus != NULL)
8320 {
8321 htab_traverse_noresize (dwo_file->tus,
8322 process_skeletonless_type_unit, info);
8323 }
8324
8325 return 1;
8326}
8327
8328/* Scan all TUs of DWO files, verifying we've processed them.
8329 This is needed in case a TU was emitted without its skeleton.
8330 Note: This can't be done until we know what all the DWO files are. */
8331
8332static void
ed2dc618 8333process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
8334{
8335 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 8336 if (get_dwp_file (dwarf2_per_objfile) == NULL
6aa5f3a6
DE
8337 && dwarf2_per_objfile->dwo_files != NULL)
8338 {
8339 htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
8340 process_dwo_file_for_skeletonless_type_units,
ed2dc618 8341 dwarf2_per_objfile);
6aa5f3a6 8342 }
348e048f
DE
8343}
8344
ed2dc618 8345/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
8346
8347static void
ed2dc618 8348set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 8349{
b76e467d 8350 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
95554aad 8351 {
95554aad 8352 struct partial_symtab *pst = per_cu->v.psymtab;
95554aad 8353
36586728
TT
8354 if (pst == NULL)
8355 continue;
8356
b76e467d 8357 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
8358 {
8359 /* Set the 'user' field only if it is not already set. */
8360 if (pst->dependencies[j]->user == NULL)
8361 pst->dependencies[j]->user = pst;
8362 }
8363 }
8364}
8365
93311388
DE
8366/* Build the partial symbol table by doing a quick pass through the
8367 .debug_info and .debug_abbrev sections. */
72bf9492 8368
93311388 8369static void
ed2dc618 8370dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 8371{
ed2dc618 8372 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 8373
b4f54984 8374 if (dwarf_read_debug)
45cfd468
DE
8375 {
8376 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 8377 objfile_name (objfile));
45cfd468
DE
8378 }
8379
98bfdba5
PA
8380 dwarf2_per_objfile->reading_partial_symbols = 1;
8381
be391dca 8382 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
91c24f0a 8383
93311388
DE
8384 /* Any cached compilation units will be linked by the per-objfile
8385 read_in_chain. Make sure to free them when we're done. */
11ed8cad 8386 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 8387
ed2dc618 8388 build_type_psymtabs (dwarf2_per_objfile);
348e048f 8389
ed2dc618 8390 create_all_comp_units (dwarf2_per_objfile);
c906108c 8391
60606b2c
TT
8392 /* Create a temporary address map on a temporary obstack. We later
8393 copy this to the final obstack. */
8268c778 8394 auto_obstack temp_obstack;
791afaa2
TT
8395
8396 scoped_restore save_psymtabs_addrmap
8397 = make_scoped_restore (&objfile->psymtabs_addrmap,
8398 addrmap_create_mutable (&temp_obstack));
72bf9492 8399
b76e467d
SM
8400 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
8401 process_psymtab_comp_unit (per_cu, 0, language_minimal);
ff013f42 8402
6aa5f3a6 8403 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 8404 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
8405
8406 /* Now that all TUs have been processed we can fill in the dependencies. */
8407 if (dwarf2_per_objfile->type_unit_groups != NULL)
8408 {
8409 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
ed2dc618 8410 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
8411 }
8412
b4f54984 8413 if (dwarf_read_debug)
ed2dc618 8414 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 8415
ed2dc618 8416 set_partial_user (dwarf2_per_objfile);
95554aad 8417
ff013f42
JK
8418 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
8419 &objfile->objfile_obstack);
791afaa2
TT
8420 /* At this point we want to keep the address map. */
8421 save_psymtabs_addrmap.release ();
ff013f42 8422
b4f54984 8423 if (dwarf_read_debug)
45cfd468 8424 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 8425 objfile_name (objfile));
ae038cb0
DJ
8426}
8427
3019eac3 8428/* die_reader_func for load_partial_comp_unit. */
ae038cb0
DJ
8429
8430static void
dee91e82 8431load_partial_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 8432 const gdb_byte *info_ptr,
dee91e82
DE
8433 struct die_info *comp_unit_die,
8434 int has_children,
8435 void *data)
ae038cb0 8436{
dee91e82 8437 struct dwarf2_cu *cu = reader->cu;
ae038cb0 8438
95554aad 8439 prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
ae038cb0 8440
ae038cb0
DJ
8441 /* Check if comp unit has_children.
8442 If so, read the rest of the partial symbols from this comp unit.
0963b4bd 8443 If not, there's no more debug_info for this comp unit. */
d85a05f0 8444 if (has_children)
dee91e82
DE
8445 load_partial_dies (reader, info_ptr, 0);
8446}
98bfdba5 8447
dee91e82
DE
8448/* Load the partial DIEs for a secondary CU into memory.
8449 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8450
dee91e82
DE
8451static void
8452load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
8453{
58f0c718 8454 init_cutu_and_read_dies (this_cu, NULL, 1, 1, false,
f4dc4d17 8455 load_partial_comp_unit_reader, NULL);
ae038cb0
DJ
8456}
8457
ae038cb0 8458static void
ed2dc618 8459read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 8460 struct dwarf2_section_info *section,
f1902523 8461 struct dwarf2_section_info *abbrev_section,
b76e467d 8462 unsigned int is_dwz)
ae038cb0 8463{
d521ce57 8464 const gdb_byte *info_ptr;
ed2dc618 8465 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 8466
b4f54984 8467 if (dwarf_read_debug)
bf6af496 8468 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
a32a8923
DE
8469 get_section_name (section),
8470 get_section_file_name (section));
bf6af496 8471
36586728 8472 dwarf2_read_section (objfile, section);
ae038cb0 8473
36586728 8474 info_ptr = section->buffer;
6e70227d 8475
36586728 8476 while (info_ptr < section->buffer + section->size)
ae038cb0 8477 {
ae038cb0 8478 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8479
9c541725 8480 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8481
f1902523 8482 comp_unit_head cu_header;
ed2dc618
SM
8483 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8484 abbrev_section, info_ptr,
8485 rcuh_kind::COMPILE);
ae038cb0
DJ
8486
8487 /* Save the compilation unit for later lookup. */
f1902523
JK
8488 if (cu_header.unit_type != DW_UT_type)
8489 {
8490 this_cu = XOBNEW (&objfile->objfile_obstack,
8491 struct dwarf2_per_cu_data);
8492 memset (this_cu, 0, sizeof (*this_cu));
8493 }
8494 else
8495 {
8496 auto sig_type = XOBNEW (&objfile->objfile_obstack,
8497 struct signatured_type);
8498 memset (sig_type, 0, sizeof (*sig_type));
8499 sig_type->signature = cu_header.signature;
8500 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8501 this_cu = &sig_type->per_cu;
8502 }
8503 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8504 this_cu->sect_off = sect_off;
f1902523 8505 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8506 this_cu->is_dwz = is_dwz;
e3b94546 8507 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 8508 this_cu->section = section;
ae038cb0 8509
b76e467d 8510 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8511
8512 info_ptr = info_ptr + this_cu->length;
8513 }
36586728
TT
8514}
8515
8516/* Create a list of all compilation units in OBJFILE.
8517 This is only done for -readnow and building partial symtabs. */
8518
8519static void
ed2dc618 8520create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 8521{
b76e467d 8522 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
ed2dc618 8523 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
b76e467d 8524 &dwarf2_per_objfile->abbrev, 0);
36586728 8525
b76e467d 8526 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 8527 if (dwz != NULL)
ed2dc618 8528 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
b76e467d 8529 1);
c906108c
SS
8530}
8531
5734ee8b 8532/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8533 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8534 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8535 DW_AT_ranges). See the comments of add_partial_subprogram on how
8536 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8537
72bf9492
DJ
8538static void
8539scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8540 CORE_ADDR *highpc, int set_addrmap,
8541 struct dwarf2_cu *cu)
c906108c 8542{
72bf9492 8543 struct partial_die_info *pdi;
c906108c 8544
91c24f0a
DC
8545 /* Now, march along the PDI's, descending into ones which have
8546 interesting children but skipping the children of the other ones,
8547 until we reach the end of the compilation unit. */
c906108c 8548
72bf9492 8549 pdi = first_die;
91c24f0a 8550
72bf9492
DJ
8551 while (pdi != NULL)
8552 {
52356b79 8553 pdi->fixup (cu);
c906108c 8554
f55ee35c 8555 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8556 children, so we need to look at them. Ditto for anonymous
8557 enums. */
933c6fe4 8558
72bf9492 8559 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8560 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8561 || pdi->tag == DW_TAG_imported_unit
8562 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8563 {
72bf9492 8564 switch (pdi->tag)
c906108c
SS
8565 {
8566 case DW_TAG_subprogram:
b1dc1806 8567 case DW_TAG_inlined_subroutine:
cdc07690 8568 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 8569 break;
72929c62 8570 case DW_TAG_constant:
c906108c
SS
8571 case DW_TAG_variable:
8572 case DW_TAG_typedef:
91c24f0a 8573 case DW_TAG_union_type:
72bf9492 8574 if (!pdi->is_declaration)
63d06c5c 8575 {
72bf9492 8576 add_partial_symbol (pdi, cu);
63d06c5c
DC
8577 }
8578 break;
c906108c 8579 case DW_TAG_class_type:
680b30c7 8580 case DW_TAG_interface_type:
c906108c 8581 case DW_TAG_structure_type:
72bf9492 8582 if (!pdi->is_declaration)
c906108c 8583 {
72bf9492 8584 add_partial_symbol (pdi, cu);
c906108c 8585 }
b7fee5a3
KS
8586 if ((cu->language == language_rust
8587 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8588 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8589 set_addrmap, cu);
c906108c 8590 break;
91c24f0a 8591 case DW_TAG_enumeration_type:
72bf9492
DJ
8592 if (!pdi->is_declaration)
8593 add_partial_enumeration (pdi, cu);
c906108c
SS
8594 break;
8595 case DW_TAG_base_type:
a02abb62 8596 case DW_TAG_subrange_type:
c906108c 8597 /* File scope base type definitions are added to the partial
c5aa993b 8598 symbol table. */
72bf9492 8599 add_partial_symbol (pdi, cu);
c906108c 8600 break;
d9fa45fe 8601 case DW_TAG_namespace:
cdc07690 8602 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8603 break;
5d7cb8df 8604 case DW_TAG_module:
cdc07690 8605 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8606 break;
95554aad
TT
8607 case DW_TAG_imported_unit:
8608 {
8609 struct dwarf2_per_cu_data *per_cu;
8610
f4dc4d17
DE
8611 /* For now we don't handle imported units in type units. */
8612 if (cu->per_cu->is_debug_types)
8613 {
8614 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8615 " supported in type units [in module %s]"),
518817b3 8616 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
8617 }
8618
e3b94546
SM
8619 per_cu = dwarf2_find_containing_comp_unit
8620 (pdi->d.sect_off, pdi->is_dwz,
518817b3 8621 cu->per_cu->dwarf2_per_objfile);
95554aad
TT
8622
8623 /* Go read the partial unit, if needed. */
8624 if (per_cu->v.psymtab == NULL)
b93601f3 8625 process_psymtab_comp_unit (per_cu, 1, cu->language);
95554aad 8626
f4dc4d17 8627 VEC_safe_push (dwarf2_per_cu_ptr,
796a7ff8 8628 cu->per_cu->imported_symtabs, per_cu);
95554aad
TT
8629 }
8630 break;
74921315
KS
8631 case DW_TAG_imported_declaration:
8632 add_partial_symbol (pdi, cu);
8633 break;
c906108c
SS
8634 default:
8635 break;
8636 }
8637 }
8638
72bf9492
DJ
8639 /* If the die has a sibling, skip to the sibling. */
8640
8641 pdi = pdi->die_sibling;
8642 }
8643}
8644
8645/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8646
72bf9492 8647 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8648 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8649 Enumerators are an exception; they use the scope of their parent
8650 enumeration type, i.e. the name of the enumeration type is not
8651 prepended to the enumerator.
91c24f0a 8652
72bf9492
DJ
8653 There are two complexities. One is DW_AT_specification; in this
8654 case "parent" means the parent of the target of the specification,
8655 instead of the direct parent of the DIE. The other is compilers
8656 which do not emit DW_TAG_namespace; in this case we try to guess
8657 the fully qualified name of structure types from their members'
8658 linkage names. This must be done using the DIE's children rather
8659 than the children of any DW_AT_specification target. We only need
8660 to do this for structures at the top level, i.e. if the target of
8661 any DW_AT_specification (if any; otherwise the DIE itself) does not
8662 have a parent. */
8663
8664/* Compute the scope prefix associated with PDI's parent, in
8665 compilation unit CU. The result will be allocated on CU's
8666 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8667 field. NULL is returned if no prefix is necessary. */
15d034d0 8668static const char *
72bf9492
DJ
8669partial_die_parent_scope (struct partial_die_info *pdi,
8670 struct dwarf2_cu *cu)
8671{
15d034d0 8672 const char *grandparent_scope;
72bf9492 8673 struct partial_die_info *parent, *real_pdi;
91c24f0a 8674
72bf9492
DJ
8675 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8676 then this means the parent of the specification DIE. */
8677
8678 real_pdi = pdi;
72bf9492 8679 while (real_pdi->has_specification)
36586728
TT
8680 real_pdi = find_partial_die (real_pdi->spec_offset,
8681 real_pdi->spec_is_dwz, cu);
72bf9492
DJ
8682
8683 parent = real_pdi->die_parent;
8684 if (parent == NULL)
8685 return NULL;
8686
8687 if (parent->scope_set)
8688 return parent->scope;
8689
52356b79 8690 parent->fixup (cu);
72bf9492 8691
10b3939b 8692 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8693
acebe513
UW
8694 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8695 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8696 Work around this problem here. */
8697 if (cu->language == language_cplus
6e70227d 8698 && parent->tag == DW_TAG_namespace
acebe513
UW
8699 && strcmp (parent->name, "::") == 0
8700 && grandparent_scope == NULL)
8701 {
8702 parent->scope = NULL;
8703 parent->scope_set = 1;
8704 return NULL;
8705 }
8706
9c6c53f7
SA
8707 if (pdi->tag == DW_TAG_enumerator)
8708 /* Enumerators should not get the name of the enumeration as a prefix. */
8709 parent->scope = grandparent_scope;
8710 else if (parent->tag == DW_TAG_namespace
f55ee35c 8711 || parent->tag == DW_TAG_module
72bf9492
DJ
8712 || parent->tag == DW_TAG_structure_type
8713 || parent->tag == DW_TAG_class_type
680b30c7 8714 || parent->tag == DW_TAG_interface_type
ceeb3d5a
TT
8715 || parent->tag == DW_TAG_union_type
8716 || parent->tag == DW_TAG_enumeration_type)
72bf9492
DJ
8717 {
8718 if (grandparent_scope == NULL)
8719 parent->scope = parent->name;
8720 else
3e43a32a
MS
8721 parent->scope = typename_concat (&cu->comp_unit_obstack,
8722 grandparent_scope,
f55ee35c 8723 parent->name, 0, cu);
72bf9492 8724 }
72bf9492
DJ
8725 else
8726 {
8727 /* FIXME drow/2004-04-01: What should we be doing with
8728 function-local names? For partial symbols, we should probably be
8729 ignoring them. */
b98664d3 8730 complaint (_("unhandled containing DIE tag %d for DIE at %s"),
9d8780f0 8731 parent->tag, sect_offset_str (pdi->sect_off));
72bf9492 8732 parent->scope = grandparent_scope;
c906108c
SS
8733 }
8734
72bf9492
DJ
8735 parent->scope_set = 1;
8736 return parent->scope;
8737}
8738
8739/* Return the fully scoped name associated with PDI, from compilation unit
8740 CU. The result will be allocated with malloc. */
4568ecf9 8741
72bf9492
DJ
8742static char *
8743partial_die_full_name (struct partial_die_info *pdi,
8744 struct dwarf2_cu *cu)
8745{
15d034d0 8746 const char *parent_scope;
72bf9492 8747
98bfdba5
PA
8748 /* If this is a template instantiation, we can not work out the
8749 template arguments from partial DIEs. So, unfortunately, we have
8750 to go through the full DIEs. At least any work we do building
8751 types here will be reused if full symbols are loaded later. */
8752 if (pdi->has_template_arguments)
8753 {
52356b79 8754 pdi->fixup (cu);
98bfdba5
PA
8755
8756 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8757 {
8758 struct die_info *die;
8759 struct attribute attr;
8760 struct dwarf2_cu *ref_cu = cu;
8761
b64f50a1 8762 /* DW_FORM_ref_addr is using section offset. */
b4069958 8763 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8764 attr.form = DW_FORM_ref_addr;
9c541725 8765 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8766 die = follow_die_ref (NULL, &attr, &ref_cu);
8767
8768 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
8769 }
8770 }
8771
72bf9492
DJ
8772 parent_scope = partial_die_parent_scope (pdi, cu);
8773 if (parent_scope == NULL)
8774 return NULL;
8775 else
f55ee35c 8776 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
c906108c
SS
8777}
8778
8779static void
72bf9492 8780add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8781{
518817b3
SM
8782 struct dwarf2_per_objfile *dwarf2_per_objfile
8783 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 8784 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 8785 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 8786 CORE_ADDR addr = 0;
15d034d0 8787 const char *actual_name = NULL;
e142c38c 8788 CORE_ADDR baseaddr;
15d034d0 8789 char *built_actual_name;
e142c38c
DJ
8790
8791 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 8792
15d034d0
TT
8793 built_actual_name = partial_die_full_name (pdi, cu);
8794 if (built_actual_name != NULL)
8795 actual_name = built_actual_name;
63d06c5c 8796
72bf9492
DJ
8797 if (actual_name == NULL)
8798 actual_name = pdi->name;
8799
c906108c
SS
8800 switch (pdi->tag)
8801 {
b1dc1806 8802 case DW_TAG_inlined_subroutine:
c906108c 8803 case DW_TAG_subprogram:
3e29f34a 8804 addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
2cfa0c8d 8805 if (pdi->is_external || cu->language == language_ada)
c906108c 8806 {
2cfa0c8d
JB
8807 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
8808 of the global scope. But in Ada, we want to be able to access
8809 nested procedures globally. So all Ada subprograms are stored
8810 in the global scope. */
f47fb265 8811 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8812 built_actual_name != NULL,
f47fb265
MS
8813 VAR_DOMAIN, LOC_BLOCK,
8814 &objfile->global_psymbols,
1762568f 8815 addr, cu->language, objfile);
c906108c
SS
8816 }
8817 else
8818 {
f47fb265 8819 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8820 built_actual_name != NULL,
f47fb265
MS
8821 VAR_DOMAIN, LOC_BLOCK,
8822 &objfile->static_psymbols,
1762568f 8823 addr, cu->language, objfile);
c906108c 8824 }
0c1b455e
TT
8825
8826 if (pdi->main_subprogram && actual_name != NULL)
8827 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8828 break;
72929c62
JB
8829 case DW_TAG_constant:
8830 {
af5bf4ad 8831 std::vector<partial_symbol *> *list;
72929c62
JB
8832
8833 if (pdi->is_external)
8834 list = &objfile->global_psymbols;
8835 else
8836 list = &objfile->static_psymbols;
f47fb265 8837 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8838 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
1762568f 8839 list, 0, cu->language, objfile);
72929c62
JB
8840 }
8841 break;
c906108c 8842 case DW_TAG_variable:
95554aad
TT
8843 if (pdi->d.locdesc)
8844 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8845
95554aad 8846 if (pdi->d.locdesc
caac4577
JG
8847 && addr == 0
8848 && !dwarf2_per_objfile->has_section_at_zero)
8849 {
8850 /* A global or static variable may also have been stripped
8851 out by the linker if unused, in which case its address
8852 will be nullified; do not add such variables into partial
8853 symbol table then. */
8854 }
8855 else if (pdi->is_external)
c906108c
SS
8856 {
8857 /* Global Variable.
8858 Don't enter into the minimal symbol tables as there is
8859 a minimal symbol table entry from the ELF symbols already.
8860 Enter into partial symbol table if it has a location
8861 descriptor or a type.
8862 If the location descriptor is missing, new_symbol will create
8863 a LOC_UNRESOLVED symbol, the address of the variable will then
8864 be determined from the minimal symbol table whenever the variable
8865 is referenced.
8866 The address for the partial symbol table entry is not
8867 used by GDB, but it comes in handy for debugging partial symbol
8868 table building. */
8869
95554aad 8870 if (pdi->d.locdesc || pdi->has_type)
f47fb265 8871 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8872 built_actual_name != NULL,
f47fb265
MS
8873 VAR_DOMAIN, LOC_STATIC,
8874 &objfile->global_psymbols,
1762568f 8875 addr + baseaddr,
f47fb265 8876 cu->language, objfile);
c906108c
SS
8877 }
8878 else
8879 {
ff908ebf
AW
8880 int has_loc = pdi->d.locdesc != NULL;
8881
8882 /* Static Variable. Skip symbols whose value we cannot know (those
8883 without location descriptors or constant values). */
8884 if (!has_loc && !pdi->has_const_value)
decbce07 8885 {
15d034d0 8886 xfree (built_actual_name);
decbce07
MS
8887 return;
8888 }
ff908ebf 8889
f47fb265 8890 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8891 built_actual_name != NULL,
f47fb265
MS
8892 VAR_DOMAIN, LOC_STATIC,
8893 &objfile->static_psymbols,
ff908ebf 8894 has_loc ? addr + baseaddr : (CORE_ADDR) 0,
f47fb265 8895 cu->language, objfile);
c906108c
SS
8896 }
8897 break;
8898 case DW_TAG_typedef:
8899 case DW_TAG_base_type:
a02abb62 8900 case DW_TAG_subrange_type:
38d518c9 8901 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8902 built_actual_name != NULL,
176620f1 8903 VAR_DOMAIN, LOC_TYPEDEF,
c906108c 8904 &objfile->static_psymbols,
1762568f 8905 0, cu->language, objfile);
c906108c 8906 break;
74921315 8907 case DW_TAG_imported_declaration:
72bf9492
DJ
8908 case DW_TAG_namespace:
8909 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8910 built_actual_name != NULL,
72bf9492
DJ
8911 VAR_DOMAIN, LOC_TYPEDEF,
8912 &objfile->global_psymbols,
1762568f 8913 0, cu->language, objfile);
72bf9492 8914 break;
530e8392
KB
8915 case DW_TAG_module:
8916 add_psymbol_to_list (actual_name, strlen (actual_name),
8917 built_actual_name != NULL,
8918 MODULE_DOMAIN, LOC_TYPEDEF,
8919 &objfile->global_psymbols,
1762568f 8920 0, cu->language, objfile);
530e8392 8921 break;
c906108c 8922 case DW_TAG_class_type:
680b30c7 8923 case DW_TAG_interface_type:
c906108c
SS
8924 case DW_TAG_structure_type:
8925 case DW_TAG_union_type:
8926 case DW_TAG_enumeration_type:
fa4028e9
JB
8927 /* Skip external references. The DWARF standard says in the section
8928 about "Structure, Union, and Class Type Entries": "An incomplete
8929 structure, union or class type is represented by a structure,
8930 union or class entry that does not have a byte size attribute
8931 and that has a DW_AT_declaration attribute." */
8932 if (!pdi->has_byte_size && pdi->is_declaration)
decbce07 8933 {
15d034d0 8934 xfree (built_actual_name);
decbce07
MS
8935 return;
8936 }
fa4028e9 8937
63d06c5c
DC
8938 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8939 static vs. global. */
38d518c9 8940 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8941 built_actual_name != NULL,
176620f1 8942 STRUCT_DOMAIN, LOC_TYPEDEF,
9c37b5ae 8943 cu->language == language_cplus
63d06c5c
DC
8944 ? &objfile->global_psymbols
8945 : &objfile->static_psymbols,
1762568f 8946 0, cu->language, objfile);
c906108c 8947
c906108c
SS
8948 break;
8949 case DW_TAG_enumerator:
38d518c9 8950 add_psymbol_to_list (actual_name, strlen (actual_name),
15d034d0 8951 built_actual_name != NULL,
176620f1 8952 VAR_DOMAIN, LOC_CONST,
9c37b5ae 8953 cu->language == language_cplus
f6fe98ef
DJ
8954 ? &objfile->global_psymbols
8955 : &objfile->static_psymbols,
1762568f 8956 0, cu->language, objfile);
c906108c
SS
8957 break;
8958 default:
8959 break;
8960 }
5c4e30ca 8961
15d034d0 8962 xfree (built_actual_name);
c906108c
SS
8963}
8964
5c4e30ca
DC
8965/* Read a partial die corresponding to a namespace; also, add a symbol
8966 corresponding to that namespace to the symbol table. NAMESPACE is
8967 the name of the enclosing namespace. */
91c24f0a 8968
72bf9492
DJ
8969static void
8970add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8971 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8972 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8973{
72bf9492 8974 /* Add a symbol for the namespace. */
e7c27a73 8975
72bf9492 8976 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8977
8978 /* Now scan partial symbols in that namespace. */
8979
91c24f0a 8980 if (pdi->has_children)
cdc07690 8981 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8982}
8983
5d7cb8df
JK
8984/* Read a partial die corresponding to a Fortran module. */
8985
8986static void
8987add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8988 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8989{
530e8392
KB
8990 /* Add a symbol for the namespace. */
8991
8992 add_partial_symbol (pdi, cu);
8993
f55ee35c 8994 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8995
8996 if (pdi->has_children)
cdc07690 8997 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8998}
8999
b1dc1806
XR
9000/* Read a partial die corresponding to a subprogram or an inlined
9001 subprogram and create a partial symbol for that subprogram.
9002 When the CU language allows it, this routine also defines a partial
9003 symbol for each nested subprogram that this subprogram contains.
9004 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
9005 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 9006
cdc07690
YQ
9007 PDI may also be a lexical block, in which case we simply search
9008 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
9009 Again, this is only performed when the CU language allows this
9010 type of definitions. */
9011
9012static void
9013add_partial_subprogram (struct partial_die_info *pdi,
9014 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 9015 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 9016{
b1dc1806 9017 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
9018 {
9019 if (pdi->has_pc_info)
9020 {
9021 if (pdi->lowpc < *lowpc)
9022 *lowpc = pdi->lowpc;
9023 if (pdi->highpc > *highpc)
9024 *highpc = pdi->highpc;
cdc07690 9025 if (set_addrmap)
5734ee8b 9026 {
518817b3 9027 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a
MR
9028 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9029 CORE_ADDR baseaddr;
9030 CORE_ADDR highpc;
9031 CORE_ADDR lowpc;
5734ee8b
DJ
9032
9033 baseaddr = ANOFFSET (objfile->section_offsets,
9034 SECT_OFF_TEXT (objfile));
3e29f34a
MR
9035 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9036 pdi->lowpc + baseaddr);
9037 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
9038 pdi->highpc + baseaddr);
9039 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
9291a0cd 9040 cu->per_cu->v.psymtab);
5734ee8b 9041 }
481860b3
GB
9042 }
9043
9044 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
9045 {
bc30ff58 9046 if (!pdi->is_declaration)
e8d05480
JB
9047 /* Ignore subprogram DIEs that do not have a name, they are
9048 illegal. Do not emit a complaint at this point, we will
9049 do so when we convert this psymtab into a symtab. */
9050 if (pdi->name)
9051 add_partial_symbol (pdi, cu);
bc30ff58
JB
9052 }
9053 }
6e70227d 9054
bc30ff58
JB
9055 if (! pdi->has_children)
9056 return;
9057
9058 if (cu->language == language_ada)
9059 {
9060 pdi = pdi->die_child;
9061 while (pdi != NULL)
9062 {
52356b79 9063 pdi->fixup (cu);
bc30ff58 9064 if (pdi->tag == DW_TAG_subprogram
b1dc1806 9065 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 9066 || pdi->tag == DW_TAG_lexical_block)
cdc07690 9067 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
9068 pdi = pdi->die_sibling;
9069 }
9070 }
9071}
9072
91c24f0a
DC
9073/* Read a partial die corresponding to an enumeration type. */
9074
72bf9492
DJ
9075static void
9076add_partial_enumeration (struct partial_die_info *enum_pdi,
9077 struct dwarf2_cu *cu)
91c24f0a 9078{
72bf9492 9079 struct partial_die_info *pdi;
91c24f0a
DC
9080
9081 if (enum_pdi->name != NULL)
72bf9492
DJ
9082 add_partial_symbol (enum_pdi, cu);
9083
9084 pdi = enum_pdi->die_child;
9085 while (pdi)
91c24f0a 9086 {
72bf9492 9087 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
b98664d3 9088 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 9089 else
72bf9492
DJ
9090 add_partial_symbol (pdi, cu);
9091 pdi = pdi->die_sibling;
91c24f0a 9092 }
91c24f0a
DC
9093}
9094
6caca83c
CC
9095/* Return the initial uleb128 in the die at INFO_PTR. */
9096
9097static unsigned int
d521ce57 9098peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
9099{
9100 unsigned int bytes_read;
9101
9102 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9103}
9104
685af9cd
TT
9105/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
9106 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
9107
4bb7a0a7
DJ
9108 Return the corresponding abbrev, or NULL if the number is zero (indicating
9109 an empty DIE). In either case *BYTES_READ will be set to the length of
9110 the initial number. */
9111
9112static struct abbrev_info *
685af9cd
TT
9113peek_die_abbrev (const die_reader_specs &reader,
9114 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 9115{
685af9cd 9116 dwarf2_cu *cu = reader.cu;
518817b3 9117 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
685af9cd
TT
9118 unsigned int abbrev_number
9119 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
9120
9121 if (abbrev_number == 0)
9122 return NULL;
9123
685af9cd 9124 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
9125 if (!abbrev)
9126 {
422b9917 9127 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 9128 " at offset %s [in module %s]"),
422b9917 9129 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 9130 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
9131 }
9132
9133 return abbrev;
9134}
9135
93311388
DE
9136/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9137 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
9138 DIE. Any children of the skipped DIEs will also be skipped. */
9139
d521ce57
TT
9140static const gdb_byte *
9141skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 9142{
4bb7a0a7
DJ
9143 while (1)
9144 {
685af9cd
TT
9145 unsigned int bytes_read;
9146 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
9147
4bb7a0a7
DJ
9148 if (abbrev == NULL)
9149 return info_ptr + bytes_read;
9150 else
dee91e82 9151 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
9152 }
9153}
9154
93311388
DE
9155/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
9156 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
9157 abbrev corresponding to that skipped uleb128 should be passed in
9158 ABBREV. Returns a pointer to this DIE's sibling, skipping any
9159 children. */
9160
d521ce57
TT
9161static const gdb_byte *
9162skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 9163 struct abbrev_info *abbrev)
4bb7a0a7
DJ
9164{
9165 unsigned int bytes_read;
9166 struct attribute attr;
dee91e82
DE
9167 bfd *abfd = reader->abfd;
9168 struct dwarf2_cu *cu = reader->cu;
d521ce57 9169 const gdb_byte *buffer = reader->buffer;
f664829e 9170 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
9171 unsigned int form, i;
9172
9173 for (i = 0; i < abbrev->num_attrs; i++)
9174 {
9175 /* The only abbrev we care about is DW_AT_sibling. */
9176 if (abbrev->attrs[i].name == DW_AT_sibling)
9177 {
dee91e82 9178 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 9179 if (attr.form == DW_FORM_ref_addr)
b98664d3 9180 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 9181 else
b9502d3f 9182 {
9c541725
PA
9183 sect_offset off = dwarf2_get_ref_die_offset (&attr);
9184 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
9185
9186 if (sibling_ptr < info_ptr)
b98664d3 9187 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
9188 else if (sibling_ptr > reader->buffer_end)
9189 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f
WN
9190 else
9191 return sibling_ptr;
9192 }
4bb7a0a7
DJ
9193 }
9194
9195 /* If it isn't DW_AT_sibling, skip this attribute. */
9196 form = abbrev->attrs[i].form;
9197 skip_attribute:
9198 switch (form)
9199 {
4bb7a0a7 9200 case DW_FORM_ref_addr:
ae411497
TT
9201 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
9202 and later it is offset sized. */
9203 if (cu->header.version == 2)
9204 info_ptr += cu->header.addr_size;
9205 else
9206 info_ptr += cu->header.offset_size;
9207 break;
36586728
TT
9208 case DW_FORM_GNU_ref_alt:
9209 info_ptr += cu->header.offset_size;
9210 break;
ae411497 9211 case DW_FORM_addr:
4bb7a0a7
DJ
9212 info_ptr += cu->header.addr_size;
9213 break;
9214 case DW_FORM_data1:
9215 case DW_FORM_ref1:
9216 case DW_FORM_flag:
9217 info_ptr += 1;
9218 break;
2dc7f7b3 9219 case DW_FORM_flag_present:
43988095 9220 case DW_FORM_implicit_const:
2dc7f7b3 9221 break;
4bb7a0a7
DJ
9222 case DW_FORM_data2:
9223 case DW_FORM_ref2:
9224 info_ptr += 2;
9225 break;
9226 case DW_FORM_data4:
9227 case DW_FORM_ref4:
9228 info_ptr += 4;
9229 break;
9230 case DW_FORM_data8:
9231 case DW_FORM_ref8:
55f1336d 9232 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
9233 info_ptr += 8;
9234 break;
0224619f
JK
9235 case DW_FORM_data16:
9236 info_ptr += 16;
9237 break;
4bb7a0a7 9238 case DW_FORM_string:
9b1c24c8 9239 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
9240 info_ptr += bytes_read;
9241 break;
2dc7f7b3 9242 case DW_FORM_sec_offset:
4bb7a0a7 9243 case DW_FORM_strp:
36586728 9244 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
9245 info_ptr += cu->header.offset_size;
9246 break;
2dc7f7b3 9247 case DW_FORM_exprloc:
4bb7a0a7
DJ
9248 case DW_FORM_block:
9249 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9250 info_ptr += bytes_read;
9251 break;
9252 case DW_FORM_block1:
9253 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9254 break;
9255 case DW_FORM_block2:
9256 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9257 break;
9258 case DW_FORM_block4:
9259 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9260 break;
9261 case DW_FORM_sdata:
9262 case DW_FORM_udata:
9263 case DW_FORM_ref_udata:
3019eac3
DE
9264 case DW_FORM_GNU_addr_index:
9265 case DW_FORM_GNU_str_index:
d521ce57 9266 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
9267 break;
9268 case DW_FORM_indirect:
9269 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9270 info_ptr += bytes_read;
9271 /* We need to continue parsing from here, so just go back to
9272 the top. */
9273 goto skip_attribute;
9274
9275 default:
3e43a32a
MS
9276 error (_("Dwarf Error: Cannot handle %s "
9277 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
9278 dwarf_form_name (form),
9279 bfd_get_filename (abfd));
9280 }
9281 }
9282
9283 if (abbrev->has_children)
dee91e82 9284 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
9285 else
9286 return info_ptr;
9287}
9288
93311388 9289/* Locate ORIG_PDI's sibling.
dee91e82 9290 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 9291
d521ce57 9292static const gdb_byte *
dee91e82
DE
9293locate_pdi_sibling (const struct die_reader_specs *reader,
9294 struct partial_die_info *orig_pdi,
d521ce57 9295 const gdb_byte *info_ptr)
91c24f0a
DC
9296{
9297 /* Do we know the sibling already? */
72bf9492 9298
91c24f0a
DC
9299 if (orig_pdi->sibling)
9300 return orig_pdi->sibling;
9301
9302 /* Are there any children to deal with? */
9303
9304 if (!orig_pdi->has_children)
9305 return info_ptr;
9306
4bb7a0a7 9307 /* Skip the children the long way. */
91c24f0a 9308
dee91e82 9309 return skip_children (reader, info_ptr);
91c24f0a
DC
9310}
9311
257e7a09 9312/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 9313 not NULL. */
c906108c
SS
9314
9315static void
257e7a09
YQ
9316dwarf2_read_symtab (struct partial_symtab *self,
9317 struct objfile *objfile)
c906108c 9318{
ed2dc618
SM
9319 struct dwarf2_per_objfile *dwarf2_per_objfile
9320 = get_dwarf2_per_objfile (objfile);
9321
257e7a09 9322 if (self->readin)
c906108c 9323 {
442e4d9c 9324 warning (_("bug: psymtab for %s is already read in."),
257e7a09 9325 self->filename);
442e4d9c
YQ
9326 }
9327 else
9328 {
9329 if (info_verbose)
c906108c 9330 {
442e4d9c 9331 printf_filtered (_("Reading in symbols for %s..."),
257e7a09 9332 self->filename);
442e4d9c 9333 gdb_flush (gdb_stdout);
c906108c 9334 }
c906108c 9335
442e4d9c
YQ
9336 /* If this psymtab is constructed from a debug-only objfile, the
9337 has_section_at_zero flag will not necessarily be correct. We
9338 can get the correct value for this flag by looking at the data
9339 associated with the (presumably stripped) associated objfile. */
9340 if (objfile->separate_debug_objfile_backlink)
9341 {
9342 struct dwarf2_per_objfile *dpo_backlink
ed2dc618 9343 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
9a619af0 9344
442e4d9c
YQ
9345 dwarf2_per_objfile->has_section_at_zero
9346 = dpo_backlink->has_section_at_zero;
9347 }
b2ab525c 9348
442e4d9c 9349 dwarf2_per_objfile->reading_partial_symbols = 0;
98bfdba5 9350
257e7a09 9351 psymtab_to_symtab_1 (self);
c906108c 9352
442e4d9c
YQ
9353 /* Finish up the debug error message. */
9354 if (info_verbose)
9355 printf_filtered (_("done.\n"));
c906108c 9356 }
95554aad 9357
ed2dc618 9358 process_cu_includes (dwarf2_per_objfile);
c906108c 9359}
9cdd5dbd
DE
9360\f
9361/* Reading in full CUs. */
c906108c 9362
10b3939b
DJ
9363/* Add PER_CU to the queue. */
9364
9365static void
95554aad
TT
9366queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
9367 enum language pretend_language)
10b3939b
DJ
9368{
9369 struct dwarf2_queue_item *item;
9370
9371 per_cu->queued = 1;
8d749320 9372 item = XNEW (struct dwarf2_queue_item);
10b3939b 9373 item->per_cu = per_cu;
95554aad 9374 item->pretend_language = pretend_language;
10b3939b
DJ
9375 item->next = NULL;
9376
9377 if (dwarf2_queue == NULL)
9378 dwarf2_queue = item;
9379 else
9380 dwarf2_queue_tail->next = item;
9381
9382 dwarf2_queue_tail = item;
9383}
9384
89e63ee4
DE
9385/* If PER_CU is not yet queued, add it to the queue.
9386 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9387 dependency.
0907af0c 9388 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
9389 meaning either PER_CU is already queued or it is already loaded.
9390
9391 N.B. There is an invariant here that if a CU is queued then it is loaded.
9392 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
9393
9394static int
89e63ee4 9395maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
9396 struct dwarf2_per_cu_data *per_cu,
9397 enum language pretend_language)
9398{
9399 /* We may arrive here during partial symbol reading, if we need full
9400 DIEs to process an unusual case (e.g. template arguments). Do
9401 not queue PER_CU, just tell our caller to load its DIEs. */
ed2dc618 9402 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
0907af0c
DE
9403 {
9404 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
9405 return 1;
9406 return 0;
9407 }
9408
9409 /* Mark the dependence relation so that we don't flush PER_CU
9410 too early. */
89e63ee4
DE
9411 if (dependent_cu != NULL)
9412 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
9413
9414 /* If it's already on the queue, we have nothing to do. */
9415 if (per_cu->queued)
9416 return 0;
9417
9418 /* If the compilation unit is already loaded, just mark it as
9419 used. */
9420 if (per_cu->cu != NULL)
9421 {
9422 per_cu->cu->last_used = 0;
9423 return 0;
9424 }
9425
9426 /* Add it to the queue. */
9427 queue_comp_unit (per_cu, pretend_language);
9428
9429 return 1;
9430}
9431
10b3939b
DJ
9432/* Process the queue. */
9433
9434static void
ed2dc618 9435process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b
DJ
9436{
9437 struct dwarf2_queue_item *item, *next_item;
9438
b4f54984 9439 if (dwarf_read_debug)
45cfd468
DE
9440 {
9441 fprintf_unfiltered (gdb_stdlog,
9442 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 9443 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
9444 }
9445
03dd20cc
DJ
9446 /* The queue starts out with one item, but following a DIE reference
9447 may load a new CU, adding it to the end of the queue. */
10b3939b
DJ
9448 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
9449 {
cc12ce38
DE
9450 if ((dwarf2_per_objfile->using_index
9451 ? !item->per_cu->v.quick->compunit_symtab
9452 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
9453 /* Skip dummy CUs. */
9454 && item->per_cu->cu != NULL)
f4dc4d17
DE
9455 {
9456 struct dwarf2_per_cu_data *per_cu = item->per_cu;
73be47f5 9457 unsigned int debug_print_threshold;
247f5c4f 9458 char buf[100];
f4dc4d17 9459
247f5c4f 9460 if (per_cu->is_debug_types)
f4dc4d17 9461 {
247f5c4f
DE
9462 struct signatured_type *sig_type =
9463 (struct signatured_type *) per_cu;
9464
9d8780f0 9465 sprintf (buf, "TU %s at offset %s",
73be47f5 9466 hex_string (sig_type->signature),
9d8780f0 9467 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9468 /* There can be 100s of TUs.
9469 Only print them in verbose mode. */
9470 debug_print_threshold = 2;
f4dc4d17 9471 }
247f5c4f 9472 else
73be47f5 9473 {
9d8780f0
SM
9474 sprintf (buf, "CU at offset %s",
9475 sect_offset_str (per_cu->sect_off));
73be47f5
DE
9476 debug_print_threshold = 1;
9477 }
247f5c4f 9478
b4f54984 9479 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9480 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
9481
9482 if (per_cu->is_debug_types)
9483 process_full_type_unit (per_cu, item->pretend_language);
9484 else
9485 process_full_comp_unit (per_cu, item->pretend_language);
9486
b4f54984 9487 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 9488 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 9489 }
10b3939b
DJ
9490
9491 item->per_cu->queued = 0;
9492 next_item = item->next;
9493 xfree (item);
9494 }
9495
9496 dwarf2_queue_tail = NULL;
45cfd468 9497
b4f54984 9498 if (dwarf_read_debug)
45cfd468
DE
9499 {
9500 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 9501 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 9502 }
10b3939b
DJ
9503}
9504
10b3939b
DJ
9505/* Read in full symbols for PST, and anything it depends on. */
9506
c906108c 9507static void
fba45db2 9508psymtab_to_symtab_1 (struct partial_symtab *pst)
c906108c 9509{
10b3939b 9510 struct dwarf2_per_cu_data *per_cu;
aaa75496
JB
9511 int i;
9512
95554aad
TT
9513 if (pst->readin)
9514 return;
9515
aaa75496 9516 for (i = 0; i < pst->number_of_dependencies; i++)
95554aad
TT
9517 if (!pst->dependencies[i]->readin
9518 && pst->dependencies[i]->user == NULL)
aaa75496
JB
9519 {
9520 /* Inform about additional files that need to be read in. */
9521 if (info_verbose)
9522 {
a3f17187 9523 /* FIXME: i18n: Need to make this a single string. */
aaa75496
JB
9524 fputs_filtered (" ", gdb_stdout);
9525 wrap_here ("");
9526 fputs_filtered ("and ", gdb_stdout);
9527 wrap_here ("");
9528 printf_filtered ("%s...", pst->dependencies[i]->filename);
0963b4bd 9529 wrap_here (""); /* Flush output. */
aaa75496
JB
9530 gdb_flush (gdb_stdout);
9531 }
9532 psymtab_to_symtab_1 (pst->dependencies[i]);
9533 }
9534
9a3c8263 9535 per_cu = (struct dwarf2_per_cu_data *) pst->read_symtab_private;
10b3939b
DJ
9536
9537 if (per_cu == NULL)
aaa75496
JB
9538 {
9539 /* It's an include file, no symbols to read for it.
9540 Everything is in the parent symtab. */
9541 pst->readin = 1;
9542 return;
9543 }
c906108c 9544
58f0c718 9545 dw2_do_instantiate_symtab (per_cu, false);
10b3939b
DJ
9546}
9547
dee91e82
DE
9548/* Trivial hash function for die_info: the hash value of a DIE
9549 is its offset in .debug_info for this objfile. */
10b3939b 9550
dee91e82
DE
9551static hashval_t
9552die_hash (const void *item)
10b3939b 9553{
9a3c8263 9554 const struct die_info *die = (const struct die_info *) item;
6502dd73 9555
9c541725 9556 return to_underlying (die->sect_off);
dee91e82 9557}
63d06c5c 9558
dee91e82
DE
9559/* Trivial comparison function for die_info structures: two DIEs
9560 are equal if they have the same offset. */
98bfdba5 9561
dee91e82
DE
9562static int
9563die_eq (const void *item_lhs, const void *item_rhs)
9564{
9a3c8263
SM
9565 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9566 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9567
9c541725 9568 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9569}
c906108c 9570
dee91e82
DE
9571/* die_reader_func for load_full_comp_unit.
9572 This is identical to read_signatured_type_reader,
9573 but is kept separate for now. */
c906108c 9574
dee91e82
DE
9575static void
9576load_full_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 9577 const gdb_byte *info_ptr,
dee91e82
DE
9578 struct die_info *comp_unit_die,
9579 int has_children,
9580 void *data)
9581{
9582 struct dwarf2_cu *cu = reader->cu;
9a3c8263 9583 enum language *language_ptr = (enum language *) data;
6caca83c 9584
dee91e82
DE
9585 gdb_assert (cu->die_hash == NULL);
9586 cu->die_hash =
9587 htab_create_alloc_ex (cu->header.length / 12,
9588 die_hash,
9589 die_eq,
9590 NULL,
9591 &cu->comp_unit_obstack,
9592 hashtab_obstack_allocate,
9593 dummy_obstack_deallocate);
e142c38c 9594
dee91e82
DE
9595 if (has_children)
9596 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
9597 &info_ptr, comp_unit_die);
9598 cu->dies = comp_unit_die;
9599 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9600
9601 /* We try not to read any attributes in this function, because not
9cdd5dbd 9602 all CUs needed for references have been loaded yet, and symbol
10b3939b 9603 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9604 or we won't be able to build types correctly.
9605 Similarly, if we do not read the producer, we can not apply
9606 producer-specific interpretation. */
95554aad 9607 prepare_one_comp_unit (cu, cu->dies, *language_ptr);
dee91e82 9608}
10b3939b 9609
dee91e82 9610/* Load the DIEs associated with PER_CU into memory. */
a6c727b2 9611
dee91e82 9612static void
95554aad 9613load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
58f0c718 9614 bool skip_partial,
95554aad 9615 enum language pretend_language)
dee91e82 9616{
3019eac3 9617 gdb_assert (! this_cu->is_debug_types);
c5b7e1cb 9618
58f0c718 9619 init_cutu_and_read_dies (this_cu, NULL, 1, 1, skip_partial,
f4dc4d17 9620 load_full_comp_unit_reader, &pretend_language);
10b3939b
DJ
9621}
9622
3da10d80
KS
9623/* Add a DIE to the delayed physname list. */
9624
9625static void
9626add_to_method_list (struct type *type, int fnfield_index, int index,
9627 const char *name, struct die_info *die,
9628 struct dwarf2_cu *cu)
9629{
9630 struct delayed_method_info mi;
9631 mi.type = type;
9632 mi.fnfield_index = fnfield_index;
9633 mi.index = index;
9634 mi.name = name;
9635 mi.die = die;
c89b44cd 9636 cu->method_list.push_back (mi);
3da10d80
KS
9637}
9638
3693fdb3
PA
9639/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9640 "const" / "volatile". If so, decrements LEN by the length of the
9641 modifier and return true. Otherwise return false. */
9642
9643template<size_t N>
9644static bool
9645check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9646{
9647 size_t mod_len = sizeof (mod) - 1;
9648 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9649 {
9650 len -= mod_len;
9651 return true;
9652 }
9653 return false;
9654}
9655
3da10d80
KS
9656/* Compute the physnames of any methods on the CU's method list.
9657
9658 The computation of method physnames is delayed in order to avoid the
9659 (bad) condition that one of the method's formal parameters is of an as yet
9660 incomplete type. */
9661
9662static void
9663compute_delayed_physnames (struct dwarf2_cu *cu)
9664{
3693fdb3 9665 /* Only C++ delays computing physnames. */
c89b44cd 9666 if (cu->method_list.empty ())
3693fdb3
PA
9667 return;
9668 gdb_assert (cu->language == language_cplus);
9669
52941706 9670 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9671 {
1d06ead6 9672 const char *physname;
3da10d80 9673 struct fn_fieldlist *fn_flp
c89b44cd
TT
9674 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9675 physname = dwarf2_physname (mi.name, mi.die, cu);
9676 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9677 = physname ? physname : "";
3693fdb3
PA
9678
9679 /* Since there's no tag to indicate whether a method is a
9680 const/volatile overload, extract that information out of the
9681 demangled name. */
9682 if (physname != NULL)
9683 {
9684 size_t len = strlen (physname);
9685
9686 while (1)
9687 {
9688 if (physname[len] == ')') /* shortcut */
9689 break;
9690 else if (check_modifier (physname, len, " const"))
c89b44cd 9691 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9692 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9693 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9694 else
9695 break;
9696 }
9697 }
3da10d80 9698 }
c89b44cd
TT
9699
9700 /* The list is no longer needed. */
9701 cu->method_list.clear ();
3da10d80
KS
9702}
9703
a766d390
DE
9704/* Go objects should be embedded in a DW_TAG_module DIE,
9705 and it's not clear if/how imported objects will appear.
9706 To keep Go support simple until that's worked out,
9707 go back through what we've read and create something usable.
9708 We could do this while processing each DIE, and feels kinda cleaner,
9709 but that way is more invasive.
9710 This is to, for example, allow the user to type "p var" or "b main"
9711 without having to specify the package name, and allow lookups
9712 of module.object to work in contexts that use the expression
9713 parser. */
9714
9715static void
9716fixup_go_packaging (struct dwarf2_cu *cu)
9717{
9718 char *package_name = NULL;
9719 struct pending *list;
9720 int i;
9721
9722 for (list = global_symbols; list != NULL; list = list->next)
9723 {
9724 for (i = 0; i < list->nsyms; ++i)
9725 {
9726 struct symbol *sym = list->symbol[i];
9727
9728 if (SYMBOL_LANGUAGE (sym) == language_go
9729 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9730 {
9731 char *this_package_name = go_symbol_package_name (sym);
9732
9733 if (this_package_name == NULL)
9734 continue;
9735 if (package_name == NULL)
9736 package_name = this_package_name;
9737 else
9738 {
518817b3
SM
9739 struct objfile *objfile
9740 = cu->per_cu->dwarf2_per_objfile->objfile;
a766d390 9741 if (strcmp (package_name, this_package_name) != 0)
b98664d3 9742 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9743 (symbol_symtab (sym) != NULL
9744 ? symtab_to_filename_for_display
9745 (symbol_symtab (sym))
e3b94546 9746 : objfile_name (objfile)),
a766d390
DE
9747 this_package_name, package_name);
9748 xfree (this_package_name);
9749 }
9750 }
9751 }
9752 }
9753
9754 if (package_name != NULL)
9755 {
518817b3 9756 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
34a68019 9757 const char *saved_package_name
224c3ddb
SM
9758 = (const char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
9759 package_name,
9760 strlen (package_name));
19f392bc
UW
9761 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9762 saved_package_name);
a766d390
DE
9763 struct symbol *sym;
9764
e623cf5d 9765 sym = allocate_symbol (objfile);
f85f34ed 9766 SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
86f62fd7
TT
9767 SYMBOL_SET_NAMES (sym, saved_package_name,
9768 strlen (saved_package_name), 0, objfile);
a766d390
DE
9769 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9770 e.g., "main" finds the "main" module and not C's main(). */
9771 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9772 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9773 SYMBOL_TYPE (sym) = type;
9774
9775 add_symbol_to_list (sym, &global_symbols);
9776
9777 xfree (package_name);
9778 }
9779}
9780
c9317f21
TT
9781/* Allocate a fully-qualified name consisting of the two parts on the
9782 obstack. */
9783
9784static const char *
9785rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9786{
9787 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9788}
9789
9790/* A helper that allocates a struct discriminant_info to attach to a
9791 union type. */
9792
9793static struct discriminant_info *
9794alloc_discriminant_info (struct type *type, int discriminant_index,
9795 int default_index)
9796{
9797 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
c7b15a66
TT
9798 gdb_assert (discriminant_index == -1
9799 || (discriminant_index >= 0
9800 && discriminant_index < TYPE_NFIELDS (type)));
c9317f21 9801 gdb_assert (default_index == -1
c7b15a66 9802 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
c9317f21
TT
9803
9804 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9805
9806 struct discriminant_info *disc
9807 = ((struct discriminant_info *)
9808 TYPE_ZALLOC (type,
9809 offsetof (struct discriminant_info, discriminants)
9810 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9811 disc->default_index = default_index;
9812 disc->discriminant_index = discriminant_index;
9813
9814 struct dynamic_prop prop;
9815 prop.kind = PROP_UNDEFINED;
9816 prop.data.baton = disc;
9817
9818 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9819
9820 return disc;
9821}
9822
9823/* Some versions of rustc emitted enums in an unusual way.
9824
9825 Ordinary enums were emitted as unions. The first element of each
9826 structure in the union was named "RUST$ENUM$DISR". This element
9827 held the discriminant.
9828
9829 These versions of Rust also implemented the "non-zero"
9830 optimization. When the enum had two values, and one is empty and
9831 the other holds a pointer that cannot be zero, the pointer is used
9832 as the discriminant, with a zero value meaning the empty variant.
9833 Here, the union's first member is of the form
9834 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9835 where the fieldnos are the indices of the fields that should be
9836 traversed in order to find the field (which may be several fields deep)
9837 and the variantname is the name of the variant of the case when the
9838 field is zero.
9839
9840 This function recognizes whether TYPE is of one of these forms,
9841 and, if so, smashes it to be a variant type. */
9842
9843static void
9844quirk_rust_enum (struct type *type, struct objfile *objfile)
9845{
9846 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9847
9848 /* We don't need to deal with empty enums. */
9849 if (TYPE_NFIELDS (type) == 0)
9850 return;
9851
9852#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9853 if (TYPE_NFIELDS (type) == 1
9854 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9855 {
9856 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9857
9858 /* Decode the field name to find the offset of the
9859 discriminant. */
9860 ULONGEST bit_offset = 0;
9861 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9862 while (name[0] >= '0' && name[0] <= '9')
9863 {
9864 char *tail;
9865 unsigned long index = strtoul (name, &tail, 10);
9866 name = tail;
9867 if (*name != '$'
9868 || index >= TYPE_NFIELDS (field_type)
9869 || (TYPE_FIELD_LOC_KIND (field_type, index)
9870 != FIELD_LOC_KIND_BITPOS))
9871 {
b98664d3 9872 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9873 "[in module %s]"),
9874 TYPE_FIELD_NAME (type, 0),
9875 objfile_name (objfile));
9876 return;
9877 }
9878 ++name;
9879
9880 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9881 field_type = TYPE_FIELD_TYPE (field_type, index);
9882 }
9883
9884 /* Make a union to hold the variants. */
9885 struct type *union_type = alloc_type (objfile);
9886 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9887 TYPE_NFIELDS (union_type) = 3;
9888 TYPE_FIELDS (union_type)
9889 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9890 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9891 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9892
9893 /* Put the discriminant must at index 0. */
9894 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9895 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9896 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9897 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9898
9899 /* The order of fields doesn't really matter, so put the real
9900 field at index 1 and the data-less field at index 2. */
9901 struct discriminant_info *disc
9902 = alloc_discriminant_info (union_type, 0, 1);
9903 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9904 TYPE_FIELD_NAME (union_type, 1)
9905 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9906 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9907 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9908 TYPE_FIELD_NAME (union_type, 1));
9909
9910 const char *dataless_name
9911 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9912 name);
9913 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9914 dataless_name);
9915 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9916 /* NAME points into the original discriminant name, which
9917 already has the correct lifetime. */
9918 TYPE_FIELD_NAME (union_type, 2) = name;
9919 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9920 disc->discriminants[2] = 0;
9921
9922 /* Smash this type to be a structure type. We have to do this
9923 because the type has already been recorded. */
9924 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9925 TYPE_NFIELDS (type) = 1;
9926 TYPE_FIELDS (type)
9927 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9928
9929 /* Install the variant part. */
9930 TYPE_FIELD_TYPE (type, 0) = union_type;
9931 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9932 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9933 }
9934 else if (TYPE_NFIELDS (type) == 1)
9935 {
9936 /* We assume that a union with a single field is a univariant
9937 enum. */
9938 /* Smash this type to be a structure type. We have to do this
9939 because the type has already been recorded. */
9940 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9941
9942 /* Make a union to hold the variants. */
9943 struct type *union_type = alloc_type (objfile);
9944 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9945 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9946 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9947 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9948 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9949
9950 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9951 const char *variant_name
9952 = rust_last_path_segment (TYPE_NAME (field_type));
9953 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9954 TYPE_NAME (field_type)
9955 = rust_fully_qualify (&objfile->objfile_obstack,
c7b15a66 9956 TYPE_NAME (type), variant_name);
c9317f21
TT
9957
9958 /* Install the union in the outer struct type. */
9959 TYPE_NFIELDS (type) = 1;
9960 TYPE_FIELDS (type)
9961 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9962 TYPE_FIELD_TYPE (type, 0) = union_type;
9963 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9964 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9965
9966 alloc_discriminant_info (union_type, -1, 0);
9967 }
9968 else
9969 {
9970 struct type *disr_type = nullptr;
9971 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9972 {
9973 disr_type = TYPE_FIELD_TYPE (type, i);
9974
a037790e
TT
9975 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9976 {
9977 /* All fields of a true enum will be structs. */
9978 return;
9979 }
9980 else if (TYPE_NFIELDS (disr_type) == 0)
c9317f21
TT
9981 {
9982 /* Could be data-less variant, so keep going. */
a037790e 9983 disr_type = nullptr;
c9317f21
TT
9984 }
9985 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9986 "RUST$ENUM$DISR") != 0)
9987 {
9988 /* Not a Rust enum. */
9989 return;
9990 }
9991 else
9992 {
9993 /* Found one. */
9994 break;
9995 }
9996 }
9997
9998 /* If we got here without a discriminant, then it's probably
9999 just a union. */
10000 if (disr_type == nullptr)
10001 return;
10002
10003 /* Smash this type to be a structure type. We have to do this
10004 because the type has already been recorded. */
10005 TYPE_CODE (type) = TYPE_CODE_STRUCT;
10006
10007 /* Make a union to hold the variants. */
10008 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
10009 struct type *union_type = alloc_type (objfile);
10010 TYPE_CODE (union_type) = TYPE_CODE_UNION;
10011 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
10012 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 10013 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
10014 TYPE_FIELDS (union_type)
10015 = (struct field *) TYPE_ZALLOC (union_type,
10016 (TYPE_NFIELDS (union_type)
10017 * sizeof (struct field)));
10018
10019 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
10020 TYPE_NFIELDS (type) * sizeof (struct field));
10021
10022 /* Install the discriminant at index 0 in the union. */
10023 TYPE_FIELD (union_type, 0) = *disr_field;
10024 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
10025 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
10026
10027 /* Install the union in the outer struct type. */
10028 TYPE_FIELD_TYPE (type, 0) = union_type;
10029 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
10030 TYPE_NFIELDS (type) = 1;
10031
10032 /* Set the size and offset of the union type. */
10033 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
10034
10035 /* We need a way to find the correct discriminant given a
10036 variant name. For convenience we build a map here. */
10037 struct type *enum_type = FIELD_TYPE (*disr_field);
10038 std::unordered_map<std::string, ULONGEST> discriminant_map;
10039 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
10040 {
10041 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
10042 {
10043 const char *name
10044 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
10045 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
10046 }
10047 }
10048
10049 int n_fields = TYPE_NFIELDS (union_type);
10050 struct discriminant_info *disc
10051 = alloc_discriminant_info (union_type, 0, -1);
10052 /* Skip the discriminant here. */
10053 for (int i = 1; i < n_fields; ++i)
10054 {
10055 /* Find the final word in the name of this variant's type.
10056 That name can be used to look up the correct
10057 discriminant. */
10058 const char *variant_name
10059 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
10060 i)));
10061
10062 auto iter = discriminant_map.find (variant_name);
10063 if (iter != discriminant_map.end ())
10064 disc->discriminants[i] = iter->second;
10065
bedda9ac 10066 /* Remove the discriminant field, if it exists. */
c9317f21 10067 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
bedda9ac
TT
10068 if (TYPE_NFIELDS (sub_type) > 0)
10069 {
10070 --TYPE_NFIELDS (sub_type);
10071 ++TYPE_FIELDS (sub_type);
10072 }
c9317f21
TT
10073 TYPE_FIELD_NAME (union_type, i) = variant_name;
10074 TYPE_NAME (sub_type)
10075 = rust_fully_qualify (&objfile->objfile_obstack,
10076 TYPE_NAME (type), variant_name);
10077 }
10078 }
10079}
10080
10081/* Rewrite some Rust unions to be structures with variants parts. */
10082
10083static void
10084rust_union_quirks (struct dwarf2_cu *cu)
10085{
10086 gdb_assert (cu->language == language_rust);
52941706
SM
10087 for (type *type_ : cu->rust_unions)
10088 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
2d79090e
TT
10089 /* We don't need this any more. */
10090 cu->rust_unions.clear ();
c9317f21
TT
10091}
10092
95554aad
TT
10093/* Return the symtab for PER_CU. This works properly regardless of
10094 whether we're using the index or psymtabs. */
10095
43f3e411
DE
10096static struct compunit_symtab *
10097get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad 10098{
ed2dc618 10099 return (per_cu->dwarf2_per_objfile->using_index
43f3e411
DE
10100 ? per_cu->v.quick->compunit_symtab
10101 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
10102}
10103
10104/* A helper function for computing the list of all symbol tables
10105 included by PER_CU. */
10106
10107static void
43f3e411 10108recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
ec94af83 10109 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 10110 struct dwarf2_per_cu_data *per_cu,
43f3e411 10111 struct compunit_symtab *immediate_parent)
95554aad
TT
10112{
10113 void **slot;
10114 int ix;
43f3e411 10115 struct compunit_symtab *cust;
95554aad
TT
10116 struct dwarf2_per_cu_data *iter;
10117
10118 slot = htab_find_slot (all_children, per_cu, INSERT);
10119 if (*slot != NULL)
10120 {
10121 /* This inclusion and its children have been processed. */
10122 return;
10123 }
10124
10125 *slot = per_cu;
10126 /* Only add a CU if it has a symbol table. */
43f3e411
DE
10127 cust = get_compunit_symtab (per_cu);
10128 if (cust != NULL)
ec94af83
DE
10129 {
10130 /* If this is a type unit only add its symbol table if we haven't
10131 seen it yet (type unit per_cu's can share symtabs). */
10132 if (per_cu->is_debug_types)
10133 {
43f3e411 10134 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
10135 if (*slot == NULL)
10136 {
43f3e411
DE
10137 *slot = cust;
10138 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10139 if (cust->user == NULL)
10140 cust->user = immediate_parent;
ec94af83
DE
10141 }
10142 }
10143 else
f9125b6c 10144 {
43f3e411
DE
10145 VEC_safe_push (compunit_symtab_ptr, *result, cust);
10146 if (cust->user == NULL)
10147 cust->user = immediate_parent;
f9125b6c 10148 }
ec94af83 10149 }
95554aad
TT
10150
10151 for (ix = 0;
796a7ff8 10152 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
95554aad 10153 ++ix)
ec94af83
DE
10154 {
10155 recursively_compute_inclusions (result, all_children,
43f3e411 10156 all_type_symtabs, iter, cust);
ec94af83 10157 }
95554aad
TT
10158}
10159
43f3e411 10160/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
10161 PER_CU. */
10162
10163static void
43f3e411 10164compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 10165{
f4dc4d17
DE
10166 gdb_assert (! per_cu->is_debug_types);
10167
796a7ff8 10168 if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
95554aad
TT
10169 {
10170 int ix, len;
ec94af83 10171 struct dwarf2_per_cu_data *per_cu_iter;
43f3e411
DE
10172 struct compunit_symtab *compunit_symtab_iter;
10173 VEC (compunit_symtab_ptr) *result_symtabs = NULL;
ec94af83 10174 htab_t all_children, all_type_symtabs;
43f3e411 10175 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
10176
10177 /* If we don't have a symtab, we can just skip this case. */
43f3e411 10178 if (cust == NULL)
95554aad
TT
10179 return;
10180
10181 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10182 NULL, xcalloc, xfree);
ec94af83
DE
10183 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
10184 NULL, xcalloc, xfree);
95554aad
TT
10185
10186 for (ix = 0;
796a7ff8 10187 VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
ec94af83 10188 ix, per_cu_iter);
95554aad 10189 ++ix)
ec94af83
DE
10190 {
10191 recursively_compute_inclusions (&result_symtabs, all_children,
f9125b6c 10192 all_type_symtabs, per_cu_iter,
43f3e411 10193 cust);
ec94af83 10194 }
95554aad 10195
ec94af83 10196 /* Now we have a transitive closure of all the included symtabs. */
43f3e411
DE
10197 len = VEC_length (compunit_symtab_ptr, result_symtabs);
10198 cust->includes
ed2dc618 10199 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 10200 struct compunit_symtab *, len + 1);
95554aad 10201 for (ix = 0;
43f3e411
DE
10202 VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
10203 compunit_symtab_iter);
95554aad 10204 ++ix)
43f3e411
DE
10205 cust->includes[ix] = compunit_symtab_iter;
10206 cust->includes[len] = NULL;
95554aad 10207
43f3e411 10208 VEC_free (compunit_symtab_ptr, result_symtabs);
95554aad 10209 htab_delete (all_children);
ec94af83 10210 htab_delete (all_type_symtabs);
95554aad
TT
10211 }
10212}
10213
10214/* Compute the 'includes' field for the symtabs of all the CUs we just
10215 read. */
10216
10217static void
ed2dc618 10218process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad
TT
10219{
10220 int ix;
10221 struct dwarf2_per_cu_data *iter;
10222
10223 for (ix = 0;
10224 VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
10225 ix, iter);
10226 ++ix)
f4dc4d17
DE
10227 {
10228 if (! iter->is_debug_types)
43f3e411 10229 compute_compunit_symtab_includes (iter);
f4dc4d17 10230 }
95554aad
TT
10231
10232 VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
10233}
10234
9cdd5dbd 10235/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
10236 already been loaded into memory. */
10237
10238static void
95554aad
TT
10239process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
10240 enum language pretend_language)
10b3939b 10241{
10b3939b 10242 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10243 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10244 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 10245 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 10246 CORE_ADDR lowpc, highpc;
43f3e411 10247 struct compunit_symtab *cust;
10b3939b 10248 CORE_ADDR baseaddr;
4359dff1 10249 struct block *static_block;
3e29f34a 10250 CORE_ADDR addr;
10b3939b
DJ
10251
10252 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10253
10b3939b 10254 buildsym_init ();
33c7c59d 10255 scoped_free_pendings free_pending;
c89b44cd
TT
10256
10257 /* Clear the list here in case something was left over. */
10258 cu->method_list.clear ();
10b3939b
DJ
10259
10260 cu->list_in_scope = &file_symbols;
c906108c 10261
95554aad
TT
10262 cu->language = pretend_language;
10263 cu->language_defn = language_def (cu->language);
10264
c906108c 10265 /* Do line number decoding in read_file_scope () */
10b3939b 10266 process_die (cu->dies, cu);
c906108c 10267
a766d390
DE
10268 /* For now fudge the Go package. */
10269 if (cu->language == language_go)
10270 fixup_go_packaging (cu);
10271
3da10d80
KS
10272 /* Now that we have processed all the DIEs in the CU, all the types
10273 should be complete, and it should now be safe to compute all of the
10274 physnames. */
10275 compute_delayed_physnames (cu);
3da10d80 10276
c9317f21
TT
10277 if (cu->language == language_rust)
10278 rust_union_quirks (cu);
10279
fae299cd
DC
10280 /* Some compilers don't define a DW_AT_high_pc attribute for the
10281 compilation unit. If the DW_AT_high_pc is missing, synthesize
10282 it, by scanning the DIE's below the compilation unit. */
10b3939b 10283 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 10284
3e29f34a
MR
10285 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
10286 static_block = end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
10287
10288 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10289 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10290 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10291 addrmap to help ensure it has an accurate map of pc values belonging to
10292 this comp unit. */
10293 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10294
43f3e411
DE
10295 cust = end_symtab_from_static_block (static_block,
10296 SECT_OFF_TEXT (objfile), 0);
c906108c 10297
43f3e411 10298 if (cust != NULL)
c906108c 10299 {
df15bd07 10300 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 10301
8be455d7
JK
10302 /* Set symtab language to language from DW_AT_language. If the
10303 compilation is from a C file generated by language preprocessors, do
10304 not set the language if it was already deduced by start_subfile. */
43f3e411 10305 if (!(cu->language == language_c
40e3ad0e 10306 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 10307 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
10308
10309 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10310 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
10311 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10312 there were bugs in prologue debug info, fixed later in GCC-4.5
10313 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
10314
10315 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10316 needed, it would be wrong due to missing DW_AT_producer there.
10317
10318 Still one can confuse GDB by using non-standard GCC compilation
10319 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
10320 */
ab260dad 10321 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 10322 cust->locations_valid = 1;
e0d00bc7
JK
10323
10324 if (gcc_4_minor >= 5)
43f3e411 10325 cust->epilogue_unwind_valid = 1;
96408a79 10326
43f3e411 10327 cust->call_site_htab = cu->call_site_htab;
c906108c 10328 }
9291a0cd
TT
10329
10330 if (dwarf2_per_objfile->using_index)
43f3e411 10331 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
10332 else
10333 {
10334 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10335 pst->compunit_symtab = cust;
9291a0cd
TT
10336 pst->readin = 1;
10337 }
c906108c 10338
95554aad
TT
10339 /* Push it for inclusion processing later. */
10340 VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
f4dc4d17 10341}
45cfd468 10342
f4dc4d17
DE
10343/* Generate full symbol information for type unit PER_CU, whose DIEs have
10344 already been loaded into memory. */
10345
10346static void
10347process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
10348 enum language pretend_language)
10349{
10350 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
10351 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
10352 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 10353 struct compunit_symtab *cust;
0186c6a7
DE
10354 struct signatured_type *sig_type;
10355
10356 gdb_assert (per_cu->is_debug_types);
10357 sig_type = (struct signatured_type *) per_cu;
f4dc4d17
DE
10358
10359 buildsym_init ();
33c7c59d 10360 scoped_free_pendings free_pending;
c89b44cd
TT
10361
10362 /* Clear the list here in case something was left over. */
10363 cu->method_list.clear ();
f4dc4d17
DE
10364
10365 cu->list_in_scope = &file_symbols;
10366
10367 cu->language = pretend_language;
10368 cu->language_defn = language_def (cu->language);
10369
10370 /* The symbol tables are set up in read_type_unit_scope. */
10371 process_die (cu->dies, cu);
10372
10373 /* For now fudge the Go package. */
10374 if (cu->language == language_go)
10375 fixup_go_packaging (cu);
10376
10377 /* Now that we have processed all the DIEs in the CU, all the types
10378 should be complete, and it should now be safe to compute all of the
10379 physnames. */
10380 compute_delayed_physnames (cu);
f4dc4d17 10381
c9317f21
TT
10382 if (cu->language == language_rust)
10383 rust_union_quirks (cu);
10384
f4dc4d17
DE
10385 /* TUs share symbol tables.
10386 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
10387 of it with end_expandable_symtab. Otherwise, complete the addition of
10388 this TU's symbols to the existing symtab. */
43f3e411 10389 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 10390 {
43f3e411
DE
10391 cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
10392 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 10393
43f3e411 10394 if (cust != NULL)
f4dc4d17
DE
10395 {
10396 /* Set symtab language to language from DW_AT_language. If the
10397 compilation is from a C file generated by language preprocessors,
10398 do not set the language if it was already deduced by
10399 start_subfile. */
43f3e411
DE
10400 if (!(cu->language == language_c
10401 && COMPUNIT_FILETABS (cust)->language != language_c))
10402 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
10403 }
10404 }
10405 else
10406 {
0ab9ce85 10407 augment_type_symtab ();
43f3e411 10408 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
10409 }
10410
10411 if (dwarf2_per_objfile->using_index)
43f3e411 10412 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
10413 else
10414 {
10415 struct partial_symtab *pst = per_cu->v.psymtab;
43f3e411 10416 pst->compunit_symtab = cust;
f4dc4d17 10417 pst->readin = 1;
45cfd468 10418 }
c906108c
SS
10419}
10420
95554aad
TT
10421/* Process an imported unit DIE. */
10422
10423static void
10424process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10425{
10426 struct attribute *attr;
10427
f4dc4d17
DE
10428 /* For now we don't handle imported units in type units. */
10429 if (cu->per_cu->is_debug_types)
10430 {
10431 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10432 " supported in type units [in module %s]"),
518817b3 10433 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
10434 }
10435
95554aad
TT
10436 attr = dwarf2_attr (die, DW_AT_import, cu);
10437 if (attr != NULL)
10438 {
9c541725
PA
10439 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
10440 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
10441 dwarf2_per_cu_data *per_cu
e3b94546 10442 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
518817b3 10443 cu->per_cu->dwarf2_per_objfile);
95554aad 10444
69d751e3 10445 /* If necessary, add it to the queue and load its DIEs. */
95554aad 10446 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 10447 load_full_comp_unit (per_cu, false, cu->language);
95554aad 10448
796a7ff8 10449 VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
95554aad
TT
10450 per_cu);
10451 }
10452}
10453
4c8aa72d
PA
10454/* RAII object that represents a process_die scope: i.e.,
10455 starts/finishes processing a DIE. */
10456class process_die_scope
adde2bff 10457{
4c8aa72d
PA
10458public:
10459 process_die_scope (die_info *die, dwarf2_cu *cu)
10460 : m_die (die), m_cu (cu)
10461 {
10462 /* We should only be processing DIEs not already in process. */
10463 gdb_assert (!m_die->in_process);
10464 m_die->in_process = true;
10465 }
8c3cb9fa 10466
4c8aa72d
PA
10467 ~process_die_scope ()
10468 {
10469 m_die->in_process = false;
10470
10471 /* If we're done processing the DIE for the CU that owns the line
10472 header, we don't need the line header anymore. */
10473 if (m_cu->line_header_die_owner == m_die)
10474 {
10475 delete m_cu->line_header;
10476 m_cu->line_header = NULL;
10477 m_cu->line_header_die_owner = NULL;
10478 }
10479 }
10480
10481private:
10482 die_info *m_die;
10483 dwarf2_cu *m_cu;
10484};
adde2bff 10485
c906108c
SS
10486/* Process a die and its children. */
10487
10488static void
e7c27a73 10489process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10490{
4c8aa72d 10491 process_die_scope scope (die, cu);
adde2bff 10492
c906108c
SS
10493 switch (die->tag)
10494 {
10495 case DW_TAG_padding:
10496 break;
10497 case DW_TAG_compile_unit:
95554aad 10498 case DW_TAG_partial_unit:
e7c27a73 10499 read_file_scope (die, cu);
c906108c 10500 break;
348e048f
DE
10501 case DW_TAG_type_unit:
10502 read_type_unit_scope (die, cu);
10503 break;
c906108c 10504 case DW_TAG_subprogram:
c906108c 10505 case DW_TAG_inlined_subroutine:
edb3359d 10506 read_func_scope (die, cu);
c906108c
SS
10507 break;
10508 case DW_TAG_lexical_block:
14898363
L
10509 case DW_TAG_try_block:
10510 case DW_TAG_catch_block:
e7c27a73 10511 read_lexical_block_scope (die, cu);
c906108c 10512 break;
216f72a1 10513 case DW_TAG_call_site:
96408a79
SA
10514 case DW_TAG_GNU_call_site:
10515 read_call_site_scope (die, cu);
10516 break;
c906108c 10517 case DW_TAG_class_type:
680b30c7 10518 case DW_TAG_interface_type:
c906108c
SS
10519 case DW_TAG_structure_type:
10520 case DW_TAG_union_type:
134d01f1 10521 process_structure_scope (die, cu);
c906108c
SS
10522 break;
10523 case DW_TAG_enumeration_type:
134d01f1 10524 process_enumeration_scope (die, cu);
c906108c 10525 break;
134d01f1 10526
f792889a
DJ
10527 /* These dies have a type, but processing them does not create
10528 a symbol or recurse to process the children. Therefore we can
10529 read them on-demand through read_type_die. */
c906108c 10530 case DW_TAG_subroutine_type:
72019c9c 10531 case DW_TAG_set_type:
c906108c 10532 case DW_TAG_array_type:
c906108c 10533 case DW_TAG_pointer_type:
c906108c 10534 case DW_TAG_ptr_to_member_type:
c906108c 10535 case DW_TAG_reference_type:
4297a3f0 10536 case DW_TAG_rvalue_reference_type:
c906108c 10537 case DW_TAG_string_type:
c906108c 10538 break;
134d01f1 10539
c906108c 10540 case DW_TAG_base_type:
a02abb62 10541 case DW_TAG_subrange_type:
cb249c71 10542 case DW_TAG_typedef:
134d01f1
DJ
10543 /* Add a typedef symbol for the type definition, if it has a
10544 DW_AT_name. */
f792889a 10545 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10546 break;
c906108c 10547 case DW_TAG_common_block:
e7c27a73 10548 read_common_block (die, cu);
c906108c
SS
10549 break;
10550 case DW_TAG_common_inclusion:
10551 break;
d9fa45fe 10552 case DW_TAG_namespace:
4d4ec4e5 10553 cu->processing_has_namespace_info = 1;
e7c27a73 10554 read_namespace (die, cu);
d9fa45fe 10555 break;
5d7cb8df 10556 case DW_TAG_module:
4d4ec4e5 10557 cu->processing_has_namespace_info = 1;
5d7cb8df
JK
10558 read_module (die, cu);
10559 break;
d9fa45fe 10560 case DW_TAG_imported_declaration:
74921315
KS
10561 cu->processing_has_namespace_info = 1;
10562 if (read_namespace_alias (die, cu))
10563 break;
86a73007
TT
10564 /* The declaration is not a global namespace alias. */
10565 /* Fall through. */
d9fa45fe 10566 case DW_TAG_imported_module:
4d4ec4e5 10567 cu->processing_has_namespace_info = 1;
27aa8d6a
SW
10568 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10569 || cu->language != language_fortran))
b98664d3 10570 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10571 dwarf_tag_name (die->tag));
10572 read_import_statement (die, cu);
d9fa45fe 10573 break;
95554aad
TT
10574
10575 case DW_TAG_imported_unit:
10576 process_imported_unit_die (die, cu);
10577 break;
10578
71a3c369
TT
10579 case DW_TAG_variable:
10580 read_variable (die, cu);
10581 break;
10582
c906108c 10583 default:
e7c27a73 10584 new_symbol (die, NULL, cu);
c906108c
SS
10585 break;
10586 }
10587}
ca69b9e6
DE
10588\f
10589/* DWARF name computation. */
c906108c 10590
94af9270
KS
10591/* A helper function for dwarf2_compute_name which determines whether DIE
10592 needs to have the name of the scope prepended to the name listed in the
10593 die. */
10594
10595static int
10596die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10597{
1c809c68
TT
10598 struct attribute *attr;
10599
94af9270
KS
10600 switch (die->tag)
10601 {
10602 case DW_TAG_namespace:
10603 case DW_TAG_typedef:
10604 case DW_TAG_class_type:
10605 case DW_TAG_interface_type:
10606 case DW_TAG_structure_type:
10607 case DW_TAG_union_type:
10608 case DW_TAG_enumeration_type:
10609 case DW_TAG_enumerator:
10610 case DW_TAG_subprogram:
08a76f8a 10611 case DW_TAG_inlined_subroutine:
94af9270 10612 case DW_TAG_member:
74921315 10613 case DW_TAG_imported_declaration:
94af9270
KS
10614 return 1;
10615
10616 case DW_TAG_variable:
c2b0a229 10617 case DW_TAG_constant:
94af9270
KS
10618 /* We only need to prefix "globally" visible variables. These include
10619 any variable marked with DW_AT_external or any variable that
10620 lives in a namespace. [Variables in anonymous namespaces
10621 require prefixing, but they are not DW_AT_external.] */
10622
10623 if (dwarf2_attr (die, DW_AT_specification, cu))
10624 {
10625 struct dwarf2_cu *spec_cu = cu;
9a619af0 10626
94af9270
KS
10627 return die_needs_namespace (die_specification (die, &spec_cu),
10628 spec_cu);
10629 }
10630
1c809c68 10631 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10632 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10633 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10634 return 0;
10635 /* A variable in a lexical block of some kind does not need a
10636 namespace, even though in C++ such variables may be external
10637 and have a mangled name. */
10638 if (die->parent->tag == DW_TAG_lexical_block
10639 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10640 || die->parent->tag == DW_TAG_catch_block
10641 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10642 return 0;
10643 return 1;
94af9270
KS
10644
10645 default:
10646 return 0;
10647 }
10648}
10649
73b9be8b
KS
10650/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10651 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10652 defined for the given DIE. */
10653
10654static struct attribute *
10655dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10656{
10657 struct attribute *attr;
10658
10659 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10660 if (attr == NULL)
10661 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10662
10663 return attr;
10664}
10665
10666/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10667 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10668 defined for the given DIE. */
10669
10670static const char *
10671dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10672{
10673 const char *linkage_name;
10674
10675 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10676 if (linkage_name == NULL)
10677 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10678
10679 return linkage_name;
10680}
10681
94af9270 10682/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10683 compute the physname for the object, which include a method's:
9c37b5ae 10684 - formal parameters (C++),
a766d390 10685 - receiver type (Go),
a766d390
DE
10686
10687 The term "physname" is a bit confusing.
10688 For C++, for example, it is the demangled name.
10689 For Go, for example, it's the mangled name.
94af9270 10690
af6b7be1
JB
10691 For Ada, return the DIE's linkage name rather than the fully qualified
10692 name. PHYSNAME is ignored..
10693
94af9270
KS
10694 The result is allocated on the objfile_obstack and canonicalized. */
10695
10696static const char *
15d034d0
TT
10697dwarf2_compute_name (const char *name,
10698 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10699 int physname)
10700{
518817b3 10701 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
bb5ed363 10702
94af9270
KS
10703 if (name == NULL)
10704 name = dwarf2_name (die, cu);
10705
2ee7123e
DE
10706 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10707 but otherwise compute it by typename_concat inside GDB.
10708 FIXME: Actually this is not really true, or at least not always true.
10709 It's all very confusing. SYMBOL_SET_NAMES doesn't try to demangle
5e2db402 10710 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10711 will set the demangled name to the result of dwarf2_full_name, and it is
10712 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10713 if (cu->language == language_ada
10714 || (cu->language == language_fortran && physname))
10715 {
10716 /* For Ada unit, we prefer the linkage name over the name, as
10717 the former contains the exported name, which the user expects
10718 to be able to reference. Ideally, we want the user to be able
10719 to reference this entity using either natural or linkage name,
10720 but we haven't started looking at this enhancement yet. */
73b9be8b 10721 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10722
2ee7123e
DE
10723 if (linkage_name != NULL)
10724 return linkage_name;
f55ee35c
JK
10725 }
10726
94af9270
KS
10727 /* These are the only languages we know how to qualify names in. */
10728 if (name != NULL
9c37b5ae 10729 && (cu->language == language_cplus
c44af4eb
TT
10730 || cu->language == language_fortran || cu->language == language_d
10731 || cu->language == language_rust))
94af9270
KS
10732 {
10733 if (die_needs_namespace (die, cu))
10734 {
0d5cff50 10735 const char *prefix;
34a68019 10736 const char *canonical_name = NULL;
94af9270 10737
d7e74731
PA
10738 string_file buf;
10739
94af9270 10740 prefix = determine_prefix (die, cu);
94af9270
KS
10741 if (*prefix != '\0')
10742 {
f55ee35c
JK
10743 char *prefixed_name = typename_concat (NULL, prefix, name,
10744 physname, cu);
9a619af0 10745
d7e74731 10746 buf.puts (prefixed_name);
94af9270
KS
10747 xfree (prefixed_name);
10748 }
10749 else
d7e74731 10750 buf.puts (name);
94af9270 10751
98bfdba5
PA
10752 /* Template parameters may be specified in the DIE's DW_AT_name, or
10753 as children with DW_TAG_template_type_param or
10754 DW_TAG_value_type_param. If the latter, add them to the name
10755 here. If the name already has template parameters, then
10756 skip this step; some versions of GCC emit both, and
10757 it is more efficient to use the pre-computed name.
10758
10759 Something to keep in mind about this process: it is very
10760 unlikely, or in some cases downright impossible, to produce
10761 something that will match the mangled name of a function.
10762 If the definition of the function has the same debug info,
10763 we should be able to match up with it anyway. But fallbacks
10764 using the minimal symbol, for instance to find a method
10765 implemented in a stripped copy of libstdc++, will not work.
10766 If we do not have debug info for the definition, we will have to
10767 match them up some other way.
10768
10769 When we do name matching there is a related problem with function
10770 templates; two instantiated function templates are allowed to
10771 differ only by their return types, which we do not add here. */
10772
10773 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10774 {
10775 struct attribute *attr;
10776 struct die_info *child;
10777 int first = 1;
10778
10779 die->building_fullname = 1;
10780
10781 for (child = die->child; child != NULL; child = child->sibling)
10782 {
10783 struct type *type;
12df843f 10784 LONGEST value;
d521ce57 10785 const gdb_byte *bytes;
98bfdba5
PA
10786 struct dwarf2_locexpr_baton *baton;
10787 struct value *v;
10788
10789 if (child->tag != DW_TAG_template_type_param
10790 && child->tag != DW_TAG_template_value_param)
10791 continue;
10792
10793 if (first)
10794 {
d7e74731 10795 buf.puts ("<");
98bfdba5
PA
10796 first = 0;
10797 }
10798 else
d7e74731 10799 buf.puts (", ");
98bfdba5
PA
10800
10801 attr = dwarf2_attr (child, DW_AT_type, cu);
10802 if (attr == NULL)
10803 {
b98664d3 10804 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10805 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10806 continue;
10807 }
10808 type = die_type (child, cu);
10809
10810 if (child->tag == DW_TAG_template_type_param)
10811 {
c1ec8cea
TT
10812 c_print_type (type, "", &buf, -1, 0, cu->language,
10813 &type_print_raw_options);
98bfdba5
PA
10814 continue;
10815 }
10816
10817 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10818 if (attr == NULL)
10819 {
b98664d3 10820 complaint (_("template parameter missing "
3e43a32a 10821 "DW_AT_const_value"));
d7e74731 10822 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10823 continue;
10824 }
10825
10826 dwarf2_const_value_attr (attr, type, name,
10827 &cu->comp_unit_obstack, cu,
10828 &value, &bytes, &baton);
10829
10830 if (TYPE_NOSIGN (type))
10831 /* GDB prints characters as NUMBER 'CHAR'. If that's
10832 changed, this can use value_print instead. */
d7e74731 10833 c_printchar (value, type, &buf);
98bfdba5
PA
10834 else
10835 {
10836 struct value_print_options opts;
10837
10838 if (baton != NULL)
10839 v = dwarf2_evaluate_loc_desc (type, NULL,
10840 baton->data,
10841 baton->size,
10842 baton->per_cu);
10843 else if (bytes != NULL)
10844 {
10845 v = allocate_value (type);
10846 memcpy (value_contents_writeable (v), bytes,
10847 TYPE_LENGTH (type));
10848 }
10849 else
10850 v = value_from_longest (type, value);
10851
3e43a32a
MS
10852 /* Specify decimal so that we do not depend on
10853 the radix. */
98bfdba5
PA
10854 get_formatted_print_options (&opts, 'd');
10855 opts.raw = 1;
d7e74731 10856 value_print (v, &buf, &opts);
98bfdba5 10857 release_value (v);
98bfdba5
PA
10858 }
10859 }
10860
10861 die->building_fullname = 0;
10862
10863 if (!first)
10864 {
10865 /* Close the argument list, with a space if necessary
10866 (nested templates). */
d7e74731
PA
10867 if (!buf.empty () && buf.string ().back () == '>')
10868 buf.puts (" >");
98bfdba5 10869 else
d7e74731 10870 buf.puts (">");
98bfdba5
PA
10871 }
10872 }
10873
9c37b5ae 10874 /* For C++ methods, append formal parameter type
94af9270 10875 information, if PHYSNAME. */
6e70227d 10876
94af9270 10877 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10878 && cu->language == language_cplus)
94af9270
KS
10879 {
10880 struct type *type = read_type_die (die, cu);
10881
d7e74731 10882 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10883 &type_print_raw_options);
94af9270 10884
9c37b5ae 10885 if (cu->language == language_cplus)
94af9270 10886 {
60430eff
DJ
10887 /* Assume that an artificial first parameter is
10888 "this", but do not crash if it is not. RealView
10889 marks unnamed (and thus unused) parameters as
10890 artificial; there is no way to differentiate
10891 the two cases. */
94af9270
KS
10892 if (TYPE_NFIELDS (type) > 0
10893 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 10894 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
10895 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10896 0))))
d7e74731 10897 buf.puts (" const");
94af9270
KS
10898 }
10899 }
10900
d7e74731 10901 const std::string &intermediate_name = buf.string ();
94af9270
KS
10902
10903 if (cu->language == language_cplus)
34a68019 10904 canonical_name
322a8516 10905 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
34a68019
TT
10906 &objfile->per_bfd->storage_obstack);
10907
10908 /* If we only computed INTERMEDIATE_NAME, or if
10909 INTERMEDIATE_NAME is already canonical, then we need to
10910 copy it to the appropriate obstack. */
322a8516 10911 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
224c3ddb
SM
10912 name = ((const char *)
10913 obstack_copy0 (&objfile->per_bfd->storage_obstack,
322a8516
PA
10914 intermediate_name.c_str (),
10915 intermediate_name.length ()));
34a68019
TT
10916 else
10917 name = canonical_name;
94af9270
KS
10918 }
10919 }
10920
10921 return name;
10922}
10923
0114d602
DJ
10924/* Return the fully qualified name of DIE, based on its DW_AT_name.
10925 If scope qualifiers are appropriate they will be added. The result
34a68019 10926 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10927 not have a name. NAME may either be from a previous call to
10928 dwarf2_name or NULL.
10929
9c37b5ae 10930 The output string will be canonicalized (if C++). */
0114d602
DJ
10931
10932static const char *
15d034d0 10933dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10934{
94af9270
KS
10935 return dwarf2_compute_name (name, die, cu, 0);
10936}
0114d602 10937
94af9270
KS
10938/* Construct a physname for the given DIE in CU. NAME may either be
10939 from a previous call to dwarf2_name or NULL. The result will be
10940 allocated on the objfile_objstack or NULL if the DIE does not have a
10941 name.
0114d602 10942
9c37b5ae 10943 The output string will be canonicalized (if C++). */
0114d602 10944
94af9270 10945static const char *
15d034d0 10946dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10947{
518817b3 10948 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
900e11f9 10949 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10950 int need_copy = 1;
10951
10952 /* In this case dwarf2_compute_name is just a shortcut not building anything
10953 on its own. */
10954 if (!die_needs_namespace (die, cu))
10955 return dwarf2_compute_name (name, die, cu, 1);
10956
73b9be8b 10957 mangled = dw2_linkage_name (die, cu);
900e11f9 10958
e98c9e7c
TT
10959 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10960 See https://github.com/rust-lang/rust/issues/32925. */
10961 if (cu->language == language_rust && mangled != NULL
10962 && strchr (mangled, '{') != NULL)
10963 mangled = NULL;
10964
900e11f9
JK
10965 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10966 has computed. */
791afaa2 10967 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10968 if (mangled != NULL)
900e11f9 10969 {
900e11f9 10970
59cc4834
JB
10971 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10972 {
10973 /* Do nothing (do not demangle the symbol name). */
10974 }
10975 else if (cu->language == language_go)
a766d390 10976 {
5e2db402
TT
10977 /* This is a lie, but we already lie to the caller new_symbol.
10978 new_symbol assumes we return the mangled name.
a766d390 10979 This just undoes that lie until things are cleaned up. */
a766d390
DE
10980 }
10981 else
10982 {
0eb876f5
JB
10983 /* Use DMGL_RET_DROP for C++ template functions to suppress
10984 their return type. It is easier for GDB users to search
10985 for such functions as `name(params)' than `long name(params)'.
10986 In such case the minimal symbol names do not match the full
10987 symbol names but for template functions there is never a need
10988 to look up their definition from their declaration so
10989 the only disadvantage remains the minimal symbol variant
10990 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10991 demangled.reset (gdb_demangle (mangled,
10992 (DMGL_PARAMS | DMGL_ANSI
10993 | DMGL_RET_DROP)));
a766d390 10994 }
900e11f9 10995 if (demangled)
791afaa2 10996 canon = demangled.get ();
900e11f9
JK
10997 else
10998 {
10999 canon = mangled;
11000 need_copy = 0;
11001 }
11002 }
11003
11004 if (canon == NULL || check_physname)
11005 {
11006 const char *physname = dwarf2_compute_name (name, die, cu, 1);
11007
11008 if (canon != NULL && strcmp (physname, canon) != 0)
11009 {
11010 /* It may not mean a bug in GDB. The compiler could also
11011 compute DW_AT_linkage_name incorrectly. But in such case
11012 GDB would need to be bug-to-bug compatible. */
11013
b98664d3 11014 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
11015 "(from linkage <%s>) - DIE at %s [in module %s]"),
11016 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 11017 objfile_name (objfile));
900e11f9
JK
11018
11019 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
11020 is available here - over computed PHYSNAME. It is safer
11021 against both buggy GDB and buggy compilers. */
11022
11023 retval = canon;
11024 }
11025 else
11026 {
11027 retval = physname;
11028 need_copy = 0;
11029 }
11030 }
11031 else
11032 retval = canon;
11033
11034 if (need_copy)
224c3ddb
SM
11035 retval = ((const char *)
11036 obstack_copy0 (&objfile->per_bfd->storage_obstack,
11037 retval, strlen (retval)));
900e11f9 11038
900e11f9 11039 return retval;
0114d602
DJ
11040}
11041
74921315
KS
11042/* Inspect DIE in CU for a namespace alias. If one exists, record
11043 a new symbol for it.
11044
11045 Returns 1 if a namespace alias was recorded, 0 otherwise. */
11046
11047static int
11048read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
11049{
11050 struct attribute *attr;
11051
11052 /* If the die does not have a name, this is not a namespace
11053 alias. */
11054 attr = dwarf2_attr (die, DW_AT_name, cu);
11055 if (attr != NULL)
11056 {
11057 int num;
11058 struct die_info *d = die;
11059 struct dwarf2_cu *imported_cu = cu;
11060
11061 /* If the compiler has nested DW_AT_imported_declaration DIEs,
11062 keep inspecting DIEs until we hit the underlying import. */
11063#define MAX_NESTED_IMPORTED_DECLARATIONS 100
11064 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
11065 {
11066 attr = dwarf2_attr (d, DW_AT_import, cu);
11067 if (attr == NULL)
11068 break;
11069
11070 d = follow_die_ref (d, attr, &imported_cu);
11071 if (d->tag != DW_TAG_imported_declaration)
11072 break;
11073 }
11074
11075 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
11076 {
b98664d3 11077 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 11078 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
11079 return 0;
11080 }
11081
11082 if (attr != NULL)
11083 {
11084 struct type *type;
9c541725 11085 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
74921315 11086
9c541725 11087 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
11088 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
11089 {
11090 /* This declaration is a global namespace alias. Add
11091 a symbol for it whose type is the aliased namespace. */
11092 new_symbol (die, type, cu);
11093 return 1;
11094 }
11095 }
11096 }
11097
11098 return 0;
11099}
11100
22cee43f
PMR
11101/* Return the using directives repository (global or local?) to use in the
11102 current context for LANGUAGE.
11103
11104 For Ada, imported declarations can materialize renamings, which *may* be
11105 global. However it is impossible (for now?) in DWARF to distinguish
11106 "external" imported declarations and "static" ones. As all imported
11107 declarations seem to be static in all other languages, make them all CU-wide
11108 global only in Ada. */
11109
11110static struct using_direct **
11111using_directives (enum language language)
11112{
11113 if (language == language_ada && context_stack_depth == 0)
11114 return &global_using_directives;
11115 else
11116 return &local_using_directives;
11117}
11118
27aa8d6a
SW
11119/* Read the import statement specified by the given die and record it. */
11120
11121static void
11122read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
11123{
518817b3 11124 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
27aa8d6a 11125 struct attribute *import_attr;
32019081 11126 struct die_info *imported_die, *child_die;
de4affc9 11127 struct dwarf2_cu *imported_cu;
27aa8d6a 11128 const char *imported_name;
794684b6 11129 const char *imported_name_prefix;
13387711
SW
11130 const char *canonical_name;
11131 const char *import_alias;
11132 const char *imported_declaration = NULL;
794684b6 11133 const char *import_prefix;
eb1e02fd 11134 std::vector<const char *> excludes;
13387711 11135
27aa8d6a
SW
11136 import_attr = dwarf2_attr (die, DW_AT_import, cu);
11137 if (import_attr == NULL)
11138 {
b98664d3 11139 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
11140 dwarf_tag_name (die->tag));
11141 return;
11142 }
11143
de4affc9
CC
11144 imported_cu = cu;
11145 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
11146 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
11147 if (imported_name == NULL)
11148 {
11149 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
11150
11151 The import in the following code:
11152 namespace A
11153 {
11154 typedef int B;
11155 }
11156
11157 int main ()
11158 {
11159 using A::B;
11160 B b;
11161 return b;
11162 }
11163
11164 ...
11165 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
11166 <52> DW_AT_decl_file : 1
11167 <53> DW_AT_decl_line : 6
11168 <54> DW_AT_import : <0x75>
11169 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
11170 <59> DW_AT_name : B
11171 <5b> DW_AT_decl_file : 1
11172 <5c> DW_AT_decl_line : 2
11173 <5d> DW_AT_type : <0x6e>
11174 ...
11175 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
11176 <76> DW_AT_byte_size : 4
11177 <77> DW_AT_encoding : 5 (signed)
11178
11179 imports the wrong die ( 0x75 instead of 0x58 ).
11180 This case will be ignored until the gcc bug is fixed. */
11181 return;
11182 }
11183
82856980
SW
11184 /* Figure out the local name after import. */
11185 import_alias = dwarf2_name (die, cu);
27aa8d6a 11186
794684b6
SW
11187 /* Figure out where the statement is being imported to. */
11188 import_prefix = determine_prefix (die, cu);
11189
11190 /* Figure out what the scope of the imported die is and prepend it
11191 to the name of the imported die. */
de4affc9 11192 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 11193
f55ee35c
JK
11194 if (imported_die->tag != DW_TAG_namespace
11195 && imported_die->tag != DW_TAG_module)
794684b6 11196 {
13387711
SW
11197 imported_declaration = imported_name;
11198 canonical_name = imported_name_prefix;
794684b6 11199 }
13387711 11200 else if (strlen (imported_name_prefix) > 0)
12aaed36 11201 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
11202 imported_name_prefix,
11203 (cu->language == language_d ? "." : "::"),
11204 imported_name, (char *) NULL);
13387711
SW
11205 else
11206 canonical_name = imported_name;
794684b6 11207
32019081
JK
11208 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
11209 for (child_die = die->child; child_die && child_die->tag;
11210 child_die = sibling_die (child_die))
11211 {
11212 /* DWARF-4: A Fortran use statement with a “rename list” may be
11213 represented by an imported module entry with an import attribute
11214 referring to the module and owned entries corresponding to those
11215 entities that are renamed as part of being imported. */
11216
11217 if (child_die->tag != DW_TAG_imported_declaration)
11218 {
b98664d3 11219 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
11220 "- DIE at %s [in module %s]"),
11221 sect_offset_str (child_die->sect_off),
11222 objfile_name (objfile));
32019081
JK
11223 continue;
11224 }
11225
11226 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
11227 if (import_attr == NULL)
11228 {
b98664d3 11229 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
11230 dwarf_tag_name (child_die->tag));
11231 continue;
11232 }
11233
11234 imported_cu = cu;
11235 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11236 &imported_cu);
11237 imported_name = dwarf2_name (imported_die, imported_cu);
11238 if (imported_name == NULL)
11239 {
b98664d3 11240 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
11241 "imported name - DIE at %s [in module %s]"),
11242 sect_offset_str (child_die->sect_off),
11243 objfile_name (objfile));
32019081
JK
11244 continue;
11245 }
11246
eb1e02fd 11247 excludes.push_back (imported_name);
32019081
JK
11248
11249 process_die (child_die, cu);
11250 }
11251
22cee43f
PMR
11252 add_using_directive (using_directives (cu->language),
11253 import_prefix,
11254 canonical_name,
11255 import_alias,
11256 imported_declaration,
11257 excludes,
11258 0,
11259 &objfile->objfile_obstack);
27aa8d6a
SW
11260}
11261
5230b05a
WT
11262/* ICC<14 does not output the required DW_AT_declaration on incomplete
11263 types, but gives them a size of zero. Starting with version 14,
11264 ICC is compatible with GCC. */
11265
11266static int
11267producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11268{
11269 if (!cu->checked_producer)
11270 check_producer (cu);
11271
11272 return cu->producer_is_icc_lt_14;
11273}
11274
1b80a9fa
JK
11275/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11276 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11277 this, it was first present in GCC release 4.3.0. */
11278
11279static int
11280producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11281{
11282 if (!cu->checked_producer)
11283 check_producer (cu);
11284
11285 return cu->producer_is_gcc_lt_4_3;
11286}
11287
d721ba37
PA
11288static file_and_directory
11289find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 11290{
d721ba37
PA
11291 file_and_directory res;
11292
9291a0cd
TT
11293 /* Find the filename. Do not use dwarf2_name here, since the filename
11294 is not a source language identifier. */
d721ba37
PA
11295 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11296 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 11297
d721ba37
PA
11298 if (res.comp_dir == NULL
11299 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11300 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 11301 {
d721ba37
PA
11302 res.comp_dir_storage = ldirname (res.name);
11303 if (!res.comp_dir_storage.empty ())
11304 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 11305 }
d721ba37 11306 if (res.comp_dir != NULL)
9291a0cd
TT
11307 {
11308 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11309 directory, get rid of it. */
d721ba37 11310 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 11311
d721ba37
PA
11312 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11313 res.comp_dir = cp + 1;
9291a0cd
TT
11314 }
11315
d721ba37
PA
11316 if (res.name == NULL)
11317 res.name = "<unknown>";
11318
11319 return res;
9291a0cd
TT
11320}
11321
f4dc4d17
DE
11322/* Handle DW_AT_stmt_list for a compilation unit.
11323 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
11324 COMP_DIR is the compilation directory. LOWPC is passed to
11325 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
11326
11327static void
11328handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 11329 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 11330{
518817b3
SM
11331 struct dwarf2_per_objfile *dwarf2_per_objfile
11332 = cu->per_cu->dwarf2_per_objfile;
527f3840 11333 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ab95328 11334 struct attribute *attr;
527f3840
JK
11335 struct line_header line_header_local;
11336 hashval_t line_header_local_hash;
527f3840
JK
11337 void **slot;
11338 int decode_mapping;
2ab95328 11339
f4dc4d17
DE
11340 gdb_assert (! cu->per_cu->is_debug_types);
11341
2ab95328 11342 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
11343 if (attr == NULL)
11344 return;
11345
9c541725 11346 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
11347
11348 /* The line header hash table is only created if needed (it exists to
11349 prevent redundant reading of the line table for partial_units).
11350 If we're given a partial_unit, we'll need it. If we're given a
11351 compile_unit, then use the line header hash table if it's already
11352 created, but don't create one just yet. */
11353
11354 if (dwarf2_per_objfile->line_header_hash == NULL
11355 && die->tag == DW_TAG_partial_unit)
2ab95328 11356 {
527f3840
JK
11357 dwarf2_per_objfile->line_header_hash
11358 = htab_create_alloc_ex (127, line_header_hash_voidp,
11359 line_header_eq_voidp,
11360 free_line_header_voidp,
11361 &objfile->objfile_obstack,
11362 hashtab_obstack_allocate,
11363 dummy_obstack_deallocate);
11364 }
2ab95328 11365
9c541725 11366 line_header_local.sect_off = line_offset;
527f3840
JK
11367 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11368 line_header_local_hash = line_header_hash (&line_header_local);
11369 if (dwarf2_per_objfile->line_header_hash != NULL)
11370 {
11371 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11372 &line_header_local,
11373 line_header_local_hash, NO_INSERT);
11374
11375 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11376 is not present in *SLOT (since if there is something in *SLOT then
11377 it will be for a partial_unit). */
11378 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 11379 {
527f3840 11380 gdb_assert (*slot != NULL);
9a3c8263 11381 cu->line_header = (struct line_header *) *slot;
527f3840 11382 return;
dee91e82 11383 }
2ab95328 11384 }
527f3840
JK
11385
11386 /* dwarf_decode_line_header does not yet provide sufficient information.
11387 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
11388 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11389 if (lh == NULL)
527f3840 11390 return;
4c8aa72d
PA
11391
11392 cu->line_header = lh.release ();
11393 cu->line_header_die_owner = die;
527f3840
JK
11394
11395 if (dwarf2_per_objfile->line_header_hash == NULL)
11396 slot = NULL;
11397 else
11398 {
11399 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash,
11400 &line_header_local,
11401 line_header_local_hash, INSERT);
11402 gdb_assert (slot != NULL);
11403 }
11404 if (slot != NULL && *slot == NULL)
11405 {
11406 /* This newly decoded line number information unit will be owned
11407 by line_header_hash hash table. */
11408 *slot = cu->line_header;
4c8aa72d 11409 cu->line_header_die_owner = NULL;
527f3840
JK
11410 }
11411 else
11412 {
11413 /* We cannot free any current entry in (*slot) as that struct line_header
11414 may be already used by multiple CUs. Create only temporary decoded
11415 line_header for this CU - it may happen at most once for each line
11416 number information unit. And if we're not using line_header_hash
11417 then this is what we want as well. */
11418 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
11419 }
11420 decode_mapping = (die->tag != DW_TAG_partial_unit);
11421 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11422 decode_mapping);
fff8551c 11423
2ab95328
TT
11424}
11425
95554aad 11426/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 11427
c906108c 11428static void
e7c27a73 11429read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11430{
518817b3
SM
11431 struct dwarf2_per_objfile *dwarf2_per_objfile
11432 = cu->per_cu->dwarf2_per_objfile;
dee91e82 11433 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 11434 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 11435 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11436 CORE_ADDR highpc = ((CORE_ADDR) 0);
11437 struct attribute *attr;
c906108c 11438 struct die_info *child_die;
e142c38c 11439 CORE_ADDR baseaddr;
6e70227d 11440
e142c38c 11441 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 11442
fae299cd 11443 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11444
11445 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11446 from finish_block. */
2acceee2 11447 if (lowpc == ((CORE_ADDR) -1))
c906108c 11448 lowpc = highpc;
3e29f34a 11449 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 11450
d721ba37 11451 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 11452
95554aad 11453 prepare_one_comp_unit (cu, die, cu->language);
303b6f5d 11454
f4b8a18d
KW
11455 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11456 standardised yet. As a workaround for the language detection we fall
11457 back to the DW_AT_producer string. */
11458 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11459 cu->language = language_opencl;
11460
3019eac3
DE
11461 /* Similar hack for Go. */
11462 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11463 set_cu_language (DW_LANG_Go, cu);
11464
d721ba37 11465 dwarf2_start_symtab (cu, fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11466
11467 /* Decode line number information if present. We do this before
11468 processing child DIEs, so that the line header table is available
11469 for DW_AT_decl_file. */
d721ba37 11470 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11471
11472 /* Process all dies in compilation unit. */
11473 if (die->child != NULL)
11474 {
11475 child_die = die->child;
11476 while (child_die && child_die->tag)
11477 {
11478 process_die (child_die, cu);
11479 child_die = sibling_die (child_die);
11480 }
11481 }
11482
11483 /* Decode macro information, if present. Dwarf 2 macro information
11484 refers to information in the line number info statement program
11485 header, so we can only read it if we've read the header
11486 successfully. */
0af92d60
JK
11487 attr = dwarf2_attr (die, DW_AT_macros, cu);
11488 if (attr == NULL)
11489 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
11490 if (attr && cu->line_header)
11491 {
11492 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 11493 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11494
43f3e411 11495 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
11496 }
11497 else
11498 {
11499 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11500 if (attr && cu->line_header)
11501 {
11502 unsigned int macro_offset = DW_UNSND (attr);
11503
43f3e411 11504 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11505 }
11506 }
3019eac3
DE
11507}
11508
f4dc4d17
DE
11509/* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
11510 Create the set of symtabs used by this TU, or if this TU is sharing
11511 symtabs with another TU and the symtabs have already been created
11512 then restore those symtabs in the line header.
11513 We don't need the pc/line-number mapping for type units. */
3019eac3
DE
11514
11515static void
f4dc4d17 11516setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
3019eac3 11517{
f4dc4d17
DE
11518 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
11519 struct type_unit_group *tu_group;
11520 int first_time;
3019eac3 11521 struct attribute *attr;
9c541725 11522 unsigned int i;
0186c6a7 11523 struct signatured_type *sig_type;
3019eac3 11524
f4dc4d17 11525 gdb_assert (per_cu->is_debug_types);
0186c6a7 11526 sig_type = (struct signatured_type *) per_cu;
3019eac3 11527
f4dc4d17 11528 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3019eac3 11529
f4dc4d17 11530 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11531 per_cu->type_unit_group may not have been set up yet. */
0186c6a7
DE
11532 if (sig_type->type_unit_group == NULL)
11533 sig_type->type_unit_group = get_type_unit_group (cu, attr);
11534 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11535
11536 /* If we've already processed this stmt_list there's no real need to
11537 do it again, we could fake it and just recreate the part we need
11538 (file name,index -> symtab mapping). If data shows this optimization
11539 is useful we can do it then. */
43f3e411 11540 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
11541
11542 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11543 debug info. */
fff8551c 11544 line_header_up lh;
f4dc4d17 11545 if (attr != NULL)
3019eac3 11546 {
9c541725 11547 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
f4dc4d17
DE
11548 lh = dwarf_decode_line_header (line_offset, cu);
11549 }
11550 if (lh == NULL)
11551 {
11552 if (first_time)
11553 dwarf2_start_symtab (cu, "", NULL, 0);
11554 else
11555 {
11556 gdb_assert (tu_group->symtabs == NULL);
0ab9ce85 11557 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 11558 }
f4dc4d17 11559 return;
3019eac3
DE
11560 }
11561
4c8aa72d
PA
11562 cu->line_header = lh.release ();
11563 cu->line_header_die_owner = die;
3019eac3 11564
f4dc4d17
DE
11565 if (first_time)
11566 {
43f3e411 11567 struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);
3019eac3 11568
1fd60fc0
DE
11569 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11570 still initializing it, and our caller (a few levels up)
11571 process_full_type_unit still needs to know if this is the first
11572 time. */
11573
4c8aa72d
PA
11574 tu_group->num_symtabs = cu->line_header->file_names.size ();
11575 tu_group->symtabs = XNEWVEC (struct symtab *,
11576 cu->line_header->file_names.size ());
3019eac3 11577
4c8aa72d 11578 for (i = 0; i < cu->line_header->file_names.size (); ++i)
f4dc4d17 11579 {
4c8aa72d 11580 file_entry &fe = cu->line_header->file_names[i];
3019eac3 11581
4c8aa72d 11582 dwarf2_start_subfile (fe.name, fe.include_dir (cu->line_header));
3019eac3 11583
f4dc4d17
DE
11584 if (current_subfile->symtab == NULL)
11585 {
4c8aa72d
PA
11586 /* NOTE: start_subfile will recognize when it's been
11587 passed a file it has already seen. So we can't
11588 assume there's a simple mapping from
11589 cu->line_header->file_names to subfiles, plus
11590 cu->line_header->file_names may contain dups. */
43f3e411
DE
11591 current_subfile->symtab
11592 = allocate_symtab (cust, current_subfile->name);
f4dc4d17
DE
11593 }
11594
8c43009f
PA
11595 fe.symtab = current_subfile->symtab;
11596 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
11597 }
11598 }
11599 else
3019eac3 11600 {
0ab9ce85 11601 restart_symtab (tu_group->compunit_symtab, "", 0);
f4dc4d17 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];
f4dc4d17 11606
4c8aa72d 11607 fe.symtab = tu_group->symtabs[i];
f4dc4d17 11608 }
3019eac3
DE
11609 }
11610
f4dc4d17
DE
11611 /* The main symtab is allocated last. Type units don't have DW_AT_name
11612 so they don't have a "real" (so to speak) symtab anyway.
11613 There is later code that will assign the main symtab to all symbols
11614 that don't have one. We need to handle the case of a symbol with a
11615 missing symtab (DW_AT_decl_file) anyway. */
11616}
3019eac3 11617
f4dc4d17
DE
11618/* Process DW_TAG_type_unit.
11619 For TUs we want to skip the first top level sibling if it's not the
11620 actual type being defined by this TU. In this case the first top
11621 level sibling is there to provide context only. */
3019eac3 11622
f4dc4d17
DE
11623static void
11624read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11625{
11626 struct die_info *child_die;
3019eac3 11627
f4dc4d17
DE
11628 prepare_one_comp_unit (cu, die, language_minimal);
11629
11630 /* Initialize (or reinitialize) the machinery for building symtabs.
11631 We do this before processing child DIEs, so that the line header table
11632 is available for DW_AT_decl_file. */
11633 setup_type_unit_groups (die, cu);
11634
11635 if (die->child != NULL)
11636 {
11637 child_die = die->child;
11638 while (child_die && child_die->tag)
11639 {
11640 process_die (child_die, cu);
11641 child_die = sibling_die (child_die);
11642 }
11643 }
3019eac3
DE
11644}
11645\f
80626a55
DE
11646/* DWO/DWP files.
11647
11648 http://gcc.gnu.org/wiki/DebugFission
11649 http://gcc.gnu.org/wiki/DebugFissionDWP
11650
11651 To simplify handling of both DWO files ("object" files with the DWARF info)
11652 and DWP files (a file with the DWOs packaged up into one file), we treat
11653 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11654
11655static hashval_t
11656hash_dwo_file (const void *item)
11657{
9a3c8263 11658 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11659 hashval_t hash;
3019eac3 11660
a2ce51a0
DE
11661 hash = htab_hash_string (dwo_file->dwo_name);
11662 if (dwo_file->comp_dir != NULL)
11663 hash += htab_hash_string (dwo_file->comp_dir);
11664 return hash;
3019eac3
DE
11665}
11666
11667static int
11668eq_dwo_file (const void *item_lhs, const void *item_rhs)
11669{
9a3c8263
SM
11670 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11671 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11672
a2ce51a0
DE
11673 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11674 return 0;
11675 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11676 return lhs->comp_dir == rhs->comp_dir;
11677 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11678}
11679
11680/* Allocate a hash table for DWO files. */
11681
11682static htab_t
ed2dc618 11683allocate_dwo_file_hash_table (struct objfile *objfile)
3019eac3 11684{
3019eac3
DE
11685 return htab_create_alloc_ex (41,
11686 hash_dwo_file,
11687 eq_dwo_file,
11688 NULL,
11689 &objfile->objfile_obstack,
11690 hashtab_obstack_allocate,
11691 dummy_obstack_deallocate);
11692}
11693
80626a55
DE
11694/* Lookup DWO file DWO_NAME. */
11695
11696static void **
ed2dc618
SM
11697lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11698 const char *dwo_name,
11699 const char *comp_dir)
80626a55
DE
11700{
11701 struct dwo_file find_entry;
11702 void **slot;
11703
11704 if (dwarf2_per_objfile->dwo_files == NULL)
ed2dc618
SM
11705 dwarf2_per_objfile->dwo_files
11706 = allocate_dwo_file_hash_table (dwarf2_per_objfile->objfile);
80626a55
DE
11707
11708 memset (&find_entry, 0, sizeof (find_entry));
0ac5b59e
DE
11709 find_entry.dwo_name = dwo_name;
11710 find_entry.comp_dir = comp_dir;
80626a55
DE
11711 slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
11712
11713 return slot;
11714}
11715
3019eac3
DE
11716static hashval_t
11717hash_dwo_unit (const void *item)
11718{
9a3c8263 11719 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11720
11721 /* This drops the top 32 bits of the id, but is ok for a hash. */
11722 return dwo_unit->signature;
11723}
11724
11725static int
11726eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11727{
9a3c8263
SM
11728 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11729 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11730
11731 /* The signature is assumed to be unique within the DWO file.
11732 So while object file CU dwo_id's always have the value zero,
11733 that's OK, assuming each object file DWO file has only one CU,
11734 and that's the rule for now. */
11735 return lhs->signature == rhs->signature;
11736}
11737
11738/* Allocate a hash table for DWO CUs,TUs.
11739 There is one of these tables for each of CUs,TUs for each DWO file. */
11740
11741static htab_t
11742allocate_dwo_unit_table (struct objfile *objfile)
11743{
11744 /* Start out with a pretty small number.
11745 Generally DWO files contain only one CU and maybe some TUs. */
11746 return htab_create_alloc_ex (3,
11747 hash_dwo_unit,
11748 eq_dwo_unit,
11749 NULL,
11750 &objfile->objfile_obstack,
11751 hashtab_obstack_allocate,
11752 dummy_obstack_deallocate);
11753}
11754
80626a55 11755/* Structure used to pass data to create_dwo_debug_info_hash_table_reader. */
3019eac3 11756
19c3d4c9 11757struct create_dwo_cu_data
3019eac3
DE
11758{
11759 struct dwo_file *dwo_file;
19c3d4c9 11760 struct dwo_unit dwo_unit;
3019eac3
DE
11761};
11762
19c3d4c9 11763/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11764
11765static void
19c3d4c9
DE
11766create_dwo_cu_reader (const struct die_reader_specs *reader,
11767 const gdb_byte *info_ptr,
11768 struct die_info *comp_unit_die,
11769 int has_children,
11770 void *datap)
3019eac3
DE
11771{
11772 struct dwarf2_cu *cu = reader->cu;
9c541725 11773 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11774 struct dwarf2_section_info *section = cu->per_cu->section;
9a3c8263 11775 struct create_dwo_cu_data *data = (struct create_dwo_cu_data *) datap;
3019eac3 11776 struct dwo_file *dwo_file = data->dwo_file;
19c3d4c9 11777 struct dwo_unit *dwo_unit = &data->dwo_unit;
3019eac3 11778 struct attribute *attr;
3019eac3
DE
11779
11780 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
11781 if (attr == NULL)
11782 {
b98664d3 11783 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11784 " its dwo_id [in module %s]"),
9d8780f0 11785 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11786 return;
11787 }
11788
3019eac3
DE
11789 dwo_unit->dwo_file = dwo_file;
11790 dwo_unit->signature = DW_UNSND (attr);
8a0459fd 11791 dwo_unit->section = section;
9c541725 11792 dwo_unit->sect_off = sect_off;
3019eac3
DE
11793 dwo_unit->length = cu->per_cu->length;
11794
b4f54984 11795 if (dwarf_read_debug)
9d8780f0
SM
11796 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11797 sect_offset_str (sect_off),
9c541725 11798 hex_string (dwo_unit->signature));
3019eac3
DE
11799}
11800
33c5cd75 11801/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11802 Note: This function processes DWO files only, not DWP files. */
3019eac3 11803
33c5cd75 11804static void
ed2dc618
SM
11805create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11806 struct dwo_file &dwo_file, dwarf2_section_info &section,
33c5cd75 11807 htab_t &cus_htab)
3019eac3
DE
11808{
11809 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 11810 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11811
33c5cd75
DB
11812 dwarf2_read_section (objfile, &section);
11813 info_ptr = section.buffer;
3019eac3
DE
11814
11815 if (info_ptr == NULL)
33c5cd75 11816 return;
3019eac3 11817
b4f54984 11818 if (dwarf_read_debug)
19c3d4c9
DE
11819 {
11820 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
33c5cd75
DB
11821 get_section_name (&section),
11822 get_section_file_name (&section));
19c3d4c9 11823 }
3019eac3 11824
33c5cd75 11825 end_ptr = info_ptr + section.size;
3019eac3
DE
11826 while (info_ptr < end_ptr)
11827 {
11828 struct dwarf2_per_cu_data per_cu;
33c5cd75
DB
11829 struct create_dwo_cu_data create_dwo_cu_data;
11830 struct dwo_unit *dwo_unit;
11831 void **slot;
11832 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3 11833
19c3d4c9
DE
11834 memset (&create_dwo_cu_data.dwo_unit, 0,
11835 sizeof (create_dwo_cu_data.dwo_unit));
3019eac3 11836 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 11837 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3019eac3 11838 per_cu.is_debug_types = 0;
33c5cd75
DB
11839 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11840 per_cu.section = &section;
c5ed0576 11841 create_dwo_cu_data.dwo_file = &dwo_file;
33c5cd75
DB
11842
11843 init_cutu_and_read_dies_no_follow (
11844 &per_cu, &dwo_file, create_dwo_cu_reader, &create_dwo_cu_data);
11845 info_ptr += per_cu.length;
11846
11847 // If the unit could not be parsed, skip it.
11848 if (create_dwo_cu_data.dwo_unit.dwo_file == NULL)
11849 continue;
3019eac3 11850
33c5cd75
DB
11851 if (cus_htab == NULL)
11852 cus_htab = allocate_dwo_unit_table (objfile);
19c3d4c9 11853
33c5cd75
DB
11854 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11855 *dwo_unit = create_dwo_cu_data.dwo_unit;
11856 slot = htab_find_slot (cus_htab, dwo_unit, INSERT);
11857 gdb_assert (slot != NULL);
11858 if (*slot != NULL)
19c3d4c9 11859 {
33c5cd75
DB
11860 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11861 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11862
b98664d3 11863 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11864 " the entry at offset %s, signature %s"),
11865 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11866 hex_string (dwo_unit->signature));
19c3d4c9 11867 }
33c5cd75 11868 *slot = (void *)dwo_unit;
3019eac3 11869 }
3019eac3
DE
11870}
11871
80626a55
DE
11872/* DWP file .debug_{cu,tu}_index section format:
11873 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11874
d2415c6c
DE
11875 DWP Version 1:
11876
80626a55
DE
11877 Both index sections have the same format, and serve to map a 64-bit
11878 signature to a set of section numbers. Each section begins with a header,
11879 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11880 indexes, and a pool of 32-bit section numbers. The index sections will be
11881 aligned at 8-byte boundaries in the file.
11882
d2415c6c
DE
11883 The index section header consists of:
11884
11885 V, 32 bit version number
11886 -, 32 bits unused
11887 N, 32 bit number of compilation units or type units in the index
11888 M, 32 bit number of slots in the hash table
80626a55 11889
d2415c6c 11890 Numbers are recorded using the byte order of the application binary.
80626a55 11891
d2415c6c
DE
11892 The hash table begins at offset 16 in the section, and consists of an array
11893 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11894 order of the application binary). Unused slots in the hash table are 0.
11895 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11896
d2415c6c
DE
11897 The parallel table begins immediately after the hash table
11898 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11899 array of 32-bit indexes (using the byte order of the application binary),
11900 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11901 table contains a 32-bit index into the pool of section numbers. For unused
11902 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11903
73869dc2
DE
11904 The pool of section numbers begins immediately following the hash table
11905 (at offset 16 + 12 * M from the beginning of the section). The pool of
11906 section numbers consists of an array of 32-bit words (using the byte order
11907 of the application binary). Each item in the array is indexed starting
11908 from 0. The hash table entry provides the index of the first section
11909 number in the set. Additional section numbers in the set follow, and the
11910 set is terminated by a 0 entry (section number 0 is not used in ELF).
11911
11912 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11913 section must be the first entry in the set, and the .debug_abbrev.dwo must
11914 be the second entry. Other members of the set may follow in any order.
11915
11916 ---
11917
11918 DWP Version 2:
11919
11920 DWP Version 2 combines all the .debug_info, etc. sections into one,
11921 and the entries in the index tables are now offsets into these sections.
11922 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11923 section.
11924
11925 Index Section Contents:
11926 Header
11927 Hash Table of Signatures dwp_hash_table.hash_table
11928 Parallel Table of Indices dwp_hash_table.unit_table
11929 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11930 Table of Section Sizes dwp_hash_table.v2.sizes
11931
11932 The index section header consists of:
11933
11934 V, 32 bit version number
11935 L, 32 bit number of columns in the table of section offsets
11936 N, 32 bit number of compilation units or type units in the index
11937 M, 32 bit number of slots in the hash table
11938
11939 Numbers are recorded using the byte order of the application binary.
11940
11941 The hash table has the same format as version 1.
11942 The parallel table of indices has the same format as version 1,
11943 except that the entries are origin-1 indices into the table of sections
11944 offsets and the table of section sizes.
11945
11946 The table of offsets begins immediately following the parallel table
11947 (at offset 16 + 12 * M from the beginning of the section). The table is
11948 a two-dimensional array of 32-bit words (using the byte order of the
11949 application binary), with L columns and N+1 rows, in row-major order.
11950 Each row in the array is indexed starting from 0. The first row provides
11951 a key to the remaining rows: each column in this row provides an identifier
11952 for a debug section, and the offsets in the same column of subsequent rows
11953 refer to that section. The section identifiers are:
11954
11955 DW_SECT_INFO 1 .debug_info.dwo
11956 DW_SECT_TYPES 2 .debug_types.dwo
11957 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11958 DW_SECT_LINE 4 .debug_line.dwo
11959 DW_SECT_LOC 5 .debug_loc.dwo
11960 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11961 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11962 DW_SECT_MACRO 8 .debug_macro.dwo
11963
11964 The offsets provided by the CU and TU index sections are the base offsets
11965 for the contributions made by each CU or TU to the corresponding section
11966 in the package file. Each CU and TU header contains an abbrev_offset
11967 field, used to find the abbreviations table for that CU or TU within the
11968 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11969 be interpreted as relative to the base offset given in the index section.
11970 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11971 should be interpreted as relative to the base offset for .debug_line.dwo,
11972 and offsets into other debug sections obtained from DWARF attributes should
11973 also be interpreted as relative to the corresponding base offset.
11974
11975 The table of sizes begins immediately following the table of offsets.
11976 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11977 with L columns and N rows, in row-major order. Each row in the array is
11978 indexed starting from 1 (row 0 is shared by the two tables).
11979
11980 ---
11981
11982 Hash table lookup is handled the same in version 1 and 2:
11983
11984 We assume that N and M will not exceed 2^32 - 1.
11985 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11986
d2415c6c
DE
11987 Given a 64-bit compilation unit signature or a type signature S, an entry
11988 in the hash table is located as follows:
80626a55 11989
d2415c6c
DE
11990 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11991 the low-order k bits all set to 1.
80626a55 11992
d2415c6c 11993 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11994
d2415c6c
DE
11995 3) If the hash table entry at index H matches the signature, use that
11996 entry. If the hash table entry at index H is unused (all zeroes),
11997 terminate the search: the signature is not present in the table.
80626a55 11998
d2415c6c 11999 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 12000
d2415c6c 12001 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 12002 to stop at an unused slot or find the match. */
80626a55
DE
12003
12004/* Create a hash table to map DWO IDs to their CU/TU entry in
12005 .debug_{info,types}.dwo in DWP_FILE.
12006 Returns NULL if there isn't one.
12007 Note: This function processes DWP files only, not DWO files. */
12008
12009static struct dwp_hash_table *
ed2dc618
SM
12010create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
12011 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
12012{
12013 struct objfile *objfile = dwarf2_per_objfile->objfile;
400174b1 12014 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 12015 const gdb_byte *index_ptr, *index_end;
80626a55 12016 struct dwarf2_section_info *index;
73869dc2 12017 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
12018 struct dwp_hash_table *htab;
12019
12020 if (is_debug_types)
12021 index = &dwp_file->sections.tu_index;
12022 else
12023 index = &dwp_file->sections.cu_index;
12024
12025 if (dwarf2_section_empty_p (index))
12026 return NULL;
12027 dwarf2_read_section (objfile, index);
12028
12029 index_ptr = index->buffer;
12030 index_end = index_ptr + index->size;
12031
12032 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
12033 index_ptr += 4;
12034 if (version == 2)
12035 nr_columns = read_4_bytes (dbfd, index_ptr);
12036 else
12037 nr_columns = 0;
12038 index_ptr += 4;
80626a55
DE
12039 nr_units = read_4_bytes (dbfd, index_ptr);
12040 index_ptr += 4;
12041 nr_slots = read_4_bytes (dbfd, index_ptr);
12042 index_ptr += 4;
12043
73869dc2 12044 if (version != 1 && version != 2)
80626a55 12045 {
21aa081e 12046 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 12047 " [in module %s]"),
21aa081e 12048 pulongest (version), dwp_file->name);
80626a55
DE
12049 }
12050 if (nr_slots != (nr_slots & -nr_slots))
12051 {
21aa081e 12052 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 12053 " is not power of 2 [in module %s]"),
21aa081e 12054 pulongest (nr_slots), dwp_file->name);
80626a55
DE
12055 }
12056
12057 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
12058 htab->version = version;
12059 htab->nr_columns = nr_columns;
80626a55
DE
12060 htab->nr_units = nr_units;
12061 htab->nr_slots = nr_slots;
12062 htab->hash_table = index_ptr;
12063 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
12064
12065 /* Exit early if the table is empty. */
12066 if (nr_slots == 0 || nr_units == 0
12067 || (version == 2 && nr_columns == 0))
12068 {
12069 /* All must be zero. */
12070 if (nr_slots != 0 || nr_units != 0
12071 || (version == 2 && nr_columns != 0))
12072 {
b98664d3 12073 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
12074 " all zero [in modules %s]"),
12075 dwp_file->name);
12076 }
12077 return htab;
12078 }
12079
12080 if (version == 1)
12081 {
12082 htab->section_pool.v1.indices =
12083 htab->unit_table + sizeof (uint32_t) * nr_slots;
12084 /* It's harder to decide whether the section is too small in v1.
12085 V1 is deprecated anyway so we punt. */
12086 }
12087 else
12088 {
12089 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
12090 int *ids = htab->section_pool.v2.section_ids;
12091 /* Reverse map for error checking. */
12092 int ids_seen[DW_SECT_MAX + 1];
12093 int i;
12094
12095 if (nr_columns < 2)
12096 {
12097 error (_("Dwarf Error: bad DWP hash table, too few columns"
12098 " in section table [in module %s]"),
12099 dwp_file->name);
12100 }
12101 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
12102 {
12103 error (_("Dwarf Error: bad DWP hash table, too many columns"
12104 " in section table [in module %s]"),
12105 dwp_file->name);
12106 }
12107 memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12108 memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
12109 for (i = 0; i < nr_columns; ++i)
12110 {
12111 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
12112
12113 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
12114 {
12115 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
12116 " in section table [in module %s]"),
12117 id, dwp_file->name);
12118 }
12119 if (ids_seen[id] != -1)
12120 {
12121 error (_("Dwarf Error: bad DWP hash table, duplicate section"
12122 " id %d in section table [in module %s]"),
12123 id, dwp_file->name);
12124 }
12125 ids_seen[id] = i;
12126 ids[i] = id;
12127 }
12128 /* Must have exactly one info or types section. */
12129 if (((ids_seen[DW_SECT_INFO] != -1)
12130 + (ids_seen[DW_SECT_TYPES] != -1))
12131 != 1)
12132 {
12133 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
12134 " DWO info/types section [in module %s]"),
12135 dwp_file->name);
12136 }
12137 /* Must have an abbrev section. */
12138 if (ids_seen[DW_SECT_ABBREV] == -1)
12139 {
12140 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12141 " section [in module %s]"),
12142 dwp_file->name);
12143 }
12144 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12145 htab->section_pool.v2.sizes =
12146 htab->section_pool.v2.offsets + (sizeof (uint32_t)
12147 * nr_units * nr_columns);
12148 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
12149 * nr_units * nr_columns))
12150 > index_end)
12151 {
12152 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12153 " [in module %s]"),
12154 dwp_file->name);
12155 }
12156 }
80626a55
DE
12157
12158 return htab;
12159}
12160
12161/* Update SECTIONS with the data from SECTP.
12162
12163 This function is like the other "locate" section routines that are
12164 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 12165 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
12166
12167 The result is non-zero for success, or zero if an error was found. */
12168
12169static int
73869dc2
DE
12170locate_v1_virtual_dwo_sections (asection *sectp,
12171 struct virtual_v1_dwo_sections *sections)
80626a55
DE
12172{
12173 const struct dwop_section_names *names = &dwop_section_names;
12174
12175 if (section_is_p (sectp->name, &names->abbrev_dwo))
12176 {
12177 /* There can be only one. */
049412e3 12178 if (sections->abbrev.s.section != NULL)
80626a55 12179 return 0;
049412e3 12180 sections->abbrev.s.section = sectp;
80626a55
DE
12181 sections->abbrev.size = bfd_get_section_size (sectp);
12182 }
12183 else if (section_is_p (sectp->name, &names->info_dwo)
12184 || section_is_p (sectp->name, &names->types_dwo))
12185 {
12186 /* There can be only one. */
049412e3 12187 if (sections->info_or_types.s.section != NULL)
80626a55 12188 return 0;
049412e3 12189 sections->info_or_types.s.section = sectp;
80626a55
DE
12190 sections->info_or_types.size = bfd_get_section_size (sectp);
12191 }
12192 else if (section_is_p (sectp->name, &names->line_dwo))
12193 {
12194 /* There can be only one. */
049412e3 12195 if (sections->line.s.section != NULL)
80626a55 12196 return 0;
049412e3 12197 sections->line.s.section = sectp;
80626a55
DE
12198 sections->line.size = bfd_get_section_size (sectp);
12199 }
12200 else if (section_is_p (sectp->name, &names->loc_dwo))
12201 {
12202 /* There can be only one. */
049412e3 12203 if (sections->loc.s.section != NULL)
80626a55 12204 return 0;
049412e3 12205 sections->loc.s.section = sectp;
80626a55
DE
12206 sections->loc.size = bfd_get_section_size (sectp);
12207 }
12208 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12209 {
12210 /* There can be only one. */
049412e3 12211 if (sections->macinfo.s.section != NULL)
80626a55 12212 return 0;
049412e3 12213 sections->macinfo.s.section = sectp;
80626a55
DE
12214 sections->macinfo.size = bfd_get_section_size (sectp);
12215 }
12216 else if (section_is_p (sectp->name, &names->macro_dwo))
12217 {
12218 /* There can be only one. */
049412e3 12219 if (sections->macro.s.section != NULL)
80626a55 12220 return 0;
049412e3 12221 sections->macro.s.section = sectp;
80626a55
DE
12222 sections->macro.size = bfd_get_section_size (sectp);
12223 }
12224 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12225 {
12226 /* There can be only one. */
049412e3 12227 if (sections->str_offsets.s.section != NULL)
80626a55 12228 return 0;
049412e3 12229 sections->str_offsets.s.section = sectp;
80626a55
DE
12230 sections->str_offsets.size = bfd_get_section_size (sectp);
12231 }
12232 else
12233 {
12234 /* No other kind of section is valid. */
12235 return 0;
12236 }
12237
12238 return 1;
12239}
12240
73869dc2
DE
12241/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12242 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12243 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12244 This is for DWP version 1 files. */
80626a55
DE
12245
12246static struct dwo_unit *
ed2dc618
SM
12247create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12248 struct dwp_file *dwp_file,
73869dc2
DE
12249 uint32_t unit_index,
12250 const char *comp_dir,
12251 ULONGEST signature, int is_debug_types)
80626a55
DE
12252{
12253 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
12254 const struct dwp_hash_table *dwp_htab =
12255 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12256 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
12257 const char *kind = is_debug_types ? "TU" : "CU";
12258 struct dwo_file *dwo_file;
12259 struct dwo_unit *dwo_unit;
73869dc2 12260 struct virtual_v1_dwo_sections sections;
80626a55 12261 void **dwo_file_slot;
80626a55
DE
12262 int i;
12263
73869dc2
DE
12264 gdb_assert (dwp_file->version == 1);
12265
b4f54984 12266 if (dwarf_read_debug)
80626a55 12267 {
73869dc2 12268 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 12269 kind,
73869dc2 12270 pulongest (unit_index), hex_string (signature),
80626a55
DE
12271 dwp_file->name);
12272 }
12273
19ac8c2e 12274 /* Fetch the sections of this DWO unit.
80626a55
DE
12275 Put a limit on the number of sections we look for so that bad data
12276 doesn't cause us to loop forever. */
12277
73869dc2 12278#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
12279 (1 /* .debug_info or .debug_types */ \
12280 + 1 /* .debug_abbrev */ \
12281 + 1 /* .debug_line */ \
12282 + 1 /* .debug_loc */ \
12283 + 1 /* .debug_str_offsets */ \
19ac8c2e 12284 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
12285 + 1 /* trailing zero */)
12286
12287 memset (&sections, 0, sizeof (sections));
80626a55 12288
73869dc2 12289 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
12290 {
12291 asection *sectp;
12292 uint32_t section_nr =
12293 read_4_bytes (dbfd,
73869dc2
DE
12294 dwp_htab->section_pool.v1.indices
12295 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
12296
12297 if (section_nr == 0)
12298 break;
12299 if (section_nr >= dwp_file->num_sections)
12300 {
12301 error (_("Dwarf Error: bad DWP hash table, section number too large"
12302 " [in module %s]"),
12303 dwp_file->name);
12304 }
12305
12306 sectp = dwp_file->elf_sections[section_nr];
73869dc2 12307 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
12308 {
12309 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12310 " [in module %s]"),
12311 dwp_file->name);
12312 }
12313 }
12314
12315 if (i < 2
a32a8923
DE
12316 || dwarf2_section_empty_p (&sections.info_or_types)
12317 || dwarf2_section_empty_p (&sections.abbrev))
80626a55
DE
12318 {
12319 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12320 " [in module %s]"),
12321 dwp_file->name);
12322 }
73869dc2 12323 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
12324 {
12325 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12326 " [in module %s]"),
12327 dwp_file->name);
12328 }
12329
12330 /* It's easier for the rest of the code if we fake a struct dwo_file and
12331 have dwo_unit "live" in that. At least for now.
12332
12333 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 12334 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
12335 file, we can combine them back into a virtual DWO file to save space
12336 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
12337 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12338
791afaa2
TT
12339 std::string virtual_dwo_name =
12340 string_printf ("virtual-dwo/%d-%d-%d-%d",
12341 get_section_id (&sections.abbrev),
12342 get_section_id (&sections.line),
12343 get_section_id (&sections.loc),
12344 get_section_id (&sections.str_offsets));
80626a55 12345 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12346 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12347 virtual_dwo_name.c_str (),
12348 comp_dir);
80626a55
DE
12349 /* Create one if necessary. */
12350 if (*dwo_file_slot == NULL)
12351 {
b4f54984 12352 if (dwarf_read_debug)
80626a55
DE
12353 {
12354 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12355 virtual_dwo_name.c_str ());
80626a55
DE
12356 }
12357 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
12358 dwo_file->dwo_name
12359 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
791afaa2
TT
12360 virtual_dwo_name.c_str (),
12361 virtual_dwo_name.size ());
0ac5b59e 12362 dwo_file->comp_dir = comp_dir;
80626a55
DE
12363 dwo_file->sections.abbrev = sections.abbrev;
12364 dwo_file->sections.line = sections.line;
12365 dwo_file->sections.loc = sections.loc;
12366 dwo_file->sections.macinfo = sections.macinfo;
12367 dwo_file->sections.macro = sections.macro;
12368 dwo_file->sections.str_offsets = sections.str_offsets;
12369 /* The "str" section is global to the entire DWP file. */
12370 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 12371 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
12372 there's no need to record it in dwo_file.
12373 Also, we can't simply record type sections in dwo_file because
12374 we record a pointer into the vector in dwo_unit. As we collect more
12375 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
12376 for it, invalidating all copies of pointers into the previous
12377 contents. */
80626a55
DE
12378 *dwo_file_slot = dwo_file;
12379 }
12380 else
12381 {
b4f54984 12382 if (dwarf_read_debug)
80626a55
DE
12383 {
12384 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12385 virtual_dwo_name.c_str ());
80626a55 12386 }
9a3c8263 12387 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 12388 }
80626a55
DE
12389
12390 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12391 dwo_unit->dwo_file = dwo_file;
12392 dwo_unit->signature = signature;
8d749320
SM
12393 dwo_unit->section =
12394 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 12395 *dwo_unit->section = sections.info_or_types;
57d63ce2 12396 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
12397
12398 return dwo_unit;
12399}
12400
73869dc2
DE
12401/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
12402 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
12403 piece within that section used by a TU/CU, return a virtual section
12404 of just that piece. */
12405
12406static struct dwarf2_section_info
ed2dc618
SM
12407create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
12408 struct dwarf2_section_info *section,
73869dc2
DE
12409 bfd_size_type offset, bfd_size_type size)
12410{
12411 struct dwarf2_section_info result;
12412 asection *sectp;
12413
12414 gdb_assert (section != NULL);
12415 gdb_assert (!section->is_virtual);
12416
12417 memset (&result, 0, sizeof (result));
12418 result.s.containing_section = section;
12419 result.is_virtual = 1;
12420
12421 if (size == 0)
12422 return result;
12423
12424 sectp = get_section_bfd_section (section);
12425
12426 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12427 bounds of the real section. This is a pretty-rare event, so just
12428 flag an error (easier) instead of a warning and trying to cope. */
12429 if (sectp == NULL
12430 || offset + size > bfd_get_section_size (sectp))
12431 {
73869dc2
DE
12432 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
12433 " in section %s [in module %s]"),
12434 sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
12435 objfile_name (dwarf2_per_objfile->objfile));
12436 }
12437
12438 result.virtual_offset = offset;
12439 result.size = size;
12440 return result;
12441}
12442
12443/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12444 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12445 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12446 This is for DWP version 2 files. */
12447
12448static struct dwo_unit *
ed2dc618
SM
12449create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
12450 struct dwp_file *dwp_file,
73869dc2
DE
12451 uint32_t unit_index,
12452 const char *comp_dir,
12453 ULONGEST signature, int is_debug_types)
12454{
12455 struct objfile *objfile = dwarf2_per_objfile->objfile;
12456 const struct dwp_hash_table *dwp_htab =
12457 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12458 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
12459 const char *kind = is_debug_types ? "TU" : "CU";
12460 struct dwo_file *dwo_file;
12461 struct dwo_unit *dwo_unit;
12462 struct virtual_v2_dwo_sections sections;
12463 void **dwo_file_slot;
73869dc2
DE
12464 int i;
12465
12466 gdb_assert (dwp_file->version == 2);
12467
b4f54984 12468 if (dwarf_read_debug)
73869dc2
DE
12469 {
12470 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
12471 kind,
12472 pulongest (unit_index), hex_string (signature),
12473 dwp_file->name);
12474 }
12475
12476 /* Fetch the section offsets of this DWO unit. */
12477
12478 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12479
12480 for (i = 0; i < dwp_htab->nr_columns; ++i)
12481 {
12482 uint32_t offset = read_4_bytes (dbfd,
12483 dwp_htab->section_pool.v2.offsets
12484 + (((unit_index - 1) * dwp_htab->nr_columns
12485 + i)
12486 * sizeof (uint32_t)));
12487 uint32_t size = read_4_bytes (dbfd,
12488 dwp_htab->section_pool.v2.sizes
12489 + (((unit_index - 1) * dwp_htab->nr_columns
12490 + i)
12491 * sizeof (uint32_t)));
12492
12493 switch (dwp_htab->section_pool.v2.section_ids[i])
12494 {
12495 case DW_SECT_INFO:
12496 case DW_SECT_TYPES:
12497 sections.info_or_types_offset = offset;
12498 sections.info_or_types_size = size;
12499 break;
12500 case DW_SECT_ABBREV:
12501 sections.abbrev_offset = offset;
12502 sections.abbrev_size = size;
12503 break;
12504 case DW_SECT_LINE:
12505 sections.line_offset = offset;
12506 sections.line_size = size;
12507 break;
12508 case DW_SECT_LOC:
12509 sections.loc_offset = offset;
12510 sections.loc_size = size;
12511 break;
12512 case DW_SECT_STR_OFFSETS:
12513 sections.str_offsets_offset = offset;
12514 sections.str_offsets_size = size;
12515 break;
12516 case DW_SECT_MACINFO:
12517 sections.macinfo_offset = offset;
12518 sections.macinfo_size = size;
12519 break;
12520 case DW_SECT_MACRO:
12521 sections.macro_offset = offset;
12522 sections.macro_size = size;
12523 break;
12524 }
12525 }
12526
12527 /* It's easier for the rest of the code if we fake a struct dwo_file and
12528 have dwo_unit "live" in that. At least for now.
12529
12530 The DWP file can be made up of a random collection of CUs and TUs.
12531 However, for each CU + set of TUs that came from the same original DWO
12532 file, we can combine them back into a virtual DWO file to save space
12533 (fewer struct dwo_file objects to allocate). Remember that for really
12534 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12535
791afaa2
TT
12536 std::string virtual_dwo_name =
12537 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12538 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12539 (long) (sections.line_size ? sections.line_offset : 0),
12540 (long) (sections.loc_size ? sections.loc_offset : 0),
12541 (long) (sections.str_offsets_size
12542 ? sections.str_offsets_offset : 0));
73869dc2 12543 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12544 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12545 virtual_dwo_name.c_str (),
12546 comp_dir);
73869dc2
DE
12547 /* Create one if necessary. */
12548 if (*dwo_file_slot == NULL)
12549 {
b4f54984 12550 if (dwarf_read_debug)
73869dc2
DE
12551 {
12552 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12553 virtual_dwo_name.c_str ());
73869dc2
DE
12554 }
12555 dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
224c3ddb
SM
12556 dwo_file->dwo_name
12557 = (const char *) obstack_copy0 (&objfile->objfile_obstack,
791afaa2
TT
12558 virtual_dwo_name.c_str (),
12559 virtual_dwo_name.size ());
73869dc2
DE
12560 dwo_file->comp_dir = comp_dir;
12561 dwo_file->sections.abbrev =
ed2dc618 12562 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
12563 sections.abbrev_offset, sections.abbrev_size);
12564 dwo_file->sections.line =
ed2dc618 12565 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
12566 sections.line_offset, sections.line_size);
12567 dwo_file->sections.loc =
ed2dc618 12568 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
12569 sections.loc_offset, sections.loc_size);
12570 dwo_file->sections.macinfo =
ed2dc618 12571 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
12572 sections.macinfo_offset, sections.macinfo_size);
12573 dwo_file->sections.macro =
ed2dc618 12574 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
12575 sections.macro_offset, sections.macro_size);
12576 dwo_file->sections.str_offsets =
ed2dc618
SM
12577 create_dwp_v2_section (dwarf2_per_objfile,
12578 &dwp_file->sections.str_offsets,
73869dc2
DE
12579 sections.str_offsets_offset,
12580 sections.str_offsets_size);
12581 /* The "str" section is global to the entire DWP file. */
12582 dwo_file->sections.str = dwp_file->sections.str;
12583 /* The info or types section is assigned below to dwo_unit,
12584 there's no need to record it in dwo_file.
12585 Also, we can't simply record type sections in dwo_file because
12586 we record a pointer into the vector in dwo_unit. As we collect more
12587 types we'll grow the vector and eventually have to reallocate space
12588 for it, invalidating all copies of pointers into the previous
12589 contents. */
12590 *dwo_file_slot = dwo_file;
12591 }
12592 else
12593 {
b4f54984 12594 if (dwarf_read_debug)
73869dc2
DE
12595 {
12596 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12597 virtual_dwo_name.c_str ());
73869dc2 12598 }
9a3c8263 12599 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12600 }
73869dc2
DE
12601
12602 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
12603 dwo_unit->dwo_file = dwo_file;
12604 dwo_unit->signature = signature;
8d749320
SM
12605 dwo_unit->section =
12606 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
ed2dc618
SM
12607 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12608 is_debug_types
73869dc2
DE
12609 ? &dwp_file->sections.types
12610 : &dwp_file->sections.info,
12611 sections.info_or_types_offset,
12612 sections.info_or_types_size);
12613 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12614
12615 return dwo_unit;
12616}
12617
57d63ce2
DE
12618/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12619 Returns NULL if the signature isn't found. */
80626a55
DE
12620
12621static struct dwo_unit *
ed2dc618
SM
12622lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12623 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12624 ULONGEST signature, int is_debug_types)
80626a55 12625{
57d63ce2
DE
12626 const struct dwp_hash_table *dwp_htab =
12627 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12628 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12629 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12630 uint32_t hash = signature & mask;
12631 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12632 unsigned int i;
12633 void **slot;
870f88f7 12634 struct dwo_unit find_dwo_cu;
80626a55
DE
12635
12636 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12637 find_dwo_cu.signature = signature;
19ac8c2e
DE
12638 slot = htab_find_slot (is_debug_types
12639 ? dwp_file->loaded_tus
12640 : dwp_file->loaded_cus,
12641 &find_dwo_cu, INSERT);
80626a55
DE
12642
12643 if (*slot != NULL)
9a3c8263 12644 return (struct dwo_unit *) *slot;
80626a55
DE
12645
12646 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12647 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12648 {
12649 ULONGEST signature_in_table;
12650
12651 signature_in_table =
57d63ce2 12652 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12653 if (signature_in_table == signature)
12654 {
57d63ce2
DE
12655 uint32_t unit_index =
12656 read_4_bytes (dbfd,
12657 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12658
73869dc2
DE
12659 if (dwp_file->version == 1)
12660 {
ed2dc618
SM
12661 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12662 dwp_file, unit_index,
73869dc2
DE
12663 comp_dir, signature,
12664 is_debug_types);
12665 }
12666 else
12667 {
ed2dc618
SM
12668 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12669 dwp_file, unit_index,
73869dc2
DE
12670 comp_dir, signature,
12671 is_debug_types);
12672 }
9a3c8263 12673 return (struct dwo_unit *) *slot;
80626a55
DE
12674 }
12675 if (signature_in_table == 0)
12676 return NULL;
12677 hash = (hash + hash2) & mask;
12678 }
12679
12680 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12681 " [in module %s]"),
12682 dwp_file->name);
12683}
12684
ab5088bf 12685/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12686 Open the file specified by FILE_NAME and hand it off to BFD for
12687 preliminary analysis. Return a newly initialized bfd *, which
12688 includes a canonicalized copy of FILE_NAME.
80626a55 12689 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12690 SEARCH_CWD is true if the current directory is to be searched.
12691 It will be searched before debug-file-directory.
13aaf454
DE
12692 If successful, the file is added to the bfd include table of the
12693 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12694 If unable to find/open the file, return NULL.
3019eac3
DE
12695 NOTE: This function is derived from symfile_bfd_open. */
12696
192b62ce 12697static gdb_bfd_ref_ptr
ed2dc618
SM
12698try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12699 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12700{
24b9144d 12701 int desc;
9c02c129
DE
12702 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12703 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12704 to debug_file_directory. */
e0cc99a6 12705 const char *search_path;
9c02c129
DE
12706 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12707
e0cc99a6 12708 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12709 if (search_cwd)
12710 {
12711 if (*debug_file_directory != '\0')
e0cc99a6
TT
12712 {
12713 search_path_holder.reset (concat (".", dirname_separator_string,
12714 debug_file_directory,
12715 (char *) NULL));
12716 search_path = search_path_holder.get ();
12717 }
6ac97d4c 12718 else
e0cc99a6 12719 search_path = ".";
6ac97d4c 12720 }
9c02c129 12721 else
e0cc99a6 12722 search_path = debug_file_directory;
3019eac3 12723
24b9144d 12724 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12725 if (is_dwp)
12726 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12727
12728 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12729 desc = openp (search_path, flags, file_name,
3019eac3
DE
12730 O_RDONLY | O_BINARY, &absolute_name);
12731 if (desc < 0)
12732 return NULL;
12733
e0cc99a6
TT
12734 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12735 gnutarget, desc));
9c02c129
DE
12736 if (sym_bfd == NULL)
12737 return NULL;
192b62ce 12738 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12739
192b62ce
TT
12740 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12741 return NULL;
3019eac3 12742
13aaf454
DE
12743 /* Success. Record the bfd as having been included by the objfile's bfd.
12744 This is important because things like demangled_names_hash lives in the
12745 objfile's per_bfd space and may have references to things like symbol
12746 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12747 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12748
3019eac3
DE
12749 return sym_bfd;
12750}
12751
ab5088bf 12752/* Try to open DWO file FILE_NAME.
3019eac3
DE
12753 COMP_DIR is the DW_AT_comp_dir attribute.
12754 The result is the bfd handle of the file.
12755 If there is a problem finding or opening the file, return NULL.
12756 Upon success, the canonicalized path of the file is stored in the bfd,
12757 same as symfile_bfd_open. */
12758
192b62ce 12759static gdb_bfd_ref_ptr
ed2dc618
SM
12760open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12761 const char *file_name, const char *comp_dir)
3019eac3 12762{
80626a55 12763 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12764 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12765 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12766
12767 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12768
12769 if (comp_dir != NULL)
12770 {
b36cec19
PA
12771 char *path_to_try = concat (comp_dir, SLASH_STRING,
12772 file_name, (char *) NULL);
3019eac3
DE
12773
12774 /* NOTE: If comp_dir is a relative path, this will also try the
12775 search path, which seems useful. */
ed2dc618
SM
12776 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
12777 path_to_try,
12778 0 /*is_dwp*/,
192b62ce 12779 1 /*search_cwd*/));
3019eac3
DE
12780 xfree (path_to_try);
12781 if (abfd != NULL)
12782 return abfd;
12783 }
12784
12785 /* That didn't work, try debug-file-directory, which, despite its name,
12786 is a list of paths. */
12787
12788 if (*debug_file_directory == '\0')
12789 return NULL;
12790
ed2dc618
SM
12791 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12792 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12793}
12794
80626a55
DE
12795/* This function is mapped across the sections and remembers the offset and
12796 size of each of the DWO debugging sections we are interested in. */
12797
12798static void
12799dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12800{
9a3c8263 12801 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12802 const struct dwop_section_names *names = &dwop_section_names;
12803
12804 if (section_is_p (sectp->name, &names->abbrev_dwo))
12805 {
049412e3 12806 dwo_sections->abbrev.s.section = sectp;
80626a55
DE
12807 dwo_sections->abbrev.size = bfd_get_section_size (sectp);
12808 }
12809 else if (section_is_p (sectp->name, &names->info_dwo))
12810 {
049412e3 12811 dwo_sections->info.s.section = sectp;
80626a55
DE
12812 dwo_sections->info.size = bfd_get_section_size (sectp);
12813 }
12814 else if (section_is_p (sectp->name, &names->line_dwo))
12815 {
049412e3 12816 dwo_sections->line.s.section = sectp;
80626a55
DE
12817 dwo_sections->line.size = bfd_get_section_size (sectp);
12818 }
12819 else if (section_is_p (sectp->name, &names->loc_dwo))
12820 {
049412e3 12821 dwo_sections->loc.s.section = sectp;
80626a55
DE
12822 dwo_sections->loc.size = bfd_get_section_size (sectp);
12823 }
12824 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12825 {
049412e3 12826 dwo_sections->macinfo.s.section = sectp;
80626a55
DE
12827 dwo_sections->macinfo.size = bfd_get_section_size (sectp);
12828 }
12829 else if (section_is_p (sectp->name, &names->macro_dwo))
12830 {
049412e3 12831 dwo_sections->macro.s.section = sectp;
80626a55
DE
12832 dwo_sections->macro.size = bfd_get_section_size (sectp);
12833 }
12834 else if (section_is_p (sectp->name, &names->str_dwo))
12835 {
049412e3 12836 dwo_sections->str.s.section = sectp;
80626a55
DE
12837 dwo_sections->str.size = bfd_get_section_size (sectp);
12838 }
12839 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12840 {
049412e3 12841 dwo_sections->str_offsets.s.section = sectp;
80626a55
DE
12842 dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
12843 }
12844 else if (section_is_p (sectp->name, &names->types_dwo))
12845 {
12846 struct dwarf2_section_info type_section;
12847
12848 memset (&type_section, 0, sizeof (type_section));
049412e3 12849 type_section.s.section = sectp;
80626a55
DE
12850 type_section.size = bfd_get_section_size (sectp);
12851 VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
12852 &type_section);
12853 }
12854}
12855
ab5088bf 12856/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12857 by PER_CU. This is for the non-DWP case.
80626a55 12858 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12859
12860static struct dwo_file *
0ac5b59e
DE
12861open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12862 const char *dwo_name, const char *comp_dir)
3019eac3 12863{
ed2dc618 12864 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 12865 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 12866
ed2dc618 12867 gdb_bfd_ref_ptr dbfd (open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir));
80626a55
DE
12868 if (dbfd == NULL)
12869 {
b4f54984 12870 if (dwarf_read_debug)
80626a55
DE
12871 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12872 return NULL;
12873 }
263db9a1
TT
12874
12875 /* We use a unique pointer here, despite the obstack allocation,
12876 because a dwo_file needs some cleanup if it is abandoned. */
12877 dwo_file_up dwo_file (OBSTACK_ZALLOC (&objfile->objfile_obstack,
12878 struct dwo_file));
0ac5b59e
DE
12879 dwo_file->dwo_name = dwo_name;
12880 dwo_file->comp_dir = comp_dir;
192b62ce 12881 dwo_file->dbfd = dbfd.release ();
3019eac3 12882
192b62ce
TT
12883 bfd_map_over_sections (dwo_file->dbfd, dwarf2_locate_dwo_sections,
12884 &dwo_file->sections);
3019eac3 12885
ed2dc618
SM
12886 create_cus_hash_table (dwarf2_per_objfile, *dwo_file, dwo_file->sections.info,
12887 dwo_file->cus);
3019eac3 12888
263db9a1 12889 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 12890 dwo_file->sections.types, dwo_file->tus);
3019eac3 12891
b4f54984 12892 if (dwarf_read_debug)
80626a55
DE
12893 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12894
263db9a1 12895 return dwo_file.release ();
3019eac3
DE
12896}
12897
80626a55 12898/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12899 size of each of the DWP debugging sections common to version 1 and 2 that
12900 we are interested in. */
3019eac3 12901
80626a55 12902static void
73869dc2
DE
12903dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12904 void *dwp_file_ptr)
3019eac3 12905{
9a3c8263 12906 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
12907 const struct dwop_section_names *names = &dwop_section_names;
12908 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12909
80626a55 12910 /* Record the ELF section number for later lookup: this is what the
73869dc2 12911 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12912 gdb_assert (elf_section_nr < dwp_file->num_sections);
12913 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12914
80626a55
DE
12915 /* Look for specific sections that we need. */
12916 if (section_is_p (sectp->name, &names->str_dwo))
12917 {
049412e3 12918 dwp_file->sections.str.s.section = sectp;
80626a55
DE
12919 dwp_file->sections.str.size = bfd_get_section_size (sectp);
12920 }
12921 else if (section_is_p (sectp->name, &names->cu_index))
12922 {
049412e3 12923 dwp_file->sections.cu_index.s.section = sectp;
80626a55
DE
12924 dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
12925 }
12926 else if (section_is_p (sectp->name, &names->tu_index))
12927 {
049412e3 12928 dwp_file->sections.tu_index.s.section = sectp;
80626a55
DE
12929 dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
12930 }
12931}
3019eac3 12932
73869dc2
DE
12933/* This function is mapped across the sections and remembers the offset and
12934 size of each of the DWP version 2 debugging sections that we are interested
12935 in. This is split into a separate function because we don't know if we
12936 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12937
12938static void
12939dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12940{
9a3c8263 12941 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12942 const struct dwop_section_names *names = &dwop_section_names;
12943 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12944
12945 /* Record the ELF section number for later lookup: this is what the
12946 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12947 gdb_assert (elf_section_nr < dwp_file->num_sections);
12948 dwp_file->elf_sections[elf_section_nr] = sectp;
12949
12950 /* Look for specific sections that we need. */
12951 if (section_is_p (sectp->name, &names->abbrev_dwo))
12952 {
049412e3 12953 dwp_file->sections.abbrev.s.section = sectp;
73869dc2
DE
12954 dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
12955 }
12956 else if (section_is_p (sectp->name, &names->info_dwo))
12957 {
049412e3 12958 dwp_file->sections.info.s.section = sectp;
73869dc2
DE
12959 dwp_file->sections.info.size = bfd_get_section_size (sectp);
12960 }
12961 else if (section_is_p (sectp->name, &names->line_dwo))
12962 {
049412e3 12963 dwp_file->sections.line.s.section = sectp;
73869dc2
DE
12964 dwp_file->sections.line.size = bfd_get_section_size (sectp);
12965 }
12966 else if (section_is_p (sectp->name, &names->loc_dwo))
12967 {
049412e3 12968 dwp_file->sections.loc.s.section = sectp;
73869dc2
DE
12969 dwp_file->sections.loc.size = bfd_get_section_size (sectp);
12970 }
12971 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12972 {
049412e3 12973 dwp_file->sections.macinfo.s.section = sectp;
73869dc2
DE
12974 dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
12975 }
12976 else if (section_is_p (sectp->name, &names->macro_dwo))
12977 {
049412e3 12978 dwp_file->sections.macro.s.section = sectp;
73869dc2
DE
12979 dwp_file->sections.macro.size = bfd_get_section_size (sectp);
12980 }
12981 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12982 {
049412e3 12983 dwp_file->sections.str_offsets.s.section = sectp;
73869dc2
DE
12984 dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
12985 }
12986 else if (section_is_p (sectp->name, &names->types_dwo))
12987 {
049412e3 12988 dwp_file->sections.types.s.section = sectp;
73869dc2
DE
12989 dwp_file->sections.types.size = bfd_get_section_size (sectp);
12990 }
12991}
12992
80626a55 12993/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 12994
80626a55
DE
12995static hashval_t
12996hash_dwp_loaded_cutus (const void *item)
12997{
9a3c8263 12998 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 12999
80626a55
DE
13000 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13001 return dwo_unit->signature;
3019eac3
DE
13002}
13003
80626a55 13004/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 13005
80626a55
DE
13006static int
13007eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 13008{
9a3c8263
SM
13009 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13010 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 13011
80626a55
DE
13012 return dua->signature == dub->signature;
13013}
3019eac3 13014
80626a55 13015/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 13016
80626a55
DE
13017static htab_t
13018allocate_dwp_loaded_cutus_table (struct objfile *objfile)
13019{
13020 return htab_create_alloc_ex (3,
13021 hash_dwp_loaded_cutus,
13022 eq_dwp_loaded_cutus,
13023 NULL,
13024 &objfile->objfile_obstack,
13025 hashtab_obstack_allocate,
13026 dummy_obstack_deallocate);
13027}
3019eac3 13028
ab5088bf
DE
13029/* Try to open DWP file FILE_NAME.
13030 The result is the bfd handle of the file.
13031 If there is a problem finding or opening the file, return NULL.
13032 Upon success, the canonicalized path of the file is stored in the bfd,
13033 same as symfile_bfd_open. */
13034
192b62ce 13035static gdb_bfd_ref_ptr
ed2dc618
SM
13036open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
13037 const char *file_name)
ab5088bf 13038{
ed2dc618
SM
13039 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
13040 1 /*is_dwp*/,
192b62ce 13041 1 /*search_cwd*/));
6ac97d4c
DE
13042 if (abfd != NULL)
13043 return abfd;
13044
13045 /* Work around upstream bug 15652.
13046 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13047 [Whether that's a "bug" is debatable, but it is getting in our way.]
13048 We have no real idea where the dwp file is, because gdb's realpath-ing
13049 of the executable's path may have discarded the needed info.
13050 [IWBN if the dwp file name was recorded in the executable, akin to
13051 .gnu_debuglink, but that doesn't exist yet.]
13052 Strip the directory from FILE_NAME and search again. */
13053 if (*debug_file_directory != '\0')
13054 {
13055 /* Don't implicitly search the current directory here.
13056 If the user wants to search "." to handle this case,
13057 it must be added to debug-file-directory. */
ed2dc618
SM
13058 return try_open_dwop_file (dwarf2_per_objfile,
13059 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
13060 0 /*search_cwd*/);
13061 }
13062
13063 return NULL;
ab5088bf
DE
13064}
13065
80626a55
DE
13066/* Initialize the use of the DWP file for the current objfile.
13067 By convention the name of the DWP file is ${objfile}.dwp.
13068 The result is NULL if it can't be found. */
a766d390 13069
400174b1 13070static std::unique_ptr<struct dwp_file>
ed2dc618 13071open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
13072{
13073 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 13074
82bf32bc
JK
13075 /* Try to find first .dwp for the binary file before any symbolic links
13076 resolving. */
6c447423
DE
13077
13078 /* If the objfile is a debug file, find the name of the real binary
13079 file and get the name of dwp file from there. */
d721ba37 13080 std::string dwp_name;
6c447423
DE
13081 if (objfile->separate_debug_objfile_backlink != NULL)
13082 {
13083 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13084 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 13085
d721ba37 13086 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
13087 }
13088 else
d721ba37
PA
13089 dwp_name = objfile->original_name;
13090
13091 dwp_name += ".dwp";
80626a55 13092
ed2dc618 13093 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
13094 if (dbfd == NULL
13095 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13096 {
13097 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
13098 dwp_name = objfile_name (objfile);
13099 dwp_name += ".dwp";
ed2dc618 13100 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
13101 }
13102
80626a55
DE
13103 if (dbfd == NULL)
13104 {
b4f54984 13105 if (dwarf_read_debug)
d721ba37 13106 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 13107 return std::unique_ptr<dwp_file> ();
3019eac3 13108 }
400174b1
TT
13109
13110 const char *name = bfd_get_filename (dbfd.get ());
13111 std::unique_ptr<struct dwp_file> dwp_file
13112 (new struct dwp_file (name, std::move (dbfd)));
c906108c 13113
80626a55 13114 /* +1: section 0 is unused */
192b62ce 13115 dwp_file->num_sections = bfd_count_sections (dwp_file->dbfd) + 1;
80626a55
DE
13116 dwp_file->elf_sections =
13117 OBSTACK_CALLOC (&objfile->objfile_obstack,
13118 dwp_file->num_sections, asection *);
13119
400174b1
TT
13120 bfd_map_over_sections (dwp_file->dbfd.get (),
13121 dwarf2_locate_common_dwp_sections,
13122 dwp_file.get ());
80626a55 13123
400174b1
TT
13124 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13125 0);
80626a55 13126
400174b1
TT
13127 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
13128 1);
80626a55 13129
73869dc2 13130 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
13131 if (dwp_file->cus && dwp_file->tus
13132 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
13133 {
13134 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 13135 pretty bizarre. We use pulongest here because that's the established
4d65956b 13136 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
13137 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13138 " TU version %s [in DWP file %s]"),
13139 pulongest (dwp_file->cus->version),
d721ba37 13140 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 13141 }
08302ed2
DE
13142
13143 if (dwp_file->cus)
13144 dwp_file->version = dwp_file->cus->version;
13145 else if (dwp_file->tus)
13146 dwp_file->version = dwp_file->tus->version;
13147 else
13148 dwp_file->version = 2;
73869dc2
DE
13149
13150 if (dwp_file->version == 2)
400174b1
TT
13151 bfd_map_over_sections (dwp_file->dbfd.get (),
13152 dwarf2_locate_v2_dwp_sections,
13153 dwp_file.get ());
73869dc2 13154
19ac8c2e
DE
13155 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
13156 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
80626a55 13157
b4f54984 13158 if (dwarf_read_debug)
80626a55
DE
13159 {
13160 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
13161 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
13162 " %s CUs, %s TUs\n",
13163 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13164 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
13165 }
13166
13167 return dwp_file;
3019eac3 13168}
c906108c 13169
ab5088bf
DE
13170/* Wrapper around open_and_init_dwp_file, only open it once. */
13171
13172static struct dwp_file *
ed2dc618 13173get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf
DE
13174{
13175 if (! dwarf2_per_objfile->dwp_checked)
13176 {
ed2dc618
SM
13177 dwarf2_per_objfile->dwp_file
13178 = open_and_init_dwp_file (dwarf2_per_objfile);
ab5088bf
DE
13179 dwarf2_per_objfile->dwp_checked = 1;
13180 }
400174b1 13181 return dwarf2_per_objfile->dwp_file.get ();
ab5088bf
DE
13182}
13183
80626a55
DE
13184/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13185 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13186 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 13187 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
13188 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13189
13190 This is called, for example, when wanting to read a variable with a
13191 complex location. Therefore we don't want to do file i/o for every call.
13192 Therefore we don't want to look for a DWO file on every call.
13193 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13194 then we check if we've already seen DWO_NAME, and only THEN do we check
13195 for a DWO file.
13196
1c658ad5 13197 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 13198 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 13199
3019eac3 13200static struct dwo_unit *
80626a55
DE
13201lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
13202 const char *dwo_name, const char *comp_dir,
13203 ULONGEST signature, int is_debug_types)
3019eac3 13204{
ed2dc618 13205 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
3019eac3 13206 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
13207 const char *kind = is_debug_types ? "TU" : "CU";
13208 void **dwo_file_slot;
3019eac3 13209 struct dwo_file *dwo_file;
80626a55 13210 struct dwp_file *dwp_file;
cb1df416 13211
6a506a2d
DE
13212 /* First see if there's a DWP file.
13213 If we have a DWP file but didn't find the DWO inside it, don't
13214 look for the original DWO file. It makes gdb behave differently
13215 depending on whether one is debugging in the build tree. */
cf2c3c16 13216
ed2dc618 13217 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 13218 if (dwp_file != NULL)
cf2c3c16 13219 {
80626a55
DE
13220 const struct dwp_hash_table *dwp_htab =
13221 is_debug_types ? dwp_file->tus : dwp_file->cus;
13222
13223 if (dwp_htab != NULL)
13224 {
13225 struct dwo_unit *dwo_cutu =
ed2dc618 13226 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 13227 signature, is_debug_types);
80626a55
DE
13228
13229 if (dwo_cutu != NULL)
13230 {
b4f54984 13231 if (dwarf_read_debug)
80626a55
DE
13232 {
13233 fprintf_unfiltered (gdb_stdlog,
13234 "Virtual DWO %s %s found: @%s\n",
13235 kind, hex_string (signature),
13236 host_address_to_string (dwo_cutu));
13237 }
13238 return dwo_cutu;
13239 }
13240 }
13241 }
6a506a2d 13242 else
80626a55 13243 {
6a506a2d 13244 /* No DWP file, look for the DWO file. */
80626a55 13245
ed2dc618
SM
13246 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
13247 dwo_name, comp_dir);
6a506a2d 13248 if (*dwo_file_slot == NULL)
80626a55 13249 {
6a506a2d
DE
13250 /* Read in the file and build a table of the CUs/TUs it contains. */
13251 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 13252 }
6a506a2d 13253 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 13254 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 13255
6a506a2d 13256 if (dwo_file != NULL)
19c3d4c9 13257 {
6a506a2d
DE
13258 struct dwo_unit *dwo_cutu = NULL;
13259
13260 if (is_debug_types && dwo_file->tus)
13261 {
13262 struct dwo_unit find_dwo_cutu;
13263
13264 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13265 find_dwo_cutu.signature = signature;
9a3c8263
SM
13266 dwo_cutu
13267 = (struct dwo_unit *) htab_find (dwo_file->tus, &find_dwo_cutu);
6a506a2d 13268 }
33c5cd75 13269 else if (!is_debug_types && dwo_file->cus)
80626a55 13270 {
33c5cd75
DB
13271 struct dwo_unit find_dwo_cutu;
13272
13273 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13274 find_dwo_cutu.signature = signature;
13275 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus,
13276 &find_dwo_cutu);
6a506a2d
DE
13277 }
13278
13279 if (dwo_cutu != NULL)
13280 {
b4f54984 13281 if (dwarf_read_debug)
6a506a2d
DE
13282 {
13283 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
13284 kind, dwo_name, hex_string (signature),
13285 host_address_to_string (dwo_cutu));
13286 }
13287 return dwo_cutu;
80626a55
DE
13288 }
13289 }
2e276125 13290 }
9cdd5dbd 13291
80626a55
DE
13292 /* We didn't find it. This could mean a dwo_id mismatch, or
13293 someone deleted the DWO/DWP file, or the search path isn't set up
13294 correctly to find the file. */
13295
b4f54984 13296 if (dwarf_read_debug)
80626a55
DE
13297 {
13298 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
13299 kind, dwo_name, hex_string (signature));
13300 }
3019eac3 13301
6656a72d
DE
13302 /* This is a warning and not a complaint because it can be caused by
13303 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
13304 {
13305 /* Print the name of the DWP file if we looked there, helps the user
13306 better diagnose the problem. */
791afaa2 13307 std::string dwp_text;
43942612
DE
13308
13309 if (dwp_file != NULL)
791afaa2
TT
13310 dwp_text = string_printf (" [in DWP file %s]",
13311 lbasename (dwp_file->name));
43942612 13312
9d8780f0 13313 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612
DE
13314 " [in module %s]"),
13315 kind, dwo_name, hex_string (signature),
791afaa2 13316 dwp_text.c_str (),
43942612 13317 this_unit->is_debug_types ? "TU" : "CU",
9d8780f0 13318 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
43942612 13319 }
3019eac3 13320 return NULL;
5fb290d7
DJ
13321}
13322
80626a55
DE
13323/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13324 See lookup_dwo_cutu_unit for details. */
13325
13326static struct dwo_unit *
13327lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
13328 const char *dwo_name, const char *comp_dir,
13329 ULONGEST signature)
13330{
13331 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
13332}
13333
13334/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13335 See lookup_dwo_cutu_unit for details. */
13336
13337static struct dwo_unit *
13338lookup_dwo_type_unit (struct signatured_type *this_tu,
13339 const char *dwo_name, const char *comp_dir)
13340{
13341 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
13342}
13343
89e63ee4
DE
13344/* Traversal function for queue_and_load_all_dwo_tus. */
13345
13346static int
13347queue_and_load_dwo_tu (void **slot, void *info)
13348{
13349 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
13350 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
13351 ULONGEST signature = dwo_unit->signature;
13352 struct signatured_type *sig_type =
13353 lookup_dwo_signatured_type (per_cu->cu, signature);
13354
13355 if (sig_type != NULL)
13356 {
13357 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13358
13359 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13360 a real dependency of PER_CU on SIG_TYPE. That is detected later
13361 while processing PER_CU. */
13362 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
13363 load_full_type_unit (sig_cu);
13364 VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
13365 }
13366
13367 return 1;
13368}
13369
13370/* Queue all TUs contained in the DWO of PER_CU to be read in.
13371 The DWO may have the only definition of the type, though it may not be
13372 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13373 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13374
13375static void
13376queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
13377{
13378 struct dwo_unit *dwo_unit;
13379 struct dwo_file *dwo_file;
13380
13381 gdb_assert (!per_cu->is_debug_types);
ed2dc618 13382 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
89e63ee4
DE
13383 gdb_assert (per_cu->cu != NULL);
13384
13385 dwo_unit = per_cu->cu->dwo_unit;
13386 gdb_assert (dwo_unit != NULL);
13387
13388 dwo_file = dwo_unit->dwo_file;
13389 if (dwo_file->tus != NULL)
13390 htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
13391}
13392
3019eac3 13393/* Free all resources associated with DWO_FILE.
5dafb3d1 13394 Close the DWO file and munmap the sections. */
348e048f
DE
13395
13396static void
5dafb3d1 13397free_dwo_file (struct dwo_file *dwo_file)
348e048f 13398{
5c6fa7ab 13399 /* Note: dbfd is NULL for virtual DWO files. */
80626a55 13400 gdb_bfd_unref (dwo_file->dbfd);
348e048f 13401
3019eac3
DE
13402 VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
13403}
348e048f 13404
3019eac3 13405/* Traversal function for free_dwo_files. */
2ab95328 13406
3019eac3
DE
13407static int
13408free_dwo_file_from_slot (void **slot, void *info)
13409{
13410 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
348e048f 13411
5dafb3d1 13412 free_dwo_file (dwo_file);
348e048f 13413
3019eac3
DE
13414 return 1;
13415}
348e048f 13416
3019eac3 13417/* Free all resources associated with DWO_FILES. */
348e048f 13418
3019eac3
DE
13419static void
13420free_dwo_files (htab_t dwo_files, struct objfile *objfile)
13421{
13422 htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
348e048f 13423}
3019eac3
DE
13424\f
13425/* Read in various DIEs. */
348e048f 13426
d389af10 13427/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
13428 Inherit only the children of the DW_AT_abstract_origin DIE not being
13429 already referenced by DW_AT_abstract_origin from the children of the
13430 current DIE. */
d389af10
JK
13431
13432static void
13433inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13434{
13435 struct die_info *child_die;
791afaa2 13436 sect_offset *offsetp;
d389af10
JK
13437 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13438 struct die_info *origin_die;
13439 /* Iterator of the ORIGIN_DIE children. */
13440 struct die_info *origin_child_die;
d389af10 13441 struct attribute *attr;
cd02d79d
PA
13442 struct dwarf2_cu *origin_cu;
13443 struct pending **origin_previous_list_in_scope;
d389af10
JK
13444
13445 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13446 if (!attr)
13447 return;
13448
cd02d79d
PA
13449 /* Note that following die references may follow to a die in a
13450 different cu. */
13451
13452 origin_cu = cu;
13453 origin_die = follow_die_ref (die, attr, &origin_cu);
13454
13455 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13456 symbols in. */
13457 origin_previous_list_in_scope = origin_cu->list_in_scope;
13458 origin_cu->list_in_scope = cu->list_in_scope;
13459
edb3359d
DJ
13460 if (die->tag != origin_die->tag
13461 && !(die->tag == DW_TAG_inlined_subroutine
13462 && origin_die->tag == DW_TAG_subprogram))
b98664d3 13463 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
13464 sect_offset_str (die->sect_off),
13465 sect_offset_str (origin_die->sect_off));
d389af10 13466
791afaa2 13467 std::vector<sect_offset> offsets;
d389af10 13468
3ea89b92
PMR
13469 for (child_die = die->child;
13470 child_die && child_die->tag;
13471 child_die = sibling_die (child_die))
13472 {
13473 struct die_info *child_origin_die;
13474 struct dwarf2_cu *child_origin_cu;
13475
13476 /* We are trying to process concrete instance entries:
216f72a1 13477 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13478 it's not relevant to our analysis here. i.e. detecting DIEs that are
13479 present in the abstract instance but not referenced in the concrete
13480 one. */
216f72a1
JK
13481 if (child_die->tag == DW_TAG_call_site
13482 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
13483 continue;
13484
c38f313d
DJ
13485 /* For each CHILD_DIE, find the corresponding child of
13486 ORIGIN_DIE. If there is more than one layer of
13487 DW_AT_abstract_origin, follow them all; there shouldn't be,
13488 but GCC versions at least through 4.4 generate this (GCC PR
13489 40573). */
3ea89b92
PMR
13490 child_origin_die = child_die;
13491 child_origin_cu = cu;
c38f313d
DJ
13492 while (1)
13493 {
cd02d79d
PA
13494 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13495 child_origin_cu);
c38f313d
DJ
13496 if (attr == NULL)
13497 break;
cd02d79d
PA
13498 child_origin_die = follow_die_ref (child_origin_die, attr,
13499 &child_origin_cu);
c38f313d
DJ
13500 }
13501
d389af10
JK
13502 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13503 counterpart may exist. */
c38f313d 13504 if (child_origin_die != child_die)
d389af10 13505 {
edb3359d
DJ
13506 if (child_die->tag != child_origin_die->tag
13507 && !(child_die->tag == DW_TAG_inlined_subroutine
13508 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 13509 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13510 "different tags"),
9d8780f0
SM
13511 sect_offset_str (child_die->sect_off),
13512 sect_offset_str (child_origin_die->sect_off));
c38f313d 13513 if (child_origin_die->parent != origin_die)
b98664d3 13514 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13515 "different parents"),
9d8780f0
SM
13516 sect_offset_str (child_die->sect_off),
13517 sect_offset_str (child_origin_die->sect_off));
c38f313d 13518 else
791afaa2 13519 offsets.push_back (child_origin_die->sect_off);
d389af10 13520 }
d389af10 13521 }
791afaa2
TT
13522 std::sort (offsets.begin (), offsets.end ());
13523 sect_offset *offsets_end = offsets.data () + offsets.size ();
13524 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13525 if (offsetp[-1] == *offsetp)
b98664d3 13526 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
13527 "to DIE %s as their abstract origin"),
13528 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13529
791afaa2 13530 offsetp = offsets.data ();
d389af10
JK
13531 origin_child_die = origin_die->child;
13532 while (origin_child_die && origin_child_die->tag)
13533 {
13534 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13535 while (offsetp < offsets_end
9c541725 13536 && *offsetp < origin_child_die->sect_off)
d389af10 13537 offsetp++;
b64f50a1 13538 if (offsetp >= offsets_end
9c541725 13539 || *offsetp > origin_child_die->sect_off)
d389af10 13540 {
adde2bff
DE
13541 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13542 Check whether we're already processing ORIGIN_CHILD_DIE.
13543 This can happen with mutually referenced abstract_origins.
13544 PR 16581. */
13545 if (!origin_child_die->in_process)
13546 process_die (origin_child_die, origin_cu);
d389af10
JK
13547 }
13548 origin_child_die = sibling_die (origin_child_die);
13549 }
cd02d79d 13550 origin_cu->list_in_scope = origin_previous_list_in_scope;
d389af10
JK
13551}
13552
c906108c 13553static void
e7c27a73 13554read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13555{
518817b3 13556 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13557 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 13558 struct context_stack *newobj;
c906108c
SS
13559 CORE_ADDR lowpc;
13560 CORE_ADDR highpc;
13561 struct die_info *child_die;
edb3359d 13562 struct attribute *attr, *call_line, *call_file;
15d034d0 13563 const char *name;
e142c38c 13564 CORE_ADDR baseaddr;
801e3a5b 13565 struct block *block;
edb3359d 13566 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13567 std::vector<struct symbol *> template_args;
34eaf542 13568 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13569
13570 if (inlined_func)
13571 {
13572 /* If we do not have call site information, we can't show the
13573 caller of this inlined function. That's too confusing, so
13574 only use the scope for local variables. */
13575 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13576 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13577 if (call_line == NULL || call_file == NULL)
13578 {
13579 read_lexical_block_scope (die, cu);
13580 return;
13581 }
13582 }
c906108c 13583
e142c38c
DJ
13584 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13585
94af9270 13586 name = dwarf2_name (die, cu);
c906108c 13587
e8d05480
JB
13588 /* Ignore functions with missing or empty names. These are actually
13589 illegal according to the DWARF standard. */
13590 if (name == NULL)
13591 {
b98664d3 13592 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13593 sect_offset_str (die->sect_off));
e8d05480
JB
13594 return;
13595 }
13596
13597 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13598 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13599 <= PC_BOUNDS_INVALID)
e8d05480 13600 {
ae4d0c03
PM
13601 attr = dwarf2_attr (die, DW_AT_external, cu);
13602 if (!attr || !DW_UNSND (attr))
b98664d3 13603 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13604 "for subprogram DIE at %s"),
13605 sect_offset_str (die->sect_off));
e8d05480
JB
13606 return;
13607 }
c906108c 13608
3e29f34a
MR
13609 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13610 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13611
34eaf542
TT
13612 /* If we have any template arguments, then we must allocate a
13613 different sort of symbol. */
13614 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
13615 {
13616 if (child_die->tag == DW_TAG_template_type_param
13617 || child_die->tag == DW_TAG_template_value_param)
13618 {
e623cf5d 13619 templ_func = allocate_template_symbol (objfile);
cf724bc9 13620 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13621 break;
13622 }
13623 }
13624
fe978cb0 13625 newobj = push_context (0, lowpc);
5e2db402
TT
13626 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13627 (struct symbol *) templ_func);
4c2df51b 13628
4cecd739
DJ
13629 /* If there is a location expression for DW_AT_frame_base, record
13630 it. */
e142c38c 13631 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
4c2df51b 13632 if (attr)
fe978cb0 13633 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13634
63e43d3a
PMR
13635 /* If there is a location for the static link, record it. */
13636 newobj->static_link = NULL;
13637 attr = dwarf2_attr (die, DW_AT_static_link, cu);
13638 if (attr)
13639 {
224c3ddb
SM
13640 newobj->static_link
13641 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
63e43d3a
PMR
13642 attr_to_dynamic_prop (attr, die, cu, newobj->static_link);
13643 }
13644
e142c38c 13645 cu->list_in_scope = &local_symbols;
c906108c 13646
639d11d3 13647 if (die->child != NULL)
c906108c 13648 {
639d11d3 13649 child_die = die->child;
c906108c
SS
13650 while (child_die && child_die->tag)
13651 {
34eaf542
TT
13652 if (child_die->tag == DW_TAG_template_type_param
13653 || child_die->tag == DW_TAG_template_value_param)
13654 {
13655 struct symbol *arg = new_symbol (child_die, NULL, cu);
13656
f1078f66 13657 if (arg != NULL)
2f4732b0 13658 template_args.push_back (arg);
34eaf542
TT
13659 }
13660 else
13661 process_die (child_die, cu);
c906108c
SS
13662 child_die = sibling_die (child_die);
13663 }
13664 }
13665
d389af10
JK
13666 inherit_abstract_dies (die, cu);
13667
4a811a97
UW
13668 /* If we have a DW_AT_specification, we might need to import using
13669 directives from the context of the specification DIE. See the
13670 comment in determine_prefix. */
13671 if (cu->language == language_cplus
13672 && dwarf2_attr (die, DW_AT_specification, cu))
13673 {
13674 struct dwarf2_cu *spec_cu = cu;
13675 struct die_info *spec_die = die_specification (die, &spec_cu);
13676
13677 while (spec_die)
13678 {
13679 child_die = spec_die->child;
13680 while (child_die && child_die->tag)
13681 {
13682 if (child_die->tag == DW_TAG_imported_module)
13683 process_die (child_die, spec_cu);
13684 child_die = sibling_die (child_die);
13685 }
13686
13687 /* In some cases, GCC generates specification DIEs that
13688 themselves contain DW_AT_specification attributes. */
13689 spec_die = die_specification (spec_die, &spec_cu);
13690 }
13691 }
13692
fe978cb0 13693 newobj = pop_context ();
c906108c 13694 /* Make a block for the local symbols within. */
fe978cb0 13695 block = finish_block (newobj->name, &local_symbols, newobj->old_blocks,
63e43d3a 13696 newobj->static_link, lowpc, highpc);
801e3a5b 13697
df8a16a1 13698 /* For C++, set the block's scope. */
45280282
IB
13699 if ((cu->language == language_cplus
13700 || cu->language == language_fortran
c44af4eb
TT
13701 || cu->language == language_d
13702 || cu->language == language_rust)
4d4ec4e5 13703 && cu->processing_has_namespace_info)
195a3f6c
TT
13704 block_set_scope (block, determine_prefix (die, cu),
13705 &objfile->objfile_obstack);
df8a16a1 13706
801e3a5b
JB
13707 /* If we have address ranges, record them. */
13708 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13709
fe978cb0 13710 gdbarch_make_symbol_special (gdbarch, newobj->name, objfile);
3e29f34a 13711
34eaf542 13712 /* Attach template arguments to function. */
2f4732b0 13713 if (!template_args.empty ())
34eaf542
TT
13714 {
13715 gdb_assert (templ_func != NULL);
13716
2f4732b0 13717 templ_func->n_template_arguments = template_args.size ();
34eaf542 13718 templ_func->template_arguments
8d749320
SM
13719 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13720 templ_func->n_template_arguments);
34eaf542 13721 memcpy (templ_func->template_arguments,
2f4732b0 13722 template_args.data (),
34eaf542 13723 (templ_func->n_template_arguments * sizeof (struct symbol *)));
34eaf542
TT
13724 }
13725
208d8187
JB
13726 /* In C++, we can have functions nested inside functions (e.g., when
13727 a function declares a class that has methods). This means that
13728 when we finish processing a function scope, we may need to go
13729 back to building a containing block's symbol lists. */
fe978cb0 13730 local_symbols = newobj->locals;
22cee43f 13731 local_using_directives = newobj->local_using_directives;
208d8187 13732
921e78cf
JB
13733 /* If we've finished processing a top-level function, subsequent
13734 symbols go in the file symbol list. */
13735 if (outermost_context_p ())
e142c38c 13736 cu->list_in_scope = &file_symbols;
c906108c
SS
13737}
13738
13739/* Process all the DIES contained within a lexical block scope. Start
13740 a new scope, process the dies, and then close the scope. */
13741
13742static void
e7c27a73 13743read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13744{
518817b3 13745 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13746 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 13747 struct context_stack *newobj;
c906108c
SS
13748 CORE_ADDR lowpc, highpc;
13749 struct die_info *child_die;
e142c38c
DJ
13750 CORE_ADDR baseaddr;
13751
13752 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c
SS
13753
13754 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13755 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13756 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13757 be nasty. Might be easier to properly extend generic blocks to
af34e669 13758 describe ranges. */
e385593e
JK
13759 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13760 {
13761 case PC_BOUNDS_NOT_PRESENT:
13762 /* DW_TAG_lexical_block has no attributes, process its children as if
13763 there was no wrapping by that DW_TAG_lexical_block.
13764 GCC does no longer produces such DWARF since GCC r224161. */
13765 for (child_die = die->child;
13766 child_die != NULL && child_die->tag;
13767 child_die = sibling_die (child_die))
13768 process_die (child_die, cu);
13769 return;
13770 case PC_BOUNDS_INVALID:
13771 return;
13772 }
3e29f34a
MR
13773 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13774 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c
SS
13775
13776 push_context (0, lowpc);
639d11d3 13777 if (die->child != NULL)
c906108c 13778 {
639d11d3 13779 child_die = die->child;
c906108c
SS
13780 while (child_die && child_die->tag)
13781 {
e7c27a73 13782 process_die (child_die, cu);
c906108c
SS
13783 child_die = sibling_die (child_die);
13784 }
13785 }
3ea89b92 13786 inherit_abstract_dies (die, cu);
fe978cb0 13787 newobj = pop_context ();
c906108c 13788
22cee43f 13789 if (local_symbols != NULL || local_using_directives != NULL)
c906108c 13790 {
801e3a5b 13791 struct block *block
63e43d3a 13792 = finish_block (0, &local_symbols, newobj->old_blocks, NULL,
fe978cb0 13793 newobj->start_addr, highpc);
801e3a5b
JB
13794
13795 /* Note that recording ranges after traversing children, as we
13796 do here, means that recording a parent's ranges entails
13797 walking across all its children's ranges as they appear in
13798 the address map, which is quadratic behavior.
13799
13800 It would be nicer to record the parent's ranges before
13801 traversing its children, simply overriding whatever you find
13802 there. But since we don't even decide whether to create a
13803 block until after we've traversed its children, that's hard
13804 to do. */
13805 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13806 }
fe978cb0 13807 local_symbols = newobj->locals;
22cee43f 13808 local_using_directives = newobj->local_using_directives;
c906108c
SS
13809}
13810
216f72a1 13811/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13812
13813static void
13814read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13815{
518817b3 13816 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
96408a79
SA
13817 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13818 CORE_ADDR pc, baseaddr;
13819 struct attribute *attr;
13820 struct call_site *call_site, call_site_local;
13821 void **slot;
13822 int nparams;
13823 struct die_info *child_die;
13824
13825 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
13826
216f72a1
JK
13827 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13828 if (attr == NULL)
13829 {
13830 /* This was a pre-DWARF-5 GNU extension alias
13831 for DW_AT_call_return_pc. */
13832 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13833 }
96408a79
SA
13834 if (!attr)
13835 {
b98664d3 13836 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13837 "DIE %s [in module %s]"),
13838 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13839 return;
13840 }
31aa7e4e 13841 pc = attr_value_as_address (attr) + baseaddr;
3e29f34a 13842 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13843
13844 if (cu->call_site_htab == NULL)
13845 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13846 NULL, &objfile->objfile_obstack,
13847 hashtab_obstack_allocate, NULL);
13848 call_site_local.pc = pc;
13849 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13850 if (*slot != NULL)
13851 {
b98664d3 13852 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13853 "DIE %s [in module %s]"),
13854 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13855 objfile_name (objfile));
96408a79
SA
13856 return;
13857 }
13858
13859 /* Count parameters at the caller. */
13860
13861 nparams = 0;
13862 for (child_die = die->child; child_die && child_die->tag;
13863 child_die = sibling_die (child_die))
13864 {
216f72a1
JK
13865 if (child_die->tag != DW_TAG_call_site_parameter
13866 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13867 {
b98664d3 13868 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13869 "DW_TAG_call_site child DIE %s [in module %s]"),
13870 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13871 objfile_name (objfile));
96408a79
SA
13872 continue;
13873 }
13874
13875 nparams++;
13876 }
13877
224c3ddb
SM
13878 call_site
13879 = ((struct call_site *)
13880 obstack_alloc (&objfile->objfile_obstack,
13881 sizeof (*call_site)
13882 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13883 *slot = call_site;
13884 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13885 call_site->pc = pc;
13886
216f72a1
JK
13887 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13888 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13889 {
13890 struct die_info *func_die;
13891
13892 /* Skip also over DW_TAG_inlined_subroutine. */
13893 for (func_die = die->parent;
13894 func_die && func_die->tag != DW_TAG_subprogram
13895 && func_die->tag != DW_TAG_subroutine_type;
13896 func_die = func_die->parent);
13897
216f72a1
JK
13898 /* DW_AT_call_all_calls is a superset
13899 of DW_AT_call_all_tail_calls. */
96408a79 13900 if (func_die
216f72a1 13901 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 13902 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13903 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13904 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13905 {
13906 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13907 not complete. But keep CALL_SITE for look ups via call_site_htab,
13908 both the initial caller containing the real return address PC and
13909 the final callee containing the current PC of a chain of tail
13910 calls do not need to have the tail call list complete. But any
13911 function candidate for a virtual tail call frame searched via
13912 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13913 determined unambiguously. */
13914 }
13915 else
13916 {
13917 struct type *func_type = NULL;
13918
13919 if (func_die)
13920 func_type = get_die_type (func_die, cu);
13921 if (func_type != NULL)
13922 {
13923 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13924
13925 /* Enlist this call site to the function. */
13926 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13927 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13928 }
13929 else
b98664d3 13930 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13931 "DIE %s [in module %s]"),
13932 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13933 }
13934 }
13935
216f72a1
JK
13936 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13937 if (attr == NULL)
13938 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13939 if (attr == NULL)
13940 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13941 if (attr == NULL)
216f72a1
JK
13942 {
13943 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13944 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13945 }
96408a79
SA
13946 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
13947 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
13948 /* Keep NULL DWARF_BLOCK. */;
13949 else if (attr_form_is_block (attr))
13950 {
13951 struct dwarf2_locexpr_baton *dlbaton;
13952
8d749320 13953 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
13954 dlbaton->data = DW_BLOCK (attr)->data;
13955 dlbaton->size = DW_BLOCK (attr)->size;
13956 dlbaton->per_cu = cu->per_cu;
13957
13958 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13959 }
7771576e 13960 else if (attr_form_is_ref (attr))
96408a79 13961 {
96408a79
SA
13962 struct dwarf2_cu *target_cu = cu;
13963 struct die_info *target_die;
13964
ac9ec31b 13965 target_die = follow_die_ref (die, attr, &target_cu);
518817b3 13966 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
96408a79
SA
13967 if (die_is_declaration (target_die, target_cu))
13968 {
7d45c7c3 13969 const char *target_physname;
9112db09
JK
13970
13971 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13972 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13973 if (target_physname == NULL)
9112db09 13974 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 13975 if (target_physname == NULL)
b98664d3 13976 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13977 "physname, for referencing DIE %s [in module %s]"),
13978 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13979 else
7d455152 13980 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
13981 }
13982 else
13983 {
13984 CORE_ADDR lowpc;
13985
13986 /* DW_AT_entry_pc should be preferred. */
3a2b436a 13987 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 13988 <= PC_BOUNDS_INVALID)
b98664d3 13989 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13990 "low pc, for referencing DIE %s [in module %s]"),
13991 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13992 else
3e29f34a
MR
13993 {
13994 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13995 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13996 }
96408a79
SA
13997 }
13998 }
13999 else
b98664d3 14000 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
14001 "block nor reference, for DIE %s [in module %s]"),
14002 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14003
14004 call_site->per_cu = cu->per_cu;
14005
14006 for (child_die = die->child;
14007 child_die && child_die->tag;
14008 child_die = sibling_die (child_die))
14009 {
96408a79 14010 struct call_site_parameter *parameter;
1788b2d3 14011 struct attribute *loc, *origin;
96408a79 14012
216f72a1
JK
14013 if (child_die->tag != DW_TAG_call_site_parameter
14014 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
14015 {
14016 /* Already printed the complaint above. */
14017 continue;
14018 }
14019
14020 gdb_assert (call_site->parameter_count < nparams);
14021 parameter = &call_site->parameter[call_site->parameter_count];
14022
1788b2d3
JK
14023 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14024 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 14025 register is contained in DW_AT_call_value. */
96408a79 14026
24c5c679 14027 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
14028 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14029 if (origin == NULL)
14030 {
14031 /* This was a pre-DWARF-5 GNU extension alias
14032 for DW_AT_call_parameter. */
14033 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14034 }
7771576e 14035 if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
1788b2d3 14036 {
1788b2d3 14037 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725
PA
14038
14039 sect_offset sect_off
14040 = (sect_offset) dwarf2_get_ref_die_offset (origin);
14041 if (!offset_in_cu_p (&cu->header, sect_off))
d76b7dbc
JK
14042 {
14043 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14044 binding can be done only inside one CU. Such referenced DIE
14045 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 14046 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
14047 "DW_TAG_call_site child DIE %s [in module %s]"),
14048 sect_offset_str (child_die->sect_off),
9c541725 14049 objfile_name (objfile));
d76b7dbc
JK
14050 continue;
14051 }
9c541725
PA
14052 parameter->u.param_cu_off
14053 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3
JK
14054 }
14055 else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
96408a79 14056 {
b98664d3 14057 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
14058 "DW_TAG_call_site child DIE %s [in module %s]"),
14059 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
14060 continue;
14061 }
24c5c679 14062 else
96408a79 14063 {
24c5c679
JK
14064 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
14065 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
14066 if (parameter->u.dwarf_reg != -1)
14067 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
14068 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
14069 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
14070 &parameter->u.fb_offset))
14071 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14072 else
14073 {
b98664d3 14074 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 14075 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 14076 "DW_TAG_call_site child DIE %s "
24c5c679 14077 "[in module %s]"),
9d8780f0 14078 sect_offset_str (child_die->sect_off),
9c541725 14079 objfile_name (objfile));
24c5c679
JK
14080 continue;
14081 }
96408a79
SA
14082 }
14083
216f72a1
JK
14084 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14085 if (attr == NULL)
14086 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
96408a79
SA
14087 if (!attr_form_is_block (attr))
14088 {
b98664d3 14089 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
14090 "DW_TAG_call_site child DIE %s [in module %s]"),
14091 sect_offset_str (child_die->sect_off),
9c541725 14092 objfile_name (objfile));
96408a79
SA
14093 continue;
14094 }
14095 parameter->value = DW_BLOCK (attr)->data;
14096 parameter->value_size = DW_BLOCK (attr)->size;
14097
14098 /* Parameters are not pre-cleared by memset above. */
14099 parameter->data_value = NULL;
14100 parameter->data_value_size = 0;
14101 call_site->parameter_count++;
14102
216f72a1
JK
14103 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14104 if (attr == NULL)
14105 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
96408a79
SA
14106 if (attr)
14107 {
14108 if (!attr_form_is_block (attr))
b98664d3 14109 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
14110 "DW_TAG_call_site child DIE %s [in module %s]"),
14111 sect_offset_str (child_die->sect_off),
9c541725 14112 objfile_name (objfile));
96408a79
SA
14113 else
14114 {
14115 parameter->data_value = DW_BLOCK (attr)->data;
14116 parameter->data_value_size = DW_BLOCK (attr)->size;
14117 }
14118 }
14119 }
14120}
14121
71a3c369
TT
14122/* Helper function for read_variable. If DIE represents a virtual
14123 table, then return the type of the concrete object that is
14124 associated with the virtual table. Otherwise, return NULL. */
14125
14126static struct type *
14127rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14128{
14129 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14130 if (attr == NULL)
14131 return NULL;
14132
14133 /* Find the type DIE. */
14134 struct die_info *type_die = NULL;
14135 struct dwarf2_cu *type_cu = cu;
14136
14137 if (attr_form_is_ref (attr))
14138 type_die = follow_die_ref (die, attr, &type_cu);
14139 if (type_die == NULL)
14140 return NULL;
14141
14142 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14143 return NULL;
14144 return die_containing_type (type_die, type_cu);
14145}
14146
14147/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14148
14149static void
14150read_variable (struct die_info *die, struct dwarf2_cu *cu)
14151{
14152 struct rust_vtable_symbol *storage = NULL;
14153
14154 if (cu->language == language_rust)
14155 {
14156 struct type *containing_type = rust_containing_type (die, cu);
14157
14158 if (containing_type != NULL)
14159 {
518817b3 14160 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
71a3c369
TT
14161
14162 storage = OBSTACK_ZALLOC (&objfile->objfile_obstack,
14163 struct rust_vtable_symbol);
14164 initialize_objfile_symbol (storage);
14165 storage->concrete_type = containing_type;
cf724bc9 14166 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
14167 }
14168 }
14169
5e2db402 14170 new_symbol (die, NULL, cu, storage);
71a3c369
TT
14171}
14172
43988095
JK
14173/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14174 reading .debug_rnglists.
14175 Callback's type should be:
14176 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14177 Return true if the attributes are present and valid, otherwise,
14178 return false. */
14179
14180template <typename Callback>
14181static bool
14182dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
14183 Callback &&callback)
14184{
ed2dc618 14185 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14186 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14187 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 14188 bfd *obfd = objfile->obfd;
43988095
JK
14189 /* Base address selection entry. */
14190 CORE_ADDR base;
14191 int found_base;
43988095 14192 const gdb_byte *buffer;
43988095
JK
14193 CORE_ADDR baseaddr;
14194 bool overflow = false;
14195
14196 found_base = cu->base_known;
14197 base = cu->base_address;
14198
14199 dwarf2_read_section (objfile, &dwarf2_per_objfile->rnglists);
14200 if (offset >= dwarf2_per_objfile->rnglists.size)
14201 {
b98664d3 14202 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
14203 offset);
14204 return false;
14205 }
14206 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
14207
14208 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14209
14210 while (1)
14211 {
7814882a
JK
14212 /* Initialize it due to a false compiler warning. */
14213 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
14214 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
14215 + dwarf2_per_objfile->rnglists.size);
14216 unsigned int bytes_read;
14217
14218 if (buffer == buf_end)
14219 {
14220 overflow = true;
14221 break;
14222 }
14223 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14224 switch (rlet)
14225 {
14226 case DW_RLE_end_of_list:
14227 break;
14228 case DW_RLE_base_address:
14229 if (buffer + cu->header.addr_size > buf_end)
14230 {
14231 overflow = true;
14232 break;
14233 }
14234 base = read_address (obfd, buffer, cu, &bytes_read);
14235 found_base = 1;
14236 buffer += bytes_read;
14237 break;
14238 case DW_RLE_start_length:
14239 if (buffer + cu->header.addr_size > buf_end)
14240 {
14241 overflow = true;
14242 break;
14243 }
14244 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14245 buffer += bytes_read;
14246 range_end = (range_beginning
14247 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14248 buffer += bytes_read;
14249 if (buffer > buf_end)
14250 {
14251 overflow = true;
14252 break;
14253 }
14254 break;
14255 case DW_RLE_offset_pair:
14256 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14257 buffer += bytes_read;
14258 if (buffer > buf_end)
14259 {
14260 overflow = true;
14261 break;
14262 }
14263 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14264 buffer += bytes_read;
14265 if (buffer > buf_end)
14266 {
14267 overflow = true;
14268 break;
14269 }
14270 break;
14271 case DW_RLE_start_end:
14272 if (buffer + 2 * cu->header.addr_size > buf_end)
14273 {
14274 overflow = true;
14275 break;
14276 }
14277 range_beginning = read_address (obfd, buffer, cu, &bytes_read);
14278 buffer += bytes_read;
14279 range_end = read_address (obfd, buffer, cu, &bytes_read);
14280 buffer += bytes_read;
14281 break;
14282 default:
b98664d3 14283 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14284 return false;
14285 }
14286 if (rlet == DW_RLE_end_of_list || overflow)
14287 break;
14288 if (rlet == DW_RLE_base_address)
14289 continue;
14290
14291 if (!found_base)
14292 {
14293 /* We have no valid base address for the ranges
14294 data. */
b98664d3 14295 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14296 return false;
14297 }
14298
14299 if (range_beginning > range_end)
14300 {
14301 /* Inverted range entries are invalid. */
b98664d3 14302 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
14303 return false;
14304 }
14305
14306 /* Empty range entries have no effect. */
14307 if (range_beginning == range_end)
14308 continue;
14309
14310 range_beginning += base;
14311 range_end += base;
14312
14313 /* A not-uncommon case of bad debug info.
14314 Don't pollute the addrmap with bad data. */
14315 if (range_beginning + baseaddr == 0
14316 && !dwarf2_per_objfile->has_section_at_zero)
14317 {
b98664d3 14318 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
14319 " [in module %s]"), objfile_name (objfile));
14320 continue;
14321 }
14322
14323 callback (range_beginning, range_end);
14324 }
14325
14326 if (overflow)
14327 {
b98664d3 14328 complaint (_("Offset %d is not terminated "
43988095
JK
14329 "for DW_AT_ranges attribute"),
14330 offset);
14331 return false;
14332 }
14333
14334 return true;
14335}
14336
14337/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14338 Callback's type should be:
14339 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 14340 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 14341
43988095 14342template <typename Callback>
43039443 14343static int
5f46c5a5 14344dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 14345 Callback &&callback)
43039443 14346{
ed2dc618 14347 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 14348 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 14349 struct objfile *objfile = dwarf2_per_objfile->objfile;
43039443
JK
14350 struct comp_unit_head *cu_header = &cu->header;
14351 bfd *obfd = objfile->obfd;
14352 unsigned int addr_size = cu_header->addr_size;
14353 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14354 /* Base address selection entry. */
14355 CORE_ADDR base;
14356 int found_base;
14357 unsigned int dummy;
d521ce57 14358 const gdb_byte *buffer;
ff013f42 14359 CORE_ADDR baseaddr;
43039443 14360
43988095
JK
14361 if (cu_header->version >= 5)
14362 return dwarf2_rnglists_process (offset, cu, callback);
14363
d00adf39
DE
14364 found_base = cu->base_known;
14365 base = cu->base_address;
43039443 14366
be391dca 14367 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
dce234bc 14368 if (offset >= dwarf2_per_objfile->ranges.size)
43039443 14369 {
b98664d3 14370 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
14371 offset);
14372 return 0;
14373 }
dce234bc 14374 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 14375
e7030f15 14376 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
ff013f42 14377
43039443
JK
14378 while (1)
14379 {
14380 CORE_ADDR range_beginning, range_end;
14381
14382 range_beginning = read_address (obfd, buffer, cu, &dummy);
14383 buffer += addr_size;
14384 range_end = read_address (obfd, buffer, cu, &dummy);
14385 buffer += addr_size;
14386 offset += 2 * addr_size;
14387
14388 /* An end of list marker is a pair of zero addresses. */
14389 if (range_beginning == 0 && range_end == 0)
14390 /* Found the end of list entry. */
14391 break;
14392
14393 /* Each base address selection entry is a pair of 2 values.
14394 The first is the largest possible address, the second is
14395 the base address. Check for a base address here. */
14396 if ((range_beginning & mask) == mask)
14397 {
28d2bfb9
AB
14398 /* If we found the largest possible address, then we already
14399 have the base address in range_end. */
14400 base = range_end;
43039443
JK
14401 found_base = 1;
14402 continue;
14403 }
14404
14405 if (!found_base)
14406 {
14407 /* We have no valid base address for the ranges
14408 data. */
b98664d3 14409 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
14410 return 0;
14411 }
14412
9277c30c
UW
14413 if (range_beginning > range_end)
14414 {
14415 /* Inverted range entries are invalid. */
b98664d3 14416 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
14417 return 0;
14418 }
14419
14420 /* Empty range entries have no effect. */
14421 if (range_beginning == range_end)
14422 continue;
14423
43039443
JK
14424 range_beginning += base;
14425 range_end += base;
14426
01093045
DE
14427 /* A not-uncommon case of bad debug info.
14428 Don't pollute the addrmap with bad data. */
14429 if (range_beginning + baseaddr == 0
14430 && !dwarf2_per_objfile->has_section_at_zero)
14431 {
b98664d3 14432 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 14433 " [in module %s]"), objfile_name (objfile));
01093045
DE
14434 continue;
14435 }
14436
5f46c5a5
JK
14437 callback (range_beginning, range_end);
14438 }
14439
14440 return 1;
14441}
14442
14443/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14444 Return 1 if the attributes are present and valid, otherwise, return 0.
14445 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14446
14447static int
14448dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14449 CORE_ADDR *high_return, struct dwarf2_cu *cu,
14450 struct partial_symtab *ranges_pst)
14451{
518817b3 14452 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5f46c5a5
JK
14453 struct gdbarch *gdbarch = get_objfile_arch (objfile);
14454 const CORE_ADDR baseaddr = ANOFFSET (objfile->section_offsets,
14455 SECT_OFF_TEXT (objfile));
14456 int low_set = 0;
14457 CORE_ADDR low = 0;
14458 CORE_ADDR high = 0;
14459 int retval;
14460
14461 retval = dwarf2_ranges_process (offset, cu,
14462 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14463 {
9277c30c 14464 if (ranges_pst != NULL)
3e29f34a
MR
14465 {
14466 CORE_ADDR lowpc;
14467 CORE_ADDR highpc;
14468
14469 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14470 range_beginning + baseaddr);
14471 highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
14472 range_end + baseaddr);
14473 addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
14474 ranges_pst);
14475 }
ff013f42 14476
43039443
JK
14477 /* FIXME: This is recording everything as a low-high
14478 segment of consecutive addresses. We should have a
14479 data structure for discontiguous block ranges
14480 instead. */
14481 if (! low_set)
14482 {
14483 low = range_beginning;
14484 high = range_end;
14485 low_set = 1;
14486 }
14487 else
14488 {
14489 if (range_beginning < low)
14490 low = range_beginning;
14491 if (range_end > high)
14492 high = range_end;
14493 }
5f46c5a5
JK
14494 });
14495 if (!retval)
14496 return 0;
43039443
JK
14497
14498 if (! low_set)
14499 /* If the first entry is an end-of-list marker, the range
14500 describes an empty scope, i.e. no instructions. */
14501 return 0;
14502
14503 if (low_return)
14504 *low_return = low;
14505 if (high_return)
14506 *high_return = high;
14507 return 1;
14508}
14509
3a2b436a
JK
14510/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14511 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14512 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14513
3a2b436a 14514static enum pc_bounds_kind
af34e669 14515dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0
DJ
14516 CORE_ADDR *highpc, struct dwarf2_cu *cu,
14517 struct partial_symtab *pst)
c906108c 14518{
518817b3
SM
14519 struct dwarf2_per_objfile *dwarf2_per_objfile
14520 = cu->per_cu->dwarf2_per_objfile;
c906108c 14521 struct attribute *attr;
91da1414 14522 struct attribute *attr_high;
af34e669
DJ
14523 CORE_ADDR low = 0;
14524 CORE_ADDR high = 0;
e385593e 14525 enum pc_bounds_kind ret;
c906108c 14526
91da1414
MW
14527 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14528 if (attr_high)
af34e669 14529 {
e142c38c 14530 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
af34e669 14531 if (attr)
91da1414 14532 {
31aa7e4e
JB
14533 low = attr_value_as_address (attr);
14534 high = attr_value_as_address (attr_high);
14535 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14536 high += low;
91da1414 14537 }
af34e669
DJ
14538 else
14539 /* Found high w/o low attribute. */
e385593e 14540 return PC_BOUNDS_INVALID;
af34e669
DJ
14541
14542 /* Found consecutive range of addresses. */
3a2b436a 14543 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14544 }
c906108c 14545 else
af34e669 14546 {
e142c38c 14547 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
14548 if (attr != NULL)
14549 {
ab435259
DE
14550 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14551 We take advantage of the fact that DW_AT_ranges does not appear
14552 in DW_TAG_compile_unit of DWO files. */
14553 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14554 unsigned int ranges_offset = (DW_UNSND (attr)
14555 + (need_ranges_base
14556 ? cu->ranges_base
14557 : 0));
2e3cf129 14558
af34e669 14559 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14560 .debug_ranges section. */
2e3cf129 14561 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 14562 return PC_BOUNDS_INVALID;
43039443 14563 /* Found discontinuous range of addresses. */
3a2b436a 14564 ret = PC_BOUNDS_RANGES;
af34e669 14565 }
e385593e
JK
14566 else
14567 return PC_BOUNDS_NOT_PRESENT;
af34e669 14568 }
c906108c 14569
48fbe735 14570 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14571 if (high <= low)
e385593e 14572 return PC_BOUNDS_INVALID;
c906108c
SS
14573
14574 /* When using the GNU linker, .gnu.linkonce. sections are used to
14575 eliminate duplicate copies of functions and vtables and such.
14576 The linker will arbitrarily choose one and discard the others.
14577 The AT_*_pc values for such functions refer to local labels in
14578 these sections. If the section from that file was discarded, the
14579 labels are not in the output, so the relocs get a value of 0.
14580 If this is a discarded function, mark the pc bounds as invalid,
14581 so that GDB will ignore it. */
72dca2f5 14582 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 14583 return PC_BOUNDS_INVALID;
c906108c
SS
14584
14585 *lowpc = low;
96408a79
SA
14586 if (highpc)
14587 *highpc = high;
af34e669 14588 return ret;
c906108c
SS
14589}
14590
b084d499
JB
14591/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14592 its low and high PC addresses. Do nothing if these addresses could not
14593 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14594 and HIGHPC to the high address if greater than HIGHPC. */
14595
14596static void
14597dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14598 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14599 struct dwarf2_cu *cu)
14600{
14601 CORE_ADDR low, high;
14602 struct die_info *child = die->child;
14603
e385593e 14604 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14605 {
325fac50
PA
14606 *lowpc = std::min (*lowpc, low);
14607 *highpc = std::max (*highpc, high);
b084d499
JB
14608 }
14609
14610 /* If the language does not allow nested subprograms (either inside
14611 subprograms or lexical blocks), we're done. */
14612 if (cu->language != language_ada)
14613 return;
6e70227d 14614
b084d499
JB
14615 /* Check all the children of the given DIE. If it contains nested
14616 subprograms, then check their pc bounds. Likewise, we need to
14617 check lexical blocks as well, as they may also contain subprogram
14618 definitions. */
14619 while (child && child->tag)
14620 {
14621 if (child->tag == DW_TAG_subprogram
14622 || child->tag == DW_TAG_lexical_block)
14623 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
14624 child = sibling_die (child);
14625 }
14626}
14627
fae299cd
DC
14628/* Get the low and high pc's represented by the scope DIE, and store
14629 them in *LOWPC and *HIGHPC. If the correct values can't be
14630 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14631
14632static void
14633get_scope_pc_bounds (struct die_info *die,
14634 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14635 struct dwarf2_cu *cu)
14636{
14637 CORE_ADDR best_low = (CORE_ADDR) -1;
14638 CORE_ADDR best_high = (CORE_ADDR) 0;
14639 CORE_ADDR current_low, current_high;
14640
3a2b436a 14641 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14642 >= PC_BOUNDS_RANGES)
fae299cd
DC
14643 {
14644 best_low = current_low;
14645 best_high = current_high;
14646 }
14647 else
14648 {
14649 struct die_info *child = die->child;
14650
14651 while (child && child->tag)
14652 {
14653 switch (child->tag) {
14654 case DW_TAG_subprogram:
b084d499 14655 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14656 break;
14657 case DW_TAG_namespace:
f55ee35c 14658 case DW_TAG_module:
fae299cd
DC
14659 /* FIXME: carlton/2004-01-16: Should we do this for
14660 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14661 that current GCC's always emit the DIEs corresponding
14662 to definitions of methods of classes as children of a
14663 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14664 the DIEs giving the declarations, which could be
14665 anywhere). But I don't see any reason why the
14666 standards says that they have to be there. */
14667 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14668
14669 if (current_low != ((CORE_ADDR) -1))
14670 {
325fac50
PA
14671 best_low = std::min (best_low, current_low);
14672 best_high = std::max (best_high, current_high);
fae299cd
DC
14673 }
14674 break;
14675 default:
0963b4bd 14676 /* Ignore. */
fae299cd
DC
14677 break;
14678 }
14679
14680 child = sibling_die (child);
14681 }
14682 }
14683
14684 *lowpc = best_low;
14685 *highpc = best_high;
14686}
14687
801e3a5b
JB
14688/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14689 in DIE. */
380bca97 14690
801e3a5b
JB
14691static void
14692dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14693 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14694{
518817b3 14695 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 14696 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 14697 struct attribute *attr;
91da1414 14698 struct attribute *attr_high;
801e3a5b 14699
91da1414
MW
14700 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14701 if (attr_high)
801e3a5b 14702 {
801e3a5b
JB
14703 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
14704 if (attr)
14705 {
31aa7e4e
JB
14706 CORE_ADDR low = attr_value_as_address (attr);
14707 CORE_ADDR high = attr_value_as_address (attr_high);
14708
14709 if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
14710 high += low;
9a619af0 14711
3e29f34a
MR
14712 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14713 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
14714 record_block_range (block, low, high - 1);
801e3a5b
JB
14715 }
14716 }
14717
14718 attr = dwarf2_attr (die, DW_AT_ranges, cu);
14719 if (attr)
14720 {
ab435259
DE
14721 /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
14722 We take advantage of the fact that DW_AT_ranges does not appear
14723 in DW_TAG_compile_unit of DWO files. */
14724 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14725
14726 /* The value of the DW_AT_ranges attribute is the offset of the
14727 address range list in the .debug_ranges section. */
ab435259
DE
14728 unsigned long offset = (DW_UNSND (attr)
14729 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14730
5f46c5a5
JK
14731 dwarf2_ranges_process (offset, cu,
14732 [&] (CORE_ADDR start, CORE_ADDR end)
14733 {
58fdfd2c
JK
14734 start += baseaddr;
14735 end += baseaddr;
5f46c5a5
JK
14736 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14737 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
14738 record_block_range (block, start, end - 1);
14739 });
801e3a5b
JB
14740 }
14741}
14742
685b1105
JK
14743/* Check whether the producer field indicates either of GCC < 4.6, or the
14744 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14745
685b1105
JK
14746static void
14747check_producer (struct dwarf2_cu *cu)
60d5a603 14748{
38360086 14749 int major, minor;
60d5a603
JK
14750
14751 if (cu->producer == NULL)
14752 {
14753 /* For unknown compilers expect their behavior is DWARF version
14754 compliant.
14755
14756 GCC started to support .debug_types sections by -gdwarf-4 since
14757 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14758 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14759 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14760 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14761 }
b1ffba5a 14762 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14763 {
38360086
MW
14764 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14765 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14766 }
5230b05a
WT
14767 else if (producer_is_icc (cu->producer, &major, &minor))
14768 cu->producer_is_icc_lt_14 = major < 14;
685b1105
JK
14769 else
14770 {
14771 /* For other non-GCC compilers, expect their behavior is DWARF version
14772 compliant. */
60d5a603
JK
14773 }
14774
ba919b58 14775 cu->checked_producer = 1;
685b1105 14776}
ba919b58 14777
685b1105
JK
14778/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14779 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14780 during 4.6.0 experimental. */
14781
14782static int
14783producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14784{
14785 if (!cu->checked_producer)
14786 check_producer (cu);
14787
14788 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14789}
14790
14791/* Return the default accessibility type if it is not overriden by
14792 DW_AT_accessibility. */
14793
14794static enum dwarf_access_attribute
14795dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14796{
14797 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14798 {
14799 /* The default DWARF 2 accessibility for members is public, the default
14800 accessibility for inheritance is private. */
14801
14802 if (die->tag != DW_TAG_inheritance)
14803 return DW_ACCESS_public;
14804 else
14805 return DW_ACCESS_private;
14806 }
14807 else
14808 {
14809 /* DWARF 3+ defines the default accessibility a different way. The same
14810 rules apply now for DW_TAG_inheritance as for the members and it only
14811 depends on the container kind. */
14812
14813 if (die->parent->tag == DW_TAG_class_type)
14814 return DW_ACCESS_private;
14815 else
14816 return DW_ACCESS_public;
14817 }
14818}
14819
74ac6d43
TT
14820/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14821 offset. If the attribute was not found return 0, otherwise return
14822 1. If it was found but could not properly be handled, set *OFFSET
14823 to 0. */
14824
14825static int
14826handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14827 LONGEST *offset)
14828{
14829 struct attribute *attr;
14830
14831 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14832 if (attr != NULL)
14833 {
14834 *offset = 0;
14835
14836 /* Note that we do not check for a section offset first here.
14837 This is because DW_AT_data_member_location is new in DWARF 4,
14838 so if we see it, we can assume that a constant form is really
14839 a constant and not a section offset. */
14840 if (attr_form_is_constant (attr))
14841 *offset = dwarf2_get_attr_constant_value (attr, 0);
14842 else if (attr_form_is_section_offset (attr))
14843 dwarf2_complex_location_expr_complaint ();
14844 else if (attr_form_is_block (attr))
14845 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14846 else
14847 dwarf2_complex_location_expr_complaint ();
14848
14849 return 1;
14850 }
14851
14852 return 0;
14853}
14854
c906108c
SS
14855/* Add an aggregate field to the field list. */
14856
14857static void
107d2387 14858dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14859 struct dwarf2_cu *cu)
6e70227d 14860{
518817b3 14861 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5e2b427d 14862 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
14863 struct nextfield *new_field;
14864 struct attribute *attr;
14865 struct field *fp;
15d034d0 14866 const char *fieldname = "";
c906108c 14867
7d0ccb61
DJ
14868 if (die->tag == DW_TAG_inheritance)
14869 {
be2daae6
TT
14870 fip->baseclasses.emplace_back ();
14871 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14872 }
14873 else
14874 {
be2daae6
TT
14875 fip->fields.emplace_back ();
14876 new_field = &fip->fields.back ();
7d0ccb61 14877 }
be2daae6 14878
c906108c
SS
14879 fip->nfields++;
14880
e142c38c 14881 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c
SS
14882 if (attr)
14883 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
14884 else
14885 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
14886 if (new_field->accessibility != DW_ACCESS_public)
14887 fip->non_public_fields = 1;
60d5a603 14888
e142c38c 14889 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
c906108c
SS
14890 if (attr)
14891 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
14892 else
14893 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
14894
14895 fp = &new_field->field;
a9a9bd0f 14896
e142c38c 14897 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 14898 {
74ac6d43
TT
14899 LONGEST offset;
14900
a9a9bd0f 14901 /* Data member other than a C++ static data member. */
6e70227d 14902
c906108c 14903 /* Get type of field. */
e7c27a73 14904 fp->type = die_type (die, cu);
c906108c 14905
d6a843b5 14906 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 14907
c906108c 14908 /* Get bit size of field (zero if none). */
e142c38c 14909 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
c906108c
SS
14910 if (attr)
14911 {
14912 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14913 }
14914 else
14915 {
14916 FIELD_BITSIZE (*fp) = 0;
14917 }
14918
14919 /* Get bit offset of field. */
74ac6d43
TT
14920 if (handle_data_member_location (die, cu, &offset))
14921 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 14922 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
c906108c
SS
14923 if (attr)
14924 {
5e2b427d 14925 if (gdbarch_bits_big_endian (gdbarch))
c906108c
SS
14926 {
14927 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
14928 additional bit offset from the MSB of the containing
14929 anonymous object to the MSB of the field. We don't
14930 have to do anything special since we don't need to
14931 know the size of the anonymous object. */
f41f5e61 14932 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
14933 }
14934 else
14935 {
14936 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
14937 MSB of the anonymous object, subtract off the number of
14938 bits from the MSB of the field to the MSB of the
14939 object, and then subtract off the number of bits of
14940 the field itself. The result is the bit offset of
14941 the LSB of the field. */
c906108c
SS
14942 int anonymous_size;
14943 int bit_offset = DW_UNSND (attr);
14944
e142c38c 14945 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
14946 if (attr)
14947 {
14948 /* The size of the anonymous object containing
14949 the bit field is explicit, so use the
14950 indicated size (in bytes). */
14951 anonymous_size = DW_UNSND (attr);
14952 }
14953 else
14954 {
14955 /* The size of the anonymous object containing
14956 the bit field must be inferred from the type
14957 attribute of the data member containing the
14958 bit field. */
14959 anonymous_size = TYPE_LENGTH (fp->type);
14960 }
f41f5e61
PA
14961 SET_FIELD_BITPOS (*fp,
14962 (FIELD_BITPOS (*fp)
14963 + anonymous_size * bits_per_byte
14964 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
14965 }
14966 }
da5b30da
AA
14967 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14968 if (attr != NULL)
14969 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
14970 + dwarf2_get_attr_constant_value (attr, 0)));
c906108c
SS
14971
14972 /* Get name of field. */
39cbfefa
DJ
14973 fieldname = dwarf2_name (die, cu);
14974 if (fieldname == NULL)
14975 fieldname = "";
d8151005
DJ
14976
14977 /* The name is already allocated along with this objfile, so we don't
14978 need to duplicate it for the type. */
14979 fp->name = fieldname;
c906108c
SS
14980
14981 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 14982 pointer or virtual base class pointer) to private. */
e142c38c 14983 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 14984 {
d48cc9dd 14985 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
14986 new_field->accessibility = DW_ACCESS_private;
14987 fip->non_public_fields = 1;
14988 }
14989 }
a9a9bd0f 14990 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 14991 {
a9a9bd0f
DC
14992 /* C++ static member. */
14993
14994 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14995 is a declaration, but all versions of G++ as of this writing
14996 (so through at least 3.2.1) incorrectly generate
14997 DW_TAG_variable tags. */
6e70227d 14998
ff355380 14999 const char *physname;
c906108c 15000
a9a9bd0f 15001 /* Get name of field. */
39cbfefa
DJ
15002 fieldname = dwarf2_name (die, cu);
15003 if (fieldname == NULL)
c906108c
SS
15004 return;
15005
254e6b9e 15006 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
15007 if (attr
15008 /* Only create a symbol if this is an external value.
15009 new_symbol checks this and puts the value in the global symbol
15010 table, which we want. If it is not external, new_symbol
15011 will try to put the value in cu->list_in_scope which is wrong. */
15012 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
15013 {
15014 /* A static const member, not much different than an enum as far as
15015 we're concerned, except that we can support more types. */
15016 new_symbol (die, NULL, cu);
15017 }
15018
2df3850c 15019 /* Get physical name. */
ff355380 15020 physname = dwarf2_physname (fieldname, die, cu);
c906108c 15021
d8151005
DJ
15022 /* The name is already allocated along with this objfile, so we don't
15023 need to duplicate it for the type. */
15024 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 15025 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 15026 FIELD_NAME (*fp) = fieldname;
c906108c
SS
15027 }
15028 else if (die->tag == DW_TAG_inheritance)
15029 {
74ac6d43 15030 LONGEST offset;
d4b96c9a 15031
74ac6d43
TT
15032 /* C++ base class field. */
15033 if (handle_data_member_location (die, cu, &offset))
15034 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 15035 FIELD_BITSIZE (*fp) = 0;
e7c27a73 15036 FIELD_TYPE (*fp) = die_type (die, cu);
c906108c 15037 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
c906108c 15038 }
2ddeaf8a
TT
15039 else if (die->tag == DW_TAG_variant_part)
15040 {
15041 /* process_structure_scope will treat this DIE as a union. */
15042 process_structure_scope (die, cu);
15043
15044 /* The variant part is relative to the start of the enclosing
15045 structure. */
15046 SET_FIELD_BITPOS (*fp, 0);
15047 fp->type = get_die_type (die, cu);
15048 fp->artificial = 1;
15049 fp->name = "<<variant>>";
15050 }
15051 else
15052 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
15053}
15054
883fd55a
KS
15055/* Can the type given by DIE define another type? */
15056
15057static bool
15058type_can_define_types (const struct die_info *die)
15059{
15060 switch (die->tag)
15061 {
15062 case DW_TAG_typedef:
15063 case DW_TAG_class_type:
15064 case DW_TAG_structure_type:
15065 case DW_TAG_union_type:
15066 case DW_TAG_enumeration_type:
15067 return true;
15068
15069 default:
15070 return false;
15071 }
15072}
15073
15074/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
15075
15076static void
883fd55a
KS
15077dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15078 struct dwarf2_cu *cu)
6e70227d 15079{
be2daae6
TT
15080 struct decl_field fp;
15081 memset (&fp, 0, sizeof (fp));
98751a41 15082
883fd55a 15083 gdb_assert (type_can_define_types (die));
98751a41 15084
883fd55a 15085 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
15086 fp.name = dwarf2_name (die, cu);
15087 fp.type = read_type_die (die, cu);
98751a41 15088
c191a687
KS
15089 /* Save accessibility. */
15090 enum dwarf_access_attribute accessibility;
15091 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
15092 if (attr != NULL)
15093 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
15094 else
15095 accessibility = dwarf2_default_access_attribute (die, cu);
15096 switch (accessibility)
15097 {
15098 case DW_ACCESS_public:
15099 /* The assumed value if neither private nor protected. */
15100 break;
15101 case DW_ACCESS_private:
be2daae6 15102 fp.is_private = 1;
c191a687
KS
15103 break;
15104 case DW_ACCESS_protected:
be2daae6 15105 fp.is_protected = 1;
c191a687
KS
15106 break;
15107 default:
b98664d3 15108 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
15109 }
15110
883fd55a 15111 if (die->tag == DW_TAG_typedef)
be2daae6 15112 fip->typedef_field_list.push_back (fp);
883fd55a 15113 else
be2daae6 15114 fip->nested_types_list.push_back (fp);
98751a41
JK
15115}
15116
c906108c
SS
15117/* Create the vector of fields, and attach it to the type. */
15118
15119static void
fba45db2 15120dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15121 struct dwarf2_cu *cu)
c906108c
SS
15122{
15123 int nfields = fip->nfields;
15124
15125 /* Record the field count, allocate space for the array of fields,
15126 and create blank accessibility bitfields if necessary. */
15127 TYPE_NFIELDS (type) = nfields;
15128 TYPE_FIELDS (type) = (struct field *)
be2daae6 15129 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
c906108c 15130
b4ba55a1 15131 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
15132 {
15133 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15134
15135 TYPE_FIELD_PRIVATE_BITS (type) =
15136 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15137 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15138
15139 TYPE_FIELD_PROTECTED_BITS (type) =
15140 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15141 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15142
774b6a14
TT
15143 TYPE_FIELD_IGNORE_BITS (type) =
15144 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15145 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
15146 }
15147
15148 /* If the type has baseclasses, allocate and clear a bit vector for
15149 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 15150 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 15151 {
be2daae6 15152 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 15153 unsigned char *pointer;
c906108c
SS
15154
15155 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 15156 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 15157 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
15158 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15159 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
15160 }
15161
2ddeaf8a
TT
15162 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
15163 {
15164 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
15165
be2daae6 15166 for (int index = 0; index < nfields; ++index)
2ddeaf8a 15167 {
be2daae6
TT
15168 struct nextfield &field = fip->fields[index];
15169
15170 if (field.variant.is_discriminant)
2ddeaf8a 15171 di->discriminant_index = index;
be2daae6 15172 else if (field.variant.default_branch)
2ddeaf8a
TT
15173 di->default_index = index;
15174 else
be2daae6 15175 di->discriminants[index] = field.variant.discriminant_value;
2ddeaf8a
TT
15176 }
15177 }
15178
be2daae6
TT
15179 /* Copy the saved-up fields into the field vector. */
15180 for (int i = 0; i < nfields; ++i)
c906108c 15181 {
be2daae6
TT
15182 struct nextfield &field
15183 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15184 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 15185
be2daae6
TT
15186 TYPE_FIELD (type, i) = field.field;
15187 switch (field.accessibility)
c906108c 15188 {
c5aa993b 15189 case DW_ACCESS_private:
b4ba55a1 15190 if (cu->language != language_ada)
be2daae6 15191 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 15192 break;
c906108c 15193
c5aa993b 15194 case DW_ACCESS_protected:
b4ba55a1 15195 if (cu->language != language_ada)
be2daae6 15196 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 15197 break;
c906108c 15198
c5aa993b
JM
15199 case DW_ACCESS_public:
15200 break;
c906108c 15201
c5aa993b
JM
15202 default:
15203 /* Unknown accessibility. Complain and treat it as public. */
15204 {
b98664d3 15205 complaint (_("unsupported accessibility %d"),
be2daae6 15206 field.accessibility);
c5aa993b
JM
15207 }
15208 break;
c906108c 15209 }
be2daae6 15210 if (i < fip->baseclasses.size ())
c906108c 15211 {
be2daae6 15212 switch (field.virtuality)
c906108c 15213 {
c5aa993b
JM
15214 case DW_VIRTUALITY_virtual:
15215 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15216 if (cu->language == language_ada)
a73c6dcd 15217 error (_("unexpected virtuality in component of Ada type"));
be2daae6 15218 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 15219 break;
c906108c
SS
15220 }
15221 }
c906108c
SS
15222 }
15223}
15224
7d27a96d
TT
15225/* Return true if this member function is a constructor, false
15226 otherwise. */
15227
15228static int
15229dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15230{
15231 const char *fieldname;
fe978cb0 15232 const char *type_name;
7d27a96d
TT
15233 int len;
15234
15235 if (die->parent == NULL)
15236 return 0;
15237
15238 if (die->parent->tag != DW_TAG_structure_type
15239 && die->parent->tag != DW_TAG_union_type
15240 && die->parent->tag != DW_TAG_class_type)
15241 return 0;
15242
15243 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15244 type_name = dwarf2_name (die->parent, cu);
15245 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15246 return 0;
15247
15248 len = strlen (fieldname);
fe978cb0
PA
15249 return (strncmp (fieldname, type_name, len) == 0
15250 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15251}
15252
c906108c
SS
15253/* Add a member function to the proper fieldlist. */
15254
15255static void
107d2387 15256dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15257 struct type *type, struct dwarf2_cu *cu)
c906108c 15258{
518817b3 15259 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15260 struct attribute *attr;
c906108c 15261 int i;
be2daae6 15262 struct fnfieldlist *flp = nullptr;
c906108c 15263 struct fn_field *fnp;
15d034d0 15264 const char *fieldname;
f792889a 15265 struct type *this_type;
60d5a603 15266 enum dwarf_access_attribute accessibility;
c906108c 15267
b4ba55a1 15268 if (cu->language == language_ada)
a73c6dcd 15269 error (_("unexpected member function in Ada type"));
b4ba55a1 15270
2df3850c 15271 /* Get name of member function. */
39cbfefa
DJ
15272 fieldname = dwarf2_name (die, cu);
15273 if (fieldname == NULL)
2df3850c 15274 return;
c906108c 15275
c906108c 15276 /* Look up member function name in fieldlist. */
be2daae6 15277 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15278 {
27bfe10e 15279 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15280 {
15281 flp = &fip->fnfieldlists[i];
15282 break;
15283 }
c906108c
SS
15284 }
15285
be2daae6
TT
15286 /* Create a new fnfieldlist if necessary. */
15287 if (flp == nullptr)
c906108c 15288 {
be2daae6
TT
15289 fip->fnfieldlists.emplace_back ();
15290 flp = &fip->fnfieldlists.back ();
c906108c 15291 flp->name = fieldname;
be2daae6 15292 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15293 }
15294
be2daae6
TT
15295 /* Create a new member function field and add it to the vector of
15296 fnfieldlists. */
15297 flp->fnfields.emplace_back ();
15298 fnp = &flp->fnfields.back ();
3da10d80
KS
15299
15300 /* Delay processing of the physname until later. */
9c37b5ae 15301 if (cu->language == language_cplus)
be2daae6
TT
15302 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15303 die, cu);
3da10d80
KS
15304 else
15305 {
1d06ead6 15306 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15307 fnp->physname = physname ? physname : "";
15308 }
15309
c906108c 15310 fnp->type = alloc_type (objfile);
f792889a
DJ
15311 this_type = read_type_die (die, cu);
15312 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 15313 {
f792889a 15314 int nparams = TYPE_NFIELDS (this_type);
c906108c 15315
f792889a 15316 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15317 of the method itself (TYPE_CODE_METHOD). */
15318 smash_to_method_type (fnp->type, type,
f792889a
DJ
15319 TYPE_TARGET_TYPE (this_type),
15320 TYPE_FIELDS (this_type),
15321 TYPE_NFIELDS (this_type),
15322 TYPE_VARARGS (this_type));
c906108c
SS
15323
15324 /* Handle static member functions.
c5aa993b 15325 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15326 member functions. G++ helps GDB by marking the first
15327 parameter for non-static member functions (which is the this
15328 pointer) as artificial. We obtain this information from
15329 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15330 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15331 fnp->voffset = VOFFSET_STATIC;
15332 }
15333 else
b98664d3 15334 complaint (_("member function type missing for '%s'"),
3da10d80 15335 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15336
15337 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15338 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15339 fnp->fcontext = die_containing_type (die, cu);
c906108c 15340
3e43a32a
MS
15341 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15342 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15343
15344 /* Get accessibility. */
e142c38c 15345 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
c906108c 15346 if (attr)
aead7601 15347 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
15348 else
15349 accessibility = dwarf2_default_access_attribute (die, cu);
15350 switch (accessibility)
c906108c 15351 {
60d5a603
JK
15352 case DW_ACCESS_private:
15353 fnp->is_private = 1;
15354 break;
15355 case DW_ACCESS_protected:
15356 fnp->is_protected = 1;
15357 break;
c906108c
SS
15358 }
15359
b02dede2 15360 /* Check for artificial methods. */
e142c38c 15361 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15362 if (attr && DW_UNSND (attr) != 0)
15363 fnp->is_artificial = 1;
15364
7d27a96d
TT
15365 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15366
0d564a31 15367 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15368 function. For older versions of GCC, this is an offset in the
15369 appropriate virtual table, as specified by DW_AT_containing_type.
15370 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15371 to the object address. */
15372
e142c38c 15373 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
aec5aa8b 15374 if (attr)
8e19ed76 15375 {
aec5aa8b 15376 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
8e19ed76 15377 {
aec5aa8b
TT
15378 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15379 {
15380 /* Old-style GCC. */
15381 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15382 }
15383 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15384 || (DW_BLOCK (attr)->size > 1
15385 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15386 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15387 {
aec5aa8b
TT
15388 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15389 if ((fnp->voffset % cu->header.addr_size) != 0)
15390 dwarf2_complex_location_expr_complaint ();
15391 else
15392 fnp->voffset /= cu->header.addr_size;
15393 fnp->voffset += 2;
15394 }
15395 else
15396 dwarf2_complex_location_expr_complaint ();
15397
15398 if (!fnp->fcontext)
7e993ebf
KS
15399 {
15400 /* If there is no `this' field and no DW_AT_containing_type,
15401 we cannot actually find a base class context for the
15402 vtable! */
15403 if (TYPE_NFIELDS (this_type) == 0
15404 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15405 {
b98664d3 15406 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15407 "function \"%s\" (offset %s)"),
15408 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15409 }
15410 else
15411 {
15412 fnp->fcontext
15413 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15414 }
15415 }
aec5aa8b 15416 }
3690dd37 15417 else if (attr_form_is_section_offset (attr))
8e19ed76 15418 {
4d3c2250 15419 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15420 }
15421 else
15422 {
4d3c2250
KB
15423 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15424 fieldname);
8e19ed76 15425 }
0d564a31 15426 }
d48cc9dd
DJ
15427 else
15428 {
15429 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15430 if (attr && DW_UNSND (attr))
15431 {
15432 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15433 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15434 "but the vtable offset is not specified"),
9d8780f0 15435 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15436 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15437 TYPE_CPLUS_DYNAMIC (type) = 1;
15438 }
15439 }
c906108c
SS
15440}
15441
15442/* Create the vector of member function fields, and attach it to the type. */
15443
15444static void
fba45db2 15445dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15446 struct dwarf2_cu *cu)
c906108c 15447{
b4ba55a1 15448 if (cu->language == language_ada)
a73c6dcd 15449 error (_("unexpected member functions in Ada type"));
b4ba55a1 15450
c906108c
SS
15451 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15452 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15453 TYPE_ALLOC (type,
15454 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15455
be2daae6 15456 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15457 {
be2daae6 15458 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15459 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15460
be2daae6
TT
15461 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15462 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15463 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15464 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15465
15466 for (int k = 0; k < nf.fnfields.size (); ++k)
15467 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15468 }
15469
be2daae6 15470 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15471}
15472
1168df01
JB
15473/* Returns non-zero if NAME is the name of a vtable member in CU's
15474 language, zero otherwise. */
15475static int
15476is_vtable_name (const char *name, struct dwarf2_cu *cu)
15477{
15478 static const char vptr[] = "_vptr";
15479
9c37b5ae
TT
15480 /* Look for the C++ form of the vtable. */
15481 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15482 return 1;
15483
15484 return 0;
15485}
15486
c0dd20ea 15487/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15488 functions, with the ABI-specified layout. If TYPE describes
15489 such a structure, smash it into a member function type.
61049d3b
DJ
15490
15491 GCC shouldn't do this; it should just output pointer to member DIEs.
15492 This is GCC PR debug/28767. */
c0dd20ea 15493
0b92b5bb
TT
15494static void
15495quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15496{
09e2d7c7 15497 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15498
15499 /* Check for a structure with no name and two children. */
0b92b5bb
TT
15500 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15501 return;
c0dd20ea
DJ
15502
15503 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15504 if (TYPE_FIELD_NAME (type, 0) == NULL
15505 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15506 || TYPE_FIELD_NAME (type, 1) == NULL
15507 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15508 return;
c0dd20ea
DJ
15509
15510 /* Find the type of the method. */
0b92b5bb 15511 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
15512 if (pfn_type == NULL
15513 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15514 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 15515 return;
c0dd20ea
DJ
15516
15517 /* Look for the "this" argument. */
15518 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15519 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 15520 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 15521 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 15522 return;
c0dd20ea 15523
09e2d7c7 15524 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 15525 new_type = alloc_type (objfile);
09e2d7c7 15526 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
15527 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15528 TYPE_VARARGS (pfn_type));
0b92b5bb 15529 smash_to_methodptr_type (type, new_type);
c0dd20ea 15530}
1168df01 15531
2b4424c3
TT
15532/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15533 appropriate error checking and issuing complaints if there is a
15534 problem. */
15535
15536static ULONGEST
15537get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15538{
15539 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15540
15541 if (attr == nullptr)
15542 return 0;
15543
15544 if (!attr_form_is_constant (attr))
15545 {
b98664d3 15546 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15547 " - DIE at %s [in module %s]"),
15548 sect_offset_str (die->sect_off),
15549 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15550 return 0;
15551 }
15552
15553 ULONGEST align;
15554 if (attr->form == DW_FORM_sdata)
15555 {
15556 LONGEST val = DW_SND (attr);
15557 if (val < 0)
15558 {
b98664d3 15559 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
15560 " - DIE at %s [in module %s]"),
15561 sect_offset_str (die->sect_off),
15562 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15563 return 0;
15564 }
15565 align = val;
15566 }
15567 else
15568 align = DW_UNSND (attr);
15569
15570 if (align == 0)
15571 {
b98664d3 15572 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15573 " - DIE at %s [in module %s]"),
15574 sect_offset_str (die->sect_off),
15575 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15576 return 0;
15577 }
15578 if ((align & (align - 1)) != 0)
15579 {
b98664d3 15580 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15581 " - DIE at %s [in module %s]"),
15582 sect_offset_str (die->sect_off),
15583 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15584 return 0;
15585 }
15586
15587 return align;
15588}
15589
15590/* If the DIE has a DW_AT_alignment attribute, use its value to set
15591 the alignment for TYPE. */
15592
15593static void
15594maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15595 struct type *type)
15596{
15597 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15598 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15599 " - DIE at %s [in module %s]"),
15600 sect_offset_str (die->sect_off),
15601 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15602}
685b1105 15603
c906108c 15604/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15605 (definition) to create a type for the structure or union. Fill in
15606 the type's name and general properties; the members will not be
83655187
DE
15607 processed until process_structure_scope. A symbol table entry for
15608 the type will also not be done until process_structure_scope (assuming
15609 the type has a name).
c906108c 15610
c767944b
DJ
15611 NOTE: we need to call these functions regardless of whether or not the
15612 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15613 structure or union. This gets the type entered into our set of
83655187 15614 user defined types. */
c906108c 15615
f792889a 15616static struct type *
134d01f1 15617read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15618{
518817b3 15619 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
15620 struct type *type;
15621 struct attribute *attr;
15d034d0 15622 const char *name;
c906108c 15623
348e048f
DE
15624 /* If the definition of this type lives in .debug_types, read that type.
15625 Don't follow DW_AT_specification though, that will take us back up
15626 the chain and we want to go down. */
45e58e77 15627 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
15628 if (attr)
15629 {
ac9ec31b 15630 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15631
ac9ec31b 15632 /* The type's CU may not be the same as CU.
02142a6c 15633 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15634 return set_die_type (die, type, cu);
15635 }
15636
c0dd20ea 15637 type = alloc_type (objfile);
c906108c 15638 INIT_CPLUS_SPECIFIC (type);
93311388 15639
39cbfefa
DJ
15640 name = dwarf2_name (die, cu);
15641 if (name != NULL)
c906108c 15642 {
987504bb 15643 if (cu->language == language_cplus
c44af4eb
TT
15644 || cu->language == language_d
15645 || cu->language == language_rust)
63d06c5c 15646 {
15d034d0 15647 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15648
15649 /* dwarf2_full_name might have already finished building the DIE's
15650 type. If so, there is no need to continue. */
15651 if (get_die_type (die, cu) != NULL)
15652 return get_die_type (die, cu);
15653
e86ca25f 15654 TYPE_NAME (type) = full_name;
63d06c5c
DC
15655 }
15656 else
15657 {
d8151005
DJ
15658 /* The name is already allocated along with this objfile, so
15659 we don't need to duplicate it for the type. */
e86ca25f 15660 TYPE_NAME (type) = name;
63d06c5c 15661 }
c906108c
SS
15662 }
15663
15664 if (die->tag == DW_TAG_structure_type)
15665 {
15666 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15667 }
15668 else if (die->tag == DW_TAG_union_type)
15669 {
15670 TYPE_CODE (type) = TYPE_CODE_UNION;
15671 }
2ddeaf8a
TT
15672 else if (die->tag == DW_TAG_variant_part)
15673 {
15674 TYPE_CODE (type) = TYPE_CODE_UNION;
15675 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15676 }
c906108c
SS
15677 else
15678 {
4753d33b 15679 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
15680 }
15681
0cc2414c
TT
15682 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15683 TYPE_DECLARED_CLASS (type) = 1;
15684
e142c38c 15685 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
15686 if (attr)
15687 {
155bfbd3
JB
15688 if (attr_form_is_constant (attr))
15689 TYPE_LENGTH (type) = DW_UNSND (attr);
15690 else
15691 {
15692 /* For the moment, dynamic type sizes are not supported
15693 by GDB's struct type. The actual size is determined
15694 on-demand when resolving the type of a given object,
15695 so set the type's length to zero for now. Otherwise,
15696 we record an expression as the length, and that expression
15697 could lead to a very large value, which could eventually
15698 lead to us trying to allocate that much memory when creating
15699 a value of that type. */
15700 TYPE_LENGTH (type) = 0;
15701 }
c906108c
SS
15702 }
15703 else
15704 {
15705 TYPE_LENGTH (type) = 0;
15706 }
15707
2b4424c3
TT
15708 maybe_set_alignment (cu, die, type);
15709
5230b05a 15710 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15711 {
5230b05a
WT
15712 /* ICC<14 does not output the required DW_AT_declaration on
15713 incomplete types, but gives them a size of zero. */
422b1cb0 15714 TYPE_STUB (type) = 1;
685b1105
JK
15715 }
15716 else
15717 TYPE_STUB_SUPPORTED (type) = 1;
15718
dc718098 15719 if (die_is_declaration (die, cu))
876cecd0 15720 TYPE_STUB (type) = 1;
a6c727b2
DJ
15721 else if (attr == NULL && die->child == NULL
15722 && producer_is_realview (cu->producer))
15723 /* RealView does not output the required DW_AT_declaration
15724 on incomplete types. */
15725 TYPE_STUB (type) = 1;
dc718098 15726
c906108c
SS
15727 /* We need to add the type field to the die immediately so we don't
15728 infinitely recurse when dealing with pointers to the structure
0963b4bd 15729 type within the structure itself. */
1c379e20 15730 set_die_type (die, type, cu);
c906108c 15731
7e314c57
JK
15732 /* set_die_type should be already done. */
15733 set_descriptive_type (type, die, cu);
15734
c767944b
DJ
15735 return type;
15736}
15737
2ddeaf8a
TT
15738/* A helper for process_structure_scope that handles a single member
15739 DIE. */
15740
15741static void
15742handle_struct_member_die (struct die_info *child_die, struct type *type,
15743 struct field_info *fi,
15744 std::vector<struct symbol *> *template_args,
15745 struct dwarf2_cu *cu)
15746{
15747 if (child_die->tag == DW_TAG_member
15748 || child_die->tag == DW_TAG_variable
15749 || child_die->tag == DW_TAG_variant_part)
15750 {
15751 /* NOTE: carlton/2002-11-05: A C++ static data member
15752 should be a DW_TAG_member that is a declaration, but
15753 all versions of G++ as of this writing (so through at
15754 least 3.2.1) incorrectly generate DW_TAG_variable
15755 tags for them instead. */
15756 dwarf2_add_field (fi, child_die, cu);
15757 }
15758 else if (child_die->tag == DW_TAG_subprogram)
15759 {
15760 /* Rust doesn't have member functions in the C++ sense.
15761 However, it does emit ordinary functions as children
15762 of a struct DIE. */
15763 if (cu->language == language_rust)
15764 read_func_scope (child_die, cu);
15765 else
15766 {
15767 /* C++ member function. */
15768 dwarf2_add_member_fn (fi, child_die, type, cu);
15769 }
15770 }
15771 else if (child_die->tag == DW_TAG_inheritance)
15772 {
15773 /* C++ base class field. */
15774 dwarf2_add_field (fi, child_die, cu);
15775 }
15776 else if (type_can_define_types (child_die))
15777 dwarf2_add_type_defn (fi, child_die, cu);
15778 else if (child_die->tag == DW_TAG_template_type_param
15779 || child_die->tag == DW_TAG_template_value_param)
15780 {
15781 struct symbol *arg = new_symbol (child_die, NULL, cu);
15782
15783 if (arg != NULL)
15784 template_args->push_back (arg);
15785 }
15786 else if (child_die->tag == DW_TAG_variant)
15787 {
15788 /* In a variant we want to get the discriminant and also add a
15789 field for our sole member child. */
15790 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15791
15792 for (struct die_info *variant_child = child_die->child;
15793 variant_child != NULL;
15794 variant_child = sibling_die (variant_child))
15795 {
15796 if (variant_child->tag == DW_TAG_member)
15797 {
15798 handle_struct_member_die (variant_child, type, fi,
15799 template_args, cu);
15800 /* Only handle the one. */
15801 break;
15802 }
15803 }
15804
15805 /* We don't handle this but we might as well report it if we see
15806 it. */
15807 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
b98664d3 15808 complaint (_("DW_AT_discr_list is not supported yet"
2ddeaf8a
TT
15809 " - DIE at %s [in module %s]"),
15810 sect_offset_str (child_die->sect_off),
15811 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15812
15813 /* The first field was just added, so we can stash the
15814 discriminant there. */
be2daae6 15815 gdb_assert (!fi->fields.empty ());
2ddeaf8a 15816 if (discr == NULL)
be2daae6 15817 fi->fields.back ().variant.default_branch = true;
2ddeaf8a 15818 else
be2daae6 15819 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
2ddeaf8a
TT
15820 }
15821}
15822
c767944b
DJ
15823/* Finish creating a structure or union type, including filling in
15824 its members and creating a symbol for it. */
15825
15826static void
15827process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15828{
518817b3 15829 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
ca040673 15830 struct die_info *child_die;
c767944b
DJ
15831 struct type *type;
15832
15833 type = get_die_type (die, cu);
15834 if (type == NULL)
15835 type = read_structure_type (die, cu);
15836
2ddeaf8a
TT
15837 /* When reading a DW_TAG_variant_part, we need to notice when we
15838 read the discriminant member, so we can record it later in the
15839 discriminant_info. */
15840 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
15841 sect_offset discr_offset;
15842
15843 if (is_variant_part)
15844 {
15845 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15846 if (discr == NULL)
15847 {
15848 /* Maybe it's a univariant form, an extension we support.
15849 In this case arrange not to check the offset. */
15850 is_variant_part = false;
15851 }
15852 else if (attr_form_is_ref (discr))
15853 {
15854 struct dwarf2_cu *target_cu = cu;
15855 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15856
15857 discr_offset = target_die->sect_off;
15858 }
15859 else
15860 {
b98664d3 15861 complaint (_("DW_AT_discr does not have DIE reference form"
2ddeaf8a
TT
15862 " - DIE at %s [in module %s]"),
15863 sect_offset_str (die->sect_off),
15864 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15865 is_variant_part = false;
15866 }
15867 }
15868
e142c38c 15869 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15870 {
15871 struct field_info fi;
2f4732b0 15872 std::vector<struct symbol *> template_args;
c906108c 15873
639d11d3 15874 child_die = die->child;
c906108c
SS
15875
15876 while (child_die && child_die->tag)
15877 {
2ddeaf8a 15878 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
34eaf542 15879
2ddeaf8a 15880 if (is_variant_part && discr_offset == child_die->sect_off)
be2daae6 15881 fi.fields.back ().variant.is_discriminant = true;
34eaf542 15882
c906108c
SS
15883 child_die = sibling_die (child_die);
15884 }
15885
34eaf542 15886 /* Attach template arguments to type. */
2f4732b0 15887 if (!template_args.empty ())
34eaf542
TT
15888 {
15889 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 15890 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 15891 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
15892 = XOBNEWVEC (&objfile->objfile_obstack,
15893 struct symbol *,
15894 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 15895 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 15896 template_args.data (),
34eaf542
TT
15897 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15898 * sizeof (struct symbol *)));
34eaf542
TT
15899 }
15900
c906108c
SS
15901 /* Attach fields and member functions to the type. */
15902 if (fi.nfields)
e7c27a73 15903 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 15904 if (!fi.fnfieldlists.empty ())
c906108c 15905 {
e7c27a73 15906 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 15907
c5aa993b 15908 /* Get the type which refers to the base class (possibly this
c906108c 15909 class itself) which contains the vtable pointer for the current
0d564a31
DJ
15910 class from the DW_AT_containing_type attribute. This use of
15911 DW_AT_containing_type is a GNU extension. */
c906108c 15912
e142c38c 15913 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 15914 {
e7c27a73 15915 struct type *t = die_containing_type (die, cu);
c906108c 15916
ae6ae975 15917 set_type_vptr_basetype (type, t);
c906108c
SS
15918 if (type == t)
15919 {
c906108c
SS
15920 int i;
15921
15922 /* Our own class provides vtbl ptr. */
15923 for (i = TYPE_NFIELDS (t) - 1;
15924 i >= TYPE_N_BASECLASSES (t);
15925 --i)
15926 {
0d5cff50 15927 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 15928
1168df01 15929 if (is_vtable_name (fieldname, cu))
c906108c 15930 {
ae6ae975 15931 set_type_vptr_fieldno (type, i);
c906108c
SS
15932 break;
15933 }
15934 }
15935
15936 /* Complain if virtual function table field not found. */
15937 if (i < TYPE_N_BASECLASSES (t))
b98664d3 15938 complaint (_("virtual function table pointer "
3e43a32a 15939 "not found when defining class '%s'"),
e86ca25f 15940 TYPE_NAME (type) ? TYPE_NAME (type) : "");
c906108c
SS
15941 }
15942 else
15943 {
ae6ae975 15944 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
15945 }
15946 }
f6235d4c 15947 else if (cu->producer
61012eef 15948 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
15949 {
15950 /* The IBM XLC compiler does not provide direct indication
15951 of the containing type, but the vtable pointer is
15952 always named __vfp. */
15953
15954 int i;
15955
15956 for (i = TYPE_NFIELDS (type) - 1;
15957 i >= TYPE_N_BASECLASSES (type);
15958 --i)
15959 {
15960 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15961 {
ae6ae975
DE
15962 set_type_vptr_fieldno (type, i);
15963 set_type_vptr_basetype (type, type);
f6235d4c
EZ
15964 break;
15965 }
15966 }
15967 }
c906108c 15968 }
98751a41
JK
15969
15970 /* Copy fi.typedef_field_list linked list elements content into the
15971 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 15972 if (!fi.typedef_field_list.empty ())
98751a41 15973 {
be2daae6 15974 int count = fi.typedef_field_list.size ();
98751a41 15975
a0d7a4ff 15976 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 15977 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 15978 = ((struct decl_field *)
be2daae6
TT
15979 TYPE_ALLOC (type,
15980 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15981 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 15982
be2daae6
TT
15983 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15984 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 15985 }
c767944b 15986
883fd55a
KS
15987 /* Copy fi.nested_types_list linked list elements content into the
15988 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 15989 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 15990 {
be2daae6 15991 int count = fi.nested_types_list.size ();
883fd55a
KS
15992
15993 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15994 TYPE_NESTED_TYPES_ARRAY (type)
15995 = ((struct decl_field *)
be2daae6
TT
15996 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15997 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 15998
be2daae6
TT
15999 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16000 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 16001 }
c906108c 16002 }
63d06c5c 16003
bb5ed363 16004 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
16005 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16006 cu->rust_unions.push_back (type);
0b92b5bb 16007
90aeadfc
DC
16008 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16009 snapshots) has been known to create a die giving a declaration
16010 for a class that has, as a child, a die giving a definition for a
16011 nested class. So we have to process our children even if the
16012 current die is a declaration. Normally, of course, a declaration
16013 won't have any children at all. */
134d01f1 16014
ca040673
DE
16015 child_die = die->child;
16016
90aeadfc
DC
16017 while (child_die != NULL && child_die->tag)
16018 {
16019 if (child_die->tag == DW_TAG_member
16020 || child_die->tag == DW_TAG_variable
34eaf542
TT
16021 || child_die->tag == DW_TAG_inheritance
16022 || child_die->tag == DW_TAG_template_value_param
16023 || child_die->tag == DW_TAG_template_type_param)
134d01f1 16024 {
90aeadfc 16025 /* Do nothing. */
134d01f1 16026 }
90aeadfc
DC
16027 else
16028 process_die (child_die, cu);
134d01f1 16029
90aeadfc 16030 child_die = sibling_die (child_die);
134d01f1
DJ
16031 }
16032
fa4028e9
JB
16033 /* Do not consider external references. According to the DWARF standard,
16034 these DIEs are identified by the fact that they have no byte_size
16035 attribute, and a declaration attribute. */
16036 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
16037 || !die_is_declaration (die, cu))
c767944b 16038 new_symbol (die, type, cu);
134d01f1
DJ
16039}
16040
55426c9d
JB
16041/* Assuming DIE is an enumeration type, and TYPE is its associated type,
16042 update TYPE using some information only available in DIE's children. */
16043
16044static void
16045update_enumeration_type_from_children (struct die_info *die,
16046 struct type *type,
16047 struct dwarf2_cu *cu)
16048{
60f7655a 16049 struct die_info *child_die;
55426c9d
JB
16050 int unsigned_enum = 1;
16051 int flag_enum = 1;
16052 ULONGEST mask = 0;
55426c9d 16053
8268c778 16054 auto_obstack obstack;
55426c9d 16055
60f7655a
DE
16056 for (child_die = die->child;
16057 child_die != NULL && child_die->tag;
16058 child_die = sibling_die (child_die))
55426c9d
JB
16059 {
16060 struct attribute *attr;
16061 LONGEST value;
16062 const gdb_byte *bytes;
16063 struct dwarf2_locexpr_baton *baton;
16064 const char *name;
60f7655a 16065
55426c9d
JB
16066 if (child_die->tag != DW_TAG_enumerator)
16067 continue;
16068
16069 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16070 if (attr == NULL)
16071 continue;
16072
16073 name = dwarf2_name (child_die, cu);
16074 if (name == NULL)
16075 name = "<anonymous enumerator>";
16076
16077 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16078 &value, &bytes, &baton);
16079 if (value < 0)
16080 {
16081 unsigned_enum = 0;
16082 flag_enum = 0;
16083 }
16084 else if ((mask & value) != 0)
16085 flag_enum = 0;
16086 else
16087 mask |= value;
16088
16089 /* If we already know that the enum type is neither unsigned, nor
16090 a flag type, no need to look at the rest of the enumerates. */
16091 if (!unsigned_enum && !flag_enum)
16092 break;
55426c9d
JB
16093 }
16094
16095 if (unsigned_enum)
16096 TYPE_UNSIGNED (type) = 1;
16097 if (flag_enum)
16098 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16099}
16100
134d01f1
DJ
16101/* Given a DW_AT_enumeration_type die, set its type. We do not
16102 complete the type's fields yet, or create any symbols. */
c906108c 16103
f792889a 16104static struct type *
134d01f1 16105read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16106{
518817b3 16107 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16108 struct type *type;
c906108c 16109 struct attribute *attr;
0114d602 16110 const char *name;
134d01f1 16111
348e048f
DE
16112 /* If the definition of this type lives in .debug_types, read that type.
16113 Don't follow DW_AT_specification though, that will take us back up
16114 the chain and we want to go down. */
45e58e77 16115 attr = dwarf2_attr_no_follow (die, DW_AT_signature);
348e048f
DE
16116 if (attr)
16117 {
ac9ec31b 16118 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16119
ac9ec31b 16120 /* The type's CU may not be the same as CU.
02142a6c 16121 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16122 return set_die_type (die, type, cu);
16123 }
16124
c906108c
SS
16125 type = alloc_type (objfile);
16126
16127 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 16128 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16129 if (name != NULL)
e86ca25f 16130 TYPE_NAME (type) = name;
c906108c 16131
0626fc76
TT
16132 attr = dwarf2_attr (die, DW_AT_type, cu);
16133 if (attr != NULL)
16134 {
16135 struct type *underlying_type = die_type (die, cu);
16136
16137 TYPE_TARGET_TYPE (type) = underlying_type;
16138 }
16139
e142c38c 16140 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
16141 if (attr)
16142 {
16143 TYPE_LENGTH (type) = DW_UNSND (attr);
16144 }
16145 else
16146 {
16147 TYPE_LENGTH (type) = 0;
16148 }
16149
2b4424c3
TT
16150 maybe_set_alignment (cu, die, type);
16151
137033e9
JB
16152 /* The enumeration DIE can be incomplete. In Ada, any type can be
16153 declared as private in the package spec, and then defined only
16154 inside the package body. Such types are known as Taft Amendment
16155 Types. When another package uses such a type, an incomplete DIE
16156 may be generated by the compiler. */
02eb380e 16157 if (die_is_declaration (die, cu))
876cecd0 16158 TYPE_STUB (type) = 1;
02eb380e 16159
0626fc76
TT
16160 /* Finish the creation of this type by using the enum's children.
16161 We must call this even when the underlying type has been provided
16162 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
16163 update_enumeration_type_from_children (die, type, cu);
16164
0626fc76
TT
16165 /* If this type has an underlying type that is not a stub, then we
16166 may use its attributes. We always use the "unsigned" attribute
16167 in this situation, because ordinarily we guess whether the type
16168 is unsigned -- but the guess can be wrong and the underlying type
16169 can tell us the reality. However, we defer to a local size
16170 attribute if one exists, because this lets the compiler override
16171 the underlying type if needed. */
16172 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16173 {
16174 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
16175 if (TYPE_LENGTH (type) == 0)
16176 TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
2b4424c3
TT
16177 if (TYPE_RAW_ALIGN (type) == 0
16178 && TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)) != 0)
16179 set_type_align (type, TYPE_RAW_ALIGN (TYPE_TARGET_TYPE (type)));
0626fc76
TT
16180 }
16181
3d567982
TT
16182 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16183
f792889a 16184 return set_die_type (die, type, cu);
134d01f1
DJ
16185}
16186
16187/* Given a pointer to a die which begins an enumeration, process all
16188 the dies that define the members of the enumeration, and create the
16189 symbol for the enumeration type.
16190
16191 NOTE: We reverse the order of the element list. */
16192
16193static void
16194process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16195{
f792889a 16196 struct type *this_type;
134d01f1 16197
f792889a
DJ
16198 this_type = get_die_type (die, cu);
16199 if (this_type == NULL)
16200 this_type = read_enumeration_type (die, cu);
9dc481d3 16201
639d11d3 16202 if (die->child != NULL)
c906108c 16203 {
9dc481d3
DE
16204 struct die_info *child_die;
16205 struct symbol *sym;
16206 struct field *fields = NULL;
16207 int num_fields = 0;
15d034d0 16208 const char *name;
9dc481d3 16209
639d11d3 16210 child_die = die->child;
c906108c
SS
16211 while (child_die && child_die->tag)
16212 {
16213 if (child_die->tag != DW_TAG_enumerator)
16214 {
e7c27a73 16215 process_die (child_die, cu);
c906108c
SS
16216 }
16217 else
16218 {
39cbfefa
DJ
16219 name = dwarf2_name (child_die, cu);
16220 if (name)
c906108c 16221 {
f792889a 16222 sym = new_symbol (child_die, this_type, cu);
c906108c
SS
16223
16224 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
16225 {
16226 fields = (struct field *)
16227 xrealloc (fields,
16228 (num_fields + DW_FIELD_ALLOC_CHUNK)
c5aa993b 16229 * sizeof (struct field));
c906108c
SS
16230 }
16231
3567439c 16232 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
c906108c 16233 FIELD_TYPE (fields[num_fields]) = NULL;
14e75d8e 16234 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
c906108c
SS
16235 FIELD_BITSIZE (fields[num_fields]) = 0;
16236
16237 num_fields++;
16238 }
16239 }
16240
16241 child_die = sibling_die (child_die);
16242 }
16243
16244 if (num_fields)
16245 {
f792889a
DJ
16246 TYPE_NFIELDS (this_type) = num_fields;
16247 TYPE_FIELDS (this_type) = (struct field *)
16248 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
16249 memcpy (TYPE_FIELDS (this_type), fields,
c906108c 16250 sizeof (struct field) * num_fields);
b8c9b27d 16251 xfree (fields);
c906108c 16252 }
c906108c 16253 }
134d01f1 16254
6c83ed52
TT
16255 /* If we are reading an enum from a .debug_types unit, and the enum
16256 is a declaration, and the enum is not the signatured type in the
16257 unit, then we do not want to add a symbol for it. Adding a
16258 symbol would in some cases obscure the true definition of the
16259 enum, giving users an incomplete type when the definition is
16260 actually available. Note that we do not want to do this for all
16261 enums which are just declarations, because C++0x allows forward
16262 enum declarations. */
3019eac3 16263 if (cu->per_cu->is_debug_types
6c83ed52
TT
16264 && die_is_declaration (die, cu))
16265 {
52dc124a 16266 struct signatured_type *sig_type;
6c83ed52 16267
c0f78cd4 16268 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16269 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16270 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16271 return;
16272 }
16273
f792889a 16274 new_symbol (die, this_type, cu);
c906108c
SS
16275}
16276
16277/* Extract all information from a DW_TAG_array_type DIE and put it in
16278 the DIE's type field. For now, this only handles one dimensional
16279 arrays. */
16280
f792889a 16281static struct type *
e7c27a73 16282read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16283{
518817b3 16284 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16285 struct die_info *child_die;
7e314c57 16286 struct type *type;
c906108c 16287 struct type *element_type, *range_type, *index_type;
c906108c 16288 struct attribute *attr;
15d034d0 16289 const char *name;
a405673c 16290 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16291 unsigned int bit_stride = 0;
c906108c 16292
e7c27a73 16293 element_type = die_type (die, cu);
c906108c 16294
7e314c57
JK
16295 /* The die_type call above may have already set the type for this DIE. */
16296 type = get_die_type (die, cu);
16297 if (type)
16298 return type;
16299
dc53a7ad
JB
16300 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16301 if (attr != NULL)
a405673c
JB
16302 {
16303 int stride_ok;
16304
16305 byte_stride_prop
16306 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
16307 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop);
16308 if (!stride_ok)
16309 {
b98664d3 16310 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16311 " - DIE at %s [in module %s]"),
16312 sect_offset_str (die->sect_off),
518817b3 16313 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a405673c
JB
16314 /* Ignore this attribute. We will likely not be able to print
16315 arrays of this type correctly, but there is little we can do
16316 to help if we cannot read the attribute's value. */
16317 byte_stride_prop = NULL;
16318 }
16319 }
dc53a7ad
JB
16320
16321 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16322 if (attr != NULL)
16323 bit_stride = DW_UNSND (attr);
16324
c906108c
SS
16325 /* Irix 6.2 native cc creates array types without children for
16326 arrays with unspecified length. */
639d11d3 16327 if (die->child == NULL)
c906108c 16328 {
46bf5051 16329 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16330 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16331 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16332 byte_stride_prop, bit_stride);
f792889a 16333 return set_die_type (die, type, cu);
c906108c
SS
16334 }
16335
791afaa2 16336 std::vector<struct type *> range_types;
639d11d3 16337 child_die = die->child;
c906108c
SS
16338 while (child_die && child_die->tag)
16339 {
16340 if (child_die->tag == DW_TAG_subrange_type)
16341 {
f792889a 16342 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16343
f792889a 16344 if (child_type != NULL)
a02abb62 16345 {
0963b4bd
MS
16346 /* The range type was succesfully read. Save it for the
16347 array type creation. */
791afaa2 16348 range_types.push_back (child_type);
a02abb62 16349 }
c906108c
SS
16350 }
16351 child_die = sibling_die (child_die);
16352 }
16353
16354 /* Dwarf2 dimensions are output from left to right, create the
16355 necessary array types in backwards order. */
7ca2d3a3 16356
c906108c 16357 type = element_type;
7ca2d3a3
DL
16358
16359 if (read_array_order (die, cu) == DW_ORD_col_major)
16360 {
16361 int i = 0;
9a619af0 16362
791afaa2 16363 while (i < range_types.size ())
dc53a7ad 16364 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16365 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16366 }
16367 else
16368 {
791afaa2 16369 size_t ndim = range_types.size ();
7ca2d3a3 16370 while (ndim-- > 0)
dc53a7ad 16371 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16372 byte_stride_prop, bit_stride);
7ca2d3a3 16373 }
c906108c 16374
f5f8a009
EZ
16375 /* Understand Dwarf2 support for vector types (like they occur on
16376 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16377 array type. This is not part of the Dwarf2/3 standard yet, but a
16378 custom vendor extension. The main difference between a regular
16379 array and the vector variant is that vectors are passed by value
16380 to functions. */
e142c38c 16381 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
f5f8a009 16382 if (attr)
ea37ba09 16383 make_vector_type (type);
f5f8a009 16384
dbc98a8b
KW
16385 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16386 implementation may choose to implement triple vectors using this
16387 attribute. */
16388 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
16389 if (attr)
16390 {
16391 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16392 TYPE_LENGTH (type) = DW_UNSND (attr);
16393 else
b98664d3 16394 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16395 "than the total size of elements"));
dbc98a8b
KW
16396 }
16397
39cbfefa
DJ
16398 name = dwarf2_name (die, cu);
16399 if (name)
16400 TYPE_NAME (type) = name;
6e70227d 16401
2b4424c3
TT
16402 maybe_set_alignment (cu, die, type);
16403
0963b4bd 16404 /* Install the type in the die. */
7e314c57
JK
16405 set_die_type (die, type, cu);
16406
16407 /* set_die_type should be already done. */
b4ba55a1
JB
16408 set_descriptive_type (type, die, cu);
16409
7e314c57 16410 return type;
c906108c
SS
16411}
16412
7ca2d3a3 16413static enum dwarf_array_dim_ordering
6e70227d 16414read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16415{
16416 struct attribute *attr;
16417
16418 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16419
aead7601
SM
16420 if (attr)
16421 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16422
0963b4bd
MS
16423 /* GNU F77 is a special case, as at 08/2004 array type info is the
16424 opposite order to the dwarf2 specification, but data is still
16425 laid out as per normal fortran.
7ca2d3a3 16426
0963b4bd
MS
16427 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16428 version checking. */
7ca2d3a3 16429
905e0470
PM
16430 if (cu->language == language_fortran
16431 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16432 {
16433 return DW_ORD_row_major;
16434 }
16435
6e70227d 16436 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16437 {
16438 case array_column_major:
16439 return DW_ORD_col_major;
16440 case array_row_major:
16441 default:
16442 return DW_ORD_row_major;
16443 };
16444}
16445
72019c9c 16446/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16447 the DIE's type field. */
72019c9c 16448
f792889a 16449static struct type *
72019c9c
GM
16450read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16451{
7e314c57
JK
16452 struct type *domain_type, *set_type;
16453 struct attribute *attr;
f792889a 16454
7e314c57
JK
16455 domain_type = die_type (die, cu);
16456
16457 /* The die_type call above may have already set the type for this DIE. */
16458 set_type = get_die_type (die, cu);
16459 if (set_type)
16460 return set_type;
16461
16462 set_type = create_set_type (NULL, domain_type);
16463
16464 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
d09039dd
PM
16465 if (attr)
16466 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16467
2b4424c3
TT
16468 maybe_set_alignment (cu, die, set_type);
16469
f792889a 16470 return set_die_type (die, set_type, cu);
72019c9c 16471}
7ca2d3a3 16472
0971de02
TT
16473/* A helper for read_common_block that creates a locexpr baton.
16474 SYM is the symbol which we are marking as computed.
16475 COMMON_DIE is the DIE for the common block.
16476 COMMON_LOC is the location expression attribute for the common
16477 block itself.
16478 MEMBER_LOC is the location expression attribute for the particular
16479 member of the common block that we are processing.
16480 CU is the CU from which the above come. */
16481
16482static void
16483mark_common_block_symbol_computed (struct symbol *sym,
16484 struct die_info *common_die,
16485 struct attribute *common_loc,
16486 struct attribute *member_loc,
16487 struct dwarf2_cu *cu)
16488{
518817b3
SM
16489 struct dwarf2_per_objfile *dwarf2_per_objfile
16490 = cu->per_cu->dwarf2_per_objfile;
0971de02
TT
16491 struct objfile *objfile = dwarf2_per_objfile->objfile;
16492 struct dwarf2_locexpr_baton *baton;
16493 gdb_byte *ptr;
16494 unsigned int cu_off;
16495 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
16496 LONGEST offset = 0;
16497
16498 gdb_assert (common_loc && member_loc);
16499 gdb_assert (attr_form_is_block (common_loc));
16500 gdb_assert (attr_form_is_block (member_loc)
16501 || attr_form_is_constant (member_loc));
16502
8d749320 16503 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
16504 baton->per_cu = cu->per_cu;
16505 gdb_assert (baton->per_cu);
16506
16507 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16508
16509 if (attr_form_is_constant (member_loc))
16510 {
16511 offset = dwarf2_get_attr_constant_value (member_loc, 0);
16512 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16513 }
16514 else
16515 baton->size += DW_BLOCK (member_loc)->size;
16516
224c3ddb 16517 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16518 baton->data = ptr;
16519
16520 *ptr++ = DW_OP_call4;
9c541725 16521 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16522 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16523 ptr += 4;
16524
16525 if (attr_form_is_constant (member_loc))
16526 {
16527 *ptr++ = DW_OP_addr;
16528 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16529 ptr += cu->header.addr_size;
16530 }
16531 else
16532 {
16533 /* We have to copy the data here, because DW_OP_call4 will only
16534 use a DW_AT_location attribute. */
16535 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16536 ptr += DW_BLOCK (member_loc)->size;
16537 }
16538
16539 *ptr++ = DW_OP_plus;
16540 gdb_assert (ptr - baton->data == baton->size);
16541
0971de02 16542 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16543 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16544}
16545
4357ac6c
TT
16546/* Create appropriate locally-scoped variables for all the
16547 DW_TAG_common_block entries. Also create a struct common_block
16548 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
16549 is used to sepate the common blocks name namespace from regular
16550 variable names. */
c906108c
SS
16551
16552static void
e7c27a73 16553read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16554{
0971de02
TT
16555 struct attribute *attr;
16556
16557 attr = dwarf2_attr (die, DW_AT_location, cu);
16558 if (attr)
16559 {
16560 /* Support the .debug_loc offsets. */
16561 if (attr_form_is_block (attr))
16562 {
16563 /* Ok. */
16564 }
16565 else if (attr_form_is_section_offset (attr))
16566 {
16567 dwarf2_complex_location_expr_complaint ();
16568 attr = NULL;
16569 }
16570 else
16571 {
16572 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16573 "common block member");
16574 attr = NULL;
16575 }
16576 }
16577
639d11d3 16578 if (die->child != NULL)
c906108c 16579 {
518817b3 16580 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
4357ac6c
TT
16581 struct die_info *child_die;
16582 size_t n_entries = 0, size;
16583 struct common_block *common_block;
16584 struct symbol *sym;
74ac6d43 16585
4357ac6c
TT
16586 for (child_die = die->child;
16587 child_die && child_die->tag;
16588 child_die = sibling_die (child_die))
16589 ++n_entries;
16590
16591 size = (sizeof (struct common_block)
16592 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16593 common_block
16594 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16595 size);
4357ac6c
TT
16596 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16597 common_block->n_entries = 0;
16598
16599 for (child_die = die->child;
16600 child_die && child_die->tag;
16601 child_die = sibling_die (child_die))
16602 {
16603 /* Create the symbol in the DW_TAG_common_block block in the current
16604 symbol scope. */
e7c27a73 16605 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16606 if (sym != NULL)
16607 {
16608 struct attribute *member_loc;
16609
16610 common_block->contents[common_block->n_entries++] = sym;
16611
16612 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16613 cu);
16614 if (member_loc)
16615 {
16616 /* GDB has handled this for a long time, but it is
16617 not specified by DWARF. It seems to have been
16618 emitted by gfortran at least as recently as:
16619 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16620 complaint (_("Variable in common block has "
0971de02 16621 "DW_AT_data_member_location "
9d8780f0
SM
16622 "- DIE at %s [in module %s]"),
16623 sect_offset_str (child_die->sect_off),
518817b3 16624 objfile_name (objfile));
0971de02
TT
16625
16626 if (attr_form_is_section_offset (member_loc))
16627 dwarf2_complex_location_expr_complaint ();
16628 else if (attr_form_is_constant (member_loc)
16629 || attr_form_is_block (member_loc))
16630 {
16631 if (attr)
16632 mark_common_block_symbol_computed (sym, die, attr,
16633 member_loc, cu);
16634 }
16635 else
16636 dwarf2_complex_location_expr_complaint ();
16637 }
16638 }
c906108c 16639 }
4357ac6c
TT
16640
16641 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16642 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16643 }
16644}
16645
0114d602 16646/* Create a type for a C++ namespace. */
d9fa45fe 16647
0114d602
DJ
16648static struct type *
16649read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16650{
518817b3 16651 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16652 const char *previous_prefix, *name;
9219021c 16653 int is_anonymous;
0114d602
DJ
16654 struct type *type;
16655
16656 /* For extensions, reuse the type of the original namespace. */
16657 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16658 {
16659 struct die_info *ext_die;
16660 struct dwarf2_cu *ext_cu = cu;
9a619af0 16661
0114d602
DJ
16662 ext_die = dwarf2_extension (die, &ext_cu);
16663 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16664
16665 /* EXT_CU may not be the same as CU.
02142a6c 16666 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16667 return set_die_type (die, type, cu);
16668 }
9219021c 16669
e142c38c 16670 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16671
16672 /* Now build the name of the current namespace. */
16673
0114d602
DJ
16674 previous_prefix = determine_prefix (die, cu);
16675 if (previous_prefix[0] != '\0')
16676 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16677 previous_prefix, name, 0, cu);
0114d602
DJ
16678
16679 /* Create the type. */
19f392bc 16680 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16681
60531b24 16682 return set_die_type (die, type, cu);
0114d602
DJ
16683}
16684
22cee43f 16685/* Read a namespace scope. */
0114d602
DJ
16686
16687static void
16688read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16689{
518817b3 16690 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16691 int is_anonymous;
9219021c 16692
5c4e30ca
DC
16693 /* Add a symbol associated to this if we haven't seen the namespace
16694 before. Also, add a using directive if it's an anonymous
16695 namespace. */
9219021c 16696
f2f0e013 16697 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16698 {
16699 struct type *type;
16700
0114d602 16701 type = read_type_die (die, cu);
e7c27a73 16702 new_symbol (die, type, cu);
5c4e30ca 16703
e8e80198 16704 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16705 if (is_anonymous)
0114d602
DJ
16706 {
16707 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16708
eb1e02fd 16709 std::vector<const char *> excludes;
22cee43f
PMR
16710 add_using_directive (using_directives (cu->language),
16711 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 16712 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16713 }
5c4e30ca 16714 }
9219021c 16715
639d11d3 16716 if (die->child != NULL)
d9fa45fe 16717 {
639d11d3 16718 struct die_info *child_die = die->child;
6e70227d 16719
d9fa45fe
DC
16720 while (child_die && child_die->tag)
16721 {
e7c27a73 16722 process_die (child_die, cu);
d9fa45fe
DC
16723 child_die = sibling_die (child_die);
16724 }
16725 }
38d518c9
EZ
16726}
16727
f55ee35c
JK
16728/* Read a Fortran module as type. This DIE can be only a declaration used for
16729 imported module. Still we need that type as local Fortran "use ... only"
16730 declaration imports depend on the created type in determine_prefix. */
16731
16732static struct type *
16733read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16734{
518817b3 16735 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15d034d0 16736 const char *module_name;
f55ee35c
JK
16737 struct type *type;
16738
16739 module_name = dwarf2_name (die, cu);
16740 if (!module_name)
b98664d3 16741 complaint (_("DW_TAG_module has no name, offset %s"),
9d8780f0 16742 sect_offset_str (die->sect_off));
19f392bc 16743 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 16744
f55ee35c
JK
16745 return set_die_type (die, type, cu);
16746}
16747
5d7cb8df
JK
16748/* Read a Fortran module. */
16749
16750static void
16751read_module (struct die_info *die, struct dwarf2_cu *cu)
16752{
16753 struct die_info *child_die = die->child;
530e8392
KB
16754 struct type *type;
16755
16756 type = read_type_die (die, cu);
16757 new_symbol (die, type, cu);
5d7cb8df 16758
5d7cb8df
JK
16759 while (child_die && child_die->tag)
16760 {
16761 process_die (child_die, cu);
16762 child_die = sibling_die (child_die);
16763 }
16764}
16765
38d518c9
EZ
16766/* Return the name of the namespace represented by DIE. Set
16767 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16768 namespace. */
16769
16770static const char *
e142c38c 16771namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16772{
16773 struct die_info *current_die;
16774 const char *name = NULL;
16775
16776 /* Loop through the extensions until we find a name. */
16777
16778 for (current_die = die;
16779 current_die != NULL;
f2f0e013 16780 current_die = dwarf2_extension (die, &cu))
38d518c9 16781 {
96553a0c
DE
16782 /* We don't use dwarf2_name here so that we can detect the absence
16783 of a name -> anonymous namespace. */
7d45c7c3 16784 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16785
38d518c9
EZ
16786 if (name != NULL)
16787 break;
16788 }
16789
16790 /* Is it an anonymous namespace? */
16791
16792 *is_anonymous = (name == NULL);
16793 if (*is_anonymous)
2b1dbab0 16794 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16795
16796 return name;
d9fa45fe
DC
16797}
16798
c906108c
SS
16799/* Extract all information from a DW_TAG_pointer_type DIE and add to
16800 the user defined type vector. */
16801
f792889a 16802static struct type *
e7c27a73 16803read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16804{
518817b3
SM
16805 struct gdbarch *gdbarch
16806 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
e7c27a73 16807 struct comp_unit_head *cu_header = &cu->header;
c906108c 16808 struct type *type;
8b2dbe47
KB
16809 struct attribute *attr_byte_size;
16810 struct attribute *attr_address_class;
16811 int byte_size, addr_class;
7e314c57
JK
16812 struct type *target_type;
16813
16814 target_type = die_type (die, cu);
c906108c 16815
7e314c57
JK
16816 /* The die_type call above may have already set the type for this DIE. */
16817 type = get_die_type (die, cu);
16818 if (type)
16819 return type;
16820
16821 type = lookup_pointer_type (target_type);
8b2dbe47 16822
e142c38c 16823 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
16824 if (attr_byte_size)
16825 byte_size = DW_UNSND (attr_byte_size);
c906108c 16826 else
8b2dbe47
KB
16827 byte_size = cu_header->addr_size;
16828
e142c38c 16829 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
16830 if (attr_address_class)
16831 addr_class = DW_UNSND (attr_address_class);
16832 else
16833 addr_class = DW_ADDR_none;
16834
2b4424c3
TT
16835 ULONGEST alignment = get_alignment (cu, die);
16836
16837 /* If the pointer size, alignment, or address class is different
16838 than the default, create a type variant marked as such and set
16839 the length accordingly. */
16840 if (TYPE_LENGTH (type) != byte_size
16841 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16842 && alignment != TYPE_RAW_ALIGN (type))
16843 || addr_class != DW_ADDR_none)
c906108c 16844 {
5e2b427d 16845 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
16846 {
16847 int type_flags;
16848
849957d9 16849 type_flags = gdbarch_address_class_type_flags
5e2b427d 16850 (gdbarch, byte_size, addr_class);
876cecd0
TT
16851 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16852 == 0);
8b2dbe47
KB
16853 type = make_type_with_address_space (type, type_flags);
16854 }
16855 else if (TYPE_LENGTH (type) != byte_size)
16856 {
b98664d3 16857 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 16858 }
2b4424c3
TT
16859 else if (TYPE_RAW_ALIGN (type) != alignment)
16860 {
b98664d3 16861 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
16862 " - DIE at %s [in module %s]"),
16863 sect_offset_str (die->sect_off),
16864 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16865 }
6e70227d 16866 else
9a619af0
MS
16867 {
16868 /* Should we also complain about unhandled address classes? */
16869 }
c906108c 16870 }
8b2dbe47
KB
16871
16872 TYPE_LENGTH (type) = byte_size;
2b4424c3 16873 set_type_align (type, alignment);
f792889a 16874 return set_die_type (die, type, cu);
c906108c
SS
16875}
16876
16877/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16878 the user defined type vector. */
16879
f792889a 16880static struct type *
e7c27a73 16881read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
16882{
16883 struct type *type;
16884 struct type *to_type;
16885 struct type *domain;
16886
e7c27a73
DJ
16887 to_type = die_type (die, cu);
16888 domain = die_containing_type (die, cu);
0d5de010 16889
7e314c57
JK
16890 /* The calls above may have already set the type for this DIE. */
16891 type = get_die_type (die, cu);
16892 if (type)
16893 return type;
16894
0d5de010
DJ
16895 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16896 type = lookup_methodptr_type (to_type);
7078baeb
TT
16897 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16898 {
518817b3
SM
16899 struct type *new_type
16900 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
7078baeb
TT
16901
16902 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16903 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16904 TYPE_VARARGS (to_type));
16905 type = lookup_methodptr_type (new_type);
16906 }
0d5de010
DJ
16907 else
16908 type = lookup_memberptr_type (to_type, domain);
c906108c 16909
f792889a 16910 return set_die_type (die, type, cu);
c906108c
SS
16911}
16912
4297a3f0 16913/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
16914 the user defined type vector. */
16915
f792889a 16916static struct type *
4297a3f0
AV
16917read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16918 enum type_code refcode)
c906108c 16919{
e7c27a73 16920 struct comp_unit_head *cu_header = &cu->header;
7e314c57 16921 struct type *type, *target_type;
c906108c
SS
16922 struct attribute *attr;
16923
4297a3f0
AV
16924 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16925
7e314c57
JK
16926 target_type = die_type (die, cu);
16927
16928 /* The die_type call above may have already set the type for this DIE. */
16929 type = get_die_type (die, cu);
16930 if (type)
16931 return type;
16932
4297a3f0 16933 type = lookup_reference_type (target_type, refcode);
e142c38c 16934 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
16935 if (attr)
16936 {
16937 TYPE_LENGTH (type) = DW_UNSND (attr);
16938 }
16939 else
16940 {
107d2387 16941 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 16942 }
2b4424c3 16943 maybe_set_alignment (cu, die, type);
f792889a 16944 return set_die_type (die, type, cu);
c906108c
SS
16945}
16946
cf363f18
MW
16947/* Add the given cv-qualifiers to the element type of the array. GCC
16948 outputs DWARF type qualifiers that apply to an array, not the
16949 element type. But GDB relies on the array element type to carry
16950 the cv-qualifiers. This mimics section 6.7.3 of the C99
16951 specification. */
16952
16953static struct type *
16954add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16955 struct type *base_type, int cnst, int voltl)
16956{
16957 struct type *el_type, *inner_array;
16958
16959 base_type = copy_type (base_type);
16960 inner_array = base_type;
16961
16962 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16963 {
16964 TYPE_TARGET_TYPE (inner_array) =
16965 copy_type (TYPE_TARGET_TYPE (inner_array));
16966 inner_array = TYPE_TARGET_TYPE (inner_array);
16967 }
16968
16969 el_type = TYPE_TARGET_TYPE (inner_array);
16970 cnst |= TYPE_CONST (el_type);
16971 voltl |= TYPE_VOLATILE (el_type);
16972 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16973
16974 return set_die_type (die, base_type, cu);
16975}
16976
f792889a 16977static struct type *
e7c27a73 16978read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16979{
f792889a 16980 struct type *base_type, *cv_type;
c906108c 16981
e7c27a73 16982 base_type = die_type (die, cu);
7e314c57
JK
16983
16984 /* The die_type call above may have already set the type for this DIE. */
16985 cv_type = get_die_type (die, cu);
16986 if (cv_type)
16987 return cv_type;
16988
2f608a3a
KW
16989 /* In case the const qualifier is applied to an array type, the element type
16990 is so qualified, not the array type (section 6.7.3 of C99). */
16991 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 16992 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 16993
f792889a
DJ
16994 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16995 return set_die_type (die, cv_type, cu);
c906108c
SS
16996}
16997
f792889a 16998static struct type *
e7c27a73 16999read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17000{
f792889a 17001 struct type *base_type, *cv_type;
c906108c 17002
e7c27a73 17003 base_type = die_type (die, cu);
7e314c57
JK
17004
17005 /* The die_type call above may have already set the type for this DIE. */
17006 cv_type = get_die_type (die, cu);
17007 if (cv_type)
17008 return cv_type;
17009
cf363f18
MW
17010 /* In case the volatile qualifier is applied to an array type, the
17011 element type is so qualified, not the array type (section 6.7.3
17012 of C99). */
17013 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
17014 return add_array_cv_type (die, cu, base_type, 0, 1);
17015
f792889a
DJ
17016 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17017 return set_die_type (die, cv_type, cu);
c906108c
SS
17018}
17019
06d66ee9
TT
17020/* Handle DW_TAG_restrict_type. */
17021
17022static struct type *
17023read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17024{
17025 struct type *base_type, *cv_type;
17026
17027 base_type = die_type (die, cu);
17028
17029 /* The die_type call above may have already set the type for this DIE. */
17030 cv_type = get_die_type (die, cu);
17031 if (cv_type)
17032 return cv_type;
17033
17034 cv_type = make_restrict_type (base_type);
17035 return set_die_type (die, cv_type, cu);
17036}
17037
a2c2acaf
MW
17038/* Handle DW_TAG_atomic_type. */
17039
17040static struct type *
17041read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17042{
17043 struct type *base_type, *cv_type;
17044
17045 base_type = die_type (die, cu);
17046
17047 /* The die_type call above may have already set the type for this DIE. */
17048 cv_type = get_die_type (die, cu);
17049 if (cv_type)
17050 return cv_type;
17051
17052 cv_type = make_atomic_type (base_type);
17053 return set_die_type (die, cv_type, cu);
17054}
17055
c906108c
SS
17056/* Extract all information from a DW_TAG_string_type DIE and add to
17057 the user defined type vector. It isn't really a user defined type,
17058 but it behaves like one, with other DIE's using an AT_user_def_type
17059 attribute to reference it. */
17060
f792889a 17061static struct type *
e7c27a73 17062read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17063{
518817b3 17064 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3b7538c0 17065 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
17066 struct type *type, *range_type, *index_type, *char_type;
17067 struct attribute *attr;
17068 unsigned int length;
17069
e142c38c 17070 attr = dwarf2_attr (die, DW_AT_string_length, cu);
c906108c
SS
17071 if (attr)
17072 {
17073 length = DW_UNSND (attr);
17074 }
17075 else
17076 {
0963b4bd 17077 /* Check for the DW_AT_byte_size attribute. */
e142c38c 17078 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
b21b22e0
PS
17079 if (attr)
17080 {
17081 length = DW_UNSND (attr);
17082 }
17083 else
17084 {
17085 length = 1;
17086 }
c906108c 17087 }
6ccb9162 17088
46bf5051 17089 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 17090 range_type = create_static_range_type (NULL, index_type, 1, length);
3b7538c0
UW
17091 char_type = language_string_char_type (cu->language_defn, gdbarch);
17092 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17093
f792889a 17094 return set_die_type (die, type, cu);
c906108c
SS
17095}
17096
4d804846
JB
17097/* Assuming that DIE corresponds to a function, returns nonzero
17098 if the function is prototyped. */
17099
17100static int
17101prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17102{
17103 struct attribute *attr;
17104
17105 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17106 if (attr && (DW_UNSND (attr) != 0))
17107 return 1;
17108
17109 /* The DWARF standard implies that the DW_AT_prototyped attribute
17110 is only meaninful for C, but the concept also extends to other
17111 languages that allow unprototyped functions (Eg: Objective C).
17112 For all other languages, assume that functions are always
17113 prototyped. */
17114 if (cu->language != language_c
17115 && cu->language != language_objc
17116 && cu->language != language_opencl)
17117 return 1;
17118
17119 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17120 prototyped and unprototyped functions; default to prototyped,
17121 since that is more common in modern code (and RealView warns
17122 about unprototyped functions). */
17123 if (producer_is_realview (cu->producer))
17124 return 1;
17125
17126 return 0;
17127}
17128
c906108c
SS
17129/* Handle DIES due to C code like:
17130
17131 struct foo
c5aa993b
JM
17132 {
17133 int (*funcp)(int a, long l);
17134 int b;
17135 };
c906108c 17136
0963b4bd 17137 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17138
f792889a 17139static struct type *
e7c27a73 17140read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17141{
518817b3 17142 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0963b4bd
MS
17143 struct type *type; /* Type that this function returns. */
17144 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17145 struct attribute *attr;
17146
e7c27a73 17147 type = die_type (die, cu);
7e314c57
JK
17148
17149 /* The die_type call above may have already set the type for this DIE. */
17150 ftype = get_die_type (die, cu);
17151 if (ftype)
17152 return ftype;
17153
0c8b41f1 17154 ftype = lookup_function_type (type);
c906108c 17155
4d804846 17156 if (prototyped_function_p (die, cu))
a6c727b2 17157 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17158
c055b101
CV
17159 /* Store the calling convention in the type if it's available in
17160 the subroutine die. Otherwise set the calling convention to
17161 the default value DW_CC_normal. */
17162 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
54fcddd0
UW
17163 if (attr)
17164 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
17165 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17166 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17167 else
17168 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17169
743649fd
MW
17170 /* Record whether the function returns normally to its caller or not
17171 if the DWARF producer set that information. */
17172 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17173 if (attr && (DW_UNSND (attr) != 0))
17174 TYPE_NO_RETURN (ftype) = 1;
17175
76c10ea2
GM
17176 /* We need to add the subroutine type to the die immediately so
17177 we don't infinitely recurse when dealing with parameters
0963b4bd 17178 declared as the same subroutine type. */
76c10ea2 17179 set_die_type (die, ftype, cu);
6e70227d 17180
639d11d3 17181 if (die->child != NULL)
c906108c 17182 {
bb5ed363 17183 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17184 struct die_info *child_die;
8072405b 17185 int nparams, iparams;
c906108c
SS
17186
17187 /* Count the number of parameters.
17188 FIXME: GDB currently ignores vararg functions, but knows about
17189 vararg member functions. */
8072405b 17190 nparams = 0;
639d11d3 17191 child_die = die->child;
c906108c
SS
17192 while (child_die && child_die->tag)
17193 {
17194 if (child_die->tag == DW_TAG_formal_parameter)
17195 nparams++;
17196 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17197 TYPE_VARARGS (ftype) = 1;
c906108c
SS
17198 child_die = sibling_die (child_die);
17199 }
17200
17201 /* Allocate storage for parameters and fill them in. */
17202 TYPE_NFIELDS (ftype) = nparams;
17203 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 17204 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 17205
8072405b
JK
17206 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17207 even if we error out during the parameters reading below. */
17208 for (iparams = 0; iparams < nparams; iparams++)
17209 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17210
17211 iparams = 0;
639d11d3 17212 child_die = die->child;
c906108c
SS
17213 while (child_die && child_die->tag)
17214 {
17215 if (child_die->tag == DW_TAG_formal_parameter)
17216 {
3ce3b1ba
PA
17217 struct type *arg_type;
17218
17219 /* DWARF version 2 has no clean way to discern C++
17220 static and non-static member functions. G++ helps
17221 GDB by marking the first parameter for non-static
17222 member functions (which is the this pointer) as
17223 artificial. We pass this information to
17224 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17225
17226 DWARF version 3 added DW_AT_object_pointer, which GCC
17227 4.5 does not yet generate. */
e142c38c 17228 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
c906108c
SS
17229 if (attr)
17230 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17231 else
9c37b5ae 17232 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17233 arg_type = die_type (child_die, cu);
17234
17235 /* RealView does not mark THIS as const, which the testsuite
17236 expects. GCC marks THIS as const in method definitions,
17237 but not in the class specifications (GCC PR 43053). */
17238 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17239 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17240 {
17241 int is_this = 0;
17242 struct dwarf2_cu *arg_cu = cu;
17243 const char *name = dwarf2_name (child_die, cu);
17244
17245 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
17246 if (attr)
17247 {
17248 /* If the compiler emits this, use it. */
17249 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17250 is_this = 1;
17251 }
17252 else if (name && strcmp (name, "this") == 0)
17253 /* Function definitions will have the argument names. */
17254 is_this = 1;
17255 else if (name == NULL && iparams == 0)
17256 /* Declarations may not have the names, so like
17257 elsewhere in GDB, assume an artificial first
17258 argument is "this". */
17259 is_this = 1;
17260
17261 if (is_this)
17262 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17263 arg_type, 0);
17264 }
17265
17266 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
17267 iparams++;
17268 }
17269 child_die = sibling_die (child_die);
17270 }
17271 }
17272
76c10ea2 17273 return ftype;
c906108c
SS
17274}
17275
f792889a 17276static struct type *
e7c27a73 17277read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17278{
518817b3 17279 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 17280 const char *name = NULL;
3c8e0968 17281 struct type *this_type, *target_type;
c906108c 17282
94af9270 17283 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17284 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17285 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17286 set_die_type (die, this_type, cu);
3c8e0968
DE
17287 target_type = die_type (die, cu);
17288 if (target_type != this_type)
17289 TYPE_TARGET_TYPE (this_type) = target_type;
17290 else
17291 {
17292 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17293 spec and cause infinite loops in GDB. */
b98664d3 17294 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17295 "- DIE at %s [in module %s]"),
17296 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17297 TYPE_TARGET_TYPE (this_type) = NULL;
17298 }
f792889a 17299 return this_type;
c906108c
SS
17300}
17301
9b790ce7
UW
17302/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17303 (which may be different from NAME) to the architecture back-end to allow
17304 it to guess the correct format if necessary. */
17305
17306static struct type *
17307dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
17308 const char *name_hint)
17309{
17310 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17311 const struct floatformat **format;
17312 struct type *type;
17313
17314 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17315 if (format)
17316 type = init_float_type (objfile, bits, name, format);
17317 else
77b7c781 17318 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17319
17320 return type;
17321}
17322
c906108c
SS
17323/* Find a representation of a given base type and install
17324 it in the TYPE field of the die. */
17325
f792889a 17326static struct type *
e7c27a73 17327read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17328{
518817b3 17329 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
17330 struct type *type;
17331 struct attribute *attr;
19f392bc 17332 int encoding = 0, bits = 0;
15d034d0 17333 const char *name;
c906108c 17334
e142c38c 17335 attr = dwarf2_attr (die, DW_AT_encoding, cu);
c906108c
SS
17336 if (attr)
17337 {
17338 encoding = DW_UNSND (attr);
17339 }
e142c38c 17340 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
c906108c
SS
17341 if (attr)
17342 {
19f392bc 17343 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
c906108c 17344 }
39cbfefa 17345 name = dwarf2_name (die, cu);
6ccb9162 17346 if (!name)
c906108c 17347 {
b98664d3 17348 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
c906108c 17349 }
6ccb9162
UW
17350
17351 switch (encoding)
c906108c 17352 {
6ccb9162
UW
17353 case DW_ATE_address:
17354 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17355 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17356 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17357 break;
17358 case DW_ATE_boolean:
19f392bc 17359 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17360 break;
17361 case DW_ATE_complex_float:
9b790ce7 17362 type = dwarf2_init_float_type (objfile, bits / 2, NULL, name);
19f392bc 17363 type = init_complex_type (objfile, name, type);
6ccb9162
UW
17364 break;
17365 case DW_ATE_decimal_float:
19f392bc 17366 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17367 break;
17368 case DW_ATE_float:
9b790ce7 17369 type = dwarf2_init_float_type (objfile, bits, name, name);
6ccb9162
UW
17370 break;
17371 case DW_ATE_signed:
19f392bc 17372 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
17373 break;
17374 case DW_ATE_unsigned:
3b2b8fea
TT
17375 if (cu->language == language_fortran
17376 && name
61012eef 17377 && startswith (name, "character("))
19f392bc
UW
17378 type = init_character_type (objfile, bits, 1, name);
17379 else
17380 type = init_integer_type (objfile, bits, 1, name);
6ccb9162
UW
17381 break;
17382 case DW_ATE_signed_char:
6e70227d 17383 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17384 || cu->language == language_pascal
17385 || cu->language == language_fortran)
19f392bc
UW
17386 type = init_character_type (objfile, bits, 0, name);
17387 else
17388 type = init_integer_type (objfile, bits, 0, name);
6ccb9162
UW
17389 break;
17390 case DW_ATE_unsigned_char:
868a0084 17391 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17392 || cu->language == language_pascal
c44af4eb
TT
17393 || cu->language == language_fortran
17394 || cu->language == language_rust)
19f392bc
UW
17395 type = init_character_type (objfile, bits, 1, name);
17396 else
17397 type = init_integer_type (objfile, bits, 1, name);
6ccb9162 17398 break;
75079b2b 17399 case DW_ATE_UTF:
53e710ac
PA
17400 {
17401 gdbarch *arch = get_objfile_arch (objfile);
17402
17403 if (bits == 16)
17404 type = builtin_type (arch)->builtin_char16;
17405 else if (bits == 32)
17406 type = builtin_type (arch)->builtin_char32;
17407 else
17408 {
b98664d3 17409 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac
PA
17410 bits);
17411 type = init_integer_type (objfile, bits, 1, name);
17412 }
17413 return set_die_type (die, type, cu);
17414 }
75079b2b
TT
17415 break;
17416
6ccb9162 17417 default:
b98664d3 17418 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 17419 dwarf_type_encoding_name (encoding));
77b7c781 17420 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17421 break;
c906108c 17422 }
6ccb9162 17423
0114d602 17424 if (name && strcmp (name, "char") == 0)
876cecd0 17425 TYPE_NOSIGN (type) = 1;
0114d602 17426
2b4424c3
TT
17427 maybe_set_alignment (cu, die, type);
17428
f792889a 17429 return set_die_type (die, type, cu);
c906108c
SS
17430}
17431
80180f79
SA
17432/* Parse dwarf attribute if it's a block, reference or constant and put the
17433 resulting value of the attribute into struct bound_prop.
17434 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17435
17436static int
17437attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
17438 struct dwarf2_cu *cu, struct dynamic_prop *prop)
17439{
17440 struct dwarf2_property_baton *baton;
518817b3
SM
17441 struct obstack *obstack
17442 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
80180f79
SA
17443
17444 if (attr == NULL || prop == NULL)
17445 return 0;
17446
17447 if (attr_form_is_block (attr))
17448 {
8d749320 17449 baton = XOBNEW (obstack, struct dwarf2_property_baton);
80180f79
SA
17450 baton->referenced_type = NULL;
17451 baton->locexpr.per_cu = cu->per_cu;
17452 baton->locexpr.size = DW_BLOCK (attr)->size;
17453 baton->locexpr.data = DW_BLOCK (attr)->data;
17454 prop->data.baton = baton;
17455 prop->kind = PROP_LOCEXPR;
17456 gdb_assert (prop->data.baton != NULL);
17457 }
17458 else if (attr_form_is_ref (attr))
17459 {
17460 struct dwarf2_cu *target_cu = cu;
17461 struct die_info *target_die;
17462 struct attribute *target_attr;
17463
17464 target_die = follow_die_ref (die, attr, &target_cu);
17465 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17466 if (target_attr == NULL)
17467 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17468 target_cu);
80180f79
SA
17469 if (target_attr == NULL)
17470 return 0;
17471
df25ebbd 17472 switch (target_attr->name)
80180f79 17473 {
df25ebbd
JB
17474 case DW_AT_location:
17475 if (attr_form_is_section_offset (target_attr))
17476 {
8d749320 17477 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
17478 baton->referenced_type = die_type (target_die, target_cu);
17479 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17480 prop->data.baton = baton;
17481 prop->kind = PROP_LOCLIST;
17482 gdb_assert (prop->data.baton != NULL);
17483 }
17484 else if (attr_form_is_block (target_attr))
17485 {
8d749320 17486 baton = XOBNEW (obstack, struct dwarf2_property_baton);
df25ebbd
JB
17487 baton->referenced_type = die_type (target_die, target_cu);
17488 baton->locexpr.per_cu = cu->per_cu;
17489 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17490 baton->locexpr.data = DW_BLOCK (target_attr)->data;
17491 prop->data.baton = baton;
17492 prop->kind = PROP_LOCEXPR;
17493 gdb_assert (prop->data.baton != NULL);
17494 }
17495 else
17496 {
17497 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17498 "dynamic property");
17499 return 0;
17500 }
17501 break;
17502 case DW_AT_data_member_location:
17503 {
17504 LONGEST offset;
17505
17506 if (!handle_data_member_location (target_die, target_cu,
17507 &offset))
17508 return 0;
17509
8d749320 17510 baton = XOBNEW (obstack, struct dwarf2_property_baton);
6ad395a7
JB
17511 baton->referenced_type = read_type_die (target_die->parent,
17512 target_cu);
df25ebbd
JB
17513 baton->offset_info.offset = offset;
17514 baton->offset_info.type = die_type (target_die, target_cu);
17515 prop->data.baton = baton;
17516 prop->kind = PROP_ADDR_OFFSET;
17517 break;
17518 }
80180f79
SA
17519 }
17520 }
17521 else if (attr_form_is_constant (attr))
17522 {
17523 prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
17524 prop->kind = PROP_CONST;
17525 }
17526 else
17527 {
17528 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17529 dwarf2_name (die, cu));
17530 return 0;
17531 }
17532
17533 return 1;
17534}
17535
a02abb62
JB
17536/* Read the given DW_AT_subrange DIE. */
17537
f792889a 17538static struct type *
a02abb62
JB
17539read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17540{
4c9ad8c2 17541 struct type *base_type, *orig_base_type;
a02abb62
JB
17542 struct type *range_type;
17543 struct attribute *attr;
729efb13 17544 struct dynamic_prop low, high;
4fae6e18 17545 int low_default_is_valid;
c451ebe5 17546 int high_bound_is_count = 0;
15d034d0 17547 const char *name;
43bbcdc2 17548 LONGEST negative_mask;
e77813c8 17549
4c9ad8c2
TT
17550 orig_base_type = die_type (die, cu);
17551 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17552 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17553 creating the range type, but we use the result of check_typedef
17554 when examining properties of the type. */
17555 base_type = check_typedef (orig_base_type);
a02abb62 17556
7e314c57
JK
17557 /* The die_type call above may have already set the type for this DIE. */
17558 range_type = get_die_type (die, cu);
17559 if (range_type)
17560 return range_type;
17561
729efb13
SA
17562 low.kind = PROP_CONST;
17563 high.kind = PROP_CONST;
17564 high.data.const_val = 0;
17565
4fae6e18
JK
17566 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17567 omitting DW_AT_lower_bound. */
17568 switch (cu->language)
6e70227d 17569 {
4fae6e18
JK
17570 case language_c:
17571 case language_cplus:
729efb13 17572 low.data.const_val = 0;
4fae6e18
JK
17573 low_default_is_valid = 1;
17574 break;
17575 case language_fortran:
729efb13 17576 low.data.const_val = 1;
4fae6e18
JK
17577 low_default_is_valid = 1;
17578 break;
17579 case language_d:
4fae6e18 17580 case language_objc:
c44af4eb 17581 case language_rust:
729efb13 17582 low.data.const_val = 0;
4fae6e18
JK
17583 low_default_is_valid = (cu->header.version >= 4);
17584 break;
17585 case language_ada:
17586 case language_m2:
17587 case language_pascal:
729efb13 17588 low.data.const_val = 1;
4fae6e18
JK
17589 low_default_is_valid = (cu->header.version >= 4);
17590 break;
17591 default:
729efb13 17592 low.data.const_val = 0;
4fae6e18
JK
17593 low_default_is_valid = 0;
17594 break;
a02abb62
JB
17595 }
17596
e142c38c 17597 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
a02abb62 17598 if (attr)
11c1ba78 17599 attr_to_dynamic_prop (attr, die, cu, &low);
4fae6e18 17600 else if (!low_default_is_valid)
b98664d3 17601 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
17602 "- DIE at %s [in module %s]"),
17603 sect_offset_str (die->sect_off),
518817b3 17604 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a02abb62 17605
e142c38c 17606 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
80180f79 17607 if (!attr_to_dynamic_prop (attr, die, cu, &high))
e77813c8
PM
17608 {
17609 attr = dwarf2_attr (die, DW_AT_count, cu);
c451ebe5 17610 if (attr_to_dynamic_prop (attr, die, cu, &high))
6b662e19 17611 {
c451ebe5
SA
17612 /* If bounds are constant do the final calculation here. */
17613 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17614 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17615 else
17616 high_bound_is_count = 1;
c2ff108b 17617 }
e77813c8
PM
17618 }
17619
17620 /* Dwarf-2 specifications explicitly allows to create subrange types
17621 without specifying a base type.
17622 In that case, the base type must be set to the type of
17623 the lower bound, upper bound or count, in that order, if any of these
17624 three attributes references an object that has a type.
17625 If no base type is found, the Dwarf-2 specifications say that
17626 a signed integer type of size equal to the size of an address should
17627 be used.
17628 For the following C code: `extern char gdb_int [];'
17629 GCC produces an empty range DIE.
17630 FIXME: muller/2010-05-28: Possible references to object for low bound,
0963b4bd 17631 high bound or count are not yet handled by this code. */
e77813c8
PM
17632 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
17633 {
518817b3 17634 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e77813c8
PM
17635 struct gdbarch *gdbarch = get_objfile_arch (objfile);
17636 int addr_size = gdbarch_addr_bit (gdbarch) /8;
17637 struct type *int_type = objfile_type (objfile)->builtin_int;
17638
17639 /* Test "int", "long int", and "long long int" objfile types,
17640 and select the first one having a size above or equal to the
17641 architecture address size. */
17642 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17643 base_type = int_type;
17644 else
17645 {
17646 int_type = objfile_type (objfile)->builtin_long;
17647 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17648 base_type = int_type;
17649 else
17650 {
17651 int_type = objfile_type (objfile)->builtin_long_long;
17652 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
17653 base_type = int_type;
17654 }
17655 }
17656 }
a02abb62 17657
dbb9c2b1
JB
17658 /* Normally, the DWARF producers are expected to use a signed
17659 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17660 But this is unfortunately not always the case, as witnessed
17661 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17662 is used instead. To work around that ambiguity, we treat
17663 the bounds as signed, and thus sign-extend their values, when
17664 the base type is signed. */
6e70227d 17665 negative_mask =
66c6502d 17666 -((LONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17667 if (low.kind == PROP_CONST
17668 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17669 low.data.const_val |= negative_mask;
17670 if (high.kind == PROP_CONST
17671 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17672 high.data.const_val |= negative_mask;
43bbcdc2 17673
729efb13 17674 range_type = create_range_type (NULL, orig_base_type, &low, &high);
a02abb62 17675
c451ebe5
SA
17676 if (high_bound_is_count)
17677 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17678
c2ff108b
JK
17679 /* Ada expects an empty array on no boundary attributes. */
17680 if (attr == NULL && cu->language != language_ada)
729efb13 17681 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 17682
39cbfefa
DJ
17683 name = dwarf2_name (die, cu);
17684 if (name)
17685 TYPE_NAME (range_type) = name;
6e70227d 17686
e142c38c 17687 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
a02abb62
JB
17688 if (attr)
17689 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17690
2b4424c3
TT
17691 maybe_set_alignment (cu, die, range_type);
17692
7e314c57
JK
17693 set_die_type (die, range_type, cu);
17694
17695 /* set_die_type should be already done. */
b4ba55a1
JB
17696 set_descriptive_type (range_type, die, cu);
17697
7e314c57 17698 return range_type;
a02abb62 17699}
6e70227d 17700
f792889a 17701static struct type *
81a17f79
JB
17702read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17703{
17704 struct type *type;
81a17f79 17705
518817b3
SM
17706 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17707 NULL);
0114d602 17708 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 17709
74a2f8ff
JB
17710 /* In Ada, an unspecified type is typically used when the description
17711 of the type is defered to a different unit. When encountering
17712 such a type, we treat it as a stub, and try to resolve it later on,
17713 when needed. */
17714 if (cu->language == language_ada)
17715 TYPE_STUB (type) = 1;
17716
f792889a 17717 return set_die_type (die, type, cu);
81a17f79 17718}
a02abb62 17719
639d11d3
DC
17720/* Read a single die and all its descendents. Set the die's sibling
17721 field to NULL; set other fields in the die correctly, and set all
17722 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17723 location of the info_ptr after reading all of those dies. PARENT
17724 is the parent of the die in question. */
17725
17726static struct die_info *
dee91e82 17727read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
17728 const gdb_byte *info_ptr,
17729 const gdb_byte **new_info_ptr,
dee91e82 17730 struct die_info *parent)
639d11d3
DC
17731{
17732 struct die_info *die;
d521ce57 17733 const gdb_byte *cur_ptr;
639d11d3
DC
17734 int has_children;
17735
bf6af496 17736 cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
1d325ec1
DJ
17737 if (die == NULL)
17738 {
17739 *new_info_ptr = cur_ptr;
17740 return NULL;
17741 }
93311388 17742 store_in_ref_table (die, reader->cu);
639d11d3
DC
17743
17744 if (has_children)
bf6af496 17745 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
17746 else
17747 {
17748 die->child = NULL;
17749 *new_info_ptr = cur_ptr;
17750 }
17751
17752 die->sibling = NULL;
17753 die->parent = parent;
17754 return die;
17755}
17756
17757/* Read a die, all of its descendents, and all of its siblings; set
17758 all of the fields of all of the dies correctly. Arguments are as
17759 in read_die_and_children. */
17760
17761static struct die_info *
bf6af496 17762read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
17763 const gdb_byte *info_ptr,
17764 const gdb_byte **new_info_ptr,
bf6af496 17765 struct die_info *parent)
639d11d3
DC
17766{
17767 struct die_info *first_die, *last_sibling;
d521ce57 17768 const gdb_byte *cur_ptr;
639d11d3 17769
c906108c 17770 cur_ptr = info_ptr;
639d11d3
DC
17771 first_die = last_sibling = NULL;
17772
17773 while (1)
c906108c 17774 {
639d11d3 17775 struct die_info *die
dee91e82 17776 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 17777
1d325ec1 17778 if (die == NULL)
c906108c 17779 {
639d11d3
DC
17780 *new_info_ptr = cur_ptr;
17781 return first_die;
c906108c 17782 }
1d325ec1
DJ
17783
17784 if (!first_die)
17785 first_die = die;
c906108c 17786 else
1d325ec1
DJ
17787 last_sibling->sibling = die;
17788
17789 last_sibling = die;
c906108c 17790 }
c906108c
SS
17791}
17792
bf6af496
DE
17793/* Read a die, all of its descendents, and all of its siblings; set
17794 all of the fields of all of the dies correctly. Arguments are as
17795 in read_die_and_children.
17796 This the main entry point for reading a DIE and all its children. */
17797
17798static struct die_info *
17799read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
17800 const gdb_byte *info_ptr,
17801 const gdb_byte **new_info_ptr,
bf6af496
DE
17802 struct die_info *parent)
17803{
17804 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17805 new_info_ptr, parent);
17806
b4f54984 17807 if (dwarf_die_debug)
bf6af496
DE
17808 {
17809 fprintf_unfiltered (gdb_stdlog,
17810 "Read die from %s@0x%x of %s:\n",
a32a8923 17811 get_section_name (reader->die_section),
bf6af496
DE
17812 (unsigned) (info_ptr - reader->die_section->buffer),
17813 bfd_get_filename (reader->abfd));
b4f54984 17814 dump_die (die, dwarf_die_debug);
bf6af496
DE
17815 }
17816
17817 return die;
17818}
17819
3019eac3
DE
17820/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17821 attributes.
17822 The caller is responsible for filling in the extra attributes
17823 and updating (*DIEP)->num_attrs.
17824 Set DIEP to point to a newly allocated die with its information,
17825 except for its child, sibling, and parent fields.
17826 Set HAS_CHILDREN to tell whether the die has children or not. */
93311388 17827
d521ce57 17828static const gdb_byte *
3019eac3 17829read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 17830 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3 17831 int *has_children, int num_extra_attrs)
93311388 17832{
b64f50a1 17833 unsigned int abbrev_number, bytes_read, i;
93311388
DE
17834 struct abbrev_info *abbrev;
17835 struct die_info *die;
17836 struct dwarf2_cu *cu = reader->cu;
17837 bfd *abfd = reader->abfd;
17838
9c541725 17839 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
17840 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17841 info_ptr += bytes_read;
17842 if (!abbrev_number)
17843 {
17844 *diep = NULL;
17845 *has_children = 0;
17846 return info_ptr;
17847 }
17848
685af9cd 17849 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 17850 if (!abbrev)
348e048f
DE
17851 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17852 abbrev_number,
17853 bfd_get_filename (abfd));
17854
3019eac3 17855 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 17856 die->sect_off = sect_off;
93311388
DE
17857 die->tag = abbrev->tag;
17858 die->abbrev = abbrev_number;
17859
3019eac3
DE
17860 /* Make the result usable.
17861 The caller needs to update num_attrs after adding the extra
17862 attributes. */
93311388
DE
17863 die->num_attrs = abbrev->num_attrs;
17864
17865 for (i = 0; i < abbrev->num_attrs; ++i)
dee91e82
DE
17866 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17867 info_ptr);
93311388
DE
17868
17869 *diep = die;
17870 *has_children = abbrev->has_children;
17871 return info_ptr;
17872}
17873
3019eac3
DE
17874/* Read a die and all its attributes.
17875 Set DIEP to point to a newly allocated die with its information,
17876 except for its child, sibling, and parent fields.
17877 Set HAS_CHILDREN to tell whether the die has children or not. */
17878
d521ce57 17879static const gdb_byte *
3019eac3 17880read_full_die (const struct die_reader_specs *reader,
d521ce57 17881 struct die_info **diep, const gdb_byte *info_ptr,
3019eac3
DE
17882 int *has_children)
17883{
d521ce57 17884 const gdb_byte *result;
bf6af496
DE
17885
17886 result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
17887
b4f54984 17888 if (dwarf_die_debug)
bf6af496
DE
17889 {
17890 fprintf_unfiltered (gdb_stdlog,
17891 "Read die from %s@0x%x of %s:\n",
a32a8923 17892 get_section_name (reader->die_section),
bf6af496
DE
17893 (unsigned) (info_ptr - reader->die_section->buffer),
17894 bfd_get_filename (reader->abfd));
b4f54984 17895 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
17896 }
17897
17898 return result;
3019eac3 17899}
433df2d4
DE
17900\f
17901/* Abbreviation tables.
3019eac3 17902
433df2d4 17903 In DWARF version 2, the description of the debugging information is
c906108c
SS
17904 stored in a separate .debug_abbrev section. Before we read any
17905 dies from a section we read in all abbreviations and install them
433df2d4
DE
17906 in a hash table. */
17907
17908/* Allocate space for a struct abbrev_info object in ABBREV_TABLE. */
17909
685af9cd
TT
17910struct abbrev_info *
17911abbrev_table::alloc_abbrev ()
433df2d4
DE
17912{
17913 struct abbrev_info *abbrev;
17914
685af9cd 17915 abbrev = XOBNEW (&abbrev_obstack, struct abbrev_info);
433df2d4 17916 memset (abbrev, 0, sizeof (struct abbrev_info));
8d749320 17917
433df2d4
DE
17918 return abbrev;
17919}
17920
17921/* Add an abbreviation to the table. */
c906108c 17922
685af9cd
TT
17923void
17924abbrev_table::add_abbrev (unsigned int abbrev_number,
17925 struct abbrev_info *abbrev)
433df2d4
DE
17926{
17927 unsigned int hash_number;
17928
17929 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768
YQ
17930 abbrev->next = m_abbrevs[hash_number];
17931 m_abbrevs[hash_number] = abbrev;
433df2d4 17932}
dee91e82 17933
433df2d4
DE
17934/* Look up an abbrev in the table.
17935 Returns NULL if the abbrev is not found. */
17936
685af9cd
TT
17937struct abbrev_info *
17938abbrev_table::lookup_abbrev (unsigned int abbrev_number)
c906108c 17939{
433df2d4
DE
17940 unsigned int hash_number;
17941 struct abbrev_info *abbrev;
17942
17943 hash_number = abbrev_number % ABBREV_HASH_SIZE;
4a17f768 17944 abbrev = m_abbrevs[hash_number];
433df2d4
DE
17945
17946 while (abbrev)
17947 {
17948 if (abbrev->number == abbrev_number)
17949 return abbrev;
17950 abbrev = abbrev->next;
17951 }
17952 return NULL;
17953}
17954
17955/* Read in an abbrev table. */
17956
685af9cd 17957static abbrev_table_up
ed2dc618
SM
17958abbrev_table_read_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
17959 struct dwarf2_section_info *section,
9c541725 17960 sect_offset sect_off)
433df2d4
DE
17961{
17962 struct objfile *objfile = dwarf2_per_objfile->objfile;
a32a8923 17963 bfd *abfd = get_section_bfd_owner (section);
d521ce57 17964 const gdb_byte *abbrev_ptr;
c906108c
SS
17965 struct abbrev_info *cur_abbrev;
17966 unsigned int abbrev_number, bytes_read, abbrev_name;
433df2d4 17967 unsigned int abbrev_form;
f3dd6933
DJ
17968 struct attr_abbrev *cur_attrs;
17969 unsigned int allocated_attrs;
c906108c 17970
685af9cd 17971 abbrev_table_up abbrev_table (new struct abbrev_table (sect_off));
c906108c 17972
433df2d4 17973 dwarf2_read_section (objfile, section);
9c541725 17974 abbrev_ptr = section->buffer + to_underlying (sect_off);
c906108c
SS
17975 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
17976 abbrev_ptr += bytes_read;
17977
f3dd6933 17978 allocated_attrs = ATTR_ALLOC_CHUNK;
8d749320 17979 cur_attrs = XNEWVEC (struct attr_abbrev, allocated_attrs);
6e70227d 17980
0963b4bd 17981 /* Loop until we reach an abbrev number of 0. */
c906108c
SS
17982 while (abbrev_number)
17983 {
685af9cd 17984 cur_abbrev = abbrev_table->alloc_abbrev ();
c906108c
SS
17985
17986 /* read in abbrev header */
17987 cur_abbrev->number = abbrev_number;
aead7601
SM
17988 cur_abbrev->tag
17989 = (enum dwarf_tag) read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
c906108c
SS
17990 abbrev_ptr += bytes_read;
17991 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
17992 abbrev_ptr += 1;
17993
17994 /* now read in declarations */
22d2f3ab 17995 for (;;)
c906108c 17996 {
43988095
JK
17997 LONGEST implicit_const;
17998
22d2f3ab
JK
17999 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18000 abbrev_ptr += bytes_read;
18001 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18002 abbrev_ptr += bytes_read;
43988095
JK
18003 if (abbrev_form == DW_FORM_implicit_const)
18004 {
18005 implicit_const = read_signed_leb128 (abfd, abbrev_ptr,
18006 &bytes_read);
18007 abbrev_ptr += bytes_read;
18008 }
18009 else
18010 {
18011 /* Initialize it due to a false compiler warning. */
18012 implicit_const = -1;
18013 }
22d2f3ab
JK
18014
18015 if (abbrev_name == 0)
18016 break;
18017
f3dd6933 18018 if (cur_abbrev->num_attrs == allocated_attrs)
c906108c 18019 {
f3dd6933
DJ
18020 allocated_attrs += ATTR_ALLOC_CHUNK;
18021 cur_attrs
224c3ddb 18022 = XRESIZEVEC (struct attr_abbrev, cur_attrs, allocated_attrs);
c906108c 18023 }
ae038cb0 18024
aead7601
SM
18025 cur_attrs[cur_abbrev->num_attrs].name
18026 = (enum dwarf_attribute) abbrev_name;
22d2f3ab 18027 cur_attrs[cur_abbrev->num_attrs].form
aead7601 18028 = (enum dwarf_form) abbrev_form;
43988095 18029 cur_attrs[cur_abbrev->num_attrs].implicit_const = implicit_const;
22d2f3ab 18030 ++cur_abbrev->num_attrs;
c906108c
SS
18031 }
18032
8d749320
SM
18033 cur_abbrev->attrs =
18034 XOBNEWVEC (&abbrev_table->abbrev_obstack, struct attr_abbrev,
18035 cur_abbrev->num_attrs);
f3dd6933
DJ
18036 memcpy (cur_abbrev->attrs, cur_attrs,
18037 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
18038
685af9cd 18039 abbrev_table->add_abbrev (abbrev_number, cur_abbrev);
c906108c
SS
18040
18041 /* Get next abbreviation.
18042 Under Irix6 the abbreviations for a compilation unit are not
c5aa993b
JM
18043 always properly terminated with an abbrev number of 0.
18044 Exit loop if we encounter an abbreviation which we have
18045 already read (which means we are about to read the abbreviations
18046 for the next compile unit) or if the end of the abbreviation
18047 table is reached. */
433df2d4 18048 if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
c906108c
SS
18049 break;
18050 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
18051 abbrev_ptr += bytes_read;
685af9cd 18052 if (abbrev_table->lookup_abbrev (abbrev_number) != NULL)
c906108c
SS
18053 break;
18054 }
f3dd6933
DJ
18055
18056 xfree (cur_attrs);
433df2d4 18057 return abbrev_table;
c906108c
SS
18058}
18059
72bf9492
DJ
18060/* Returns nonzero if TAG represents a type that we might generate a partial
18061 symbol for. */
18062
18063static int
18064is_type_tag_for_partial (int tag)
18065{
18066 switch (tag)
18067 {
18068#if 0
18069 /* Some types that would be reasonable to generate partial symbols for,
18070 that we don't at present. */
18071 case DW_TAG_array_type:
18072 case DW_TAG_file_type:
18073 case DW_TAG_ptr_to_member_type:
18074 case DW_TAG_set_type:
18075 case DW_TAG_string_type:
18076 case DW_TAG_subroutine_type:
18077#endif
18078 case DW_TAG_base_type:
18079 case DW_TAG_class_type:
680b30c7 18080 case DW_TAG_interface_type:
72bf9492
DJ
18081 case DW_TAG_enumeration_type:
18082 case DW_TAG_structure_type:
18083 case DW_TAG_subrange_type:
18084 case DW_TAG_typedef:
18085 case DW_TAG_union_type:
18086 return 1;
18087 default:
18088 return 0;
18089 }
18090}
18091
18092/* Load all DIEs that are interesting for partial symbols into memory. */
18093
18094static struct partial_die_info *
dee91e82 18095load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18096 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18097{
dee91e82 18098 struct dwarf2_cu *cu = reader->cu;
518817b3 18099 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
72bf9492 18100 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18101 unsigned int bytes_read;
5afb4e99 18102 unsigned int load_all = 0;
72bf9492
DJ
18103 int nesting_level = 1;
18104
18105 parent_die = NULL;
18106 last_die = NULL;
18107
7adf1e79
DE
18108 gdb_assert (cu->per_cu != NULL);
18109 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18110 load_all = 1;
18111
72bf9492
DJ
18112 cu->partial_dies
18113 = htab_create_alloc_ex (cu->header.length / 12,
18114 partial_die_hash,
18115 partial_die_eq,
18116 NULL,
18117 &cu->comp_unit_obstack,
18118 hashtab_obstack_allocate,
18119 dummy_obstack_deallocate);
18120
72bf9492
DJ
18121 while (1)
18122 {
685af9cd 18123 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18124
18125 /* A NULL abbrev means the end of a series of children. */
18126 if (abbrev == NULL)
18127 {
18128 if (--nesting_level == 0)
cd9983dd
YQ
18129 return first_die;
18130
72bf9492
DJ
18131 info_ptr += bytes_read;
18132 last_die = parent_die;
18133 parent_die = parent_die->die_parent;
18134 continue;
18135 }
18136
98bfdba5
PA
18137 /* Check for template arguments. We never save these; if
18138 they're seen, we just mark the parent, and go on our way. */
18139 if (parent_die != NULL
18140 && cu->language == language_cplus
18141 && (abbrev->tag == DW_TAG_template_type_param
18142 || abbrev->tag == DW_TAG_template_value_param))
18143 {
18144 parent_die->has_template_arguments = 1;
18145
18146 if (!load_all)
18147 {
18148 /* We don't need a partial DIE for the template argument. */
dee91e82 18149 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18150 continue;
18151 }
18152 }
18153
0d99eb77 18154 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18155 Skip their other children. */
18156 if (!load_all
18157 && cu->language == language_cplus
18158 && parent_die != NULL
18159 && parent_die->tag == DW_TAG_subprogram)
18160 {
dee91e82 18161 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18162 continue;
18163 }
18164
5afb4e99
DJ
18165 /* Check whether this DIE is interesting enough to save. Normally
18166 we would not be interested in members here, but there may be
18167 later variables referencing them via DW_AT_specification (for
18168 static members). */
18169 if (!load_all
18170 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18171 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18172 && abbrev->tag != DW_TAG_enumerator
18173 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18174 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18175 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18176 && abbrev->tag != DW_TAG_variable
5afb4e99 18177 && abbrev->tag != DW_TAG_namespace
f55ee35c 18178 && abbrev->tag != DW_TAG_module
95554aad 18179 && abbrev->tag != DW_TAG_member
74921315
KS
18180 && abbrev->tag != DW_TAG_imported_unit
18181 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18182 {
18183 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18184 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18185 continue;
18186 }
18187
6f06d47b
YQ
18188 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18189 abbrev);
cd9983dd 18190
48fbe735 18191 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18192
18193 /* This two-pass algorithm for processing partial symbols has a
18194 high cost in cache pressure. Thus, handle some simple cases
18195 here which cover the majority of C partial symbols. DIEs
18196 which neither have specification tags in them, nor could have
18197 specification tags elsewhere pointing at them, can simply be
18198 processed and discarded.
18199
18200 This segment is also optional; scan_partial_symbols and
18201 add_partial_symbol will handle these DIEs if we chain
18202 them in normally. When compilers which do not emit large
18203 quantities of duplicate debug information are more common,
18204 this code can probably be removed. */
18205
18206 /* Any complete simple types at the top level (pretty much all
18207 of them, for a language without namespaces), can be processed
18208 directly. */
18209 if (parent_die == NULL
cd9983dd
YQ
18210 && pdi.has_specification == 0
18211 && pdi.is_declaration == 0
18212 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18213 || pdi.tag == DW_TAG_base_type
18214 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18215 {
cd9983dd
YQ
18216 if (building_psymtab && pdi.name != NULL)
18217 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
72bf9492 18218 VAR_DOMAIN, LOC_TYPEDEF,
bb5ed363 18219 &objfile->static_psymbols,
1762568f 18220 0, cu->language, objfile);
cd9983dd 18221 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18222 continue;
18223 }
18224
d8228535
JK
18225 /* The exception for DW_TAG_typedef with has_children above is
18226 a workaround of GCC PR debug/47510. In the case of this complaint
18227 type_name_no_tag_or_error will error on such types later.
18228
18229 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18230 it could not find the child DIEs referenced later, this is checked
18231 above. In correct DWARF DW_TAG_typedef should have no children. */
18232
cd9983dd 18233 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 18234 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18235 "- DIE at %s [in module %s]"),
cd9983dd 18236 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18237
72bf9492
DJ
18238 /* If we're at the second level, and we're an enumerator, and
18239 our parent has no specification (meaning possibly lives in a
18240 namespace elsewhere), then we can add the partial symbol now
18241 instead of queueing it. */
cd9983dd 18242 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18243 && parent_die != NULL
18244 && parent_die->die_parent == NULL
18245 && parent_die->tag == DW_TAG_enumeration_type
18246 && parent_die->has_specification == 0)
18247 {
cd9983dd 18248 if (pdi.name == NULL)
b98664d3 18249 complaint (_("malformed enumerator DIE ignored"));
72bf9492 18250 else if (building_psymtab)
cd9983dd 18251 add_psymbol_to_list (pdi.name, strlen (pdi.name), 0,
72bf9492 18252 VAR_DOMAIN, LOC_CONST,
9c37b5ae 18253 cu->language == language_cplus
bb5ed363
DE
18254 ? &objfile->global_psymbols
18255 : &objfile->static_psymbols,
1762568f 18256 0, cu->language, objfile);
72bf9492 18257
cd9983dd 18258 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18259 continue;
18260 }
18261
cd9983dd 18262 struct partial_die_info *part_die
6f06d47b 18263 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18264
72bf9492
DJ
18265 /* We'll save this DIE so link it in. */
18266 part_die->die_parent = parent_die;
18267 part_die->die_sibling = NULL;
18268 part_die->die_child = NULL;
18269
18270 if (last_die && last_die == parent_die)
18271 last_die->die_child = part_die;
18272 else if (last_die)
18273 last_die->die_sibling = part_die;
18274
18275 last_die = part_die;
18276
18277 if (first_die == NULL)
18278 first_die = part_die;
18279
18280 /* Maybe add the DIE to the hash table. Not all DIEs that we
18281 find interesting need to be in the hash table, because we
18282 also have the parent/sibling/child chains; only those that we
18283 might refer to by offset later during partial symbol reading.
18284
18285 For now this means things that might have be the target of a
18286 DW_AT_specification, DW_AT_abstract_origin, or
18287 DW_AT_extension. DW_AT_extension will refer only to
18288 namespaces; DW_AT_abstract_origin refers to functions (and
18289 many things under the function DIE, but we do not recurse
18290 into function DIEs during partial symbol reading) and
18291 possibly variables as well; DW_AT_specification refers to
18292 declarations. Declarations ought to have the DW_AT_declaration
18293 flag. It happens that GCC forgets to put it in sometimes, but
18294 only for functions, not for types.
18295
18296 Adding more things than necessary to the hash table is harmless
18297 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18298 wasted time in find_partial_die, when we reread the compilation
18299 unit with load_all_dies set. */
72bf9492 18300
5afb4e99 18301 if (load_all
72929c62 18302 || abbrev->tag == DW_TAG_constant
5afb4e99 18303 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18304 || abbrev->tag == DW_TAG_variable
18305 || abbrev->tag == DW_TAG_namespace
18306 || part_die->is_declaration)
18307 {
18308 void **slot;
18309
18310 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18311 to_underlying (part_die->sect_off),
18312 INSERT);
72bf9492
DJ
18313 *slot = part_die;
18314 }
18315
72bf9492 18316 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18317 we have no reason to follow the children of structures; for other
98bfdba5
PA
18318 languages we have to, so that we can get at method physnames
18319 to infer fully qualified class names, for DW_AT_specification,
18320 and for C++ template arguments. For C++, we also look one level
18321 inside functions to find template arguments (if the name of the
18322 function does not already contain the template arguments).
bc30ff58
JB
18323
18324 For Ada, we need to scan the children of subprograms and lexical
18325 blocks as well because Ada allows the definition of nested
18326 entities that could be interesting for the debugger, such as
18327 nested subprograms for instance. */
72bf9492 18328 if (last_die->has_children
5afb4e99
DJ
18329 && (load_all
18330 || last_die->tag == DW_TAG_namespace
f55ee35c 18331 || last_die->tag == DW_TAG_module
72bf9492 18332 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18333 || (cu->language == language_cplus
18334 && last_die->tag == DW_TAG_subprogram
18335 && (last_die->name == NULL
18336 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
18337 || (cu->language != language_c
18338 && (last_die->tag == DW_TAG_class_type
680b30c7 18339 || last_die->tag == DW_TAG_interface_type
72bf9492 18340 || last_die->tag == DW_TAG_structure_type
bc30ff58
JB
18341 || last_die->tag == DW_TAG_union_type))
18342 || (cu->language == language_ada
18343 && (last_die->tag == DW_TAG_subprogram
18344 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18345 {
18346 nesting_level++;
18347 parent_die = last_die;
18348 continue;
18349 }
18350
18351 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18352 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18353
18354 /* Back to the top, do it again. */
18355 }
18356}
18357
6f06d47b
YQ
18358partial_die_info::partial_die_info (sect_offset sect_off_,
18359 struct abbrev_info *abbrev)
18360 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18361{
18362}
18363
35cc7ed7
YQ
18364/* Read a minimal amount of information into the minimal die structure.
18365 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 18366
48fbe735
YQ
18367const gdb_byte *
18368partial_die_info::read (const struct die_reader_specs *reader,
18369 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 18370{
dee91e82 18371 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18372 struct dwarf2_per_objfile *dwarf2_per_objfile
18373 = cu->per_cu->dwarf2_per_objfile;
fa238c03 18374 unsigned int i;
c5aa993b 18375 int has_low_pc_attr = 0;
c906108c 18376 int has_high_pc_attr = 0;
91da1414 18377 int high_pc_relative = 0;
c906108c 18378
fd0a254f 18379 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 18380 {
48fbe735
YQ
18381 struct attribute attr;
18382
fd0a254f 18383 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
c906108c
SS
18384
18385 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18386 partial symbol table. */
c906108c
SS
18387 switch (attr.name)
18388 {
18389 case DW_AT_name:
48fbe735 18390 switch (tag)
71c25dea
TT
18391 {
18392 case DW_TAG_compile_unit:
95554aad 18393 case DW_TAG_partial_unit:
348e048f 18394 case DW_TAG_type_unit:
71c25dea
TT
18395 /* Compilation units have a DW_AT_name that is a filename, not
18396 a source language identifier. */
18397 case DW_TAG_enumeration_type:
18398 case DW_TAG_enumerator:
18399 /* These tags always have simple identifiers already; no need
18400 to canonicalize them. */
48fbe735 18401 name = DW_STRING (&attr);
71c25dea
TT
18402 break;
18403 default:
48fbe735
YQ
18404 {
18405 struct objfile *objfile = dwarf2_per_objfile->objfile;
18406
18407 name
18408 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
18409 &objfile->per_bfd->storage_obstack);
18410 }
71c25dea
TT
18411 break;
18412 }
c906108c 18413 break;
31ef98ae 18414 case DW_AT_linkage_name:
c906108c 18415 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18416 /* Note that both forms of linkage name might appear. We
18417 assume they will be the same, and we only store the last
18418 one we see. */
94af9270 18419 if (cu->language == language_ada)
48fbe735
YQ
18420 name = DW_STRING (&attr);
18421 linkage_name = DW_STRING (&attr);
c906108c
SS
18422 break;
18423 case DW_AT_low_pc:
18424 has_low_pc_attr = 1;
48fbe735 18425 lowpc = attr_value_as_address (&attr);
c906108c
SS
18426 break;
18427 case DW_AT_high_pc:
18428 has_high_pc_attr = 1;
48fbe735 18429 highpc = attr_value_as_address (&attr);
31aa7e4e
JB
18430 if (cu->header.version >= 4 && attr_form_is_constant (&attr))
18431 high_pc_relative = 1;
c906108c
SS
18432 break;
18433 case DW_AT_location:
0963b4bd 18434 /* Support the .debug_loc offsets. */
8e19ed76
PS
18435 if (attr_form_is_block (&attr))
18436 {
48fbe735 18437 d.locdesc = DW_BLOCK (&attr);
8e19ed76 18438 }
3690dd37 18439 else if (attr_form_is_section_offset (&attr))
8e19ed76 18440 {
4d3c2250 18441 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18442 }
18443 else
18444 {
4d3c2250
KB
18445 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18446 "partial symbol information");
8e19ed76 18447 }
c906108c 18448 break;
c906108c 18449 case DW_AT_external:
48fbe735 18450 is_external = DW_UNSND (&attr);
c906108c
SS
18451 break;
18452 case DW_AT_declaration:
48fbe735 18453 is_declaration = DW_UNSND (&attr);
c906108c
SS
18454 break;
18455 case DW_AT_type:
48fbe735 18456 has_type = 1;
c906108c
SS
18457 break;
18458 case DW_AT_abstract_origin:
18459 case DW_AT_specification:
72bf9492 18460 case DW_AT_extension:
48fbe735
YQ
18461 has_specification = 1;
18462 spec_offset = dwarf2_get_ref_die_offset (&attr);
18463 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 18464 || cu->per_cu->is_dwz);
c906108c
SS
18465 break;
18466 case DW_AT_sibling:
18467 /* Ignore absolute siblings, they might point outside of
18468 the current compile unit. */
18469 if (attr.form == DW_FORM_ref_addr)
b98664d3 18470 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 18471 else
b9502d3f 18472 {
48fbe735 18473 const gdb_byte *buffer = reader->buffer;
9c541725
PA
18474 sect_offset off = dwarf2_get_ref_die_offset (&attr);
18475 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18476
18477 if (sibling_ptr < info_ptr)
b98664d3 18478 complaint (_("DW_AT_sibling points backwards"));
22869d73
KS
18479 else if (sibling_ptr > reader->buffer_end)
18480 dwarf2_section_buffer_overflow_complaint (reader->die_section);
b9502d3f 18481 else
48fbe735 18482 sibling = sibling_ptr;
b9502d3f 18483 }
c906108c 18484 break;
fa4028e9 18485 case DW_AT_byte_size:
48fbe735 18486 has_byte_size = 1;
fa4028e9 18487 break;
ff908ebf 18488 case DW_AT_const_value:
48fbe735 18489 has_const_value = 1;
ff908ebf 18490 break;
68511cec
CES
18491 case DW_AT_calling_convention:
18492 /* DWARF doesn't provide a way to identify a program's source-level
18493 entry point. DW_AT_calling_convention attributes are only meant
18494 to describe functions' calling conventions.
18495
18496 However, because it's a necessary piece of information in
0c1b455e
TT
18497 Fortran, and before DWARF 4 DW_CC_program was the only
18498 piece of debugging information whose definition refers to
18499 a 'main program' at all, several compilers marked Fortran
18500 main programs with DW_CC_program --- even when those
18501 functions use the standard calling conventions.
18502
18503 Although DWARF now specifies a way to provide this
18504 information, we support this practice for backward
18505 compatibility. */
68511cec 18506 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 18507 && cu->language == language_fortran)
48fbe735 18508 main_subprogram = 1;
68511cec 18509 break;
481860b3
GB
18510 case DW_AT_inline:
18511 if (DW_UNSND (&attr) == DW_INL_inlined
18512 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 18513 may_be_inlined = 1;
481860b3 18514 break;
95554aad
TT
18515
18516 case DW_AT_import:
48fbe735 18517 if (tag == DW_TAG_imported_unit)
36586728 18518 {
48fbe735
YQ
18519 d.sect_off = dwarf2_get_ref_die_offset (&attr);
18520 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
18521 || cu->per_cu->is_dwz);
18522 }
95554aad
TT
18523 break;
18524
0c1b455e 18525 case DW_AT_main_subprogram:
48fbe735 18526 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
18527 break;
18528
c906108c
SS
18529 default:
18530 break;
18531 }
18532 }
18533
91da1414 18534 if (high_pc_relative)
48fbe735 18535 highpc += lowpc;
91da1414 18536
9373cf26
JK
18537 if (has_low_pc_attr && has_high_pc_attr)
18538 {
18539 /* When using the GNU linker, .gnu.linkonce. sections are used to
18540 eliminate duplicate copies of functions and vtables and such.
18541 The linker will arbitrarily choose one and discard the others.
18542 The AT_*_pc values for such functions refer to local labels in
18543 these sections. If the section from that file was discarded, the
18544 labels are not in the output, so the relocs get a value of 0.
18545 If this is a discarded function, mark the pc bounds as invalid,
18546 so that GDB will ignore it. */
48fbe735 18547 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9373cf26 18548 {
48fbe735 18549 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18550 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 18551
b98664d3 18552 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 18553 "for DIE at %s [in module %s]"),
48fbe735
YQ
18554 paddress (gdbarch, lowpc),
18555 sect_offset_str (sect_off),
9d8780f0 18556 objfile_name (objfile));
9373cf26
JK
18557 }
18558 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 18559 else if (lowpc >= highpc)
9373cf26 18560 {
48fbe735 18561 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18562 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 18563
b98664d3 18564 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 18565 "for DIE at %s [in module %s]"),
48fbe735
YQ
18566 paddress (gdbarch, lowpc),
18567 paddress (gdbarch, highpc),
18568 sect_offset_str (sect_off),
9c541725 18569 objfile_name (objfile));
9373cf26
JK
18570 }
18571 else
48fbe735 18572 has_pc_info = 1;
9373cf26 18573 }
85cbf3d3 18574
c906108c
SS
18575 return info_ptr;
18576}
18577
72bf9492
DJ
18578/* Find a cached partial DIE at OFFSET in CU. */
18579
d590ff25
YQ
18580struct partial_die_info *
18581dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
18582{
18583 struct partial_die_info *lookup_die = NULL;
6f06d47b 18584 struct partial_die_info part_die (sect_off);
72bf9492 18585
9a3c8263 18586 lookup_die = ((struct partial_die_info *)
d590ff25 18587 htab_find_with_hash (partial_dies, &part_die,
9c541725 18588 to_underlying (sect_off)));
72bf9492 18589
72bf9492
DJ
18590 return lookup_die;
18591}
18592
348e048f
DE
18593/* Find a partial DIE at OFFSET, which may or may not be in CU,
18594 except in the case of .debug_types DIEs which do not reference
18595 outside their CU (they do however referencing other types via
55f1336d 18596 DW_FORM_ref_sig8). */
72bf9492
DJ
18597
18598static struct partial_die_info *
9c541725 18599find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18600{
518817b3
SM
18601 struct dwarf2_per_objfile *dwarf2_per_objfile
18602 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18603 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
18604 struct dwarf2_per_cu_data *per_cu = NULL;
18605 struct partial_die_info *pd = NULL;
72bf9492 18606
36586728 18607 if (offset_in_dwz == cu->per_cu->is_dwz
9c541725 18608 && offset_in_cu_p (&cu->header, sect_off))
5afb4e99 18609 {
d590ff25 18610 pd = cu->find_partial_die (sect_off);
5afb4e99
DJ
18611 if (pd != NULL)
18612 return pd;
0d99eb77
DE
18613 /* We missed recording what we needed.
18614 Load all dies and try again. */
18615 per_cu = cu->per_cu;
5afb4e99 18616 }
0d99eb77
DE
18617 else
18618 {
18619 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18620 if (cu->per_cu->is_debug_types)
0d99eb77 18621 {
9d8780f0
SM
18622 error (_("Dwarf Error: Type Unit at offset %s contains"
18623 " external reference to offset %s [in module %s].\n"),
18624 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
18625 bfd_get_filename (objfile->obfd));
18626 }
9c541725 18627 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 18628 dwarf2_per_objfile);
72bf9492 18629
0d99eb77
DE
18630 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18631 load_partial_comp_unit (per_cu);
ae038cb0 18632
0d99eb77 18633 per_cu->cu->last_used = 0;
d590ff25 18634 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 18635 }
5afb4e99 18636
dee91e82
DE
18637 /* If we didn't find it, and not all dies have been loaded,
18638 load them all and try again. */
18639
5afb4e99
DJ
18640 if (pd == NULL && per_cu->load_all_dies == 0)
18641 {
5afb4e99 18642 per_cu->load_all_dies = 1;
fd820528
DE
18643
18644 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18645 THIS_CU->cu may already be in use. So we can't just free it and
18646 replace its DIEs with the ones we read in. Instead, we leave those
18647 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18648 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18649 set. */
dee91e82 18650 load_partial_comp_unit (per_cu);
5afb4e99 18651
d590ff25 18652 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
18653 }
18654
18655 if (pd == NULL)
18656 internal_error (__FILE__, __LINE__,
9d8780f0 18657 _("could not find partial DIE %s "
3e43a32a 18658 "in cache [from module %s]\n"),
9d8780f0 18659 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
5afb4e99 18660 return pd;
72bf9492
DJ
18661}
18662
abc72ce4
DE
18663/* See if we can figure out if the class lives in a namespace. We do
18664 this by looking for a member function; its demangled name will
18665 contain namespace info, if there is any. */
18666
18667static void
18668guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18669 struct dwarf2_cu *cu)
18670{
18671 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18672 what template types look like, because the demangler
18673 frequently doesn't give the same name as the debug info. We
18674 could fix this by only using the demangled name to get the
18675 prefix (but see comment in read_structure_type). */
18676
18677 struct partial_die_info *real_pdi;
18678 struct partial_die_info *child_pdi;
18679
18680 /* If this DIE (this DIE's specification, if any) has a parent, then
18681 we should not do this. We'll prepend the parent's fully qualified
18682 name when we create the partial symbol. */
18683
18684 real_pdi = struct_pdi;
18685 while (real_pdi->has_specification)
36586728
TT
18686 real_pdi = find_partial_die (real_pdi->spec_offset,
18687 real_pdi->spec_is_dwz, cu);
abc72ce4
DE
18688
18689 if (real_pdi->die_parent != NULL)
18690 return;
18691
18692 for (child_pdi = struct_pdi->die_child;
18693 child_pdi != NULL;
18694 child_pdi = child_pdi->die_sibling)
18695 {
18696 if (child_pdi->tag == DW_TAG_subprogram
18697 && child_pdi->linkage_name != NULL)
18698 {
18699 char *actual_class_name
18700 = language_class_name_from_physname (cu->language_defn,
18701 child_pdi->linkage_name);
18702 if (actual_class_name != NULL)
18703 {
518817b3 18704 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4 18705 struct_pdi->name
224c3ddb 18706 = ((const char *)
e3b94546 18707 obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb
SM
18708 actual_class_name,
18709 strlen (actual_class_name)));
abc72ce4
DE
18710 xfree (actual_class_name);
18711 }
18712 break;
18713 }
18714 }
18715}
18716
52356b79
YQ
18717void
18718partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 18719{
abc72ce4
DE
18720 /* Once we've fixed up a die, there's no point in doing so again.
18721 This also avoids a memory leak if we were to call
18722 guess_partial_die_structure_name multiple times. */
52356b79 18723 if (fixup_called)
abc72ce4
DE
18724 return;
18725
72bf9492
DJ
18726 /* If we found a reference attribute and the DIE has no name, try
18727 to find a name in the referred to DIE. */
18728
52356b79 18729 if (name == NULL && has_specification)
72bf9492
DJ
18730 {
18731 struct partial_die_info *spec_die;
72bf9492 18732
52356b79 18733 spec_die = find_partial_die (spec_offset, spec_is_dwz, cu);
72bf9492 18734
52356b79 18735 spec_die->fixup (cu);
72bf9492
DJ
18736
18737 if (spec_die->name)
18738 {
52356b79 18739 name = spec_die->name;
72bf9492
DJ
18740
18741 /* Copy DW_AT_external attribute if it is set. */
18742 if (spec_die->is_external)
52356b79 18743 is_external = spec_die->is_external;
72bf9492
DJ
18744 }
18745 }
18746
18747 /* Set default names for some unnamed DIEs. */
72bf9492 18748
52356b79
YQ
18749 if (name == NULL && tag == DW_TAG_namespace)
18750 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 18751
abc72ce4
DE
18752 /* If there is no parent die to provide a namespace, and there are
18753 children, see if we can determine the namespace from their linkage
122d1940 18754 name. */
abc72ce4 18755 if (cu->language == language_cplus
518817b3
SM
18756 && !VEC_empty (dwarf2_section_info_def,
18757 cu->per_cu->dwarf2_per_objfile->types)
52356b79
YQ
18758 && die_parent == NULL
18759 && has_children
18760 && (tag == DW_TAG_class_type
18761 || tag == DW_TAG_structure_type
18762 || tag == DW_TAG_union_type))
18763 guess_partial_die_structure_name (this, cu);
abc72ce4 18764
53832f31
TT
18765 /* GCC might emit a nameless struct or union that has a linkage
18766 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
18767 if (name == NULL
18768 && (tag == DW_TAG_class_type
18769 || tag == DW_TAG_interface_type
18770 || tag == DW_TAG_structure_type
18771 || tag == DW_TAG_union_type)
18772 && linkage_name != NULL)
53832f31
TT
18773 {
18774 char *demangled;
18775
52356b79 18776 demangled = gdb_demangle (linkage_name, DMGL_TYPES);
53832f31
TT
18777 if (demangled)
18778 {
96408a79
SA
18779 const char *base;
18780
18781 /* Strip any leading namespaces/classes, keep only the base name.
18782 DW_AT_name for named DIEs does not contain the prefixes. */
18783 base = strrchr (demangled, ':');
18784 if (base && base > demangled && base[-1] == ':')
18785 base++;
18786 else
18787 base = demangled;
18788
518817b3 18789 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
52356b79 18790 name
224c3ddb 18791 = ((const char *)
e3b94546 18792 obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb 18793 base, strlen (base)));
53832f31
TT
18794 xfree (demangled);
18795 }
18796 }
18797
52356b79 18798 fixup_called = 1;
72bf9492
DJ
18799}
18800
a8329558 18801/* Read an attribute value described by an attribute form. */
c906108c 18802
d521ce57 18803static const gdb_byte *
dee91e82
DE
18804read_attribute_value (const struct die_reader_specs *reader,
18805 struct attribute *attr, unsigned form,
43988095 18806 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 18807{
dee91e82 18808 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18809 struct dwarf2_per_objfile *dwarf2_per_objfile
18810 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18811 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 18812 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 18813 bfd *abfd = reader->abfd;
e7c27a73 18814 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
18815 unsigned int bytes_read;
18816 struct dwarf_block *blk;
18817
aead7601 18818 attr->form = (enum dwarf_form) form;
a8329558 18819 switch (form)
c906108c 18820 {
c906108c 18821 case DW_FORM_ref_addr:
ae411497 18822 if (cu->header.version == 2)
4568ecf9 18823 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
ae411497 18824 else
4568ecf9
DE
18825 DW_UNSND (attr) = read_offset (abfd, info_ptr,
18826 &cu->header, &bytes_read);
ae411497
TT
18827 info_ptr += bytes_read;
18828 break;
36586728
TT
18829 case DW_FORM_GNU_ref_alt:
18830 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18831 info_ptr += bytes_read;
18832 break;
ae411497 18833 case DW_FORM_addr:
e7c27a73 18834 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
3e29f34a 18835 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 18836 info_ptr += bytes_read;
c906108c
SS
18837 break;
18838 case DW_FORM_block2:
7b5a2f43 18839 blk = dwarf_alloc_block (cu);
c906108c
SS
18840 blk->size = read_2_bytes (abfd, info_ptr);
18841 info_ptr += 2;
18842 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18843 info_ptr += blk->size;
18844 DW_BLOCK (attr) = blk;
18845 break;
18846 case DW_FORM_block4:
7b5a2f43 18847 blk = dwarf_alloc_block (cu);
c906108c
SS
18848 blk->size = read_4_bytes (abfd, info_ptr);
18849 info_ptr += 4;
18850 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18851 info_ptr += blk->size;
18852 DW_BLOCK (attr) = blk;
18853 break;
18854 case DW_FORM_data2:
18855 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18856 info_ptr += 2;
18857 break;
18858 case DW_FORM_data4:
18859 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18860 info_ptr += 4;
18861 break;
18862 case DW_FORM_data8:
18863 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18864 info_ptr += 8;
18865 break;
0224619f
JK
18866 case DW_FORM_data16:
18867 blk = dwarf_alloc_block (cu);
18868 blk->size = 16;
18869 blk->data = read_n_bytes (abfd, info_ptr, 16);
18870 info_ptr += 16;
18871 DW_BLOCK (attr) = blk;
18872 break;
2dc7f7b3
TT
18873 case DW_FORM_sec_offset:
18874 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
18875 info_ptr += bytes_read;
18876 break;
c906108c 18877 case DW_FORM_string:
9b1c24c8 18878 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 18879 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
18880 info_ptr += bytes_read;
18881 break;
4bdf3d34 18882 case DW_FORM_strp:
36586728
TT
18883 if (!cu->per_cu->is_dwz)
18884 {
ed2dc618
SM
18885 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18886 abfd, info_ptr, cu_header,
36586728
TT
18887 &bytes_read);
18888 DW_STRING_IS_CANONICAL (attr) = 0;
18889 info_ptr += bytes_read;
18890 break;
18891 }
18892 /* FALLTHROUGH */
43988095
JK
18893 case DW_FORM_line_strp:
18894 if (!cu->per_cu->is_dwz)
18895 {
ed2dc618
SM
18896 DW_STRING (attr) = read_indirect_line_string (dwarf2_per_objfile,
18897 abfd, info_ptr,
43988095
JK
18898 cu_header, &bytes_read);
18899 DW_STRING_IS_CANONICAL (attr) = 0;
18900 info_ptr += bytes_read;
18901 break;
18902 }
18903 /* FALLTHROUGH */
36586728
TT
18904 case DW_FORM_GNU_strp_alt:
18905 {
ed2dc618 18906 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
18907 LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
18908 &bytes_read);
18909
ed2dc618
SM
18910 DW_STRING (attr) = read_indirect_string_from_dwz (objfile,
18911 dwz, str_offset);
36586728
TT
18912 DW_STRING_IS_CANONICAL (attr) = 0;
18913 info_ptr += bytes_read;
18914 }
4bdf3d34 18915 break;
2dc7f7b3 18916 case DW_FORM_exprloc:
c906108c 18917 case DW_FORM_block:
7b5a2f43 18918 blk = dwarf_alloc_block (cu);
c906108c
SS
18919 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18920 info_ptr += bytes_read;
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_block1:
7b5a2f43 18926 blk = dwarf_alloc_block (cu);
c906108c
SS
18927 blk->size = read_1_byte (abfd, info_ptr);
18928 info_ptr += 1;
18929 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18930 info_ptr += blk->size;
18931 DW_BLOCK (attr) = blk;
18932 break;
18933 case DW_FORM_data1:
18934 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18935 info_ptr += 1;
18936 break;
18937 case DW_FORM_flag:
18938 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18939 info_ptr += 1;
18940 break;
2dc7f7b3
TT
18941 case DW_FORM_flag_present:
18942 DW_UNSND (attr) = 1;
18943 break;
c906108c
SS
18944 case DW_FORM_sdata:
18945 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18946 info_ptr += bytes_read;
18947 break;
18948 case DW_FORM_udata:
18949 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18950 info_ptr += bytes_read;
18951 break;
18952 case DW_FORM_ref1:
9c541725 18953 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18954 + read_1_byte (abfd, info_ptr));
c906108c
SS
18955 info_ptr += 1;
18956 break;
18957 case DW_FORM_ref2:
9c541725 18958 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18959 + read_2_bytes (abfd, info_ptr));
c906108c
SS
18960 info_ptr += 2;
18961 break;
18962 case DW_FORM_ref4:
9c541725 18963 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18964 + read_4_bytes (abfd, info_ptr));
c906108c
SS
18965 info_ptr += 4;
18966 break;
613e1657 18967 case DW_FORM_ref8:
9c541725 18968 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18969 + read_8_bytes (abfd, info_ptr));
613e1657
KB
18970 info_ptr += 8;
18971 break;
55f1336d 18972 case DW_FORM_ref_sig8:
ac9ec31b 18973 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
18974 info_ptr += 8;
18975 break;
c906108c 18976 case DW_FORM_ref_udata:
9c541725 18977 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18978 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
18979 info_ptr += bytes_read;
18980 break;
c906108c 18981 case DW_FORM_indirect:
a8329558
KW
18982 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18983 info_ptr += bytes_read;
43988095
JK
18984 if (form == DW_FORM_implicit_const)
18985 {
18986 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18987 info_ptr += bytes_read;
18988 }
18989 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18990 info_ptr);
18991 break;
18992 case DW_FORM_implicit_const:
18993 DW_SND (attr) = implicit_const;
a8329558 18994 break;
3019eac3
DE
18995 case DW_FORM_GNU_addr_index:
18996 if (reader->dwo_file == NULL)
18997 {
18998 /* For now flag a hard error.
18999 Later we can turn this into a complaint. */
19000 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19001 dwarf_form_name (form),
19002 bfd_get_filename (abfd));
19003 }
19004 DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
19005 info_ptr += bytes_read;
19006 break;
19007 case DW_FORM_GNU_str_index:
19008 if (reader->dwo_file == NULL)
19009 {
19010 /* For now flag a hard error.
19011 Later we can turn this into a complaint if warranted. */
19012 error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
19013 dwarf_form_name (form),
19014 bfd_get_filename (abfd));
19015 }
19016 {
19017 ULONGEST str_index =
19018 read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19019
342587c4 19020 DW_STRING (attr) = read_str_index (reader, str_index);
3019eac3
DE
19021 DW_STRING_IS_CANONICAL (attr) = 0;
19022 info_ptr += bytes_read;
19023 }
19024 break;
c906108c 19025 default:
8a3fe4f8 19026 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19027 dwarf_form_name (form),
19028 bfd_get_filename (abfd));
c906108c 19029 }
28e94949 19030
36586728 19031 /* Super hack. */
7771576e 19032 if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
36586728
TT
19033 attr->form = DW_FORM_GNU_ref_alt;
19034
28e94949
JB
19035 /* We have seen instances where the compiler tried to emit a byte
19036 size attribute of -1 which ended up being encoded as an unsigned
19037 0xffffffff. Although 0xffffffff is technically a valid size value,
19038 an object of this size seems pretty unlikely so we can relatively
19039 safely treat these cases as if the size attribute was invalid and
19040 treat them as zero by default. */
19041 if (attr->name == DW_AT_byte_size
19042 && form == DW_FORM_data4
19043 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19044 {
19045 complaint
b98664d3 19046 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 19047 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19048 DW_UNSND (attr) = 0;
19049 }
28e94949 19050
c906108c
SS
19051 return info_ptr;
19052}
19053
a8329558
KW
19054/* Read an attribute described by an abbreviated attribute. */
19055
d521ce57 19056static const gdb_byte *
dee91e82
DE
19057read_attribute (const struct die_reader_specs *reader,
19058 struct attribute *attr, struct attr_abbrev *abbrev,
d521ce57 19059 const gdb_byte *info_ptr)
a8329558
KW
19060{
19061 attr->name = abbrev->name;
43988095
JK
19062 return read_attribute_value (reader, attr, abbrev->form,
19063 abbrev->implicit_const, info_ptr);
a8329558
KW
19064}
19065
0963b4bd 19066/* Read dwarf information from a buffer. */
c906108c
SS
19067
19068static unsigned int
a1855c1d 19069read_1_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19070{
fe1b8b76 19071 return bfd_get_8 (abfd, buf);
c906108c
SS
19072}
19073
19074static int
a1855c1d 19075read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
c906108c 19076{
fe1b8b76 19077 return bfd_get_signed_8 (abfd, buf);
c906108c
SS
19078}
19079
19080static unsigned int
a1855c1d 19081read_2_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19082{
fe1b8b76 19083 return bfd_get_16 (abfd, buf);
c906108c
SS
19084}
19085
21ae7a4d 19086static int
a1855c1d 19087read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19088{
19089 return bfd_get_signed_16 (abfd, buf);
19090}
19091
c906108c 19092static unsigned int
a1855c1d 19093read_4_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19094{
fe1b8b76 19095 return bfd_get_32 (abfd, buf);
c906108c
SS
19096}
19097
21ae7a4d 19098static int
a1855c1d 19099read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
21ae7a4d
JK
19100{
19101 return bfd_get_signed_32 (abfd, buf);
19102}
19103
93311388 19104static ULONGEST
a1855c1d 19105read_8_bytes (bfd *abfd, const gdb_byte *buf)
c906108c 19106{
fe1b8b76 19107 return bfd_get_64 (abfd, buf);
c906108c
SS
19108}
19109
19110static CORE_ADDR
d521ce57 19111read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
891d2f0b 19112 unsigned int *bytes_read)
c906108c 19113{
e7c27a73 19114 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19115 CORE_ADDR retval = 0;
19116
107d2387 19117 if (cu_header->signed_addr_p)
c906108c 19118 {
107d2387
AC
19119 switch (cu_header->addr_size)
19120 {
19121 case 2:
fe1b8b76 19122 retval = bfd_get_signed_16 (abfd, buf);
107d2387
AC
19123 break;
19124 case 4:
fe1b8b76 19125 retval = bfd_get_signed_32 (abfd, buf);
107d2387
AC
19126 break;
19127 case 8:
fe1b8b76 19128 retval = bfd_get_signed_64 (abfd, buf);
107d2387
AC
19129 break;
19130 default:
8e65ff28 19131 internal_error (__FILE__, __LINE__,
e2e0b3e5 19132 _("read_address: bad switch, signed [in module %s]"),
659b0389 19133 bfd_get_filename (abfd));
107d2387
AC
19134 }
19135 }
19136 else
19137 {
19138 switch (cu_header->addr_size)
19139 {
19140 case 2:
fe1b8b76 19141 retval = bfd_get_16 (abfd, buf);
107d2387
AC
19142 break;
19143 case 4:
fe1b8b76 19144 retval = bfd_get_32 (abfd, buf);
107d2387
AC
19145 break;
19146 case 8:
fe1b8b76 19147 retval = bfd_get_64 (abfd, buf);
107d2387
AC
19148 break;
19149 default:
8e65ff28 19150 internal_error (__FILE__, __LINE__,
a73c6dcd
MS
19151 _("read_address: bad switch, "
19152 "unsigned [in module %s]"),
659b0389 19153 bfd_get_filename (abfd));
107d2387 19154 }
c906108c 19155 }
64367e0a 19156
107d2387
AC
19157 *bytes_read = cu_header->addr_size;
19158 return retval;
c906108c
SS
19159}
19160
f7ef9339 19161/* Read the initial length from a section. The (draft) DWARF 3
613e1657
KB
19162 specification allows the initial length to take up either 4 bytes
19163 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
19164 bytes describe the length and all offsets will be 8 bytes in length
19165 instead of 4.
19166
f7ef9339
KB
19167 An older, non-standard 64-bit format is also handled by this
19168 function. The older format in question stores the initial length
19169 as an 8-byte quantity without an escape value. Lengths greater
19170 than 2^32 aren't very common which means that the initial 4 bytes
19171 is almost always zero. Since a length value of zero doesn't make
19172 sense for the 32-bit format, this initial zero can be considered to
19173 be an escape value which indicates the presence of the older 64-bit
19174 format. As written, the code can't detect (old format) lengths
917c78fc
MK
19175 greater than 4GB. If it becomes necessary to handle lengths
19176 somewhat larger than 4GB, we could allow other small values (such
19177 as the non-sensical values of 1, 2, and 3) to also be used as
19178 escape values indicating the presence of the old format.
f7ef9339 19179
917c78fc
MK
19180 The value returned via bytes_read should be used to increment the
19181 relevant pointer after calling read_initial_length().
c764a876 19182
613e1657
KB
19183 [ Note: read_initial_length() and read_offset() are based on the
19184 document entitled "DWARF Debugging Information Format", revision
f7ef9339 19185 3, draft 8, dated November 19, 2001. This document was obtained
613e1657
KB
19186 from:
19187
f7ef9339 19188 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
6e70227d 19189
613e1657
KB
19190 This document is only a draft and is subject to change. (So beware.)
19191
f7ef9339 19192 Details regarding the older, non-standard 64-bit format were
917c78fc
MK
19193 determined empirically by examining 64-bit ELF files produced by
19194 the SGI toolchain on an IRIX 6.5 machine.
f7ef9339
KB
19195
19196 - Kevin, July 16, 2002
613e1657
KB
19197 ] */
19198
19199static LONGEST
d521ce57 19200read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
613e1657 19201{
fe1b8b76 19202 LONGEST length = bfd_get_32 (abfd, buf);
613e1657 19203
dd373385 19204 if (length == 0xffffffff)
613e1657 19205 {
fe1b8b76 19206 length = bfd_get_64 (abfd, buf + 4);
613e1657 19207 *bytes_read = 12;
613e1657 19208 }
dd373385 19209 else if (length == 0)
f7ef9339 19210 {
dd373385 19211 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
fe1b8b76 19212 length = bfd_get_64 (abfd, buf);
f7ef9339 19213 *bytes_read = 8;
f7ef9339 19214 }
613e1657
KB
19215 else
19216 {
19217 *bytes_read = 4;
613e1657
KB
19218 }
19219
c764a876
DE
19220 return length;
19221}
dd373385 19222
c764a876
DE
19223/* Cover function for read_initial_length.
19224 Returns the length of the object at BUF, and stores the size of the
19225 initial length in *BYTES_READ and stores the size that offsets will be in
19226 *OFFSET_SIZE.
19227 If the initial length size is not equivalent to that specified in
19228 CU_HEADER then issue a complaint.
19229 This is useful when reading non-comp-unit headers. */
dd373385 19230
c764a876 19231static LONGEST
d521ce57 19232read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
c764a876
DE
19233 const struct comp_unit_head *cu_header,
19234 unsigned int *bytes_read,
19235 unsigned int *offset_size)
19236{
19237 LONGEST length = read_initial_length (abfd, buf, bytes_read);
19238
19239 gdb_assert (cu_header->initial_length_size == 4
19240 || cu_header->initial_length_size == 8
19241 || cu_header->initial_length_size == 12);
19242
19243 if (cu_header->initial_length_size != *bytes_read)
b98664d3 19244 complaint (_("intermixed 32-bit and 64-bit DWARF sections"));
dd373385 19245
c764a876 19246 *offset_size = (*bytes_read == 4) ? 4 : 8;
dd373385 19247 return length;
613e1657
KB
19248}
19249
19250/* Read an offset from the data stream. The size of the offset is
917c78fc 19251 given by cu_header->offset_size. */
613e1657
KB
19252
19253static LONGEST
d521ce57
TT
19254read_offset (bfd *abfd, const gdb_byte *buf,
19255 const struct comp_unit_head *cu_header,
891d2f0b 19256 unsigned int *bytes_read)
c764a876
DE
19257{
19258 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9a619af0 19259
c764a876
DE
19260 *bytes_read = cu_header->offset_size;
19261 return offset;
19262}
19263
19264/* Read an offset from the data stream. */
19265
19266static LONGEST
d521ce57 19267read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
613e1657
KB
19268{
19269 LONGEST retval = 0;
19270
c764a876 19271 switch (offset_size)
613e1657
KB
19272 {
19273 case 4:
fe1b8b76 19274 retval = bfd_get_32 (abfd, buf);
613e1657
KB
19275 break;
19276 case 8:
fe1b8b76 19277 retval = bfd_get_64 (abfd, buf);
613e1657
KB
19278 break;
19279 default:
8e65ff28 19280 internal_error (__FILE__, __LINE__,
c764a876 19281 _("read_offset_1: bad switch [in module %s]"),
659b0389 19282 bfd_get_filename (abfd));
613e1657
KB
19283 }
19284
917c78fc 19285 return retval;
613e1657
KB
19286}
19287
d521ce57
TT
19288static const gdb_byte *
19289read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
c906108c
SS
19290{
19291 /* If the size of a host char is 8 bits, we can return a pointer
19292 to the buffer, otherwise we have to copy the data to a buffer
19293 allocated on the temporary obstack. */
4bdf3d34 19294 gdb_assert (HOST_CHAR_BIT == 8);
c906108c 19295 return buf;
c906108c
SS
19296}
19297
d521ce57
TT
19298static const char *
19299read_direct_string (bfd *abfd, const gdb_byte *buf,
19300 unsigned int *bytes_read_ptr)
c906108c
SS
19301{
19302 /* If the size of a host char is 8 bits, we can return a pointer
19303 to the string, otherwise we have to copy the string to a buffer
19304 allocated on the temporary obstack. */
4bdf3d34 19305 gdb_assert (HOST_CHAR_BIT == 8);
c906108c
SS
19306 if (*buf == '\0')
19307 {
19308 *bytes_read_ptr = 1;
19309 return NULL;
19310 }
d521ce57
TT
19311 *bytes_read_ptr = strlen ((const char *) buf) + 1;
19312 return (const char *) buf;
4bdf3d34
JJ
19313}
19314
43988095
JK
19315/* Return pointer to string at section SECT offset STR_OFFSET with error
19316 reporting strings FORM_NAME and SECT_NAME. */
19317
d521ce57 19318static const char *
ed2dc618
SM
19319read_indirect_string_at_offset_from (struct objfile *objfile,
19320 bfd *abfd, LONGEST str_offset,
43988095
JK
19321 struct dwarf2_section_info *sect,
19322 const char *form_name,
19323 const char *sect_name)
19324{
ed2dc618 19325 dwarf2_read_section (objfile, sect);
43988095
JK
19326 if (sect->buffer == NULL)
19327 error (_("%s used without %s section [in module %s]"),
19328 form_name, sect_name, bfd_get_filename (abfd));
19329 if (str_offset >= sect->size)
19330 error (_("%s pointing outside of %s section [in module %s]"),
19331 form_name, sect_name, bfd_get_filename (abfd));
4bdf3d34 19332 gdb_assert (HOST_CHAR_BIT == 8);
43988095 19333 if (sect->buffer[str_offset] == '\0')
4bdf3d34 19334 return NULL;
43988095
JK
19335 return (const char *) (sect->buffer + str_offset);
19336}
19337
19338/* Return pointer to string at .debug_str offset STR_OFFSET. */
19339
19340static const char *
ed2dc618
SM
19341read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19342 bfd *abfd, LONGEST str_offset)
43988095 19343{
ed2dc618
SM
19344 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19345 abfd, str_offset,
43988095
JK
19346 &dwarf2_per_objfile->str,
19347 "DW_FORM_strp", ".debug_str");
19348}
19349
19350/* Return pointer to string at .debug_line_str offset STR_OFFSET. */
19351
19352static const char *
ed2dc618
SM
19353read_indirect_line_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
19354 bfd *abfd, LONGEST str_offset)
43988095 19355{
ed2dc618
SM
19356 return read_indirect_string_at_offset_from (dwarf2_per_objfile->objfile,
19357 abfd, str_offset,
43988095
JK
19358 &dwarf2_per_objfile->line_str,
19359 "DW_FORM_line_strp",
19360 ".debug_line_str");
c906108c
SS
19361}
19362
36586728
TT
19363/* Read a string at offset STR_OFFSET in the .debug_str section from
19364 the .dwz file DWZ. Throw an error if the offset is too large. If
19365 the string consists of a single NUL byte, return NULL; otherwise
19366 return a pointer to the string. */
19367
d521ce57 19368static const char *
ed2dc618
SM
19369read_indirect_string_from_dwz (struct objfile *objfile, struct dwz_file *dwz,
19370 LONGEST str_offset)
36586728 19371{
ed2dc618 19372 dwarf2_read_section (objfile, &dwz->str);
36586728
TT
19373
19374 if (dwz->str.buffer == NULL)
19375 error (_("DW_FORM_GNU_strp_alt used without .debug_str "
19376 "section [in module %s]"),
19377 bfd_get_filename (dwz->dwz_bfd));
19378 if (str_offset >= dwz->str.size)
19379 error (_("DW_FORM_GNU_strp_alt pointing outside of "
19380 ".debug_str section [in module %s]"),
19381 bfd_get_filename (dwz->dwz_bfd));
19382 gdb_assert (HOST_CHAR_BIT == 8);
19383 if (dwz->str.buffer[str_offset] == '\0')
19384 return NULL;
d521ce57 19385 return (const char *) (dwz->str.buffer + str_offset);
36586728
TT
19386}
19387
43988095
JK
19388/* Return pointer to string at .debug_str offset as read from BUF.
19389 BUF is assumed to be in a compilation unit described by CU_HEADER.
19390 Return *BYTES_READ_PTR count of bytes read from BUF. */
19391
d521ce57 19392static const char *
ed2dc618
SM
19393read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19394 const gdb_byte *buf,
cf2c3c16
TT
19395 const struct comp_unit_head *cu_header,
19396 unsigned int *bytes_read_ptr)
19397{
19398 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19399
ed2dc618 19400 return read_indirect_string_at_offset (dwarf2_per_objfile, abfd, str_offset);
cf2c3c16
TT
19401}
19402
43988095
JK
19403/* Return pointer to string at .debug_line_str offset as read from BUF.
19404 BUF is assumed to be in a compilation unit described by CU_HEADER.
19405 Return *BYTES_READ_PTR count of bytes read from BUF. */
19406
19407static const char *
ed2dc618
SM
19408read_indirect_line_string (struct dwarf2_per_objfile *dwarf2_per_objfile,
19409 bfd *abfd, const gdb_byte *buf,
43988095
JK
19410 const struct comp_unit_head *cu_header,
19411 unsigned int *bytes_read_ptr)
19412{
19413 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
19414
ed2dc618
SM
19415 return read_indirect_line_string_at_offset (dwarf2_per_objfile, abfd,
19416 str_offset);
43988095
JK
19417}
19418
19419ULONGEST
d521ce57 19420read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
43988095 19421 unsigned int *bytes_read_ptr)
c906108c 19422{
12df843f 19423 ULONGEST result;
ce5d95e1 19424 unsigned int num_read;
870f88f7 19425 int shift;
c906108c
SS
19426 unsigned char byte;
19427
19428 result = 0;
19429 shift = 0;
19430 num_read = 0;
c906108c
SS
19431 while (1)
19432 {
fe1b8b76 19433 byte = bfd_get_8 (abfd, buf);
c906108c
SS
19434 buf++;
19435 num_read++;
12df843f 19436 result |= ((ULONGEST) (byte & 127) << shift);
c906108c
SS
19437 if ((byte & 128) == 0)
19438 {
19439 break;
19440 }
19441 shift += 7;
19442 }
19443 *bytes_read_ptr = num_read;
19444 return result;
19445}
19446
12df843f 19447static LONGEST
d521ce57
TT
19448read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
19449 unsigned int *bytes_read_ptr)
c906108c 19450{
12df843f 19451 LONGEST result;
870f88f7 19452 int shift, num_read;
c906108c
SS
19453 unsigned char byte;
19454
19455 result = 0;
19456 shift = 0;
c906108c 19457 num_read = 0;
c906108c
SS
19458 while (1)
19459 {
fe1b8b76 19460 byte = bfd_get_8 (abfd, buf);
c906108c
SS
19461 buf++;
19462 num_read++;
12df843f 19463 result |= ((LONGEST) (byte & 127) << shift);
c906108c
SS
19464 shift += 7;
19465 if ((byte & 128) == 0)
19466 {
19467 break;
19468 }
19469 }
77e0b926 19470 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
12df843f 19471 result |= -(((LONGEST) 1) << shift);
c906108c
SS
19472 *bytes_read_ptr = num_read;
19473 return result;
19474}
19475
3019eac3
DE
19476/* Given index ADDR_INDEX in .debug_addr, fetch the value.
19477 ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
19478 ADDR_SIZE is the size of addresses from the CU header. */
19479
19480static CORE_ADDR
ed2dc618
SM
19481read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
19482 unsigned int addr_index, ULONGEST addr_base, int addr_size)
3019eac3
DE
19483{
19484 struct objfile *objfile = dwarf2_per_objfile->objfile;
19485 bfd *abfd = objfile->obfd;
19486 const gdb_byte *info_ptr;
19487
19488 dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
19489 if (dwarf2_per_objfile->addr.buffer == NULL)
19490 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 19491 objfile_name (objfile));
3019eac3
DE
19492 if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
19493 error (_("DW_FORM_addr_index pointing outside of "
19494 ".debug_addr section [in module %s]"),
4262abfb 19495 objfile_name (objfile));
3019eac3
DE
19496 info_ptr = (dwarf2_per_objfile->addr.buffer
19497 + addr_base + addr_index * addr_size);
19498 if (addr_size == 4)
19499 return bfd_get_32 (abfd, info_ptr);
19500 else
19501 return bfd_get_64 (abfd, info_ptr);
19502}
19503
19504/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19505
19506static CORE_ADDR
19507read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19508{
518817b3
SM
19509 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19510 cu->addr_base, cu->header.addr_size);
3019eac3
DE
19511}
19512
19513/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19514
19515static CORE_ADDR
d521ce57 19516read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
19517 unsigned int *bytes_read)
19518{
518817b3 19519 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
3019eac3
DE
19520 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19521
19522 return read_addr_index (cu, addr_index);
19523}
19524
19525/* Data structure to pass results from dwarf2_read_addr_index_reader
19526 back to dwarf2_read_addr_index. */
19527
19528struct dwarf2_read_addr_index_data
19529{
19530 ULONGEST addr_base;
19531 int addr_size;
19532};
19533
19534/* die_reader_func for dwarf2_read_addr_index. */
19535
19536static void
19537dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
d521ce57 19538 const gdb_byte *info_ptr,
3019eac3
DE
19539 struct die_info *comp_unit_die,
19540 int has_children,
19541 void *data)
19542{
19543 struct dwarf2_cu *cu = reader->cu;
19544 struct dwarf2_read_addr_index_data *aidata =
19545 (struct dwarf2_read_addr_index_data *) data;
19546
19547 aidata->addr_base = cu->addr_base;
19548 aidata->addr_size = cu->header.addr_size;
19549}
19550
19551/* Given an index in .debug_addr, fetch the value.
19552 NOTE: This can be called during dwarf expression evaluation,
19553 long after the debug information has been read, and thus per_cu->cu
19554 may no longer exist. */
19555
19556CORE_ADDR
19557dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
19558 unsigned int addr_index)
19559{
ed2dc618 19560 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3
DE
19561 struct dwarf2_cu *cu = per_cu->cu;
19562 ULONGEST addr_base;
19563 int addr_size;
19564
3019eac3
DE
19565 /* We need addr_base and addr_size.
19566 If we don't have PER_CU->cu, we have to get it.
19567 Nasty, but the alternative is storing the needed info in PER_CU,
19568 which at this point doesn't seem justified: it's not clear how frequently
19569 it would get used and it would increase the size of every PER_CU.
19570 Entry points like dwarf2_per_cu_addr_size do a similar thing
19571 so we're not in uncharted territory here.
19572 Alas we need to be a bit more complicated as addr_base is contained
19573 in the DIE.
19574
19575 We don't need to read the entire CU(/TU).
19576 We just need the header and top level die.
a1b64ce1 19577
3019eac3 19578 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 19579 For now we skip this optimization. */
3019eac3
DE
19580
19581 if (cu != NULL)
19582 {
19583 addr_base = cu->addr_base;
19584 addr_size = cu->header.addr_size;
19585 }
19586 else
19587 {
19588 struct dwarf2_read_addr_index_data aidata;
19589
a1b64ce1
DE
19590 /* Note: We can't use init_cutu_and_read_dies_simple here,
19591 we need addr_base. */
58f0c718 19592 init_cutu_and_read_dies (per_cu, NULL, 0, 0, false,
a1b64ce1 19593 dwarf2_read_addr_index_reader, &aidata);
3019eac3
DE
19594 addr_base = aidata.addr_base;
19595 addr_size = aidata.addr_size;
19596 }
19597
ed2dc618
SM
19598 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19599 addr_size);
3019eac3
DE
19600}
19601
57d63ce2
DE
19602/* Given a DW_FORM_GNU_str_index, fetch the string.
19603 This is only used by the Fission support. */
3019eac3 19604
d521ce57 19605static const char *
342587c4 19606read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
3019eac3 19607{
ed2dc618 19608 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
19609 struct dwarf2_per_objfile *dwarf2_per_objfile
19610 = cu->per_cu->dwarf2_per_objfile;
3019eac3 19611 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 19612 const char *objf_name = objfile_name (objfile);
3019eac3 19613 bfd *abfd = objfile->obfd;
73869dc2
DE
19614 struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
19615 struct dwarf2_section_info *str_offsets_section =
19616 &reader->dwo_file->sections.str_offsets;
d521ce57 19617 const gdb_byte *info_ptr;
3019eac3 19618 ULONGEST str_offset;
57d63ce2 19619 static const char form_name[] = "DW_FORM_GNU_str_index";
3019eac3 19620
73869dc2
DE
19621 dwarf2_read_section (objfile, str_section);
19622 dwarf2_read_section (objfile, str_offsets_section);
19623 if (str_section->buffer == NULL)
57d63ce2 19624 error (_("%s used without .debug_str.dwo section"
9d8780f0
SM
19625 " in CU at offset %s [in module %s]"),
19626 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19627 if (str_offsets_section->buffer == NULL)
57d63ce2 19628 error (_("%s used without .debug_str_offsets.dwo section"
9d8780f0
SM
19629 " in CU at offset %s [in module %s]"),
19630 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19631 if (str_index * cu->header.offset_size >= str_offsets_section->size)
57d63ce2 19632 error (_("%s pointing outside of .debug_str_offsets.dwo"
9d8780f0
SM
19633 " section in CU at offset %s [in module %s]"),
19634 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19635 info_ptr = (str_offsets_section->buffer
3019eac3
DE
19636 + str_index * cu->header.offset_size);
19637 if (cu->header.offset_size == 4)
19638 str_offset = bfd_get_32 (abfd, info_ptr);
19639 else
19640 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 19641 if (str_offset >= str_section->size)
57d63ce2 19642 error (_("Offset from %s pointing outside of"
9d8780f0
SM
19643 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19644 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19645 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
19646}
19647
3019eac3
DE
19648/* Return the length of an LEB128 number in BUF. */
19649
19650static int
19651leb128_size (const gdb_byte *buf)
19652{
19653 const gdb_byte *begin = buf;
19654 gdb_byte byte;
19655
19656 while (1)
19657 {
19658 byte = *buf++;
19659 if ((byte & 128) == 0)
19660 return buf - begin;
19661 }
19662}
19663
c906108c 19664static void
e142c38c 19665set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
19666{
19667 switch (lang)
19668 {
19669 case DW_LANG_C89:
76bee0cc 19670 case DW_LANG_C99:
0cfd832f 19671 case DW_LANG_C11:
c906108c 19672 case DW_LANG_C:
d1be3247 19673 case DW_LANG_UPC:
e142c38c 19674 cu->language = language_c;
c906108c 19675 break;
9c37b5ae 19676 case DW_LANG_Java:
c906108c 19677 case DW_LANG_C_plus_plus:
0cfd832f
MW
19678 case DW_LANG_C_plus_plus_11:
19679 case DW_LANG_C_plus_plus_14:
e142c38c 19680 cu->language = language_cplus;
c906108c 19681 break;
6aecb9c2
JB
19682 case DW_LANG_D:
19683 cu->language = language_d;
19684 break;
c906108c
SS
19685 case DW_LANG_Fortran77:
19686 case DW_LANG_Fortran90:
b21b22e0 19687 case DW_LANG_Fortran95:
f7de9aab
MW
19688 case DW_LANG_Fortran03:
19689 case DW_LANG_Fortran08:
e142c38c 19690 cu->language = language_fortran;
c906108c 19691 break;
a766d390
DE
19692 case DW_LANG_Go:
19693 cu->language = language_go;
19694 break;
c906108c 19695 case DW_LANG_Mips_Assembler:
e142c38c 19696 cu->language = language_asm;
c906108c
SS
19697 break;
19698 case DW_LANG_Ada83:
8aaf0b47 19699 case DW_LANG_Ada95:
bc5f45f8
JB
19700 cu->language = language_ada;
19701 break;
72019c9c
GM
19702 case DW_LANG_Modula2:
19703 cu->language = language_m2;
19704 break;
fe8e67fd
PM
19705 case DW_LANG_Pascal83:
19706 cu->language = language_pascal;
19707 break;
22566fbd
DJ
19708 case DW_LANG_ObjC:
19709 cu->language = language_objc;
19710 break;
c44af4eb
TT
19711 case DW_LANG_Rust:
19712 case DW_LANG_Rust_old:
19713 cu->language = language_rust;
19714 break;
c906108c
SS
19715 case DW_LANG_Cobol74:
19716 case DW_LANG_Cobol85:
c906108c 19717 default:
e142c38c 19718 cu->language = language_minimal;
c906108c
SS
19719 break;
19720 }
e142c38c 19721 cu->language_defn = language_def (cu->language);
c906108c
SS
19722}
19723
19724/* Return the named attribute or NULL if not there. */
19725
19726static struct attribute *
e142c38c 19727dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 19728{
a48e046c 19729 for (;;)
c906108c 19730 {
a48e046c
TT
19731 unsigned int i;
19732 struct attribute *spec = NULL;
19733
19734 for (i = 0; i < die->num_attrs; ++i)
19735 {
19736 if (die->attrs[i].name == name)
19737 return &die->attrs[i];
19738 if (die->attrs[i].name == DW_AT_specification
19739 || die->attrs[i].name == DW_AT_abstract_origin)
19740 spec = &die->attrs[i];
19741 }
19742
19743 if (!spec)
19744 break;
c906108c 19745
f2f0e013 19746 die = follow_die_ref (die, spec, &cu);
f2f0e013 19747 }
c5aa993b 19748
c906108c
SS
19749 return NULL;
19750}
19751
348e048f
DE
19752/* Return the named attribute or NULL if not there,
19753 but do not follow DW_AT_specification, etc.
19754 This is for use in contexts where we're reading .debug_types dies.
19755 Following DW_AT_specification, DW_AT_abstract_origin will take us
19756 back up the chain, and we want to go down. */
19757
19758static struct attribute *
45e58e77 19759dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
348e048f
DE
19760{
19761 unsigned int i;
19762
19763 for (i = 0; i < die->num_attrs; ++i)
19764 if (die->attrs[i].name == name)
19765 return &die->attrs[i];
19766
19767 return NULL;
19768}
19769
7d45c7c3
KB
19770/* Return the string associated with a string-typed attribute, or NULL if it
19771 is either not found or is of an incorrect type. */
19772
19773static const char *
19774dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19775{
19776 struct attribute *attr;
19777 const char *str = NULL;
19778
19779 attr = dwarf2_attr (die, name, cu);
19780
19781 if (attr != NULL)
19782 {
43988095 19783 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438
L
19784 || attr->form == DW_FORM_string
19785 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 19786 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
19787 str = DW_STRING (attr);
19788 else
b98664d3 19789 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
19790 "DIE at %s in module %s"),
19791 dwarf_attr_name (name), sect_offset_str (die->sect_off),
518817b3 19792 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7d45c7c3
KB
19793 }
19794
19795 return str;
19796}
19797
05cf31d1
JB
19798/* Return non-zero iff the attribute NAME is defined for the given DIE,
19799 and holds a non-zero value. This function should only be used for
2dc7f7b3 19800 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
19801
19802static int
19803dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19804{
19805 struct attribute *attr = dwarf2_attr (die, name, cu);
19806
19807 return (attr && DW_UNSND (attr));
19808}
19809
3ca72b44 19810static int
e142c38c 19811die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 19812{
05cf31d1
JB
19813 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19814 which value is non-zero. However, we have to be careful with
19815 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19816 (via dwarf2_flag_true_p) follows this attribute. So we may
19817 end up accidently finding a declaration attribute that belongs
19818 to a different DIE referenced by the specification attribute,
19819 even though the given DIE does not have a declaration attribute. */
19820 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19821 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
19822}
19823
63d06c5c 19824/* Return the die giving the specification for DIE, if there is
f2f0e013 19825 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
19826 containing the return value on output. If there is no
19827 specification, but there is an abstract origin, that is
19828 returned. */
63d06c5c
DC
19829
19830static struct die_info *
f2f0e013 19831die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 19832{
f2f0e013
DJ
19833 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19834 *spec_cu);
63d06c5c 19835
edb3359d
DJ
19836 if (spec_attr == NULL)
19837 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19838
63d06c5c
DC
19839 if (spec_attr == NULL)
19840 return NULL;
19841 else
f2f0e013 19842 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 19843}
c906108c 19844
527f3840
JK
19845/* Stub for free_line_header to match void * callback types. */
19846
19847static void
19848free_line_header_voidp (void *arg)
19849{
9a3c8263 19850 struct line_header *lh = (struct line_header *) arg;
527f3840 19851
fff8551c 19852 delete lh;
527f3840
JK
19853}
19854
fff8551c
PA
19855void
19856line_header::add_include_dir (const char *include_dir)
c906108c 19857{
27e0867f 19858 if (dwarf_line_debug >= 2)
fff8551c
PA
19859 fprintf_unfiltered (gdb_stdlog, "Adding dir %zu: %s\n",
19860 include_dirs.size () + 1, include_dir);
27e0867f 19861
fff8551c 19862 include_dirs.push_back (include_dir);
debd256d 19863}
6e70227d 19864
fff8551c
PA
19865void
19866line_header::add_file_name (const char *name,
ecfb656c 19867 dir_index d_index,
fff8551c
PA
19868 unsigned int mod_time,
19869 unsigned int length)
debd256d 19870{
27e0867f
DE
19871 if (dwarf_line_debug >= 2)
19872 fprintf_unfiltered (gdb_stdlog, "Adding file %u: %s\n",
fff8551c 19873 (unsigned) file_names.size () + 1, name);
27e0867f 19874
ecfb656c 19875 file_names.emplace_back (name, d_index, mod_time, length);
debd256d 19876}
6e70227d 19877
83769d0b 19878/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
19879
19880static struct dwarf2_section_info *
19881get_debug_line_section (struct dwarf2_cu *cu)
19882{
19883 struct dwarf2_section_info *section;
518817b3
SM
19884 struct dwarf2_per_objfile *dwarf2_per_objfile
19885 = cu->per_cu->dwarf2_per_objfile;
36586728
TT
19886
19887 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19888 DWO file. */
19889 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19890 section = &cu->dwo_unit->dwo_file->sections.line;
19891 else if (cu->per_cu->is_dwz)
19892 {
ed2dc618 19893 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
19894
19895 section = &dwz->line;
19896 }
19897 else
19898 section = &dwarf2_per_objfile->line;
19899
19900 return section;
19901}
19902
43988095
JK
19903/* Read directory or file name entry format, starting with byte of
19904 format count entries, ULEB128 pairs of entry formats, ULEB128 of
19905 entries count and the entries themselves in the described entry
19906 format. */
19907
19908static void
ed2dc618
SM
19909read_formatted_entries (struct dwarf2_per_objfile *dwarf2_per_objfile,
19910 bfd *abfd, const gdb_byte **bufp,
43988095
JK
19911 struct line_header *lh,
19912 const struct comp_unit_head *cu_header,
19913 void (*callback) (struct line_header *lh,
19914 const char *name,
ecfb656c 19915 dir_index d_index,
43988095
JK
19916 unsigned int mod_time,
19917 unsigned int length))
19918{
19919 gdb_byte format_count, formati;
19920 ULONGEST data_count, datai;
19921 const gdb_byte *buf = *bufp;
19922 const gdb_byte *format_header_data;
43988095
JK
19923 unsigned int bytes_read;
19924
19925 format_count = read_1_byte (abfd, buf);
19926 buf += 1;
19927 format_header_data = buf;
19928 for (formati = 0; formati < format_count; formati++)
19929 {
19930 read_unsigned_leb128 (abfd, buf, &bytes_read);
19931 buf += bytes_read;
19932 read_unsigned_leb128 (abfd, buf, &bytes_read);
19933 buf += bytes_read;
19934 }
19935
19936 data_count = read_unsigned_leb128 (abfd, buf, &bytes_read);
19937 buf += bytes_read;
19938 for (datai = 0; datai < data_count; datai++)
19939 {
19940 const gdb_byte *format = format_header_data;
19941 struct file_entry fe;
19942
43988095
JK
19943 for (formati = 0; formati < format_count; formati++)
19944 {
ecfb656c 19945 ULONGEST content_type = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 19946 format += bytes_read;
43988095 19947
ecfb656c 19948 ULONGEST form = read_unsigned_leb128 (abfd, format, &bytes_read);
43988095 19949 format += bytes_read;
ecfb656c
PA
19950
19951 gdb::optional<const char *> string;
19952 gdb::optional<unsigned int> uint;
19953
43988095
JK
19954 switch (form)
19955 {
19956 case DW_FORM_string:
ecfb656c 19957 string.emplace (read_direct_string (abfd, buf, &bytes_read));
43988095
JK
19958 buf += bytes_read;
19959 break;
19960
19961 case DW_FORM_line_strp:
ed2dc618
SM
19962 string.emplace (read_indirect_line_string (dwarf2_per_objfile,
19963 abfd, buf,
ecfb656c
PA
19964 cu_header,
19965 &bytes_read));
43988095
JK
19966 buf += bytes_read;
19967 break;
19968
19969 case DW_FORM_data1:
ecfb656c 19970 uint.emplace (read_1_byte (abfd, buf));
43988095
JK
19971 buf += 1;
19972 break;
19973
19974 case DW_FORM_data2:
ecfb656c 19975 uint.emplace (read_2_bytes (abfd, buf));
43988095
JK
19976 buf += 2;
19977 break;
19978
19979 case DW_FORM_data4:
ecfb656c 19980 uint.emplace (read_4_bytes (abfd, buf));
43988095
JK
19981 buf += 4;
19982 break;
19983
19984 case DW_FORM_data8:
ecfb656c 19985 uint.emplace (read_8_bytes (abfd, buf));
43988095
JK
19986 buf += 8;
19987 break;
19988
19989 case DW_FORM_udata:
ecfb656c 19990 uint.emplace (read_unsigned_leb128 (abfd, buf, &bytes_read));
43988095
JK
19991 buf += bytes_read;
19992 break;
19993
19994 case DW_FORM_block:
19995 /* It is valid only for DW_LNCT_timestamp which is ignored by
19996 current GDB. */
19997 break;
19998 }
ecfb656c
PA
19999
20000 switch (content_type)
20001 {
20002 case DW_LNCT_path:
20003 if (string.has_value ())
20004 fe.name = *string;
20005 break;
20006 case DW_LNCT_directory_index:
20007 if (uint.has_value ())
20008 fe.d_index = (dir_index) *uint;
20009 break;
20010 case DW_LNCT_timestamp:
20011 if (uint.has_value ())
20012 fe.mod_time = *uint;
20013 break;
20014 case DW_LNCT_size:
20015 if (uint.has_value ())
20016 fe.length = *uint;
20017 break;
20018 case DW_LNCT_MD5:
20019 break;
20020 default:
b98664d3 20021 complaint (_("Unknown format content type %s"),
ecfb656c
PA
20022 pulongest (content_type));
20023 }
43988095
JK
20024 }
20025
ecfb656c 20026 callback (lh, fe.name, fe.d_index, fe.mod_time, fe.length);
43988095
JK
20027 }
20028
20029 *bufp = buf;
20030}
20031
debd256d 20032/* Read the statement program header starting at OFFSET in
3019eac3 20033 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 20034 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
20035 Returns NULL if there is a problem reading the header, e.g., if it
20036 has a version we don't understand.
debd256d
JB
20037
20038 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
20039 the returned object point into the dwarf line section buffer,
20040 and must not be freed. */
ae2de4f8 20041
fff8551c 20042static line_header_up
9c541725 20043dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 20044{
d521ce57 20045 const gdb_byte *line_ptr;
c764a876 20046 unsigned int bytes_read, offset_size;
debd256d 20047 int i;
d521ce57 20048 const char *cur_dir, *cur_file;
3019eac3
DE
20049 struct dwarf2_section_info *section;
20050 bfd *abfd;
518817b3
SM
20051 struct dwarf2_per_objfile *dwarf2_per_objfile
20052 = cu->per_cu->dwarf2_per_objfile;
3019eac3 20053
36586728 20054 section = get_debug_line_section (cu);
3019eac3
DE
20055 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20056 if (section->buffer == NULL)
debd256d 20057 {
3019eac3 20058 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 20059 complaint (_("missing .debug_line.dwo section"));
3019eac3 20060 else
b98664d3 20061 complaint (_("missing .debug_line section"));
debd256d
JB
20062 return 0;
20063 }
20064
fceca515
DE
20065 /* We can't do this until we know the section is non-empty.
20066 Only then do we know we have such a section. */
a32a8923 20067 abfd = get_section_bfd_owner (section);
fceca515 20068
a738430d
MK
20069 /* Make sure that at least there's room for the total_length field.
20070 That could be 12 bytes long, but we're just going to fudge that. */
9c541725 20071 if (to_underlying (sect_off) + 4 >= section->size)
debd256d 20072 {
4d3c2250 20073 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20074 return 0;
20075 }
20076
fff8551c 20077 line_header_up lh (new line_header ());
debd256d 20078
9c541725 20079 lh->sect_off = sect_off;
527f3840
JK
20080 lh->offset_in_dwz = cu->per_cu->is_dwz;
20081
9c541725 20082 line_ptr = section->buffer + to_underlying (sect_off);
debd256d 20083
a738430d 20084 /* Read in the header. */
6e70227d 20085 lh->total_length =
c764a876
DE
20086 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
20087 &bytes_read, &offset_size);
debd256d 20088 line_ptr += bytes_read;
3019eac3 20089 if (line_ptr + lh->total_length > (section->buffer + section->size))
debd256d 20090 {
4d3c2250 20091 dwarf2_statement_list_fits_in_line_number_section_complaint ();
debd256d
JB
20092 return 0;
20093 }
20094 lh->statement_program_end = line_ptr + lh->total_length;
20095 lh->version = read_2_bytes (abfd, line_ptr);
20096 line_ptr += 2;
43988095 20097 if (lh->version > 5)
cd366ee8
DE
20098 {
20099 /* This is a version we don't understand. The format could have
20100 changed in ways we don't handle properly so just punt. */
b98664d3 20101 complaint (_("unsupported version in .debug_line section"));
cd366ee8
DE
20102 return NULL;
20103 }
43988095
JK
20104 if (lh->version >= 5)
20105 {
20106 gdb_byte segment_selector_size;
20107
20108 /* Skip address size. */
20109 read_1_byte (abfd, line_ptr);
20110 line_ptr += 1;
20111
20112 segment_selector_size = read_1_byte (abfd, line_ptr);
20113 line_ptr += 1;
20114 if (segment_selector_size != 0)
20115 {
b98664d3 20116 complaint (_("unsupported segment selector size %u "
43988095
JK
20117 "in .debug_line section"),
20118 segment_selector_size);
20119 return NULL;
20120 }
20121 }
c764a876
DE
20122 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
20123 line_ptr += offset_size;
debd256d
JB
20124 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
20125 line_ptr += 1;
2dc7f7b3
TT
20126 if (lh->version >= 4)
20127 {
20128 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
20129 line_ptr += 1;
20130 }
20131 else
20132 lh->maximum_ops_per_instruction = 1;
20133
20134 if (lh->maximum_ops_per_instruction == 0)
20135 {
20136 lh->maximum_ops_per_instruction = 1;
b98664d3 20137 complaint (_("invalid maximum_ops_per_instruction "
3e43a32a 20138 "in `.debug_line' section"));
2dc7f7b3
TT
20139 }
20140
debd256d
JB
20141 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
20142 line_ptr += 1;
20143 lh->line_base = read_1_signed_byte (abfd, line_ptr);
20144 line_ptr += 1;
20145 lh->line_range = read_1_byte (abfd, line_ptr);
20146 line_ptr += 1;
20147 lh->opcode_base = read_1_byte (abfd, line_ptr);
20148 line_ptr += 1;
fff8551c 20149 lh->standard_opcode_lengths.reset (new unsigned char[lh->opcode_base]);
debd256d
JB
20150
20151 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
20152 for (i = 1; i < lh->opcode_base; ++i)
20153 {
20154 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
20155 line_ptr += 1;
20156 }
20157
43988095 20158 if (lh->version >= 5)
debd256d 20159 {
43988095 20160 /* Read directory table. */
ed2dc618
SM
20161 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20162 &cu->header,
fff8551c 20163 [] (struct line_header *lh, const char *name,
ecfb656c 20164 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20165 unsigned int length)
20166 {
20167 lh->add_include_dir (name);
20168 });
debd256d 20169
43988095 20170 /* Read file name table. */
ed2dc618
SM
20171 read_formatted_entries (dwarf2_per_objfile, abfd, &line_ptr, lh.get (),
20172 &cu->header,
fff8551c 20173 [] (struct line_header *lh, const char *name,
ecfb656c 20174 dir_index d_index, unsigned int mod_time,
fff8551c
PA
20175 unsigned int length)
20176 {
ecfb656c 20177 lh->add_file_name (name, d_index, mod_time, length);
fff8551c 20178 });
43988095
JK
20179 }
20180 else
debd256d 20181 {
43988095
JK
20182 /* Read directory table. */
20183 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20184 {
20185 line_ptr += bytes_read;
fff8551c 20186 lh->add_include_dir (cur_dir);
43988095 20187 }
debd256d
JB
20188 line_ptr += bytes_read;
20189
43988095
JK
20190 /* Read file name table. */
20191 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
20192 {
ecfb656c
PA
20193 unsigned int mod_time, length;
20194 dir_index d_index;
43988095
JK
20195
20196 line_ptr += bytes_read;
ecfb656c 20197 d_index = (dir_index) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
43988095
JK
20198 line_ptr += bytes_read;
20199 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20200 line_ptr += bytes_read;
20201 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20202 line_ptr += bytes_read;
20203
ecfb656c 20204 lh->add_file_name (cur_file, d_index, mod_time, length);
43988095
JK
20205 }
20206 line_ptr += bytes_read;
debd256d 20207 }
6e70227d 20208 lh->statement_program_start = line_ptr;
debd256d 20209
3019eac3 20210 if (line_ptr > (section->buffer + section->size))
b98664d3 20211 complaint (_("line number info header doesn't "
3e43a32a 20212 "fit in `.debug_line' section"));
debd256d 20213
debd256d
JB
20214 return lh;
20215}
c906108c 20216
c6da4cef
DE
20217/* Subroutine of dwarf_decode_lines to simplify it.
20218 Return the file name of the psymtab for included file FILE_INDEX
20219 in line header LH of PST.
20220 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
20221 If space for the result is malloc'd, *NAME_HOLDER will be set.
20222 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 20223
d521ce57 20224static const char *
c6da4cef
DE
20225psymtab_include_file_name (const struct line_header *lh, int file_index,
20226 const struct partial_symtab *pst,
c89b44cd
TT
20227 const char *comp_dir,
20228 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 20229{
8c43009f 20230 const file_entry &fe = lh->file_names[file_index];
d521ce57
TT
20231 const char *include_name = fe.name;
20232 const char *include_name_to_compare = include_name;
72b9f47f 20233 const char *pst_filename;
c6da4cef
DE
20234 int file_is_pst;
20235
8c43009f 20236 const char *dir_name = fe.include_dir (lh);
c6da4cef 20237
c89b44cd 20238 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
20239 if (!IS_ABSOLUTE_PATH (include_name)
20240 && (dir_name != NULL || comp_dir != NULL))
20241 {
20242 /* Avoid creating a duplicate psymtab for PST.
20243 We do this by comparing INCLUDE_NAME and PST_FILENAME.
20244 Before we do the comparison, however, we need to account
20245 for DIR_NAME and COMP_DIR.
20246 First prepend dir_name (if non-NULL). If we still don't
20247 have an absolute path prepend comp_dir (if non-NULL).
20248 However, the directory we record in the include-file's
20249 psymtab does not contain COMP_DIR (to match the
20250 corresponding symtab(s)).
20251
20252 Example:
20253
20254 bash$ cd /tmp
20255 bash$ gcc -g ./hello.c
20256 include_name = "hello.c"
20257 dir_name = "."
20258 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
20259 DW_AT_name = "./hello.c"
20260
20261 */
c6da4cef
DE
20262
20263 if (dir_name != NULL)
20264 {
c89b44cd
TT
20265 name_holder->reset (concat (dir_name, SLASH_STRING,
20266 include_name, (char *) NULL));
20267 include_name = name_holder->get ();
c6da4cef 20268 include_name_to_compare = include_name;
c6da4cef
DE
20269 }
20270 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
20271 {
c89b44cd
TT
20272 hold_compare.reset (concat (comp_dir, SLASH_STRING,
20273 include_name, (char *) NULL));
20274 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
20275 }
20276 }
20277
20278 pst_filename = pst->filename;
c89b44cd 20279 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
20280 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
20281 {
c89b44cd
TT
20282 copied_name.reset (concat (pst->dirname, SLASH_STRING,
20283 pst_filename, (char *) NULL));
20284 pst_filename = copied_name.get ();
c6da4cef
DE
20285 }
20286
1e3fad37 20287 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 20288
c6da4cef
DE
20289 if (file_is_pst)
20290 return NULL;
20291 return include_name;
20292}
20293
d9b3de22
DE
20294/* State machine to track the state of the line number program. */
20295
6f77053d 20296class lnp_state_machine
d9b3de22 20297{
6f77053d
PA
20298public:
20299 /* Initialize a machine state for the start of a line number
20300 program. */
20301 lnp_state_machine (gdbarch *arch, line_header *lh, bool record_lines_p);
20302
8c43009f
PA
20303 file_entry *current_file ()
20304 {
20305 /* lh->file_names is 0-based, but the file name numbers in the
20306 statement program are 1-based. */
6f77053d
PA
20307 return m_line_header->file_name_at (m_file);
20308 }
20309
20310 /* Record the line in the state machine. END_SEQUENCE is true if
20311 we're processing the end of a sequence. */
20312 void record_line (bool end_sequence);
20313
20314 /* Check address and if invalid nop-out the rest of the lines in this
20315 sequence. */
20316 void check_line_address (struct dwarf2_cu *cu,
20317 const gdb_byte *line_ptr,
20318 CORE_ADDR lowpc, CORE_ADDR address);
20319
20320 void handle_set_discriminator (unsigned int discriminator)
20321 {
20322 m_discriminator = discriminator;
20323 m_line_has_non_zero_discriminator |= discriminator != 0;
20324 }
20325
20326 /* Handle DW_LNE_set_address. */
20327 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
20328 {
20329 m_op_index = 0;
20330 address += baseaddr;
20331 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
20332 }
20333
20334 /* Handle DW_LNS_advance_pc. */
20335 void handle_advance_pc (CORE_ADDR adjust);
20336
20337 /* Handle a special opcode. */
20338 void handle_special_opcode (unsigned char op_code);
20339
20340 /* Handle DW_LNS_advance_line. */
20341 void handle_advance_line (int line_delta)
20342 {
20343 advance_line (line_delta);
20344 }
20345
20346 /* Handle DW_LNS_set_file. */
20347 void handle_set_file (file_name_index file);
20348
20349 /* Handle DW_LNS_negate_stmt. */
20350 void handle_negate_stmt ()
20351 {
20352 m_is_stmt = !m_is_stmt;
20353 }
20354
20355 /* Handle DW_LNS_const_add_pc. */
20356 void handle_const_add_pc ();
20357
20358 /* Handle DW_LNS_fixed_advance_pc. */
20359 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
20360 {
20361 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20362 m_op_index = 0;
20363 }
20364
20365 /* Handle DW_LNS_copy. */
20366 void handle_copy ()
20367 {
20368 record_line (false);
20369 m_discriminator = 0;
20370 }
20371
20372 /* Handle DW_LNE_end_sequence. */
20373 void handle_end_sequence ()
20374 {
20375 m_record_line_callback = ::record_line;
20376 }
20377
20378private:
20379 /* Advance the line by LINE_DELTA. */
20380 void advance_line (int line_delta)
20381 {
20382 m_line += line_delta;
20383
20384 if (line_delta != 0)
20385 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
20386 }
20387
6f77053d
PA
20388 gdbarch *m_gdbarch;
20389
20390 /* True if we're recording lines.
20391 Otherwise we're building partial symtabs and are just interested in
20392 finding include files mentioned by the line number program. */
20393 bool m_record_lines_p;
20394
8c43009f 20395 /* The line number header. */
6f77053d 20396 line_header *m_line_header;
8c43009f 20397
6f77053d
PA
20398 /* These are part of the standard DWARF line number state machine,
20399 and initialized according to the DWARF spec. */
d9b3de22 20400
6f77053d 20401 unsigned char m_op_index = 0;
8c43009f 20402 /* The line table index (1-based) of the current file. */
6f77053d
PA
20403 file_name_index m_file = (file_name_index) 1;
20404 unsigned int m_line = 1;
20405
20406 /* These are initialized in the constructor. */
20407
20408 CORE_ADDR m_address;
20409 bool m_is_stmt;
20410 unsigned int m_discriminator;
d9b3de22
DE
20411
20412 /* Additional bits of state we need to track. */
20413
20414 /* The last file that we called dwarf2_start_subfile for.
20415 This is only used for TLLs. */
6f77053d 20416 unsigned int m_last_file = 0;
d9b3de22 20417 /* The last file a line number was recorded for. */
6f77053d 20418 struct subfile *m_last_subfile = NULL;
d9b3de22
DE
20419
20420 /* The function to call to record a line. */
6f77053d 20421 record_line_ftype *m_record_line_callback = NULL;
d9b3de22
DE
20422
20423 /* The last line number that was recorded, used to coalesce
20424 consecutive entries for the same line. This can happen, for
20425 example, when discriminators are present. PR 17276. */
6f77053d
PA
20426 unsigned int m_last_line = 0;
20427 bool m_line_has_non_zero_discriminator = false;
8c43009f 20428};
d9b3de22 20429
6f77053d
PA
20430void
20431lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
20432{
20433 CORE_ADDR addr_adj = (((m_op_index + adjust)
20434 / m_line_header->maximum_ops_per_instruction)
20435 * m_line_header->minimum_instruction_length);
20436 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20437 m_op_index = ((m_op_index + adjust)
20438 % m_line_header->maximum_ops_per_instruction);
20439}
d9b3de22 20440
6f77053d
PA
20441void
20442lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 20443{
6f77053d
PA
20444 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
20445 CORE_ADDR addr_adj = (((m_op_index
20446 + (adj_opcode / m_line_header->line_range))
20447 / m_line_header->maximum_ops_per_instruction)
20448 * m_line_header->minimum_instruction_length);
20449 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20450 m_op_index = ((m_op_index + (adj_opcode / m_line_header->line_range))
20451 % m_line_header->maximum_ops_per_instruction);
d9b3de22 20452
6f77053d
PA
20453 int line_delta = (m_line_header->line_base
20454 + (adj_opcode % m_line_header->line_range));
20455 advance_line (line_delta);
20456 record_line (false);
20457 m_discriminator = 0;
20458}
d9b3de22 20459
6f77053d
PA
20460void
20461lnp_state_machine::handle_set_file (file_name_index file)
20462{
20463 m_file = file;
20464
20465 const file_entry *fe = current_file ();
20466 if (fe == NULL)
20467 dwarf2_debug_line_missing_file_complaint ();
20468 else if (m_record_lines_p)
20469 {
20470 const char *dir = fe->include_dir (m_line_header);
20471
20472 m_last_subfile = current_subfile;
20473 m_line_has_non_zero_discriminator = m_discriminator != 0;
20474 dwarf2_start_subfile (fe->name, dir);
20475 }
20476}
20477
20478void
20479lnp_state_machine::handle_const_add_pc ()
20480{
20481 CORE_ADDR adjust
20482 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20483
20484 CORE_ADDR addr_adj
20485 = (((m_op_index + adjust)
20486 / m_line_header->maximum_ops_per_instruction)
20487 * m_line_header->minimum_instruction_length);
20488
20489 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20490 m_op_index = ((m_op_index + adjust)
20491 % m_line_header->maximum_ops_per_instruction);
20492}
d9b3de22 20493
c91513d8
PP
20494/* Ignore this record_line request. */
20495
20496static void
20497noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
20498{
20499 return;
20500}
20501
a05a36a5
DE
20502/* Return non-zero if we should add LINE to the line number table.
20503 LINE is the line to add, LAST_LINE is the last line that was added,
20504 LAST_SUBFILE is the subfile for LAST_LINE.
20505 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20506 had a non-zero discriminator.
20507
20508 We have to be careful in the presence of discriminators.
20509 E.g., for this line:
20510
20511 for (i = 0; i < 100000; i++);
20512
20513 clang can emit four line number entries for that one line,
20514 each with a different discriminator.
20515 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20516
20517 However, we want gdb to coalesce all four entries into one.
20518 Otherwise the user could stepi into the middle of the line and
20519 gdb would get confused about whether the pc really was in the
20520 middle of the line.
20521
20522 Things are further complicated by the fact that two consecutive
20523 line number entries for the same line is a heuristic used by gcc
20524 to denote the end of the prologue. So we can't just discard duplicate
20525 entries, we have to be selective about it. The heuristic we use is
20526 that we only collapse consecutive entries for the same line if at least
20527 one of those entries has a non-zero discriminator. PR 17276.
20528
20529 Note: Addresses in the line number state machine can never go backwards
20530 within one sequence, thus this coalescing is ok. */
20531
20532static int
20533dwarf_record_line_p (unsigned int line, unsigned int last_line,
20534 int line_has_non_zero_discriminator,
20535 struct subfile *last_subfile)
20536{
20537 if (current_subfile != last_subfile)
20538 return 1;
20539 if (line != last_line)
20540 return 1;
20541 /* Same line for the same file that we've seen already.
20542 As a last check, for pr 17276, only record the line if the line
20543 has never had a non-zero discriminator. */
20544 if (!line_has_non_zero_discriminator)
20545 return 1;
20546 return 0;
20547}
20548
252a6764
DE
20549/* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
20550 in the line table of subfile SUBFILE. */
20551
20552static void
d9b3de22
DE
20553dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
20554 unsigned int line, CORE_ADDR address,
20555 record_line_ftype p_record_line)
252a6764
DE
20556{
20557 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20558
27e0867f
DE
20559 if (dwarf_line_debug)
20560 {
20561 fprintf_unfiltered (gdb_stdlog,
20562 "Recording line %u, file %s, address %s\n",
20563 line, lbasename (subfile->name),
20564 paddress (gdbarch, address));
20565 }
20566
d5962de5 20567 (*p_record_line) (subfile, line, addr);
252a6764
DE
20568}
20569
20570/* Subroutine of dwarf_decode_lines_1 to simplify it.
20571 Mark the end of a set of line number records.
d9b3de22 20572 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
20573 If SUBFILE is NULL the request is ignored. */
20574
20575static void
20576dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
20577 CORE_ADDR address, record_line_ftype p_record_line)
20578{
27e0867f
DE
20579 if (subfile == NULL)
20580 return;
20581
20582 if (dwarf_line_debug)
20583 {
20584 fprintf_unfiltered (gdb_stdlog,
20585 "Finishing current line, file %s, address %s\n",
20586 lbasename (subfile->name),
20587 paddress (gdbarch, address));
20588 }
20589
d9b3de22
DE
20590 dwarf_record_line_1 (gdbarch, subfile, 0, address, p_record_line);
20591}
20592
6f77053d
PA
20593void
20594lnp_state_machine::record_line (bool end_sequence)
d9b3de22 20595{
d9b3de22
DE
20596 if (dwarf_line_debug)
20597 {
20598 fprintf_unfiltered (gdb_stdlog,
20599 "Processing actual line %u: file %u,"
20600 " address %s, is_stmt %u, discrim %u\n",
6f77053d
PA
20601 m_line, to_underlying (m_file),
20602 paddress (m_gdbarch, m_address),
20603 m_is_stmt, m_discriminator);
d9b3de22
DE
20604 }
20605
6f77053d 20606 file_entry *fe = current_file ();
8c43009f
PA
20607
20608 if (fe == NULL)
d9b3de22
DE
20609 dwarf2_debug_line_missing_file_complaint ();
20610 /* For now we ignore lines not starting on an instruction boundary.
20611 But not when processing end_sequence for compatibility with the
20612 previous version of the code. */
6f77053d 20613 else if (m_op_index == 0 || end_sequence)
d9b3de22 20614 {
8c43009f 20615 fe->included_p = 1;
6f77053d 20616 if (m_record_lines_p && m_is_stmt)
d9b3de22 20617 {
6f77053d 20618 if (m_last_subfile != current_subfile || end_sequence)
d9b3de22 20619 {
6f77053d
PA
20620 dwarf_finish_line (m_gdbarch, m_last_subfile,
20621 m_address, m_record_line_callback);
d9b3de22
DE
20622 }
20623
20624 if (!end_sequence)
20625 {
6f77053d
PA
20626 if (dwarf_record_line_p (m_line, m_last_line,
20627 m_line_has_non_zero_discriminator,
20628 m_last_subfile))
d9b3de22 20629 {
6f77053d
PA
20630 dwarf_record_line_1 (m_gdbarch, current_subfile,
20631 m_line, m_address,
20632 m_record_line_callback);
d9b3de22 20633 }
6f77053d
PA
20634 m_last_subfile = current_subfile;
20635 m_last_line = m_line;
d9b3de22
DE
20636 }
20637 }
20638 }
20639}
20640
6f77053d
PA
20641lnp_state_machine::lnp_state_machine (gdbarch *arch, line_header *lh,
20642 bool record_lines_p)
d9b3de22 20643{
6f77053d
PA
20644 m_gdbarch = arch;
20645 m_record_lines_p = record_lines_p;
20646 m_line_header = lh;
d9b3de22 20647
6f77053d 20648 m_record_line_callback = ::record_line;
d9b3de22 20649
d9b3de22
DE
20650 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20651 was a line entry for it so that the backend has a chance to adjust it
20652 and also record it in case it needs it. This is currently used by MIPS
20653 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
20654 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20655 m_is_stmt = lh->default_is_stmt;
20656 m_discriminator = 0;
252a6764
DE
20657}
20658
6f77053d
PA
20659void
20660lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20661 const gdb_byte *line_ptr,
20662 CORE_ADDR lowpc, CORE_ADDR address)
924c2928
DE
20663{
20664 /* If address < lowpc then it's not a usable value, it's outside the
20665 pc range of the CU. However, we restrict the test to only address
20666 values of zero to preserve GDB's previous behaviour which is to
20667 handle the specific case of a function being GC'd by the linker. */
20668
20669 if (address == 0 && address < lowpc)
20670 {
20671 /* This line table is for a function which has been
20672 GCd by the linker. Ignore it. PR gdb/12528 */
20673
518817b3 20674 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
924c2928
DE
20675 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20676
b98664d3 20677 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 20678 line_offset, objfile_name (objfile));
6f77053d
PA
20679 m_record_line_callback = noop_record_line;
20680 /* Note: record_line_callback is left as noop_record_line until
20681 we see DW_LNE_end_sequence. */
924c2928
DE
20682 }
20683}
20684
f3f5162e 20685/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
20686 Process the line number information in LH.
20687 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20688 program in order to set included_p for every referenced header. */
debd256d 20689
c906108c 20690static void
43f3e411
DE
20691dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20692 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 20693{
d521ce57
TT
20694 const gdb_byte *line_ptr, *extended_end;
20695 const gdb_byte *line_end;
a8c50c1f 20696 unsigned int bytes_read, extended_len;
699ca60a 20697 unsigned char op_code, extended_op;
e142c38c 20698 CORE_ADDR baseaddr;
518817b3 20699 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 20700 bfd *abfd = objfile->obfd;
fbf65064 20701 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
20702 /* True if we're recording line info (as opposed to building partial
20703 symtabs and just interested in finding include files mentioned by
20704 the line number program). */
20705 bool record_lines_p = !decode_for_pst_p;
e142c38c
DJ
20706
20707 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 20708
debd256d
JB
20709 line_ptr = lh->statement_program_start;
20710 line_end = lh->statement_program_end;
c906108c
SS
20711
20712 /* Read the statement sequences until there's nothing left. */
20713 while (line_ptr < line_end)
20714 {
6f77053d
PA
20715 /* The DWARF line number program state machine. Reset the state
20716 machine at the start of each sequence. */
20717 lnp_state_machine state_machine (gdbarch, lh, record_lines_p);
20718 bool end_sequence = false;
d9b3de22 20719
8c43009f 20720 if (record_lines_p)
c906108c 20721 {
8c43009f
PA
20722 /* Start a subfile for the current file of the state
20723 machine. */
20724 const file_entry *fe = state_machine.current_file ();
20725
20726 if (fe != NULL)
20727 dwarf2_start_subfile (fe->name, fe->include_dir (lh));
c906108c
SS
20728 }
20729
a738430d 20730 /* Decode the table. */
d9b3de22 20731 while (line_ptr < line_end && !end_sequence)
c906108c
SS
20732 {
20733 op_code = read_1_byte (abfd, line_ptr);
20734 line_ptr += 1;
9aa1fe7e 20735
debd256d 20736 if (op_code >= lh->opcode_base)
6e70227d 20737 {
8e07a239 20738 /* Special opcode. */
6f77053d 20739 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
20740 }
20741 else switch (op_code)
c906108c
SS
20742 {
20743 case DW_LNS_extended_op:
3e43a32a
MS
20744 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20745 &bytes_read);
473b7be6 20746 line_ptr += bytes_read;
a8c50c1f 20747 extended_end = line_ptr + extended_len;
c906108c
SS
20748 extended_op = read_1_byte (abfd, line_ptr);
20749 line_ptr += 1;
20750 switch (extended_op)
20751 {
20752 case DW_LNE_end_sequence:
6f77053d
PA
20753 state_machine.handle_end_sequence ();
20754 end_sequence = true;
c906108c
SS
20755 break;
20756 case DW_LNE_set_address:
d9b3de22
DE
20757 {
20758 CORE_ADDR address
20759 = read_address (abfd, line_ptr, cu, &bytes_read);
d9b3de22 20760 line_ptr += bytes_read;
6f77053d
PA
20761
20762 state_machine.check_line_address (cu, line_ptr,
20763 lowpc, address);
20764 state_machine.handle_set_address (baseaddr, address);
d9b3de22 20765 }
c906108c
SS
20766 break;
20767 case DW_LNE_define_file:
debd256d 20768 {
d521ce57 20769 const char *cur_file;
ecfb656c
PA
20770 unsigned int mod_time, length;
20771 dir_index dindex;
6e70227d 20772
3e43a32a
MS
20773 cur_file = read_direct_string (abfd, line_ptr,
20774 &bytes_read);
debd256d 20775 line_ptr += bytes_read;
ecfb656c 20776 dindex = (dir_index)
debd256d
JB
20777 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20778 line_ptr += bytes_read;
20779 mod_time =
20780 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20781 line_ptr += bytes_read;
20782 length =
20783 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20784 line_ptr += bytes_read;
ecfb656c 20785 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 20786 }
c906108c 20787 break;
d0c6ba3d 20788 case DW_LNE_set_discriminator:
6f77053d
PA
20789 {
20790 /* The discriminator is not interesting to the
20791 debugger; just ignore it. We still need to
20792 check its value though:
20793 if there are consecutive entries for the same
20794 (non-prologue) line we want to coalesce them.
20795 PR 17276. */
20796 unsigned int discr
20797 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20798 line_ptr += bytes_read;
20799
20800 state_machine.handle_set_discriminator (discr);
20801 }
d0c6ba3d 20802 break;
c906108c 20803 default:
b98664d3 20804 complaint (_("mangled .debug_line section"));
debd256d 20805 return;
c906108c 20806 }
a8c50c1f
DJ
20807 /* Make sure that we parsed the extended op correctly. If e.g.
20808 we expected a different address size than the producer used,
20809 we may have read the wrong number of bytes. */
20810 if (line_ptr != extended_end)
20811 {
b98664d3 20812 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
20813 return;
20814 }
c906108c
SS
20815 break;
20816 case DW_LNS_copy:
6f77053d 20817 state_machine.handle_copy ();
c906108c
SS
20818 break;
20819 case DW_LNS_advance_pc:
2dc7f7b3
TT
20820 {
20821 CORE_ADDR adjust
20822 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 20823 line_ptr += bytes_read;
6f77053d
PA
20824
20825 state_machine.handle_advance_pc (adjust);
2dc7f7b3 20826 }
c906108c
SS
20827 break;
20828 case DW_LNS_advance_line:
a05a36a5
DE
20829 {
20830 int line_delta
20831 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 20832 line_ptr += bytes_read;
6f77053d
PA
20833
20834 state_machine.handle_advance_line (line_delta);
a05a36a5 20835 }
c906108c
SS
20836 break;
20837 case DW_LNS_set_file:
d9b3de22 20838 {
6f77053d 20839 file_name_index file
ecfb656c
PA
20840 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20841 &bytes_read);
d9b3de22 20842 line_ptr += bytes_read;
8c43009f 20843
6f77053d 20844 state_machine.handle_set_file (file);
d9b3de22 20845 }
c906108c
SS
20846 break;
20847 case DW_LNS_set_column:
0ad93d4f 20848 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
20849 line_ptr += bytes_read;
20850 break;
20851 case DW_LNS_negate_stmt:
6f77053d 20852 state_machine.handle_negate_stmt ();
c906108c
SS
20853 break;
20854 case DW_LNS_set_basic_block:
c906108c 20855 break;
c2c6d25f
JM
20856 /* Add to the address register of the state machine the
20857 address increment value corresponding to special opcode
a738430d
MK
20858 255. I.e., this value is scaled by the minimum
20859 instruction length since special opcode 255 would have
b021a221 20860 scaled the increment. */
c906108c 20861 case DW_LNS_const_add_pc:
6f77053d 20862 state_machine.handle_const_add_pc ();
c906108c
SS
20863 break;
20864 case DW_LNS_fixed_advance_pc:
3e29f34a 20865 {
6f77053d 20866 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 20867 line_ptr += 2;
6f77053d
PA
20868
20869 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 20870 }
c906108c 20871 break;
9aa1fe7e 20872 default:
a738430d
MK
20873 {
20874 /* Unknown standard opcode, ignore it. */
9aa1fe7e 20875 int i;
a738430d 20876
debd256d 20877 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
20878 {
20879 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20880 line_ptr += bytes_read;
20881 }
20882 }
c906108c
SS
20883 }
20884 }
d9b3de22
DE
20885
20886 if (!end_sequence)
20887 dwarf2_debug_line_missing_end_sequence_complaint ();
20888
20889 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20890 in which case we still finish recording the last line). */
6f77053d 20891 state_machine.record_line (true);
c906108c 20892 }
f3f5162e
DE
20893}
20894
20895/* Decode the Line Number Program (LNP) for the given line_header
20896 structure and CU. The actual information extracted and the type
20897 of structures created from the LNP depends on the value of PST.
20898
20899 1. If PST is NULL, then this procedure uses the data from the program
20900 to create all necessary symbol tables, and their linetables.
20901
20902 2. If PST is not NULL, this procedure reads the program to determine
20903 the list of files included by the unit represented by PST, and
20904 builds all the associated partial symbol tables.
20905
20906 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20907 It is used for relative paths in the line table.
20908 NOTE: When processing partial symtabs (pst != NULL),
20909 comp_dir == pst->dirname.
20910
20911 NOTE: It is important that psymtabs have the same file name (via strcmp)
20912 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20913 symtab we don't use it in the name of the psymtabs we create.
20914 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
20915 A good testcase for this is mb-inline.exp.
20916
527f3840
JK
20917 LOWPC is the lowest address in CU (or 0 if not known).
20918
20919 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20920 for its PC<->lines mapping information. Otherwise only the filename
20921 table is read in. */
f3f5162e
DE
20922
20923static void
20924dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
c3b7b696 20925 struct dwarf2_cu *cu, struct partial_symtab *pst,
527f3840 20926 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 20927{
518817b3 20928 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 20929 const int decode_for_pst_p = (pst != NULL);
f3f5162e 20930
527f3840
JK
20931 if (decode_mapping)
20932 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
20933
20934 if (decode_for_pst_p)
20935 {
20936 int file_index;
20937
20938 /* Now that we're done scanning the Line Header Program, we can
20939 create the psymtab of each included file. */
fff8551c 20940 for (file_index = 0; file_index < lh->file_names.size (); file_index++)
aaa75496
JB
20941 if (lh->file_names[file_index].included_p == 1)
20942 {
c89b44cd 20943 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 20944 const char *include_name =
c89b44cd
TT
20945 psymtab_include_file_name (lh, file_index, pst, comp_dir,
20946 &name_holder);
c6da4cef 20947 if (include_name != NULL)
aaa75496
JB
20948 dwarf2_create_include_psymtab (include_name, pst, objfile);
20949 }
20950 }
cb1df416
DJ
20951 else
20952 {
20953 /* Make sure a symtab is created for every file, even files
20954 which contain only variables (i.e. no code with associated
20955 line numbers). */
43f3e411 20956 struct compunit_symtab *cust = buildsym_compunit_symtab ();
cb1df416 20957 int i;
cb1df416 20958
fff8551c 20959 for (i = 0; i < lh->file_names.size (); i++)
cb1df416 20960 {
8c43009f 20961 file_entry &fe = lh->file_names[i];
9a619af0 20962
8c43009f 20963 dwarf2_start_subfile (fe.name, fe.include_dir (lh));
cb1df416 20964
cb1df416 20965 if (current_subfile->symtab == NULL)
43f3e411
DE
20966 {
20967 current_subfile->symtab
20968 = allocate_symtab (cust, current_subfile->name);
20969 }
8c43009f 20970 fe.symtab = current_subfile->symtab;
cb1df416
DJ
20971 }
20972 }
c906108c
SS
20973}
20974
20975/* Start a subfile for DWARF. FILENAME is the name of the file and
20976 DIRNAME the name of the source directory which contains FILENAME
4d663531 20977 or NULL if not known.
c906108c
SS
20978 This routine tries to keep line numbers from identical absolute and
20979 relative file names in a common subfile.
20980
20981 Using the `list' example from the GDB testsuite, which resides in
20982 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20983 of /srcdir/list0.c yields the following debugging information for list0.c:
20984
c5aa993b 20985 DW_AT_name: /srcdir/list0.c
4d663531 20986 DW_AT_comp_dir: /compdir
357e46e7 20987 files.files[0].name: list0.h
c5aa993b 20988 files.files[0].dir: /srcdir
357e46e7 20989 files.files[1].name: list0.c
c5aa993b 20990 files.files[1].dir: /srcdir
c906108c
SS
20991
20992 The line number information for list0.c has to end up in a single
4f1520fb
FR
20993 subfile, so that `break /srcdir/list0.c:1' works as expected.
20994 start_subfile will ensure that this happens provided that we pass the
20995 concatenation of files.files[1].dir and files.files[1].name as the
20996 subfile's name. */
c906108c
SS
20997
20998static void
4d663531 20999dwarf2_start_subfile (const char *filename, const char *dirname)
c906108c 21000{
d521ce57 21001 char *copy = NULL;
4f1520fb 21002
4d663531 21003 /* In order not to lose the line information directory,
4f1520fb
FR
21004 we concatenate it to the filename when it makes sense.
21005 Note that the Dwarf3 standard says (speaking of filenames in line
21006 information): ``The directory index is ignored for file names
21007 that represent full path names''. Thus ignoring dirname in the
21008 `else' branch below isn't an issue. */
c906108c 21009
d5166ae1 21010 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57
TT
21011 {
21012 copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
21013 filename = copy;
21014 }
c906108c 21015
4d663531 21016 start_subfile (filename);
4f1520fb 21017
d521ce57
TT
21018 if (copy != NULL)
21019 xfree (copy);
c906108c
SS
21020}
21021
f4dc4d17
DE
21022/* Start a symtab for DWARF.
21023 NAME, COMP_DIR, LOW_PC are passed to start_symtab. */
21024
43f3e411 21025static struct compunit_symtab *
f4dc4d17 21026dwarf2_start_symtab (struct dwarf2_cu *cu,
15d034d0 21027 const char *name, const char *comp_dir, CORE_ADDR low_pc)
f4dc4d17 21028{
43f3e411 21029 struct compunit_symtab *cust
518817b3
SM
21030 = start_symtab (cu->per_cu->dwarf2_per_objfile->objfile, name, comp_dir,
21031 low_pc, cu->language);
43f3e411 21032
f4dc4d17
DE
21033 record_debugformat ("DWARF 2");
21034 record_producer (cu->producer);
21035
21036 /* We assume that we're processing GCC output. */
21037 processing_gcc_compilation = 2;
21038
4d4ec4e5 21039 cu->processing_has_namespace_info = 0;
43f3e411
DE
21040
21041 return cust;
f4dc4d17
DE
21042}
21043
4c2df51b
DJ
21044static void
21045var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 21046 struct dwarf2_cu *cu)
4c2df51b 21047{
518817b3 21048 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e7c27a73
DJ
21049 struct comp_unit_head *cu_header = &cu->header;
21050
4c2df51b
DJ
21051 /* NOTE drow/2003-01-30: There used to be a comment and some special
21052 code here to turn a symbol with DW_AT_external and a
21053 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21054 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21055 with some versions of binutils) where shared libraries could have
21056 relocations against symbols in their debug information - the
21057 minimal symbol would have the right address, but the debug info
21058 would not. It's no longer necessary, because we will explicitly
21059 apply relocations when we read in the debug information now. */
21060
21061 /* A DW_AT_location attribute with no contents indicates that a
21062 variable has been optimized away. */
21063 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
21064 {
f1e6e072 21065 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
21066 return;
21067 }
21068
21069 /* Handle one degenerate form of location expression specially, to
21070 preserve GDB's previous behavior when section offsets are
3019eac3
DE
21071 specified. If this is just a DW_OP_addr or DW_OP_GNU_addr_index
21072 then mark this symbol as LOC_STATIC. */
4c2df51b
DJ
21073
21074 if (attr_form_is_block (attr)
3019eac3
DE
21075 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
21076 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
21077 || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
21078 && (DW_BLOCK (attr)->size
21079 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 21080 {
891d2f0b 21081 unsigned int dummy;
4c2df51b 21082
3019eac3
DE
21083 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
21084 SYMBOL_VALUE_ADDRESS (sym) =
21085 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
21086 else
21087 SYMBOL_VALUE_ADDRESS (sym) =
21088 read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
f1e6e072 21089 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b
DJ
21090 fixup_symbol_section (sym, objfile);
21091 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
21092 SYMBOL_SECTION (sym));
4c2df51b
DJ
21093 return;
21094 }
21095
21096 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21097 expression evaluator, and use LOC_COMPUTED only when necessary
21098 (i.e. when the value of a register or memory location is
21099 referenced, or a thread-local block, etc.). Then again, it might
21100 not be worthwhile. I'm assuming that it isn't unless performance
21101 or memory numbers show me otherwise. */
21102
f1e6e072 21103 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 21104
f1e6e072 21105 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
8be455d7 21106 cu->has_loclist = 1;
4c2df51b
DJ
21107}
21108
c906108c
SS
21109/* Given a pointer to a DWARF information entry, figure out if we need
21110 to make a symbol table entry for it, and if so, create a new entry
21111 and return a pointer to it.
21112 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
21113 used the passed type.
21114 If SPACE is not NULL, use it to hold the new symbol. If it is
21115 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
21116
21117static struct symbol *
5e2db402
TT
21118new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21119 struct symbol *space)
c906108c 21120{
518817b3
SM
21121 struct dwarf2_per_objfile *dwarf2_per_objfile
21122 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21123 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 21124 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 21125 struct symbol *sym = NULL;
15d034d0 21126 const char *name;
c906108c
SS
21127 struct attribute *attr = NULL;
21128 struct attribute *attr2 = NULL;
e142c38c 21129 CORE_ADDR baseaddr;
e37fd15a
SW
21130 struct pending **list_to_add = NULL;
21131
edb3359d 21132 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c
DJ
21133
21134 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
c906108c 21135
94af9270 21136 name = dwarf2_name (die, cu);
c906108c
SS
21137 if (name)
21138 {
94af9270 21139 const char *linkagename;
34eaf542 21140 int suppress_add = 0;
94af9270 21141
34eaf542
TT
21142 if (space)
21143 sym = space;
21144 else
e623cf5d 21145 sym = allocate_symbol (objfile);
c906108c 21146 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
21147
21148 /* Cache this symbol's name and the name's demangled form (if any). */
f85f34ed 21149 SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
94af9270
KS
21150 linkagename = dwarf2_physname (name, die, cu);
21151 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
c906108c 21152
f55ee35c
JK
21153 /* Fortran does not have mangling standard and the mangling does differ
21154 between gfortran, iFort etc. */
21155 if (cu->language == language_fortran
b250c185 21156 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
29df156d 21157 symbol_set_demangled_name (&(sym->ginfo),
cfc594ee 21158 dwarf2_full_name (name, die, cu),
29df156d 21159 NULL);
f55ee35c 21160
c906108c 21161 /* Default assumptions.
c5aa993b 21162 Use the passed type or decode it from the die. */
176620f1 21163 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 21164 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
21165 if (type != NULL)
21166 SYMBOL_TYPE (sym) = type;
21167 else
e7c27a73 21168 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
21169 attr = dwarf2_attr (die,
21170 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
21171 cu);
c906108c
SS
21172 if (attr)
21173 {
21174 SYMBOL_LINE (sym) = DW_UNSND (attr);
21175 }
cb1df416 21176
edb3359d
DJ
21177 attr = dwarf2_attr (die,
21178 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
21179 cu);
cb1df416
DJ
21180 if (attr)
21181 {
ecfb656c 21182 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 21183 struct file_entry *fe;
9a619af0 21184
ecfb656c
PA
21185 if (cu->line_header != NULL)
21186 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
21187 else
21188 fe = NULL;
21189
21190 if (fe == NULL)
b98664d3 21191 complaint (_("file index out of range"));
8c43009f
PA
21192 else
21193 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
21194 }
21195
c906108c
SS
21196 switch (die->tag)
21197 {
21198 case DW_TAG_label:
e142c38c 21199 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
c906108c 21200 if (attr)
3e29f34a
MR
21201 {
21202 CORE_ADDR addr;
21203
21204 addr = attr_value_as_address (attr);
21205 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
21206 SYMBOL_VALUE_ADDRESS (sym) = addr;
21207 }
0f5238ed
TT
21208 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
21209 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 21210 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
0f5238ed 21211 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
21212 break;
21213 case DW_TAG_subprogram:
21214 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21215 finish_block. */
f1e6e072 21216 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 21217 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d
JB
21218 if ((attr2 && (DW_UNSND (attr2) != 0))
21219 || cu->language == language_ada)
c906108c 21220 {
2cfa0c8d
JB
21221 /* Subprograms marked external are stored as a global symbol.
21222 Ada subprograms, whether marked external or not, are always
21223 stored as a global symbol, because we want to be able to
21224 access them globally. For instance, we want to be able
21225 to break on a nested subprogram without having to
21226 specify the context. */
e37fd15a 21227 list_to_add = &global_symbols;
c906108c
SS
21228 }
21229 else
21230 {
e37fd15a 21231 list_to_add = cu->list_in_scope;
c906108c
SS
21232 }
21233 break;
edb3359d
DJ
21234 case DW_TAG_inlined_subroutine:
21235 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
21236 finish_block. */
f1e6e072 21237 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 21238 SYMBOL_INLINED (sym) = 1;
481860b3 21239 list_to_add = cu->list_in_scope;
edb3359d 21240 break;
34eaf542
TT
21241 case DW_TAG_template_value_param:
21242 suppress_add = 1;
21243 /* Fall through. */
72929c62 21244 case DW_TAG_constant:
c906108c 21245 case DW_TAG_variable:
254e6b9e 21246 case DW_TAG_member:
0963b4bd
MS
21247 /* Compilation with minimal debug info may result in
21248 variables with missing type entries. Change the
21249 misleading `void' type to something sensible. */
c906108c 21250 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 21251 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 21252
e142c38c 21253 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
21254 /* In the case of DW_TAG_member, we should only be called for
21255 static const members. */
21256 if (die->tag == DW_TAG_member)
21257 {
3863f96c
DE
21258 /* dwarf2_add_field uses die_is_declaration,
21259 so we do the same. */
254e6b9e
DE
21260 gdb_assert (die_is_declaration (die, cu));
21261 gdb_assert (attr);
21262 }
c906108c
SS
21263 if (attr)
21264 {
e7c27a73 21265 dwarf2_const_value (attr, sym, cu);
e142c38c 21266 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 21267 if (!suppress_add)
34eaf542
TT
21268 {
21269 if (attr2 && (DW_UNSND (attr2) != 0))
e37fd15a 21270 list_to_add = &global_symbols;
34eaf542 21271 else
e37fd15a 21272 list_to_add = cu->list_in_scope;
34eaf542 21273 }
c906108c
SS
21274 break;
21275 }
e142c38c 21276 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
21277 if (attr)
21278 {
e7c27a73 21279 var_decode_location (attr, sym, cu);
e142c38c 21280 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
21281
21282 /* Fortran explicitly imports any global symbols to the local
21283 scope by DW_TAG_common_block. */
21284 if (cu->language == language_fortran && die->parent
21285 && die->parent->tag == DW_TAG_common_block)
21286 attr2 = NULL;
21287
caac4577
JG
21288 if (SYMBOL_CLASS (sym) == LOC_STATIC
21289 && SYMBOL_VALUE_ADDRESS (sym) == 0
21290 && !dwarf2_per_objfile->has_section_at_zero)
21291 {
21292 /* When a static variable is eliminated by the linker,
21293 the corresponding debug information is not stripped
21294 out, but the variable address is set to null;
21295 do not add such variables into symbol table. */
21296 }
21297 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 21298 {
f55ee35c
JK
21299 /* Workaround gfortran PR debug/40040 - it uses
21300 DW_AT_location for variables in -fPIC libraries which may
21301 get overriden by other libraries/executable and get
21302 a different address. Resolve it by the minimal symbol
21303 which may come from inferior's executable using copy
21304 relocation. Make this workaround only for gfortran as for
21305 other compilers GDB cannot guess the minimal symbol
21306 Fortran mangling kind. */
21307 if (cu->language == language_fortran && die->parent
21308 && die->parent->tag == DW_TAG_module
21309 && cu->producer
28586665 21310 && startswith (cu->producer, "GNU Fortran"))
f1e6e072 21311 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
f55ee35c 21312
1c809c68
TT
21313 /* A variable with DW_AT_external is never static,
21314 but it may be block-scoped. */
21315 list_to_add = (cu->list_in_scope == &file_symbols
21316 ? &global_symbols : cu->list_in_scope);
1c809c68 21317 }
c906108c 21318 else
e37fd15a 21319 list_to_add = cu->list_in_scope;
c906108c
SS
21320 }
21321 else
21322 {
21323 /* We do not know the address of this symbol.
c5aa993b
JM
21324 If it is an external symbol and we have type information
21325 for it, enter the symbol as a LOC_UNRESOLVED symbol.
21326 The address of the variable will then be determined from
21327 the minimal symbol table whenever the variable is
21328 referenced. */
e142c38c 21329 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
21330
21331 /* Fortran explicitly imports any global symbols to the local
21332 scope by DW_TAG_common_block. */
21333 if (cu->language == language_fortran && die->parent
21334 && die->parent->tag == DW_TAG_common_block)
21335 {
21336 /* SYMBOL_CLASS doesn't matter here because
21337 read_common_block is going to reset it. */
21338 if (!suppress_add)
21339 list_to_add = cu->list_in_scope;
21340 }
21341 else if (attr2 && (DW_UNSND (attr2) != 0)
21342 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 21343 {
0fe7935b
DJ
21344 /* A variable with DW_AT_external is never static, but it
21345 may be block-scoped. */
21346 list_to_add = (cu->list_in_scope == &file_symbols
21347 ? &global_symbols : cu->list_in_scope);
21348
f1e6e072 21349 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 21350 }
442ddf59
JK
21351 else if (!die_is_declaration (die, cu))
21352 {
21353 /* Use the default LOC_OPTIMIZED_OUT class. */
21354 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
21355 if (!suppress_add)
21356 list_to_add = cu->list_in_scope;
442ddf59 21357 }
c906108c
SS
21358 }
21359 break;
21360 case DW_TAG_formal_parameter:
edb3359d
DJ
21361 /* If we are inside a function, mark this as an argument. If
21362 not, we might be looking at an argument to an inlined function
21363 when we do not have enough information to show inlined frames;
21364 pretend it's a local variable in that case so that the user can
21365 still see it. */
21366 if (context_stack_depth > 0
21367 && context_stack[context_stack_depth - 1].name != NULL)
21368 SYMBOL_IS_ARGUMENT (sym) = 1;
e142c38c 21369 attr = dwarf2_attr (die, DW_AT_location, cu);
c906108c
SS
21370 if (attr)
21371 {
e7c27a73 21372 var_decode_location (attr, sym, cu);
c906108c 21373 }
e142c38c 21374 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
21375 if (attr)
21376 {
e7c27a73 21377 dwarf2_const_value (attr, sym, cu);
c906108c 21378 }
f346a30d 21379
e37fd15a 21380 list_to_add = cu->list_in_scope;
c906108c
SS
21381 break;
21382 case DW_TAG_unspecified_parameters:
21383 /* From varargs functions; gdb doesn't seem to have any
21384 interest in this information, so just ignore it for now.
21385 (FIXME?) */
21386 break;
34eaf542
TT
21387 case DW_TAG_template_type_param:
21388 suppress_add = 1;
21389 /* Fall through. */
c906108c 21390 case DW_TAG_class_type:
680b30c7 21391 case DW_TAG_interface_type:
c906108c
SS
21392 case DW_TAG_structure_type:
21393 case DW_TAG_union_type:
72019c9c 21394 case DW_TAG_set_type:
c906108c 21395 case DW_TAG_enumeration_type:
f1e6e072 21396 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21397 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 21398
63d06c5c 21399 {
9c37b5ae 21400 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
21401 really ever be static objects: otherwise, if you try
21402 to, say, break of a class's method and you're in a file
21403 which doesn't mention that class, it won't work unless
21404 the check for all static symbols in lookup_symbol_aux
21405 saves you. See the OtherFileClass tests in
21406 gdb.c++/namespace.exp. */
21407
e37fd15a 21408 if (!suppress_add)
34eaf542 21409 {
34eaf542 21410 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 21411 && cu->language == language_cplus
34eaf542 21412 ? &global_symbols : cu->list_in_scope);
63d06c5c 21413
64382290 21414 /* The semantics of C++ state that "struct foo {
9c37b5ae 21415 ... }" also defines a typedef for "foo". */
64382290 21416 if (cu->language == language_cplus
45280282 21417 || cu->language == language_ada
c44af4eb
TT
21418 || cu->language == language_d
21419 || cu->language == language_rust)
64382290
TT
21420 {
21421 /* The symbol's name is already allocated along
21422 with this objfile, so we don't need to
21423 duplicate it for the type. */
21424 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
21425 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
21426 }
63d06c5c
DC
21427 }
21428 }
c906108c
SS
21429 break;
21430 case DW_TAG_typedef:
f1e6e072 21431 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 21432 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21433 list_to_add = cu->list_in_scope;
63d06c5c 21434 break;
c906108c 21435 case DW_TAG_base_type:
a02abb62 21436 case DW_TAG_subrange_type:
f1e6e072 21437 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21438 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21439 list_to_add = cu->list_in_scope;
c906108c
SS
21440 break;
21441 case DW_TAG_enumerator:
e142c38c 21442 attr = dwarf2_attr (die, DW_AT_const_value, cu);
c906108c
SS
21443 if (attr)
21444 {
e7c27a73 21445 dwarf2_const_value (attr, sym, cu);
c906108c 21446 }
63d06c5c
DC
21447 {
21448 /* NOTE: carlton/2003-11-10: See comment above in the
21449 DW_TAG_class_type, etc. block. */
21450
e142c38c 21451 list_to_add = (cu->list_in_scope == &file_symbols
9c37b5ae 21452 && cu->language == language_cplus
e142c38c 21453 ? &global_symbols : cu->list_in_scope);
63d06c5c 21454 }
c906108c 21455 break;
74921315 21456 case DW_TAG_imported_declaration:
5c4e30ca 21457 case DW_TAG_namespace:
f1e6e072 21458 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
e37fd15a 21459 list_to_add = &global_symbols;
5c4e30ca 21460 break;
530e8392
KB
21461 case DW_TAG_module:
21462 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21463 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
21464 list_to_add = &global_symbols;
21465 break;
4357ac6c 21466 case DW_TAG_common_block:
f1e6e072 21467 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c
TT
21468 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
21469 add_symbol_to_list (sym, cu->list_in_scope);
21470 break;
c906108c
SS
21471 default:
21472 /* Not a tag we recognize. Hopefully we aren't processing
21473 trash data, but since we must specifically ignore things
21474 we don't recognize, there is nothing else we should do at
0963b4bd 21475 this point. */
b98664d3 21476 complaint (_("unsupported tag: '%s'"),
4d3c2250 21477 dwarf_tag_name (die->tag));
c906108c
SS
21478 break;
21479 }
df8a16a1 21480
e37fd15a
SW
21481 if (suppress_add)
21482 {
21483 sym->hash_next = objfile->template_symbols;
21484 objfile->template_symbols = sym;
21485 list_to_add = NULL;
21486 }
21487
21488 if (list_to_add != NULL)
21489 add_symbol_to_list (sym, list_to_add);
21490
df8a16a1
DJ
21491 /* For the benefit of old versions of GCC, check for anonymous
21492 namespaces based on the demangled name. */
4d4ec4e5 21493 if (!cu->processing_has_namespace_info
94af9270 21494 && cu->language == language_cplus)
a10964d1 21495 cp_scan_for_anonymous_namespaces (sym, objfile);
c906108c
SS
21496 }
21497 return (sym);
21498}
21499
98bfdba5
PA
21500/* Given an attr with a DW_FORM_dataN value in host byte order,
21501 zero-extend it as appropriate for the symbol's type. The DWARF
21502 standard (v4) is not entirely clear about the meaning of using
21503 DW_FORM_dataN for a constant with a signed type, where the type is
21504 wider than the data. The conclusion of a discussion on the DWARF
21505 list was that this is unspecified. We choose to always zero-extend
21506 because that is the interpretation long in use by GCC. */
c906108c 21507
98bfdba5 21508static gdb_byte *
ff39bb5e 21509dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 21510 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 21511{
518817b3 21512 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e17a4113
UW
21513 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21514 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
21515 LONGEST l = DW_UNSND (attr);
21516
21517 if (bits < sizeof (*value) * 8)
21518 {
21519 l &= ((LONGEST) 1 << bits) - 1;
21520 *value = l;
21521 }
21522 else if (bits == sizeof (*value) * 8)
21523 *value = l;
21524 else
21525 {
224c3ddb 21526 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
21527 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21528 return bytes;
21529 }
21530
21531 return NULL;
21532}
21533
21534/* Read a constant value from an attribute. Either set *VALUE, or if
21535 the value does not fit in *VALUE, set *BYTES - either already
21536 allocated on the objfile obstack, or newly allocated on OBSTACK,
21537 or, set *BATON, if we translated the constant to a location
21538 expression. */
21539
21540static void
ff39bb5e 21541dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
21542 const char *name, struct obstack *obstack,
21543 struct dwarf2_cu *cu,
d521ce57 21544 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
21545 struct dwarf2_locexpr_baton **baton)
21546{
518817b3 21547 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
98bfdba5 21548 struct comp_unit_head *cu_header = &cu->header;
c906108c 21549 struct dwarf_block *blk;
98bfdba5
PA
21550 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21551 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21552
21553 *value = 0;
21554 *bytes = NULL;
21555 *baton = NULL;
c906108c
SS
21556
21557 switch (attr->form)
21558 {
21559 case DW_FORM_addr:
3019eac3 21560 case DW_FORM_GNU_addr_index:
ac56253d 21561 {
ac56253d
TT
21562 gdb_byte *data;
21563
98bfdba5
PA
21564 if (TYPE_LENGTH (type) != cu_header->addr_size)
21565 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 21566 cu_header->addr_size,
98bfdba5 21567 TYPE_LENGTH (type));
ac56253d
TT
21568 /* Symbols of this form are reasonably rare, so we just
21569 piggyback on the existing location code rather than writing
21570 a new implementation of symbol_computed_ops. */
8d749320 21571 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
21572 (*baton)->per_cu = cu->per_cu;
21573 gdb_assert ((*baton)->per_cu);
ac56253d 21574
98bfdba5 21575 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 21576 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 21577 (*baton)->data = data;
ac56253d
TT
21578
21579 data[0] = DW_OP_addr;
21580 store_unsigned_integer (&data[1], cu_header->addr_size,
21581 byte_order, DW_ADDR (attr));
21582 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 21583 }
c906108c 21584 break;
4ac36638 21585 case DW_FORM_string:
93b5768b 21586 case DW_FORM_strp:
3019eac3 21587 case DW_FORM_GNU_str_index:
36586728 21588 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
21589 /* DW_STRING is already allocated on the objfile obstack, point
21590 directly to it. */
d521ce57 21591 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 21592 break;
c906108c
SS
21593 case DW_FORM_block1:
21594 case DW_FORM_block2:
21595 case DW_FORM_block4:
21596 case DW_FORM_block:
2dc7f7b3 21597 case DW_FORM_exprloc:
0224619f 21598 case DW_FORM_data16:
c906108c 21599 blk = DW_BLOCK (attr);
98bfdba5
PA
21600 if (TYPE_LENGTH (type) != blk->size)
21601 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21602 TYPE_LENGTH (type));
21603 *bytes = blk->data;
c906108c 21604 break;
2df3850c
JM
21605
21606 /* The DW_AT_const_value attributes are supposed to carry the
21607 symbol's value "represented as it would be on the target
21608 architecture." By the time we get here, it's already been
21609 converted to host endianness, so we just need to sign- or
21610 zero-extend it as appropriate. */
21611 case DW_FORM_data1:
3aef2284 21612 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 21613 break;
c906108c 21614 case DW_FORM_data2:
3aef2284 21615 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 21616 break;
c906108c 21617 case DW_FORM_data4:
3aef2284 21618 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 21619 break;
c906108c 21620 case DW_FORM_data8:
3aef2284 21621 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
21622 break;
21623
c906108c 21624 case DW_FORM_sdata:
663c44ac 21625 case DW_FORM_implicit_const:
98bfdba5 21626 *value = DW_SND (attr);
2df3850c
JM
21627 break;
21628
c906108c 21629 case DW_FORM_udata:
98bfdba5 21630 *value = DW_UNSND (attr);
c906108c 21631 break;
2df3850c 21632
c906108c 21633 default:
b98664d3 21634 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 21635 dwarf_form_name (attr->form));
98bfdba5 21636 *value = 0;
c906108c
SS
21637 break;
21638 }
21639}
21640
2df3850c 21641
98bfdba5
PA
21642/* Copy constant value from an attribute to a symbol. */
21643
2df3850c 21644static void
ff39bb5e 21645dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 21646 struct dwarf2_cu *cu)
2df3850c 21647{
518817b3 21648 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12df843f 21649 LONGEST value;
d521ce57 21650 const gdb_byte *bytes;
98bfdba5 21651 struct dwarf2_locexpr_baton *baton;
2df3850c 21652
98bfdba5
PA
21653 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
21654 SYMBOL_PRINT_NAME (sym),
21655 &objfile->objfile_obstack, cu,
21656 &value, &bytes, &baton);
2df3850c 21657
98bfdba5
PA
21658 if (baton != NULL)
21659 {
98bfdba5 21660 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 21661 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
21662 }
21663 else if (bytes != NULL)
21664 {
21665 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 21666 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
21667 }
21668 else
21669 {
21670 SYMBOL_VALUE (sym) = value;
f1e6e072 21671 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 21672 }
2df3850c
JM
21673}
21674
c906108c
SS
21675/* Return the type of the die in question using its DW_AT_type attribute. */
21676
21677static struct type *
e7c27a73 21678die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21679{
c906108c 21680 struct attribute *type_attr;
c906108c 21681
e142c38c 21682 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
21683 if (!type_attr)
21684 {
518817b3 21685 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 21686 /* A missing DW_AT_type represents a void type. */
518817b3 21687 return objfile_type (objfile)->builtin_void;
c906108c 21688 }
348e048f 21689
673bfd45 21690 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21691}
21692
b4ba55a1
JB
21693/* True iff CU's producer generates GNAT Ada auxiliary information
21694 that allows to find parallel types through that information instead
21695 of having to do expensive parallel lookups by type name. */
21696
21697static int
21698need_gnat_info (struct dwarf2_cu *cu)
21699{
de4cb04a
JB
21700 /* Assume that the Ada compiler was GNAT, which always produces
21701 the auxiliary information. */
21702 return (cu->language == language_ada);
b4ba55a1
JB
21703}
21704
b4ba55a1
JB
21705/* Return the auxiliary type of the die in question using its
21706 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21707 attribute is not present. */
21708
21709static struct type *
21710die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21711{
b4ba55a1 21712 struct attribute *type_attr;
b4ba55a1
JB
21713
21714 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21715 if (!type_attr)
21716 return NULL;
21717
673bfd45 21718 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
21719}
21720
21721/* If DIE has a descriptive_type attribute, then set the TYPE's
21722 descriptive type accordingly. */
21723
21724static void
21725set_descriptive_type (struct type *type, struct die_info *die,
21726 struct dwarf2_cu *cu)
21727{
21728 struct type *descriptive_type = die_descriptive_type (die, cu);
21729
21730 if (descriptive_type)
21731 {
21732 ALLOCATE_GNAT_AUX_TYPE (type);
21733 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21734 }
21735}
21736
c906108c
SS
21737/* Return the containing type of the die in question using its
21738 DW_AT_containing_type attribute. */
21739
21740static struct type *
e7c27a73 21741die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21742{
c906108c 21743 struct attribute *type_attr;
518817b3 21744 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 21745
e142c38c 21746 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
21747 if (!type_attr)
21748 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 21749 "[in module %s]"), objfile_name (objfile));
33ac96f0 21750
673bfd45 21751 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21752}
21753
ac9ec31b
DE
21754/* Return an error marker type to use for the ill formed type in DIE/CU. */
21755
21756static struct type *
21757build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21758{
518817b3
SM
21759 struct dwarf2_per_objfile *dwarf2_per_objfile
21760 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
21761 struct objfile *objfile = dwarf2_per_objfile->objfile;
21762 char *message, *saved;
21763
9d8780f0 21764 message = xstrprintf (_("<unknown type in %s, CU %s, DIE %s>"),
4262abfb 21765 objfile_name (objfile),
9d8780f0
SM
21766 sect_offset_str (cu->header.sect_off),
21767 sect_offset_str (die->sect_off));
224c3ddb
SM
21768 saved = (char *) obstack_copy0 (&objfile->objfile_obstack,
21769 message, strlen (message));
ac9ec31b
DE
21770 xfree (message);
21771
19f392bc 21772 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
21773}
21774
673bfd45 21775/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
21776 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21777 DW_AT_containing_type.
673bfd45
DE
21778 If there is no type substitute an error marker. */
21779
c906108c 21780static struct type *
ff39bb5e 21781lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 21782 struct dwarf2_cu *cu)
c906108c 21783{
518817b3
SM
21784 struct dwarf2_per_objfile *dwarf2_per_objfile
21785 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21786 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
21787 struct type *this_type;
21788
ac9ec31b
DE
21789 gdb_assert (attr->name == DW_AT_type
21790 || attr->name == DW_AT_GNAT_descriptive_type
21791 || attr->name == DW_AT_containing_type);
21792
673bfd45
DE
21793 /* First see if we have it cached. */
21794
36586728
TT
21795 if (attr->form == DW_FORM_GNU_ref_alt)
21796 {
21797 struct dwarf2_per_cu_data *per_cu;
9c541725 21798 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
36586728 21799
ed2dc618
SM
21800 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21801 dwarf2_per_objfile);
9c541725 21802 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 21803 }
7771576e 21804 else if (attr_form_is_ref (attr))
673bfd45 21805 {
9c541725 21806 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
673bfd45 21807
9c541725 21808 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 21809 }
55f1336d 21810 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 21811 {
ac9ec31b 21812 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 21813
ac9ec31b 21814 return get_signatured_type (die, signature, cu);
673bfd45
DE
21815 }
21816 else
21817 {
b98664d3 21818 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
21819 " at %s [in module %s]"),
21820 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 21821 objfile_name (objfile));
ac9ec31b 21822 return build_error_marker_type (cu, die);
673bfd45
DE
21823 }
21824
21825 /* If not cached we need to read it in. */
21826
21827 if (this_type == NULL)
21828 {
ac9ec31b 21829 struct die_info *type_die = NULL;
673bfd45
DE
21830 struct dwarf2_cu *type_cu = cu;
21831
7771576e 21832 if (attr_form_is_ref (attr))
ac9ec31b
DE
21833 type_die = follow_die_ref (die, attr, &type_cu);
21834 if (type_die == NULL)
21835 return build_error_marker_type (cu, die);
21836 /* If we find the type now, it's probably because the type came
3019eac3
DE
21837 from an inter-CU reference and the type's CU got expanded before
21838 ours. */
ac9ec31b 21839 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
21840 }
21841
21842 /* If we still don't have a type use an error marker. */
21843
21844 if (this_type == NULL)
ac9ec31b 21845 return build_error_marker_type (cu, die);
673bfd45 21846
f792889a 21847 return this_type;
c906108c
SS
21848}
21849
673bfd45
DE
21850/* Return the type in DIE, CU.
21851 Returns NULL for invalid types.
21852
02142a6c 21853 This first does a lookup in die_type_hash,
673bfd45
DE
21854 and only reads the die in if necessary.
21855
21856 NOTE: This can be called when reading in partial or full symbols. */
21857
f792889a 21858static struct type *
e7c27a73 21859read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21860{
f792889a
DJ
21861 struct type *this_type;
21862
21863 this_type = get_die_type (die, cu);
21864 if (this_type)
21865 return this_type;
21866
673bfd45
DE
21867 return read_type_die_1 (die, cu);
21868}
21869
21870/* Read the type in DIE, CU.
21871 Returns NULL for invalid types. */
21872
21873static struct type *
21874read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21875{
21876 struct type *this_type = NULL;
21877
c906108c
SS
21878 switch (die->tag)
21879 {
21880 case DW_TAG_class_type:
680b30c7 21881 case DW_TAG_interface_type:
c906108c
SS
21882 case DW_TAG_structure_type:
21883 case DW_TAG_union_type:
f792889a 21884 this_type = read_structure_type (die, cu);
c906108c
SS
21885 break;
21886 case DW_TAG_enumeration_type:
f792889a 21887 this_type = read_enumeration_type (die, cu);
c906108c
SS
21888 break;
21889 case DW_TAG_subprogram:
21890 case DW_TAG_subroutine_type:
edb3359d 21891 case DW_TAG_inlined_subroutine:
f792889a 21892 this_type = read_subroutine_type (die, cu);
c906108c
SS
21893 break;
21894 case DW_TAG_array_type:
f792889a 21895 this_type = read_array_type (die, cu);
c906108c 21896 break;
72019c9c 21897 case DW_TAG_set_type:
f792889a 21898 this_type = read_set_type (die, cu);
72019c9c 21899 break;
c906108c 21900 case DW_TAG_pointer_type:
f792889a 21901 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
21902 break;
21903 case DW_TAG_ptr_to_member_type:
f792889a 21904 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
21905 break;
21906 case DW_TAG_reference_type:
4297a3f0
AV
21907 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21908 break;
21909 case DW_TAG_rvalue_reference_type:
21910 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
21911 break;
21912 case DW_TAG_const_type:
f792889a 21913 this_type = read_tag_const_type (die, cu);
c906108c
SS
21914 break;
21915 case DW_TAG_volatile_type:
f792889a 21916 this_type = read_tag_volatile_type (die, cu);
c906108c 21917 break;
06d66ee9
TT
21918 case DW_TAG_restrict_type:
21919 this_type = read_tag_restrict_type (die, cu);
21920 break;
c906108c 21921 case DW_TAG_string_type:
f792889a 21922 this_type = read_tag_string_type (die, cu);
c906108c
SS
21923 break;
21924 case DW_TAG_typedef:
f792889a 21925 this_type = read_typedef (die, cu);
c906108c 21926 break;
a02abb62 21927 case DW_TAG_subrange_type:
f792889a 21928 this_type = read_subrange_type (die, cu);
a02abb62 21929 break;
c906108c 21930 case DW_TAG_base_type:
f792889a 21931 this_type = read_base_type (die, cu);
c906108c 21932 break;
81a17f79 21933 case DW_TAG_unspecified_type:
f792889a 21934 this_type = read_unspecified_type (die, cu);
81a17f79 21935 break;
0114d602
DJ
21936 case DW_TAG_namespace:
21937 this_type = read_namespace_type (die, cu);
21938 break;
f55ee35c
JK
21939 case DW_TAG_module:
21940 this_type = read_module_type (die, cu);
21941 break;
a2c2acaf
MW
21942 case DW_TAG_atomic_type:
21943 this_type = read_tag_atomic_type (die, cu);
21944 break;
c906108c 21945 default:
b98664d3 21946 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 21947 dwarf_tag_name (die->tag));
c906108c
SS
21948 break;
21949 }
63d06c5c 21950
f792889a 21951 return this_type;
63d06c5c
DC
21952}
21953
abc72ce4
DE
21954/* See if we can figure out if the class lives in a namespace. We do
21955 this by looking for a member function; its demangled name will
21956 contain namespace info, if there is any.
21957 Return the computed name or NULL.
21958 Space for the result is allocated on the objfile's obstack.
21959 This is the full-die version of guess_partial_die_structure_name.
21960 In this case we know DIE has no useful parent. */
21961
21962static char *
21963guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21964{
21965 struct die_info *spec_die;
21966 struct dwarf2_cu *spec_cu;
21967 struct die_info *child;
518817b3 21968 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4
DE
21969
21970 spec_cu = cu;
21971 spec_die = die_specification (die, &spec_cu);
21972 if (spec_die != NULL)
21973 {
21974 die = spec_die;
21975 cu = spec_cu;
21976 }
21977
21978 for (child = die->child;
21979 child != NULL;
21980 child = child->sibling)
21981 {
21982 if (child->tag == DW_TAG_subprogram)
21983 {
73b9be8b 21984 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 21985
7d45c7c3 21986 if (linkage_name != NULL)
abc72ce4
DE
21987 {
21988 char *actual_name
21989 = language_class_name_from_physname (cu->language_defn,
7d45c7c3 21990 linkage_name);
abc72ce4
DE
21991 char *name = NULL;
21992
21993 if (actual_name != NULL)
21994 {
15d034d0 21995 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
21996
21997 if (die_name != NULL
21998 && strcmp (die_name, actual_name) != 0)
21999 {
22000 /* Strip off the class name from the full name.
22001 We want the prefix. */
22002 int die_name_len = strlen (die_name);
22003 int actual_name_len = strlen (actual_name);
22004
22005 /* Test for '::' as a sanity check. */
22006 if (actual_name_len > die_name_len + 2
3e43a32a
MS
22007 && actual_name[actual_name_len
22008 - die_name_len - 1] == ':')
224c3ddb 22009 name = (char *) obstack_copy0 (
e3b94546 22010 &objfile->per_bfd->storage_obstack,
224c3ddb 22011 actual_name, actual_name_len - die_name_len - 2);
abc72ce4
DE
22012 }
22013 }
22014 xfree (actual_name);
22015 return name;
22016 }
22017 }
22018 }
22019
22020 return NULL;
22021}
22022
96408a79
SA
22023/* GCC might emit a nameless typedef that has a linkage name. Determine the
22024 prefix part in such case. See
22025 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22026
a121b7c1 22027static const char *
96408a79
SA
22028anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22029{
22030 struct attribute *attr;
e6a959d6 22031 const char *base;
96408a79
SA
22032
22033 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22034 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22035 return NULL;
22036
7d45c7c3 22037 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
22038 return NULL;
22039
73b9be8b 22040 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
22041 if (attr == NULL || DW_STRING (attr) == NULL)
22042 return NULL;
22043
22044 /* dwarf2_name had to be already called. */
22045 gdb_assert (DW_STRING_IS_CANONICAL (attr));
22046
22047 /* Strip the base name, keep any leading namespaces/classes. */
22048 base = strrchr (DW_STRING (attr), ':');
22049 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
22050 return "";
22051
518817b3 22052 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e3b94546 22053 return (char *) obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb
SM
22054 DW_STRING (attr),
22055 &base[-1] - DW_STRING (attr));
96408a79
SA
22056}
22057
fdde2d81 22058/* Return the name of the namespace/class that DIE is defined within,
0114d602 22059 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 22060
0114d602
DJ
22061 For example, if we're within the method foo() in the following
22062 code:
22063
22064 namespace N {
22065 class C {
22066 void foo () {
22067 }
22068 };
22069 }
22070
22071 then determine_prefix on foo's die will return "N::C". */
fdde2d81 22072
0d5cff50 22073static const char *
e142c38c 22074determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 22075{
518817b3
SM
22076 struct dwarf2_per_objfile *dwarf2_per_objfile
22077 = cu->per_cu->dwarf2_per_objfile;
0114d602
DJ
22078 struct die_info *parent, *spec_die;
22079 struct dwarf2_cu *spec_cu;
22080 struct type *parent_type;
a121b7c1 22081 const char *retval;
63d06c5c 22082
9c37b5ae 22083 if (cu->language != language_cplus
c44af4eb
TT
22084 && cu->language != language_fortran && cu->language != language_d
22085 && cu->language != language_rust)
0114d602
DJ
22086 return "";
22087
96408a79
SA
22088 retval = anonymous_struct_prefix (die, cu);
22089 if (retval)
22090 return retval;
22091
0114d602
DJ
22092 /* We have to be careful in the presence of DW_AT_specification.
22093 For example, with GCC 3.4, given the code
22094
22095 namespace N {
22096 void foo() {
22097 // Definition of N::foo.
22098 }
22099 }
22100
22101 then we'll have a tree of DIEs like this:
22102
22103 1: DW_TAG_compile_unit
22104 2: DW_TAG_namespace // N
22105 3: DW_TAG_subprogram // declaration of N::foo
22106 4: DW_TAG_subprogram // definition of N::foo
22107 DW_AT_specification // refers to die #3
22108
22109 Thus, when processing die #4, we have to pretend that we're in
22110 the context of its DW_AT_specification, namely the contex of die
22111 #3. */
22112 spec_cu = cu;
22113 spec_die = die_specification (die, &spec_cu);
22114 if (spec_die == NULL)
22115 parent = die->parent;
22116 else
63d06c5c 22117 {
0114d602
DJ
22118 parent = spec_die->parent;
22119 cu = spec_cu;
63d06c5c 22120 }
0114d602
DJ
22121
22122 if (parent == NULL)
22123 return "";
98bfdba5
PA
22124 else if (parent->building_fullname)
22125 {
22126 const char *name;
22127 const char *parent_name;
22128
22129 /* It has been seen on RealView 2.2 built binaries,
22130 DW_TAG_template_type_param types actually _defined_ as
22131 children of the parent class:
22132
22133 enum E {};
22134 template class <class Enum> Class{};
22135 Class<enum E> class_e;
22136
22137 1: DW_TAG_class_type (Class)
22138 2: DW_TAG_enumeration_type (E)
22139 3: DW_TAG_enumerator (enum1:0)
22140 3: DW_TAG_enumerator (enum2:1)
22141 ...
22142 2: DW_TAG_template_type_param
22143 DW_AT_type DW_FORM_ref_udata (E)
22144
22145 Besides being broken debug info, it can put GDB into an
22146 infinite loop. Consider:
22147
22148 When we're building the full name for Class<E>, we'll start
22149 at Class, and go look over its template type parameters,
22150 finding E. We'll then try to build the full name of E, and
22151 reach here. We're now trying to build the full name of E,
22152 and look over the parent DIE for containing scope. In the
22153 broken case, if we followed the parent DIE of E, we'd again
22154 find Class, and once again go look at its template type
22155 arguments, etc., etc. Simply don't consider such parent die
22156 as source-level parent of this die (it can't be, the language
22157 doesn't allow it), and break the loop here. */
22158 name = dwarf2_name (die, cu);
22159 parent_name = dwarf2_name (parent, cu);
b98664d3 22160 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
22161 name ? name : "<unknown>",
22162 parent_name ? parent_name : "<unknown>");
22163 return "";
22164 }
63d06c5c 22165 else
0114d602
DJ
22166 switch (parent->tag)
22167 {
63d06c5c 22168 case DW_TAG_namespace:
0114d602 22169 parent_type = read_type_die (parent, cu);
acebe513
UW
22170 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
22171 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
22172 Work around this problem here. */
22173 if (cu->language == language_cplus
e86ca25f 22174 && strcmp (TYPE_NAME (parent_type), "::") == 0)
acebe513 22175 return "";
0114d602 22176 /* We give a name to even anonymous namespaces. */
e86ca25f 22177 return TYPE_NAME (parent_type);
63d06c5c 22178 case DW_TAG_class_type:
680b30c7 22179 case DW_TAG_interface_type:
63d06c5c 22180 case DW_TAG_structure_type:
0114d602 22181 case DW_TAG_union_type:
f55ee35c 22182 case DW_TAG_module:
0114d602 22183 parent_type = read_type_die (parent, cu);
e86ca25f
TT
22184 if (TYPE_NAME (parent_type) != NULL)
22185 return TYPE_NAME (parent_type);
0114d602
DJ
22186 else
22187 /* An anonymous structure is only allowed non-static data
22188 members; no typedefs, no member functions, et cetera.
22189 So it does not need a prefix. */
22190 return "";
abc72ce4 22191 case DW_TAG_compile_unit:
95554aad 22192 case DW_TAG_partial_unit:
abc72ce4
DE
22193 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
22194 if (cu->language == language_cplus
8b70b953 22195 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
abc72ce4
DE
22196 && die->child != NULL
22197 && (die->tag == DW_TAG_class_type
22198 || die->tag == DW_TAG_structure_type
22199 || die->tag == DW_TAG_union_type))
22200 {
22201 char *name = guess_full_die_structure_name (die, cu);
22202 if (name != NULL)
22203 return name;
22204 }
22205 return "";
3d567982
TT
22206 case DW_TAG_enumeration_type:
22207 parent_type = read_type_die (parent, cu);
22208 if (TYPE_DECLARED_CLASS (parent_type))
22209 {
e86ca25f
TT
22210 if (TYPE_NAME (parent_type) != NULL)
22211 return TYPE_NAME (parent_type);
3d567982
TT
22212 return "";
22213 }
22214 /* Fall through. */
63d06c5c 22215 default:
8176b9b8 22216 return determine_prefix (parent, cu);
63d06c5c 22217 }
63d06c5c
DC
22218}
22219
3e43a32a
MS
22220/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
22221 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
22222 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
22223 an obconcat, otherwise allocate storage for the result. The CU argument is
22224 used to determine the language and hence, the appropriate separator. */
987504bb 22225
f55ee35c 22226#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
22227
22228static char *
f55ee35c
JK
22229typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
22230 int physname, struct dwarf2_cu *cu)
63d06c5c 22231{
f55ee35c 22232 const char *lead = "";
5c315b68 22233 const char *sep;
63d06c5c 22234
3e43a32a
MS
22235 if (suffix == NULL || suffix[0] == '\0'
22236 || prefix == NULL || prefix[0] == '\0')
987504bb 22237 sep = "";
45280282
IB
22238 else if (cu->language == language_d)
22239 {
22240 /* For D, the 'main' function could be defined in any module, but it
22241 should never be prefixed. */
22242 if (strcmp (suffix, "D main") == 0)
22243 {
22244 prefix = "";
22245 sep = "";
22246 }
22247 else
22248 sep = ".";
22249 }
f55ee35c
JK
22250 else if (cu->language == language_fortran && physname)
22251 {
22252 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
22253 DW_AT_MIPS_linkage_name is preferred and used instead. */
22254
22255 lead = "__";
22256 sep = "_MOD_";
22257 }
987504bb
JJ
22258 else
22259 sep = "::";
63d06c5c 22260
6dd47d34
DE
22261 if (prefix == NULL)
22262 prefix = "";
22263 if (suffix == NULL)
22264 suffix = "";
22265
987504bb
JJ
22266 if (obs == NULL)
22267 {
3e43a32a 22268 char *retval
224c3ddb
SM
22269 = ((char *)
22270 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 22271
f55ee35c
JK
22272 strcpy (retval, lead);
22273 strcat (retval, prefix);
6dd47d34
DE
22274 strcat (retval, sep);
22275 strcat (retval, suffix);
63d06c5c
DC
22276 return retval;
22277 }
987504bb
JJ
22278 else
22279 {
22280 /* We have an obstack. */
f55ee35c 22281 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 22282 }
63d06c5c
DC
22283}
22284
c906108c
SS
22285/* Return sibling of die, NULL if no sibling. */
22286
f9aca02d 22287static struct die_info *
fba45db2 22288sibling_die (struct die_info *die)
c906108c 22289{
639d11d3 22290 return die->sibling;
c906108c
SS
22291}
22292
71c25dea
TT
22293/* Get name of a die, return NULL if not found. */
22294
15d034d0
TT
22295static const char *
22296dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
71c25dea
TT
22297 struct obstack *obstack)
22298{
22299 if (name && cu->language == language_cplus)
22300 {
2f408ecb 22301 std::string canon_name = cp_canonicalize_string (name);
71c25dea 22302
2f408ecb 22303 if (!canon_name.empty ())
71c25dea 22304 {
2f408ecb
PA
22305 if (canon_name != name)
22306 name = (const char *) obstack_copy0 (obstack,
22307 canon_name.c_str (),
22308 canon_name.length ());
71c25dea
TT
22309 }
22310 }
22311
22312 return name;
c906108c
SS
22313}
22314
96553a0c
DE
22315/* Get name of a die, return NULL if not found.
22316 Anonymous namespaces are converted to their magic string. */
9219021c 22317
15d034d0 22318static const char *
e142c38c 22319dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
22320{
22321 struct attribute *attr;
518817b3 22322 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9219021c 22323
e142c38c 22324 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 22325 if ((!attr || !DW_STRING (attr))
96553a0c 22326 && die->tag != DW_TAG_namespace
53832f31
TT
22327 && die->tag != DW_TAG_class_type
22328 && die->tag != DW_TAG_interface_type
22329 && die->tag != DW_TAG_structure_type
22330 && die->tag != DW_TAG_union_type)
71c25dea
TT
22331 return NULL;
22332
22333 switch (die->tag)
22334 {
22335 case DW_TAG_compile_unit:
95554aad 22336 case DW_TAG_partial_unit:
71c25dea
TT
22337 /* Compilation units have a DW_AT_name that is a filename, not
22338 a source language identifier. */
22339 case DW_TAG_enumeration_type:
22340 case DW_TAG_enumerator:
22341 /* These tags always have simple identifiers already; no need
22342 to canonicalize them. */
22343 return DW_STRING (attr);
907af001 22344
96553a0c
DE
22345 case DW_TAG_namespace:
22346 if (attr != NULL && DW_STRING (attr) != NULL)
22347 return DW_STRING (attr);
22348 return CP_ANONYMOUS_NAMESPACE_STR;
22349
907af001
UW
22350 case DW_TAG_class_type:
22351 case DW_TAG_interface_type:
22352 case DW_TAG_structure_type:
22353 case DW_TAG_union_type:
22354 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
22355 structures or unions. These were of the form "._%d" in GCC 4.1,
22356 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
22357 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 22358 if (attr && DW_STRING (attr)
61012eef
GB
22359 && (startswith (DW_STRING (attr), "._")
22360 || startswith (DW_STRING (attr), "<anonymous")))
907af001 22361 return NULL;
53832f31
TT
22362
22363 /* GCC might emit a nameless typedef that has a linkage name. See
22364 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22365 if (!attr || DW_STRING (attr) == NULL)
22366 {
df5c6c50 22367 char *demangled = NULL;
53832f31 22368
73b9be8b 22369 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
22370 if (attr == NULL || DW_STRING (attr) == NULL)
22371 return NULL;
22372
df5c6c50
JK
22373 /* Avoid demangling DW_STRING (attr) the second time on a second
22374 call for the same DIE. */
22375 if (!DW_STRING_IS_CANONICAL (attr))
8de20a37 22376 demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
53832f31
TT
22377
22378 if (demangled)
22379 {
e6a959d6 22380 const char *base;
96408a79 22381
53832f31 22382 /* FIXME: we already did this for the partial symbol... */
34a68019 22383 DW_STRING (attr)
224c3ddb 22384 = ((const char *)
e3b94546 22385 obstack_copy0 (&objfile->per_bfd->storage_obstack,
224c3ddb 22386 demangled, strlen (demangled)));
53832f31
TT
22387 DW_STRING_IS_CANONICAL (attr) = 1;
22388 xfree (demangled);
96408a79
SA
22389
22390 /* Strip any leading namespaces/classes, keep only the base name.
22391 DW_AT_name for named DIEs does not contain the prefixes. */
22392 base = strrchr (DW_STRING (attr), ':');
22393 if (base && base > DW_STRING (attr) && base[-1] == ':')
22394 return &base[1];
22395 else
22396 return DW_STRING (attr);
53832f31
TT
22397 }
22398 }
907af001
UW
22399 break;
22400
71c25dea 22401 default:
907af001
UW
22402 break;
22403 }
22404
22405 if (!DW_STRING_IS_CANONICAL (attr))
22406 {
22407 DW_STRING (attr)
22408 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
e3b94546 22409 &objfile->per_bfd->storage_obstack);
907af001 22410 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 22411 }
907af001 22412 return DW_STRING (attr);
9219021c
DC
22413}
22414
22415/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
22416 is none. *EXT_CU is the CU containing DIE on input, and the CU
22417 containing the return value on output. */
9219021c
DC
22418
22419static struct die_info *
f2f0e013 22420dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
22421{
22422 struct attribute *attr;
9219021c 22423
f2f0e013 22424 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
22425 if (attr == NULL)
22426 return NULL;
22427
f2f0e013 22428 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
22429}
22430
c906108c
SS
22431/* Convert a DIE tag into its string name. */
22432
f39c6ffd 22433static const char *
aa1ee363 22434dwarf_tag_name (unsigned tag)
c906108c 22435{
f39c6ffd
TT
22436 const char *name = get_DW_TAG_name (tag);
22437
22438 if (name == NULL)
22439 return "DW_TAG_<unknown>";
22440
22441 return name;
c906108c
SS
22442}
22443
22444/* Convert a DWARF attribute code into its string name. */
22445
f39c6ffd 22446static const char *
aa1ee363 22447dwarf_attr_name (unsigned attr)
c906108c 22448{
f39c6ffd
TT
22449 const char *name;
22450
c764a876 22451#ifdef MIPS /* collides with DW_AT_HP_block_index */
f39c6ffd
TT
22452 if (attr == DW_AT_MIPS_fde)
22453 return "DW_AT_MIPS_fde";
22454#else
22455 if (attr == DW_AT_HP_block_index)
22456 return "DW_AT_HP_block_index";
c764a876 22457#endif
f39c6ffd
TT
22458
22459 name = get_DW_AT_name (attr);
22460
22461 if (name == NULL)
22462 return "DW_AT_<unknown>";
22463
22464 return name;
c906108c
SS
22465}
22466
22467/* Convert a DWARF value form code into its string name. */
22468
f39c6ffd 22469static const char *
aa1ee363 22470dwarf_form_name (unsigned form)
c906108c 22471{
f39c6ffd
TT
22472 const char *name = get_DW_FORM_name (form);
22473
22474 if (name == NULL)
22475 return "DW_FORM_<unknown>";
22476
22477 return name;
c906108c
SS
22478}
22479
a121b7c1 22480static const char *
fba45db2 22481dwarf_bool_name (unsigned mybool)
c906108c
SS
22482{
22483 if (mybool)
22484 return "TRUE";
22485 else
22486 return "FALSE";
22487}
22488
22489/* Convert a DWARF type code into its string name. */
22490
f39c6ffd 22491static const char *
aa1ee363 22492dwarf_type_encoding_name (unsigned enc)
c906108c 22493{
f39c6ffd 22494 const char *name = get_DW_ATE_name (enc);
c906108c 22495
f39c6ffd
TT
22496 if (name == NULL)
22497 return "DW_ATE_<unknown>";
c906108c 22498
f39c6ffd 22499 return name;
c906108c 22500}
c906108c 22501
f9aca02d 22502static void
d97bc12b 22503dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
22504{
22505 unsigned int i;
22506
d97bc12b 22507 print_spaces (indent, f);
9d8780f0 22508 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 22509 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 22510 sect_offset_str (die->sect_off));
d97bc12b
DE
22511
22512 if (die->parent != NULL)
22513 {
22514 print_spaces (indent, f);
9d8780f0
SM
22515 fprintf_unfiltered (f, " parent at offset: %s\n",
22516 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
22517 }
22518
22519 print_spaces (indent, f);
22520 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 22521 dwarf_bool_name (die->child != NULL));
c906108c 22522
d97bc12b
DE
22523 print_spaces (indent, f);
22524 fprintf_unfiltered (f, " attributes:\n");
22525
c906108c
SS
22526 for (i = 0; i < die->num_attrs; ++i)
22527 {
d97bc12b
DE
22528 print_spaces (indent, f);
22529 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
22530 dwarf_attr_name (die->attrs[i].name),
22531 dwarf_form_name (die->attrs[i].form));
d97bc12b 22532
c906108c
SS
22533 switch (die->attrs[i].form)
22534 {
c906108c 22535 case DW_FORM_addr:
3019eac3 22536 case DW_FORM_GNU_addr_index:
d97bc12b 22537 fprintf_unfiltered (f, "address: ");
5af949e3 22538 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
22539 break;
22540 case DW_FORM_block2:
22541 case DW_FORM_block4:
22542 case DW_FORM_block:
22543 case DW_FORM_block1:
56eb65bd
SP
22544 fprintf_unfiltered (f, "block: size %s",
22545 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 22546 break;
2dc7f7b3 22547 case DW_FORM_exprloc:
56eb65bd
SP
22548 fprintf_unfiltered (f, "expression: size %s",
22549 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 22550 break;
0224619f
JK
22551 case DW_FORM_data16:
22552 fprintf_unfiltered (f, "constant of 16 bytes");
22553 break;
4568ecf9
DE
22554 case DW_FORM_ref_addr:
22555 fprintf_unfiltered (f, "ref address: ");
22556 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22557 break;
36586728
TT
22558 case DW_FORM_GNU_ref_alt:
22559 fprintf_unfiltered (f, "alt ref address: ");
22560 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22561 break;
10b3939b
DJ
22562 case DW_FORM_ref1:
22563 case DW_FORM_ref2:
22564 case DW_FORM_ref4:
4568ecf9
DE
22565 case DW_FORM_ref8:
22566 case DW_FORM_ref_udata:
d97bc12b 22567 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 22568 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 22569 break;
c906108c
SS
22570 case DW_FORM_data1:
22571 case DW_FORM_data2:
22572 case DW_FORM_data4:
ce5d95e1 22573 case DW_FORM_data8:
c906108c
SS
22574 case DW_FORM_udata:
22575 case DW_FORM_sdata:
43bbcdc2
PH
22576 fprintf_unfiltered (f, "constant: %s",
22577 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 22578 break;
2dc7f7b3
TT
22579 case DW_FORM_sec_offset:
22580 fprintf_unfiltered (f, "section offset: %s",
22581 pulongest (DW_UNSND (&die->attrs[i])));
22582 break;
55f1336d 22583 case DW_FORM_ref_sig8:
ac9ec31b
DE
22584 fprintf_unfiltered (f, "signature: %s",
22585 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 22586 break;
c906108c 22587 case DW_FORM_string:
4bdf3d34 22588 case DW_FORM_strp:
43988095 22589 case DW_FORM_line_strp:
3019eac3 22590 case DW_FORM_GNU_str_index:
36586728 22591 case DW_FORM_GNU_strp_alt:
8285870a 22592 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 22593 DW_STRING (&die->attrs[i])
8285870a
JK
22594 ? DW_STRING (&die->attrs[i]) : "",
22595 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
22596 break;
22597 case DW_FORM_flag:
22598 if (DW_UNSND (&die->attrs[i]))
d97bc12b 22599 fprintf_unfiltered (f, "flag: TRUE");
c906108c 22600 else
d97bc12b 22601 fprintf_unfiltered (f, "flag: FALSE");
c906108c 22602 break;
2dc7f7b3
TT
22603 case DW_FORM_flag_present:
22604 fprintf_unfiltered (f, "flag: TRUE");
22605 break;
a8329558 22606 case DW_FORM_indirect:
0963b4bd
MS
22607 /* The reader will have reduced the indirect form to
22608 the "base form" so this form should not occur. */
3e43a32a
MS
22609 fprintf_unfiltered (f,
22610 "unexpected attribute form: DW_FORM_indirect");
a8329558 22611 break;
663c44ac
JK
22612 case DW_FORM_implicit_const:
22613 fprintf_unfiltered (f, "constant: %s",
22614 plongest (DW_SND (&die->attrs[i])));
22615 break;
c906108c 22616 default:
d97bc12b 22617 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 22618 die->attrs[i].form);
d97bc12b 22619 break;
c906108c 22620 }
d97bc12b 22621 fprintf_unfiltered (f, "\n");
c906108c
SS
22622 }
22623}
22624
f9aca02d 22625static void
d97bc12b 22626dump_die_for_error (struct die_info *die)
c906108c 22627{
d97bc12b
DE
22628 dump_die_shallow (gdb_stderr, 0, die);
22629}
22630
22631static void
22632dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22633{
22634 int indent = level * 4;
22635
22636 gdb_assert (die != NULL);
22637
22638 if (level >= max_level)
22639 return;
22640
22641 dump_die_shallow (f, indent, die);
22642
22643 if (die->child != NULL)
c906108c 22644 {
d97bc12b
DE
22645 print_spaces (indent, f);
22646 fprintf_unfiltered (f, " Children:");
22647 if (level + 1 < max_level)
22648 {
22649 fprintf_unfiltered (f, "\n");
22650 dump_die_1 (f, level + 1, max_level, die->child);
22651 }
22652 else
22653 {
3e43a32a
MS
22654 fprintf_unfiltered (f,
22655 " [not printed, max nesting level reached]\n");
d97bc12b
DE
22656 }
22657 }
22658
22659 if (die->sibling != NULL && level > 0)
22660 {
22661 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
22662 }
22663}
22664
d97bc12b
DE
22665/* This is called from the pdie macro in gdbinit.in.
22666 It's not static so gcc will keep a copy callable from gdb. */
22667
22668void
22669dump_die (struct die_info *die, int max_level)
22670{
22671 dump_die_1 (gdb_stdlog, 0, max_level, die);
22672}
22673
f9aca02d 22674static void
51545339 22675store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22676{
51545339 22677 void **slot;
c906108c 22678
9c541725
PA
22679 slot = htab_find_slot_with_hash (cu->die_hash, die,
22680 to_underlying (die->sect_off),
b64f50a1 22681 INSERT);
51545339
DJ
22682
22683 *slot = die;
c906108c
SS
22684}
22685
b64f50a1
JK
22686/* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
22687 required kind. */
22688
22689static sect_offset
ff39bb5e 22690dwarf2_get_ref_die_offset (const struct attribute *attr)
93311388 22691{
7771576e 22692 if (attr_form_is_ref (attr))
9c541725 22693 return (sect_offset) DW_UNSND (attr);
93311388 22694
b98664d3 22695 complaint (_("unsupported die ref attribute form: '%s'"),
93311388 22696 dwarf_form_name (attr->form));
9c541725 22697 return {};
c906108c
SS
22698}
22699
43bbcdc2
PH
22700/* Return the constant value held by ATTR. Return DEFAULT_VALUE if
22701 * the value held by the attribute is not constant. */
a02abb62 22702
43bbcdc2 22703static LONGEST
ff39bb5e 22704dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
a02abb62 22705{
663c44ac 22706 if (attr->form == DW_FORM_sdata || attr->form == DW_FORM_implicit_const)
a02abb62
JB
22707 return DW_SND (attr);
22708 else if (attr->form == DW_FORM_udata
22709 || attr->form == DW_FORM_data1
22710 || attr->form == DW_FORM_data2
22711 || attr->form == DW_FORM_data4
22712 || attr->form == DW_FORM_data8)
22713 return DW_UNSND (attr);
22714 else
22715 {
0224619f 22716 /* For DW_FORM_data16 see attr_form_is_constant. */
b98664d3 22717 complaint (_("Attribute value is not a constant (%s)"),
a02abb62
JB
22718 dwarf_form_name (attr->form));
22719 return default_value;
22720 }
22721}
22722
348e048f
DE
22723/* Follow reference or signature attribute ATTR of SRC_DIE.
22724 On entry *REF_CU is the CU of SRC_DIE.
22725 On exit *REF_CU is the CU of the result. */
22726
22727static struct die_info *
ff39bb5e 22728follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
22729 struct dwarf2_cu **ref_cu)
22730{
22731 struct die_info *die;
22732
7771576e 22733 if (attr_form_is_ref (attr))
348e048f 22734 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 22735 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
22736 die = follow_die_sig (src_die, attr, ref_cu);
22737 else
22738 {
22739 dump_die_for_error (src_die);
22740 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
518817b3 22741 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
348e048f
DE
22742 }
22743
22744 return die;
03dd20cc
DJ
22745}
22746
5c631832 22747/* Follow reference OFFSET.
673bfd45
DE
22748 On entry *REF_CU is the CU of the source die referencing OFFSET.
22749 On exit *REF_CU is the CU of the result.
22750 Returns NULL if OFFSET is invalid. */
f504f079 22751
f9aca02d 22752static struct die_info *
9c541725 22753follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 22754 struct dwarf2_cu **ref_cu)
c906108c 22755{
10b3939b 22756 struct die_info temp_die;
f2f0e013 22757 struct dwarf2_cu *target_cu, *cu = *ref_cu;
518817b3
SM
22758 struct dwarf2_per_objfile *dwarf2_per_objfile
22759 = cu->per_cu->dwarf2_per_objfile;
10b3939b 22760
348e048f
DE
22761 gdb_assert (cu->per_cu != NULL);
22762
98bfdba5
PA
22763 target_cu = cu;
22764
3019eac3 22765 if (cu->per_cu->is_debug_types)
348e048f
DE
22766 {
22767 /* .debug_types CUs cannot reference anything outside their CU.
22768 If they need to, they have to reference a signatured type via
55f1336d 22769 DW_FORM_ref_sig8. */
9c541725 22770 if (!offset_in_cu_p (&cu->header, sect_off))
5c631832 22771 return NULL;
348e048f 22772 }
36586728 22773 else if (offset_in_dwz != cu->per_cu->is_dwz
9c541725 22774 || !offset_in_cu_p (&cu->header, sect_off))
10b3939b
DJ
22775 {
22776 struct dwarf2_per_cu_data *per_cu;
9a619af0 22777
9c541725 22778 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 22779 dwarf2_per_objfile);
03dd20cc
DJ
22780
22781 /* If necessary, add it to the queue and load its DIEs. */
95554aad 22782 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 22783 load_full_comp_unit (per_cu, false, cu->language);
03dd20cc 22784
10b3939b
DJ
22785 target_cu = per_cu->cu;
22786 }
98bfdba5
PA
22787 else if (cu->dies == NULL)
22788 {
22789 /* We're loading full DIEs during partial symbol reading. */
22790 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
58f0c718 22791 load_full_comp_unit (cu->per_cu, false, language_minimal);
98bfdba5 22792 }
c906108c 22793
f2f0e013 22794 *ref_cu = target_cu;
9c541725 22795 temp_die.sect_off = sect_off;
9a3c8263 22796 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
22797 &temp_die,
22798 to_underlying (sect_off));
5c631832 22799}
10b3939b 22800
5c631832
JK
22801/* Follow reference attribute ATTR of SRC_DIE.
22802 On entry *REF_CU is the CU of SRC_DIE.
22803 On exit *REF_CU is the CU of the result. */
22804
22805static struct die_info *
ff39bb5e 22806follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
22807 struct dwarf2_cu **ref_cu)
22808{
9c541725 22809 sect_offset sect_off = dwarf2_get_ref_die_offset (attr);
5c631832
JK
22810 struct dwarf2_cu *cu = *ref_cu;
22811 struct die_info *die;
22812
9c541725 22813 die = follow_die_offset (sect_off,
36586728
TT
22814 (attr->form == DW_FORM_GNU_ref_alt
22815 || cu->per_cu->is_dwz),
22816 ref_cu);
5c631832 22817 if (!die)
9d8780f0
SM
22818 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22819 "at %s [in module %s]"),
22820 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
518817b3 22821 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
348e048f 22822
5c631832
JK
22823 return die;
22824}
22825
9c541725 22826/* Return DWARF block referenced by DW_AT_location of DIE at SECT_OFF at PER_CU.
d83e736b 22827 Returned value is intended for DW_OP_call*. Returned
e3b94546
SM
22828 dwarf2_locexpr_baton->data has lifetime of
22829 PER_CU->DWARF2_PER_OBJFILE->OBJFILE. */
5c631832
JK
22830
22831struct dwarf2_locexpr_baton
9c541725 22832dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
8b9737bf
TT
22833 struct dwarf2_per_cu_data *per_cu,
22834 CORE_ADDR (*get_frame_pc) (void *baton),
22835 void *baton)
5c631832 22836{
918dd910 22837 struct dwarf2_cu *cu;
5c631832
JK
22838 struct die_info *die;
22839 struct attribute *attr;
22840 struct dwarf2_locexpr_baton retval;
12359b5e
SM
22841 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22842 struct objfile *objfile = dwarf2_per_objfile->objfile;
8cf6f0b1 22843
918dd910 22844 if (per_cu->cu == NULL)
58f0c718 22845 load_cu (per_cu, false);
918dd910 22846 cu = per_cu->cu;
cc12ce38
DE
22847 if (cu == NULL)
22848 {
22849 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22850 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22851 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22852 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22853 }
918dd910 22854
9c541725 22855 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 22856 if (!die)
9d8780f0
SM
22857 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22858 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22859
22860 attr = dwarf2_attr (die, DW_AT_location, cu);
22861 if (!attr)
22862 {
e103e986
JK
22863 /* DWARF: "If there is no such attribute, then there is no effect.".
22864 DATA is ignored if SIZE is 0. */
5c631832 22865
e103e986 22866 retval.data = NULL;
5c631832
JK
22867 retval.size = 0;
22868 }
8cf6f0b1
TT
22869 else if (attr_form_is_section_offset (attr))
22870 {
22871 struct dwarf2_loclist_baton loclist_baton;
22872 CORE_ADDR pc = (*get_frame_pc) (baton);
22873 size_t size;
22874
22875 fill_in_loclist_baton (cu, &loclist_baton, attr);
22876
22877 retval.data = dwarf2_find_location_expression (&loclist_baton,
22878 &size, pc);
22879 retval.size = size;
22880 }
5c631832
JK
22881 else
22882 {
22883 if (!attr_form_is_block (attr))
9d8780f0 22884 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 22885 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 22886 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22887
22888 retval.data = DW_BLOCK (attr)->data;
22889 retval.size = DW_BLOCK (attr)->size;
22890 }
22891 retval.per_cu = cu->per_cu;
918dd910 22892
ed2dc618 22893 age_cached_comp_units (dwarf2_per_objfile);
918dd910 22894
5c631832 22895 return retval;
348e048f
DE
22896}
22897
8b9737bf
TT
22898/* Like dwarf2_fetch_die_loc_sect_off, but take a CU
22899 offset. */
22900
22901struct dwarf2_locexpr_baton
22902dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
22903 struct dwarf2_per_cu_data *per_cu,
22904 CORE_ADDR (*get_frame_pc) (void *baton),
22905 void *baton)
22906{
9c541725 22907 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 22908
9c541725 22909 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
22910}
22911
b6807d98
TT
22912/* Write a constant of a given type as target-ordered bytes into
22913 OBSTACK. */
22914
22915static const gdb_byte *
22916write_constant_as_bytes (struct obstack *obstack,
22917 enum bfd_endian byte_order,
22918 struct type *type,
22919 ULONGEST value,
22920 LONGEST *len)
22921{
22922 gdb_byte *result;
22923
22924 *len = TYPE_LENGTH (type);
224c3ddb 22925 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22926 store_unsigned_integer (result, *len, byte_order, value);
22927
22928 return result;
22929}
22930
22931/* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
22932 pointer to the constant bytes and set LEN to the length of the
22933 data. If memory is needed, allocate it on OBSTACK. If the DIE
22934 does not have a DW_AT_const_value, return NULL. */
22935
22936const gdb_byte *
9c541725 22937dwarf2_fetch_constant_bytes (sect_offset sect_off,
b6807d98
TT
22938 struct dwarf2_per_cu_data *per_cu,
22939 struct obstack *obstack,
22940 LONGEST *len)
22941{
22942 struct dwarf2_cu *cu;
22943 struct die_info *die;
22944 struct attribute *attr;
22945 const gdb_byte *result = NULL;
22946 struct type *type;
22947 LONGEST value;
22948 enum bfd_endian byte_order;
e3b94546 22949 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 22950
b6807d98 22951 if (per_cu->cu == NULL)
58f0c718 22952 load_cu (per_cu, false);
b6807d98 22953 cu = per_cu->cu;
cc12ce38
DE
22954 if (cu == NULL)
22955 {
22956 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22957 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22958 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22959 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22960 }
b6807d98 22961
9c541725 22962 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 22963 if (!die)
9d8780f0
SM
22964 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22965 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
22966
22967 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22968 if (attr == NULL)
22969 return NULL;
22970
e3b94546 22971 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
22972 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22973
22974 switch (attr->form)
22975 {
22976 case DW_FORM_addr:
22977 case DW_FORM_GNU_addr_index:
22978 {
22979 gdb_byte *tem;
22980
22981 *len = cu->header.addr_size;
224c3ddb 22982 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22983 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22984 result = tem;
22985 }
22986 break;
22987 case DW_FORM_string:
22988 case DW_FORM_strp:
22989 case DW_FORM_GNU_str_index:
22990 case DW_FORM_GNU_strp_alt:
22991 /* DW_STRING is already allocated on the objfile obstack, point
22992 directly to it. */
22993 result = (const gdb_byte *) DW_STRING (attr);
22994 *len = strlen (DW_STRING (attr));
22995 break;
22996 case DW_FORM_block1:
22997 case DW_FORM_block2:
22998 case DW_FORM_block4:
22999 case DW_FORM_block:
23000 case DW_FORM_exprloc:
0224619f 23001 case DW_FORM_data16:
b6807d98
TT
23002 result = DW_BLOCK (attr)->data;
23003 *len = DW_BLOCK (attr)->size;
23004 break;
23005
23006 /* The DW_AT_const_value attributes are supposed to carry the
23007 symbol's value "represented as it would be on the target
23008 architecture." By the time we get here, it's already been
23009 converted to host endianness, so we just need to sign- or
23010 zero-extend it as appropriate. */
23011 case DW_FORM_data1:
23012 type = die_type (die, cu);
23013 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23014 if (result == NULL)
23015 result = write_constant_as_bytes (obstack, byte_order,
23016 type, value, len);
23017 break;
23018 case DW_FORM_data2:
23019 type = die_type (die, cu);
23020 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23021 if (result == NULL)
23022 result = write_constant_as_bytes (obstack, byte_order,
23023 type, value, len);
23024 break;
23025 case DW_FORM_data4:
23026 type = die_type (die, cu);
23027 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23028 if (result == NULL)
23029 result = write_constant_as_bytes (obstack, byte_order,
23030 type, value, len);
23031 break;
23032 case DW_FORM_data8:
23033 type = die_type (die, cu);
23034 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23035 if (result == NULL)
23036 result = write_constant_as_bytes (obstack, byte_order,
23037 type, value, len);
23038 break;
23039
23040 case DW_FORM_sdata:
663c44ac 23041 case DW_FORM_implicit_const:
b6807d98
TT
23042 type = die_type (die, cu);
23043 result = write_constant_as_bytes (obstack, byte_order,
23044 type, DW_SND (attr), len);
23045 break;
23046
23047 case DW_FORM_udata:
23048 type = die_type (die, cu);
23049 result = write_constant_as_bytes (obstack, byte_order,
23050 type, DW_UNSND (attr), len);
23051 break;
23052
23053 default:
b98664d3 23054 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
23055 dwarf_form_name (attr->form));
23056 break;
23057 }
23058
23059 return result;
23060}
23061
7942e96e
AA
23062/* Return the type of the die at OFFSET in PER_CU. Return NULL if no
23063 valid type for this die is found. */
23064
23065struct type *
9c541725 23066dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
7942e96e
AA
23067 struct dwarf2_per_cu_data *per_cu)
23068{
23069 struct dwarf2_cu *cu;
23070 struct die_info *die;
23071
7942e96e 23072 if (per_cu->cu == NULL)
58f0c718 23073 load_cu (per_cu, false);
7942e96e
AA
23074 cu = per_cu->cu;
23075 if (!cu)
23076 return NULL;
23077
9c541725 23078 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
23079 if (!die)
23080 return NULL;
23081
23082 return die_type (die, cu);
23083}
23084
8a9b8146
TT
23085/* Return the type of the DIE at DIE_OFFSET in the CU named by
23086 PER_CU. */
23087
23088struct type *
b64f50a1 23089dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
23090 struct dwarf2_per_cu_data *per_cu)
23091{
9c541725 23092 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 23093 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
23094}
23095
ac9ec31b 23096/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 23097 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
23098 On exit *REF_CU is the CU of the result.
23099 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
23100
23101static struct die_info *
ac9ec31b
DE
23102follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23103 struct dwarf2_cu **ref_cu)
348e048f 23104{
348e048f 23105 struct die_info temp_die;
348e048f
DE
23106 struct dwarf2_cu *sig_cu;
23107 struct die_info *die;
23108
ac9ec31b
DE
23109 /* While it might be nice to assert sig_type->type == NULL here,
23110 we can get here for DW_AT_imported_declaration where we need
23111 the DIE not the type. */
348e048f
DE
23112
23113 /* If necessary, add it to the queue and load its DIEs. */
23114
95554aad 23115 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 23116 read_signatured_type (sig_type);
348e048f 23117
348e048f 23118 sig_cu = sig_type->per_cu.cu;
69d751e3 23119 gdb_assert (sig_cu != NULL);
9c541725
PA
23120 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23121 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 23122 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 23123 to_underlying (temp_die.sect_off));
348e048f
DE
23124 if (die)
23125 {
ed2dc618 23126 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 23127 = (*ref_cu)->per_cu->dwarf2_per_objfile;
ed2dc618 23128
796a7ff8
DE
23129 /* For .gdb_index version 7 keep track of included TUs.
23130 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
23131 if (dwarf2_per_objfile->index_table != NULL
23132 && dwarf2_per_objfile->index_table->version <= 7)
23133 {
23134 VEC_safe_push (dwarf2_per_cu_ptr,
23135 (*ref_cu)->per_cu->imported_symtabs,
23136 sig_cu->per_cu);
23137 }
23138
348e048f
DE
23139 *ref_cu = sig_cu;
23140 return die;
23141 }
23142
ac9ec31b
DE
23143 return NULL;
23144}
23145
23146/* Follow signatured type referenced by ATTR in SRC_DIE.
23147 On entry *REF_CU is the CU of SRC_DIE.
23148 On exit *REF_CU is the CU of the result.
23149 The result is the DIE of the type.
23150 If the referenced type cannot be found an error is thrown. */
23151
23152static struct die_info *
ff39bb5e 23153follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
23154 struct dwarf2_cu **ref_cu)
23155{
23156 ULONGEST signature = DW_SIGNATURE (attr);
23157 struct signatured_type *sig_type;
23158 struct die_info *die;
23159
23160 gdb_assert (attr->form == DW_FORM_ref_sig8);
23161
a2ce51a0 23162 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
23163 /* sig_type will be NULL if the signatured type is missing from
23164 the debug info. */
23165 if (sig_type == NULL)
23166 {
23167 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23168 " from DIE at %s [in module %s]"),
23169 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23170 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23171 }
23172
23173 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23174 if (die == NULL)
23175 {
23176 dump_die_for_error (src_die);
23177 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23178 " from DIE at %s [in module %s]"),
23179 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 23180 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
23181 }
23182
23183 return die;
23184}
23185
23186/* Get the type specified by SIGNATURE referenced in DIE/CU,
23187 reading in and processing the type unit if necessary. */
23188
23189static struct type *
23190get_signatured_type (struct die_info *die, ULONGEST signature,
23191 struct dwarf2_cu *cu)
23192{
518817b3
SM
23193 struct dwarf2_per_objfile *dwarf2_per_objfile
23194 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
23195 struct signatured_type *sig_type;
23196 struct dwarf2_cu *type_cu;
23197 struct die_info *type_die;
23198 struct type *type;
23199
a2ce51a0 23200 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
23201 /* sig_type will be NULL if the signatured type is missing from
23202 the debug info. */
23203 if (sig_type == NULL)
23204 {
b98664d3 23205 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
23206 " from DIE at %s [in module %s]"),
23207 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23208 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23209 return build_error_marker_type (cu, die);
23210 }
23211
23212 /* If we already know the type we're done. */
23213 if (sig_type->type != NULL)
23214 return sig_type->type;
23215
23216 type_cu = cu;
23217 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
23218 if (type_die != NULL)
23219 {
23220 /* N.B. We need to call get_die_type to ensure only one type for this DIE
23221 is created. This is important, for example, because for c++ classes
23222 we need TYPE_NAME set which is only done by new_symbol. Blech. */
23223 type = read_type_die (type_die, type_cu);
23224 if (type == NULL)
23225 {
b98664d3 23226 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
23227 " referenced from DIE at %s [in module %s]"),
23228 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23229 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23230 type = build_error_marker_type (cu, die);
23231 }
23232 }
23233 else
23234 {
b98664d3 23235 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23236 " from DIE at %s [in module %s]"),
23237 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 23238 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23239 type = build_error_marker_type (cu, die);
23240 }
23241 sig_type->type = type;
23242
23243 return type;
23244}
23245
23246/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
23247 reading in and processing the type unit if necessary. */
23248
23249static struct type *
ff39bb5e 23250get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 23251 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
23252{
23253 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
7771576e 23254 if (attr_form_is_ref (attr))
ac9ec31b
DE
23255 {
23256 struct dwarf2_cu *type_cu = cu;
23257 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
23258
23259 return read_type_die (type_die, type_cu);
23260 }
23261 else if (attr->form == DW_FORM_ref_sig8)
23262 {
23263 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
23264 }
23265 else
23266 {
518817b3
SM
23267 struct dwarf2_per_objfile *dwarf2_per_objfile
23268 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 23269
b98664d3 23270 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
23271 " at %s [in module %s]"),
23272 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 23273 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
23274 return build_error_marker_type (cu, die);
23275 }
348e048f
DE
23276}
23277
e5fe5e75 23278/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
23279
23280static void
e5fe5e75 23281load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 23282{
52dc124a 23283 struct signatured_type *sig_type;
348e048f 23284
f4dc4d17
DE
23285 /* Caller is responsible for ensuring type_unit_groups don't get here. */
23286 gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
23287
6721b2ec
DE
23288 /* We have the per_cu, but we need the signatured_type.
23289 Fortunately this is an easy translation. */
23290 gdb_assert (per_cu->is_debug_types);
23291 sig_type = (struct signatured_type *) per_cu;
348e048f 23292
6721b2ec 23293 gdb_assert (per_cu->cu == NULL);
348e048f 23294
52dc124a 23295 read_signatured_type (sig_type);
348e048f 23296
6721b2ec 23297 gdb_assert (per_cu->cu != NULL);
348e048f
DE
23298}
23299
dee91e82
DE
23300/* die_reader_func for read_signatured_type.
23301 This is identical to load_full_comp_unit_reader,
23302 but is kept separate for now. */
348e048f
DE
23303
23304static void
dee91e82 23305read_signatured_type_reader (const struct die_reader_specs *reader,
d521ce57 23306 const gdb_byte *info_ptr,
dee91e82
DE
23307 struct die_info *comp_unit_die,
23308 int has_children,
23309 void *data)
348e048f 23310{
dee91e82 23311 struct dwarf2_cu *cu = reader->cu;
348e048f 23312
dee91e82
DE
23313 gdb_assert (cu->die_hash == NULL);
23314 cu->die_hash =
23315 htab_create_alloc_ex (cu->header.length / 12,
23316 die_hash,
23317 die_eq,
23318 NULL,
23319 &cu->comp_unit_obstack,
23320 hashtab_obstack_allocate,
23321 dummy_obstack_deallocate);
348e048f 23322
dee91e82
DE
23323 if (has_children)
23324 comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
23325 &info_ptr, comp_unit_die);
23326 cu->dies = comp_unit_die;
23327 /* comp_unit_die is not stored in die_hash, no need. */
348e048f
DE
23328
23329 /* We try not to read any attributes in this function, because not
9cdd5dbd 23330 all CUs needed for references have been loaded yet, and symbol
348e048f 23331 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
23332 or we won't be able to build types correctly.
23333 Similarly, if we do not read the producer, we can not apply
23334 producer-specific interpretation. */
95554aad 23335 prepare_one_comp_unit (cu, cu->dies, language_minimal);
dee91e82 23336}
348e048f 23337
3019eac3
DE
23338/* Read in a signatured type and build its CU and DIEs.
23339 If the type is a stub for the real type in a DWO file,
23340 read in the real type from the DWO file as well. */
dee91e82
DE
23341
23342static void
23343read_signatured_type (struct signatured_type *sig_type)
23344{
23345 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 23346
3019eac3 23347 gdb_assert (per_cu->is_debug_types);
dee91e82 23348 gdb_assert (per_cu->cu == NULL);
348e048f 23349
58f0c718 23350 init_cutu_and_read_dies (per_cu, NULL, 0, 1, false,
f4dc4d17 23351 read_signatured_type_reader, NULL);
7ee85ab1 23352 sig_type->per_cu.tu_read = 1;
c906108c
SS
23353}
23354
c906108c
SS
23355/* Decode simple location descriptions.
23356 Given a pointer to a dwarf block that defines a location, compute
23357 the location and return the value.
23358
4cecd739
DJ
23359 NOTE drow/2003-11-18: This function is called in two situations
23360 now: for the address of static or global variables (partial symbols
23361 only) and for offsets into structures which are expected to be
23362 (more or less) constant. The partial symbol case should go away,
23363 and only the constant case should remain. That will let this
23364 function complain more accurately. A few special modes are allowed
23365 without complaint for global variables (for instance, global
23366 register values and thread-local values).
c906108c
SS
23367
23368 A location description containing no operations indicates that the
4cecd739 23369 object is optimized out. The return value is 0 for that case.
6b992462
DJ
23370 FIXME drow/2003-11-16: No callers check for this case any more; soon all
23371 callers will only want a very basic result and this can become a
21ae7a4d
JK
23372 complaint.
23373
23374 Note that stack[0] is unused except as a default error return. */
c906108c
SS
23375
23376static CORE_ADDR
e7c27a73 23377decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 23378{
518817b3 23379 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
56eb65bd
SP
23380 size_t i;
23381 size_t size = blk->size;
d521ce57 23382 const gdb_byte *data = blk->data;
21ae7a4d
JK
23383 CORE_ADDR stack[64];
23384 int stacki;
23385 unsigned int bytes_read, unsnd;
23386 gdb_byte op;
c906108c 23387
21ae7a4d
JK
23388 i = 0;
23389 stacki = 0;
23390 stack[stacki] = 0;
23391 stack[++stacki] = 0;
23392
23393 while (i < size)
23394 {
23395 op = data[i++];
23396 switch (op)
23397 {
23398 case DW_OP_lit0:
23399 case DW_OP_lit1:
23400 case DW_OP_lit2:
23401 case DW_OP_lit3:
23402 case DW_OP_lit4:
23403 case DW_OP_lit5:
23404 case DW_OP_lit6:
23405 case DW_OP_lit7:
23406 case DW_OP_lit8:
23407 case DW_OP_lit9:
23408 case DW_OP_lit10:
23409 case DW_OP_lit11:
23410 case DW_OP_lit12:
23411 case DW_OP_lit13:
23412 case DW_OP_lit14:
23413 case DW_OP_lit15:
23414 case DW_OP_lit16:
23415 case DW_OP_lit17:
23416 case DW_OP_lit18:
23417 case DW_OP_lit19:
23418 case DW_OP_lit20:
23419 case DW_OP_lit21:
23420 case DW_OP_lit22:
23421 case DW_OP_lit23:
23422 case DW_OP_lit24:
23423 case DW_OP_lit25:
23424 case DW_OP_lit26:
23425 case DW_OP_lit27:
23426 case DW_OP_lit28:
23427 case DW_OP_lit29:
23428 case DW_OP_lit30:
23429 case DW_OP_lit31:
23430 stack[++stacki] = op - DW_OP_lit0;
23431 break;
f1bea926 23432
21ae7a4d
JK
23433 case DW_OP_reg0:
23434 case DW_OP_reg1:
23435 case DW_OP_reg2:
23436 case DW_OP_reg3:
23437 case DW_OP_reg4:
23438 case DW_OP_reg5:
23439 case DW_OP_reg6:
23440 case DW_OP_reg7:
23441 case DW_OP_reg8:
23442 case DW_OP_reg9:
23443 case DW_OP_reg10:
23444 case DW_OP_reg11:
23445 case DW_OP_reg12:
23446 case DW_OP_reg13:
23447 case DW_OP_reg14:
23448 case DW_OP_reg15:
23449 case DW_OP_reg16:
23450 case DW_OP_reg17:
23451 case DW_OP_reg18:
23452 case DW_OP_reg19:
23453 case DW_OP_reg20:
23454 case DW_OP_reg21:
23455 case DW_OP_reg22:
23456 case DW_OP_reg23:
23457 case DW_OP_reg24:
23458 case DW_OP_reg25:
23459 case DW_OP_reg26:
23460 case DW_OP_reg27:
23461 case DW_OP_reg28:
23462 case DW_OP_reg29:
23463 case DW_OP_reg30:
23464 case DW_OP_reg31:
23465 stack[++stacki] = op - DW_OP_reg0;
23466 if (i < size)
23467 dwarf2_complex_location_expr_complaint ();
23468 break;
c906108c 23469
21ae7a4d
JK
23470 case DW_OP_regx:
23471 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
23472 i += bytes_read;
23473 stack[++stacki] = unsnd;
23474 if (i < size)
23475 dwarf2_complex_location_expr_complaint ();
23476 break;
c906108c 23477
21ae7a4d
JK
23478 case DW_OP_addr:
23479 stack[++stacki] = read_address (objfile->obfd, &data[i],
23480 cu, &bytes_read);
23481 i += bytes_read;
23482 break;
d53d4ac5 23483
21ae7a4d
JK
23484 case DW_OP_const1u:
23485 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
23486 i += 1;
23487 break;
23488
23489 case DW_OP_const1s:
23490 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
23491 i += 1;
23492 break;
23493
23494 case DW_OP_const2u:
23495 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23496 i += 2;
23497 break;
23498
23499 case DW_OP_const2s:
23500 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23501 i += 2;
23502 break;
d53d4ac5 23503
21ae7a4d
JK
23504 case DW_OP_const4u:
23505 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23506 i += 4;
23507 break;
23508
23509 case DW_OP_const4s:
23510 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23511 i += 4;
23512 break;
23513
585861ea
JK
23514 case DW_OP_const8u:
23515 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23516 i += 8;
23517 break;
23518
21ae7a4d
JK
23519 case DW_OP_constu:
23520 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23521 &bytes_read);
23522 i += bytes_read;
23523 break;
23524
23525 case DW_OP_consts:
23526 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23527 i += bytes_read;
23528 break;
23529
23530 case DW_OP_dup:
23531 stack[stacki + 1] = stack[stacki];
23532 stacki++;
23533 break;
23534
23535 case DW_OP_plus:
23536 stack[stacki - 1] += stack[stacki];
23537 stacki--;
23538 break;
23539
23540 case DW_OP_plus_uconst:
23541 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23542 &bytes_read);
23543 i += bytes_read;
23544 break;
23545
23546 case DW_OP_minus:
23547 stack[stacki - 1] -= stack[stacki];
23548 stacki--;
23549 break;
23550
23551 case DW_OP_deref:
23552 /* If we're not the last op, then we definitely can't encode
23553 this using GDB's address_class enum. This is valid for partial
23554 global symbols, although the variable's address will be bogus
23555 in the psymtab. */
23556 if (i < size)
23557 dwarf2_complex_location_expr_complaint ();
23558 break;
23559
23560 case DW_OP_GNU_push_tls_address:
4aa4e28b 23561 case DW_OP_form_tls_address:
21ae7a4d
JK
23562 /* The top of the stack has the offset from the beginning
23563 of the thread control block at which the variable is located. */
23564 /* Nothing should follow this operator, so the top of stack would
23565 be returned. */
23566 /* This is valid for partial global symbols, but the variable's
585861ea
JK
23567 address will be bogus in the psymtab. Make it always at least
23568 non-zero to not look as a variable garbage collected by linker
23569 which have DW_OP_addr 0. */
21ae7a4d
JK
23570 if (i < size)
23571 dwarf2_complex_location_expr_complaint ();
585861ea 23572 stack[stacki]++;
21ae7a4d
JK
23573 break;
23574
23575 case DW_OP_GNU_uninit:
23576 break;
23577
3019eac3 23578 case DW_OP_GNU_addr_index:
49f6c839 23579 case DW_OP_GNU_const_index:
3019eac3
DE
23580 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23581 &bytes_read);
23582 i += bytes_read;
23583 break;
23584
21ae7a4d
JK
23585 default:
23586 {
f39c6ffd 23587 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
23588
23589 if (name)
b98664d3 23590 complaint (_("unsupported stack op: '%s'"),
21ae7a4d
JK
23591 name);
23592 else
b98664d3 23593 complaint (_("unsupported stack op: '%02x'"),
21ae7a4d
JK
23594 op);
23595 }
23596
23597 return (stack[stacki]);
d53d4ac5 23598 }
3c6e0cb3 23599
21ae7a4d
JK
23600 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23601 outside of the allocated space. Also enforce minimum>0. */
23602 if (stacki >= ARRAY_SIZE (stack) - 1)
23603 {
b98664d3 23604 complaint (_("location description stack overflow"));
21ae7a4d
JK
23605 return 0;
23606 }
23607
23608 if (stacki <= 0)
23609 {
b98664d3 23610 complaint (_("location description stack underflow"));
21ae7a4d
JK
23611 return 0;
23612 }
23613 }
23614 return (stack[stacki]);
c906108c
SS
23615}
23616
23617/* memory allocation interface */
23618
c906108c 23619static struct dwarf_block *
7b5a2f43 23620dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 23621{
8d749320 23622 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
23623}
23624
c906108c 23625static struct die_info *
b60c80d6 23626dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
23627{
23628 struct die_info *die;
b60c80d6
DJ
23629 size_t size = sizeof (struct die_info);
23630
23631 if (num_attrs > 1)
23632 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 23633
b60c80d6 23634 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
23635 memset (die, 0, sizeof (struct die_info));
23636 return (die);
23637}
2e276125
JB
23638
23639\f
23640/* Macro support. */
23641
233d95b5
JK
23642/* Return file name relative to the compilation directory of file number I in
23643 *LH's file name table. The result is allocated using xmalloc; the caller is
2e276125 23644 responsible for freeing it. */
233d95b5 23645
2e276125 23646static char *
233d95b5 23647file_file_name (int file, struct line_header *lh)
2e276125 23648{
6a83a1e6
EZ
23649 /* Is the file number a valid index into the line header's file name
23650 table? Remember that file numbers start with one, not zero. */
fff8551c 23651 if (1 <= file && file <= lh->file_names.size ())
6a83a1e6 23652 {
8c43009f 23653 const file_entry &fe = lh->file_names[file - 1];
6e70227d 23654
8c43009f
PA
23655 if (!IS_ABSOLUTE_PATH (fe.name))
23656 {
23657 const char *dir = fe.include_dir (lh);
23658 if (dir != NULL)
23659 return concat (dir, SLASH_STRING, fe.name, (char *) NULL);
23660 }
23661 return xstrdup (fe.name);
6a83a1e6 23662 }
2e276125
JB
23663 else
23664 {
6a83a1e6
EZ
23665 /* The compiler produced a bogus file number. We can at least
23666 record the macro definitions made in the file, even if we
23667 won't be able to find the file by name. */
23668 char fake_name[80];
9a619af0 23669
8c042590
PM
23670 xsnprintf (fake_name, sizeof (fake_name),
23671 "<bad macro file number %d>", file);
2e276125 23672
b98664d3 23673 complaint (_("bad file number in macro information (%d)"),
6a83a1e6 23674 file);
2e276125 23675
6a83a1e6 23676 return xstrdup (fake_name);
2e276125
JB
23677 }
23678}
23679
233d95b5
JK
23680/* Return the full name of file number I in *LH's file name table.
23681 Use COMP_DIR as the name of the current directory of the
23682 compilation. The result is allocated using xmalloc; the caller is
23683 responsible for freeing it. */
23684static char *
23685file_full_name (int file, struct line_header *lh, const char *comp_dir)
23686{
23687 /* Is the file number a valid index into the line header's file name
23688 table? Remember that file numbers start with one, not zero. */
fff8551c 23689 if (1 <= file && file <= lh->file_names.size ())
233d95b5
JK
23690 {
23691 char *relative = file_file_name (file, lh);
23692
23693 if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
23694 return relative;
b36cec19
PA
23695 return reconcat (relative, comp_dir, SLASH_STRING,
23696 relative, (char *) NULL);
233d95b5
JK
23697 }
23698 else
23699 return file_file_name (file, lh);
23700}
23701
2e276125
JB
23702
23703static struct macro_source_file *
23704macro_start_file (int file, int line,
23705 struct macro_source_file *current_file,
43f3e411 23706 struct line_header *lh)
2e276125 23707{
233d95b5
JK
23708 /* File name relative to the compilation directory of this source file. */
23709 char *file_name = file_file_name (file, lh);
2e276125 23710
2e276125 23711 if (! current_file)
abc9d0dc 23712 {
fc474241
DE
23713 /* Note: We don't create a macro table for this compilation unit
23714 at all until we actually get a filename. */
43f3e411 23715 struct macro_table *macro_table = get_macro_table ();
fc474241 23716
abc9d0dc
TT
23717 /* If we have no current file, then this must be the start_file
23718 directive for the compilation unit's main source file. */
fc474241
DE
23719 current_file = macro_set_main (macro_table, file_name);
23720 macro_define_special (macro_table);
abc9d0dc 23721 }
2e276125 23722 else
233d95b5 23723 current_file = macro_include (current_file, line, file_name);
2e276125 23724
233d95b5 23725 xfree (file_name);
6e70227d 23726
2e276125
JB
23727 return current_file;
23728}
23729
2e276125
JB
23730static const char *
23731consume_improper_spaces (const char *p, const char *body)
23732{
23733 if (*p == ' ')
23734 {
b98664d3 23735 complaint (_("macro definition contains spaces "
3e43a32a 23736 "in formal argument list:\n`%s'"),
4d3c2250 23737 body);
2e276125
JB
23738
23739 while (*p == ' ')
23740 p++;
23741 }
23742
23743 return p;
23744}
23745
23746
23747static void
23748parse_macro_definition (struct macro_source_file *file, int line,
23749 const char *body)
23750{
23751 const char *p;
23752
23753 /* The body string takes one of two forms. For object-like macro
23754 definitions, it should be:
23755
23756 <macro name> " " <definition>
23757
23758 For function-like macro definitions, it should be:
23759
23760 <macro name> "() " <definition>
23761 or
23762 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
23763
23764 Spaces may appear only where explicitly indicated, and in the
23765 <definition>.
23766
23767 The Dwarf 2 spec says that an object-like macro's name is always
23768 followed by a space, but versions of GCC around March 2002 omit
6e70227d 23769 the space when the macro's definition is the empty string.
2e276125
JB
23770
23771 The Dwarf 2 spec says that there should be no spaces between the
23772 formal arguments in a function-like macro's formal argument list,
23773 but versions of GCC around March 2002 include spaces after the
23774 commas. */
23775
23776
23777 /* Find the extent of the macro name. The macro name is terminated
23778 by either a space or null character (for an object-like macro) or
23779 an opening paren (for a function-like macro). */
23780 for (p = body; *p; p++)
23781 if (*p == ' ' || *p == '(')
23782 break;
23783
23784 if (*p == ' ' || *p == '\0')
23785 {
23786 /* It's an object-like macro. */
23787 int name_len = p - body;
3f8a7804 23788 char *name = savestring (body, name_len);
2e276125
JB
23789 const char *replacement;
23790
23791 if (*p == ' ')
23792 replacement = body + name_len + 1;
23793 else
23794 {
4d3c2250 23795 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23796 replacement = body + name_len;
23797 }
6e70227d 23798
2e276125
JB
23799 macro_define_object (file, line, name, replacement);
23800
23801 xfree (name);
23802 }
23803 else if (*p == '(')
23804 {
23805 /* It's a function-like macro. */
3f8a7804 23806 char *name = savestring (body, p - body);
2e276125
JB
23807 int argc = 0;
23808 int argv_size = 1;
8d749320 23809 char **argv = XNEWVEC (char *, argv_size);
2e276125
JB
23810
23811 p++;
23812
23813 p = consume_improper_spaces (p, body);
23814
23815 /* Parse the formal argument list. */
23816 while (*p && *p != ')')
23817 {
23818 /* Find the extent of the current argument name. */
23819 const char *arg_start = p;
23820
23821 while (*p && *p != ',' && *p != ')' && *p != ' ')
23822 p++;
23823
23824 if (! *p || p == arg_start)
4d3c2250 23825 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23826 else
23827 {
23828 /* Make sure argv has room for the new argument. */
23829 if (argc >= argv_size)
23830 {
23831 argv_size *= 2;
224c3ddb 23832 argv = XRESIZEVEC (char *, argv, argv_size);
2e276125
JB
23833 }
23834
3f8a7804 23835 argv[argc++] = savestring (arg_start, p - arg_start);
2e276125
JB
23836 }
23837
23838 p = consume_improper_spaces (p, body);
23839
23840 /* Consume the comma, if present. */
23841 if (*p == ',')
23842 {
23843 p++;
23844
23845 p = consume_improper_spaces (p, body);
23846 }
23847 }
23848
23849 if (*p == ')')
23850 {
23851 p++;
23852
23853 if (*p == ' ')
23854 /* Perfectly formed definition, no complaints. */
23855 macro_define_function (file, line, name,
6e70227d 23856 argc, (const char **) argv,
2e276125
JB
23857 p + 1);
23858 else if (*p == '\0')
23859 {
23860 /* Complain, but do define it. */
4d3c2250 23861 dwarf2_macro_malformed_definition_complaint (body);
2e276125 23862 macro_define_function (file, line, name,
6e70227d 23863 argc, (const char **) argv,
2e276125
JB
23864 p);
23865 }
23866 else
23867 /* Just complain. */
4d3c2250 23868 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23869 }
23870 else
23871 /* Just complain. */
4d3c2250 23872 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23873
23874 xfree (name);
23875 {
23876 int i;
23877
23878 for (i = 0; i < argc; i++)
23879 xfree (argv[i]);
23880 }
23881 xfree (argv);
23882 }
23883 else
4d3c2250 23884 dwarf2_macro_malformed_definition_complaint (body);
2e276125
JB
23885}
23886
cf2c3c16
TT
23887/* Skip some bytes from BYTES according to the form given in FORM.
23888 Returns the new pointer. */
2e276125 23889
d521ce57
TT
23890static const gdb_byte *
23891skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
cf2c3c16
TT
23892 enum dwarf_form form,
23893 unsigned int offset_size,
23894 struct dwarf2_section_info *section)
2e276125 23895{
cf2c3c16 23896 unsigned int bytes_read;
2e276125 23897
cf2c3c16 23898 switch (form)
2e276125 23899 {
cf2c3c16
TT
23900 case DW_FORM_data1:
23901 case DW_FORM_flag:
23902 ++bytes;
23903 break;
23904
23905 case DW_FORM_data2:
23906 bytes += 2;
23907 break;
23908
23909 case DW_FORM_data4:
23910 bytes += 4;
23911 break;
23912
23913 case DW_FORM_data8:
23914 bytes += 8;
23915 break;
23916
0224619f
JK
23917 case DW_FORM_data16:
23918 bytes += 16;
23919 break;
23920
cf2c3c16
TT
23921 case DW_FORM_string:
23922 read_direct_string (abfd, bytes, &bytes_read);
23923 bytes += bytes_read;
23924 break;
23925
23926 case DW_FORM_sec_offset:
23927 case DW_FORM_strp:
36586728 23928 case DW_FORM_GNU_strp_alt:
cf2c3c16
TT
23929 bytes += offset_size;
23930 break;
23931
23932 case DW_FORM_block:
23933 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
23934 bytes += bytes_read;
23935 break;
23936
23937 case DW_FORM_block1:
23938 bytes += 1 + read_1_byte (abfd, bytes);
23939 break;
23940 case DW_FORM_block2:
23941 bytes += 2 + read_2_bytes (abfd, bytes);
23942 break;
23943 case DW_FORM_block4:
23944 bytes += 4 + read_4_bytes (abfd, bytes);
23945 break;
23946
23947 case DW_FORM_sdata:
23948 case DW_FORM_udata:
3019eac3
DE
23949 case DW_FORM_GNU_addr_index:
23950 case DW_FORM_GNU_str_index:
d521ce57 23951 bytes = gdb_skip_leb128 (bytes, buffer_end);
f664829e
DE
23952 if (bytes == NULL)
23953 {
23954 dwarf2_section_buffer_overflow_complaint (section);
23955 return NULL;
23956 }
cf2c3c16
TT
23957 break;
23958
663c44ac
JK
23959 case DW_FORM_implicit_const:
23960 break;
23961
cf2c3c16
TT
23962 default:
23963 {
b98664d3 23964 complaint (_("invalid form 0x%x in `%s'"),
a32a8923 23965 form, get_section_name (section));
cf2c3c16
TT
23966 return NULL;
23967 }
2e276125
JB
23968 }
23969
cf2c3c16
TT
23970 return bytes;
23971}
757a13d0 23972
cf2c3c16
TT
23973/* A helper for dwarf_decode_macros that handles skipping an unknown
23974 opcode. Returns an updated pointer to the macro data buffer; or,
23975 on error, issues a complaint and returns NULL. */
757a13d0 23976
d521ce57 23977static const gdb_byte *
cf2c3c16 23978skip_unknown_opcode (unsigned int opcode,
d521ce57
TT
23979 const gdb_byte **opcode_definitions,
23980 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16
TT
23981 bfd *abfd,
23982 unsigned int offset_size,
23983 struct dwarf2_section_info *section)
23984{
23985 unsigned int bytes_read, i;
23986 unsigned long arg;
d521ce57 23987 const gdb_byte *defn;
2e276125 23988
cf2c3c16 23989 if (opcode_definitions[opcode] == NULL)
2e276125 23990 {
b98664d3 23991 complaint (_("unrecognized DW_MACFINO opcode 0x%x"),
cf2c3c16
TT
23992 opcode);
23993 return NULL;
23994 }
2e276125 23995
cf2c3c16
TT
23996 defn = opcode_definitions[opcode];
23997 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
23998 defn += bytes_read;
2e276125 23999
cf2c3c16
TT
24000 for (i = 0; i < arg; ++i)
24001 {
aead7601
SM
24002 mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end,
24003 (enum dwarf_form) defn[i], offset_size,
f664829e 24004 section);
cf2c3c16
TT
24005 if (mac_ptr == NULL)
24006 {
24007 /* skip_form_bytes already issued the complaint. */
24008 return NULL;
24009 }
24010 }
757a13d0 24011
cf2c3c16
TT
24012 return mac_ptr;
24013}
757a13d0 24014
cf2c3c16
TT
24015/* A helper function which parses the header of a macro section.
24016 If the macro section is the extended (for now called "GNU") type,
24017 then this updates *OFFSET_SIZE. Returns a pointer to just after
24018 the header, or issues a complaint and returns NULL on error. */
757a13d0 24019
d521ce57
TT
24020static const gdb_byte *
24021dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
cf2c3c16 24022 bfd *abfd,
d521ce57 24023 const gdb_byte *mac_ptr,
cf2c3c16
TT
24024 unsigned int *offset_size,
24025 int section_is_gnu)
24026{
24027 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
757a13d0 24028
cf2c3c16
TT
24029 if (section_is_gnu)
24030 {
24031 unsigned int version, flags;
757a13d0 24032
cf2c3c16 24033 version = read_2_bytes (abfd, mac_ptr);
0af92d60 24034 if (version != 4 && version != 5)
cf2c3c16 24035 {
b98664d3 24036 complaint (_("unrecognized version `%d' in .debug_macro section"),
cf2c3c16
TT
24037 version);
24038 return NULL;
24039 }
24040 mac_ptr += 2;
757a13d0 24041
cf2c3c16
TT
24042 flags = read_1_byte (abfd, mac_ptr);
24043 ++mac_ptr;
24044 *offset_size = (flags & 1) ? 8 : 4;
757a13d0 24045
cf2c3c16
TT
24046 if ((flags & 2) != 0)
24047 /* We don't need the line table offset. */
24048 mac_ptr += *offset_size;
757a13d0 24049
cf2c3c16
TT
24050 /* Vendor opcode descriptions. */
24051 if ((flags & 4) != 0)
24052 {
24053 unsigned int i, count;
757a13d0 24054
cf2c3c16
TT
24055 count = read_1_byte (abfd, mac_ptr);
24056 ++mac_ptr;
24057 for (i = 0; i < count; ++i)
24058 {
24059 unsigned int opcode, bytes_read;
24060 unsigned long arg;
24061
24062 opcode = read_1_byte (abfd, mac_ptr);
24063 ++mac_ptr;
24064 opcode_definitions[opcode] = mac_ptr;
24065 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24066 mac_ptr += bytes_read;
24067 mac_ptr += arg;
24068 }
757a13d0 24069 }
cf2c3c16 24070 }
757a13d0 24071
cf2c3c16
TT
24072 return mac_ptr;
24073}
757a13d0 24074
cf2c3c16 24075/* A helper for dwarf_decode_macros that handles the GNU extensions,
0af92d60 24076 including DW_MACRO_import. */
cf2c3c16
TT
24077
24078static void
ed2dc618
SM
24079dwarf_decode_macro_bytes (struct dwarf2_per_objfile *dwarf2_per_objfile,
24080 bfd *abfd,
d521ce57 24081 const gdb_byte *mac_ptr, const gdb_byte *mac_end,
cf2c3c16 24082 struct macro_source_file *current_file,
43f3e411 24083 struct line_header *lh,
cf2c3c16 24084 struct dwarf2_section_info *section,
36586728 24085 int section_is_gnu, int section_is_dwz,
cf2c3c16 24086 unsigned int offset_size,
8fc3fc34 24087 htab_t include_hash)
cf2c3c16 24088{
4d663531 24089 struct objfile *objfile = dwarf2_per_objfile->objfile;
cf2c3c16
TT
24090 enum dwarf_macro_record_type macinfo_type;
24091 int at_commandline;
d521ce57 24092 const gdb_byte *opcode_definitions[256];
757a13d0 24093
cf2c3c16
TT
24094 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24095 &offset_size, section_is_gnu);
24096 if (mac_ptr == NULL)
24097 {
24098 /* We already issued a complaint. */
24099 return;
24100 }
757a13d0
JK
24101
24102 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
24103 GDB is still reading the definitions from command line. First
24104 DW_MACINFO_start_file will need to be ignored as it was already executed
24105 to create CURRENT_FILE for the main source holding also the command line
24106 definitions. On first met DW_MACINFO_start_file this flag is reset to
24107 normally execute all the remaining DW_MACINFO_start_file macinfos. */
24108
24109 at_commandline = 1;
24110
24111 do
24112 {
24113 /* Do we at least have room for a macinfo type byte? */
24114 if (mac_ptr >= mac_end)
24115 {
f664829e 24116 dwarf2_section_buffer_overflow_complaint (section);
757a13d0
JK
24117 break;
24118 }
24119
aead7601 24120 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
757a13d0
JK
24121 mac_ptr++;
24122
cf2c3c16
TT
24123 /* Note that we rely on the fact that the corresponding GNU and
24124 DWARF constants are the same. */
132448f8
SM
24125 DIAGNOSTIC_PUSH
24126 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
757a13d0
JK
24127 switch (macinfo_type)
24128 {
24129 /* A zero macinfo type indicates the end of the macro
24130 information. */
24131 case 0:
24132 break;
2e276125 24133
0af92d60
JK
24134 case DW_MACRO_define:
24135 case DW_MACRO_undef:
24136 case DW_MACRO_define_strp:
24137 case DW_MACRO_undef_strp:
24138 case DW_MACRO_define_sup:
24139 case DW_MACRO_undef_sup:
2e276125 24140 {
891d2f0b 24141 unsigned int bytes_read;
2e276125 24142 int line;
d521ce57 24143 const char *body;
cf2c3c16 24144 int is_define;
2e276125 24145
cf2c3c16
TT
24146 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24147 mac_ptr += bytes_read;
24148
0af92d60
JK
24149 if (macinfo_type == DW_MACRO_define
24150 || macinfo_type == DW_MACRO_undef)
cf2c3c16
TT
24151 {
24152 body = read_direct_string (abfd, mac_ptr, &bytes_read);
24153 mac_ptr += bytes_read;
24154 }
24155 else
24156 {
24157 LONGEST str_offset;
24158
24159 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
24160 mac_ptr += offset_size;
2e276125 24161
0af92d60
JK
24162 if (macinfo_type == DW_MACRO_define_sup
24163 || macinfo_type == DW_MACRO_undef_sup
f7a35f02 24164 || section_is_dwz)
36586728 24165 {
ed2dc618
SM
24166 struct dwz_file *dwz
24167 = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728 24168
ed2dc618
SM
24169 body = read_indirect_string_from_dwz (objfile,
24170 dwz, str_offset);
36586728
TT
24171 }
24172 else
ed2dc618
SM
24173 body = read_indirect_string_at_offset (dwarf2_per_objfile,
24174 abfd, str_offset);
cf2c3c16
TT
24175 }
24176
0af92d60
JK
24177 is_define = (macinfo_type == DW_MACRO_define
24178 || macinfo_type == DW_MACRO_define_strp
24179 || macinfo_type == DW_MACRO_define_sup);
2e276125 24180 if (! current_file)
757a13d0
JK
24181 {
24182 /* DWARF violation as no main source is present. */
b98664d3 24183 complaint (_("debug info with no main source gives macro %s "
757a13d0 24184 "on line %d: %s"),
cf2c3c16
TT
24185 is_define ? _("definition") : _("undefinition"),
24186 line, body);
757a13d0
JK
24187 break;
24188 }
3e43a32a
MS
24189 if ((line == 0 && !at_commandline)
24190 || (line != 0 && at_commandline))
b98664d3 24191 complaint (_("debug info gives %s macro %s with %s line %d: %s"),
757a13d0 24192 at_commandline ? _("command-line") : _("in-file"),
cf2c3c16 24193 is_define ? _("definition") : _("undefinition"),
757a13d0
JK
24194 line == 0 ? _("zero") : _("non-zero"), line, body);
24195
cf2c3c16 24196 if (is_define)
757a13d0 24197 parse_macro_definition (current_file, line, body);
cf2c3c16
TT
24198 else
24199 {
0af92d60
JK
24200 gdb_assert (macinfo_type == DW_MACRO_undef
24201 || macinfo_type == DW_MACRO_undef_strp
24202 || macinfo_type == DW_MACRO_undef_sup);
cf2c3c16
TT
24203 macro_undef (current_file, line, body);
24204 }
2e276125
JB
24205 }
24206 break;
24207
0af92d60 24208 case DW_MACRO_start_file:
2e276125 24209 {
891d2f0b 24210 unsigned int bytes_read;
2e276125
JB
24211 int line, file;
24212
24213 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24214 mac_ptr += bytes_read;
24215 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24216 mac_ptr += bytes_read;
24217
3e43a32a
MS
24218 if ((line == 0 && !at_commandline)
24219 || (line != 0 && at_commandline))
b98664d3 24220 complaint (_("debug info gives source %d included "
757a13d0
JK
24221 "from %s at %s line %d"),
24222 file, at_commandline ? _("command-line") : _("file"),
24223 line == 0 ? _("zero") : _("non-zero"), line);
24224
24225 if (at_commandline)
24226 {
0af92d60 24227 /* This DW_MACRO_start_file was executed in the
cf2c3c16 24228 pass one. */
757a13d0
JK
24229 at_commandline = 0;
24230 }
24231 else
43f3e411 24232 current_file = macro_start_file (file, line, current_file, lh);
2e276125
JB
24233 }
24234 break;
24235
0af92d60 24236 case DW_MACRO_end_file:
2e276125 24237 if (! current_file)
b98664d3 24238 complaint (_("macro debug info has an unmatched "
3e43a32a 24239 "`close_file' directive"));
2e276125
JB
24240 else
24241 {
24242 current_file = current_file->included_by;
24243 if (! current_file)
24244 {
cf2c3c16 24245 enum dwarf_macro_record_type next_type;
2e276125
JB
24246
24247 /* GCC circa March 2002 doesn't produce the zero
24248 type byte marking the end of the compilation
24249 unit. Complain if it's not there, but exit no
24250 matter what. */
24251
24252 /* Do we at least have room for a macinfo type byte? */
24253 if (mac_ptr >= mac_end)
24254 {
f664829e 24255 dwarf2_section_buffer_overflow_complaint (section);
2e276125
JB
24256 return;
24257 }
24258
24259 /* We don't increment mac_ptr here, so this is just
24260 a look-ahead. */
aead7601
SM
24261 next_type
24262 = (enum dwarf_macro_record_type) read_1_byte (abfd,
24263 mac_ptr);
2e276125 24264 if (next_type != 0)
b98664d3 24265 complaint (_("no terminating 0-type entry for "
3e43a32a 24266 "macros in `.debug_macinfo' section"));
2e276125
JB
24267
24268 return;
24269 }
24270 }
24271 break;
24272
0af92d60
JK
24273 case DW_MACRO_import:
24274 case DW_MACRO_import_sup:
cf2c3c16
TT
24275 {
24276 LONGEST offset;
8fc3fc34 24277 void **slot;
a036ba48
TT
24278 bfd *include_bfd = abfd;
24279 struct dwarf2_section_info *include_section = section;
d521ce57 24280 const gdb_byte *include_mac_end = mac_end;
a036ba48 24281 int is_dwz = section_is_dwz;
d521ce57 24282 const gdb_byte *new_mac_ptr;
cf2c3c16
TT
24283
24284 offset = read_offset_1 (abfd, mac_ptr, offset_size);
24285 mac_ptr += offset_size;
24286
0af92d60 24287 if (macinfo_type == DW_MACRO_import_sup)
a036ba48 24288 {
ed2dc618 24289 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
a036ba48 24290
4d663531 24291 dwarf2_read_section (objfile, &dwz->macro);
a036ba48 24292
a036ba48 24293 include_section = &dwz->macro;
a32a8923 24294 include_bfd = get_section_bfd_owner (include_section);
a036ba48
TT
24295 include_mac_end = dwz->macro.buffer + dwz->macro.size;
24296 is_dwz = 1;
24297 }
24298
24299 new_mac_ptr = include_section->buffer + offset;
24300 slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
24301
8fc3fc34
TT
24302 if (*slot != NULL)
24303 {
24304 /* This has actually happened; see
24305 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
b98664d3 24306 complaint (_("recursive DW_MACRO_import in "
8fc3fc34
TT
24307 ".debug_macro section"));
24308 }
24309 else
24310 {
d521ce57 24311 *slot = (void *) new_mac_ptr;
36586728 24312
ed2dc618
SM
24313 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24314 include_bfd, new_mac_ptr,
43f3e411 24315 include_mac_end, current_file, lh,
36586728 24316 section, section_is_gnu, is_dwz,
4d663531 24317 offset_size, include_hash);
8fc3fc34 24318
d521ce57 24319 htab_remove_elt (include_hash, (void *) new_mac_ptr);
8fc3fc34 24320 }
cf2c3c16
TT
24321 }
24322 break;
24323
2e276125 24324 case DW_MACINFO_vendor_ext:
cf2c3c16
TT
24325 if (!section_is_gnu)
24326 {
24327 unsigned int bytes_read;
2e276125 24328
ac298888
TT
24329 /* This reads the constant, but since we don't recognize
24330 any vendor extensions, we ignore it. */
24331 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
cf2c3c16
TT
24332 mac_ptr += bytes_read;
24333 read_direct_string (abfd, mac_ptr, &bytes_read);
24334 mac_ptr += bytes_read;
2e276125 24335
cf2c3c16
TT
24336 /* We don't recognize any vendor extensions. */
24337 break;
24338 }
24339 /* FALLTHROUGH */
24340
24341 default:
24342 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 24343 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
24344 section);
24345 if (mac_ptr == NULL)
24346 return;
24347 break;
2e276125 24348 }
132448f8 24349 DIAGNOSTIC_POP
757a13d0 24350 } while (macinfo_type != 0);
2e276125 24351}
8e19ed76 24352
cf2c3c16 24353static void
09262596 24354dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
43f3e411 24355 int section_is_gnu)
cf2c3c16 24356{
518817b3
SM
24357 struct dwarf2_per_objfile *dwarf2_per_objfile
24358 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 24359 struct objfile *objfile = dwarf2_per_objfile->objfile;
09262596
DE
24360 struct line_header *lh = cu->line_header;
24361 bfd *abfd;
d521ce57 24362 const gdb_byte *mac_ptr, *mac_end;
cf2c3c16
TT
24363 struct macro_source_file *current_file = 0;
24364 enum dwarf_macro_record_type macinfo_type;
24365 unsigned int offset_size = cu->header.offset_size;
d521ce57 24366 const gdb_byte *opcode_definitions[256];
8fc3fc34 24367 void **slot;
09262596
DE
24368 struct dwarf2_section_info *section;
24369 const char *section_name;
24370
24371 if (cu->dwo_unit != NULL)
24372 {
24373 if (section_is_gnu)
24374 {
24375 section = &cu->dwo_unit->dwo_file->sections.macro;
24376 section_name = ".debug_macro.dwo";
24377 }
24378 else
24379 {
24380 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24381 section_name = ".debug_macinfo.dwo";
24382 }
24383 }
24384 else
24385 {
24386 if (section_is_gnu)
24387 {
24388 section = &dwarf2_per_objfile->macro;
24389 section_name = ".debug_macro";
24390 }
24391 else
24392 {
24393 section = &dwarf2_per_objfile->macinfo;
24394 section_name = ".debug_macinfo";
24395 }
24396 }
cf2c3c16 24397
bb5ed363 24398 dwarf2_read_section (objfile, section);
cf2c3c16
TT
24399 if (section->buffer == NULL)
24400 {
b98664d3 24401 complaint (_("missing %s section"), section_name);
cf2c3c16
TT
24402 return;
24403 }
a32a8923 24404 abfd = get_section_bfd_owner (section);
cf2c3c16
TT
24405
24406 /* First pass: Find the name of the base filename.
24407 This filename is needed in order to process all macros whose definition
24408 (or undefinition) comes from the command line. These macros are defined
24409 before the first DW_MACINFO_start_file entry, and yet still need to be
24410 associated to the base file.
24411
24412 To determine the base file name, we scan the macro definitions until we
24413 reach the first DW_MACINFO_start_file entry. We then initialize
24414 CURRENT_FILE accordingly so that any macro definition found before the
24415 first DW_MACINFO_start_file can still be associated to the base file. */
24416
24417 mac_ptr = section->buffer + offset;
24418 mac_end = section->buffer + section->size;
24419
24420 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
24421 &offset_size, section_is_gnu);
24422 if (mac_ptr == NULL)
24423 {
24424 /* We already issued a complaint. */
24425 return;
24426 }
24427
24428 do
24429 {
24430 /* Do we at least have room for a macinfo type byte? */
24431 if (mac_ptr >= mac_end)
24432 {
24433 /* Complaint is printed during the second pass as GDB will probably
24434 stop the first pass earlier upon finding
24435 DW_MACINFO_start_file. */
24436 break;
24437 }
24438
aead7601 24439 macinfo_type = (enum dwarf_macro_record_type) read_1_byte (abfd, mac_ptr);
cf2c3c16
TT
24440 mac_ptr++;
24441
24442 /* Note that we rely on the fact that the corresponding GNU and
24443 DWARF constants are the same. */
132448f8
SM
24444 DIAGNOSTIC_PUSH
24445 DIAGNOSTIC_IGNORE_SWITCH_DIFFERENT_ENUM_TYPES
cf2c3c16
TT
24446 switch (macinfo_type)
24447 {
24448 /* A zero macinfo type indicates the end of the macro
24449 information. */
24450 case 0:
24451 break;
24452
0af92d60
JK
24453 case DW_MACRO_define:
24454 case DW_MACRO_undef:
cf2c3c16
TT
24455 /* Only skip the data by MAC_PTR. */
24456 {
24457 unsigned int bytes_read;
24458
24459 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24460 mac_ptr += bytes_read;
24461 read_direct_string (abfd, mac_ptr, &bytes_read);
24462 mac_ptr += bytes_read;
24463 }
24464 break;
24465
0af92d60 24466 case DW_MACRO_start_file:
cf2c3c16
TT
24467 {
24468 unsigned int bytes_read;
24469 int line, file;
24470
24471 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24472 mac_ptr += bytes_read;
24473 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24474 mac_ptr += bytes_read;
24475
43f3e411 24476 current_file = macro_start_file (file, line, current_file, lh);
cf2c3c16
TT
24477 }
24478 break;
24479
0af92d60 24480 case DW_MACRO_end_file:
cf2c3c16
TT
24481 /* No data to skip by MAC_PTR. */
24482 break;
24483
0af92d60
JK
24484 case DW_MACRO_define_strp:
24485 case DW_MACRO_undef_strp:
24486 case DW_MACRO_define_sup:
24487 case DW_MACRO_undef_sup:
cf2c3c16
TT
24488 {
24489 unsigned int bytes_read;
24490
24491 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24492 mac_ptr += bytes_read;
24493 mac_ptr += offset_size;
24494 }
24495 break;
24496
0af92d60
JK
24497 case DW_MACRO_import:
24498 case DW_MACRO_import_sup:
cf2c3c16 24499 /* Note that, according to the spec, a transparent include
0af92d60 24500 chain cannot call DW_MACRO_start_file. So, we can just
cf2c3c16
TT
24501 skip this opcode. */
24502 mac_ptr += offset_size;
24503 break;
24504
24505 case DW_MACINFO_vendor_ext:
24506 /* Only skip the data by MAC_PTR. */
24507 if (!section_is_gnu)
24508 {
24509 unsigned int bytes_read;
24510
24511 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
24512 mac_ptr += bytes_read;
24513 read_direct_string (abfd, mac_ptr, &bytes_read);
24514 mac_ptr += bytes_read;
24515 }
24516 /* FALLTHROUGH */
24517
24518 default:
24519 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
f664829e 24520 mac_ptr, mac_end, abfd, offset_size,
cf2c3c16
TT
24521 section);
24522 if (mac_ptr == NULL)
24523 return;
24524 break;
24525 }
132448f8 24526 DIAGNOSTIC_POP
cf2c3c16
TT
24527 } while (macinfo_type != 0 && current_file == NULL);
24528
24529 /* Second pass: Process all entries.
24530
24531 Use the AT_COMMAND_LINE flag to determine whether we are still processing
24532 command-line macro definitions/undefinitions. This flag is unset when we
24533 reach the first DW_MACINFO_start_file entry. */
24534
fc4007c9
TT
24535 htab_up include_hash (htab_create_alloc (1, htab_hash_pointer,
24536 htab_eq_pointer,
24537 NULL, xcalloc, xfree));
8fc3fc34 24538 mac_ptr = section->buffer + offset;
fc4007c9 24539 slot = htab_find_slot (include_hash.get (), mac_ptr, INSERT);
d521ce57 24540 *slot = (void *) mac_ptr;
ed2dc618
SM
24541 dwarf_decode_macro_bytes (dwarf2_per_objfile,
24542 abfd, mac_ptr, mac_end,
43f3e411 24543 current_file, lh, section,
fc4007c9
TT
24544 section_is_gnu, 0, offset_size,
24545 include_hash.get ());
cf2c3c16
TT
24546}
24547
8e19ed76 24548/* Check if the attribute's form is a DW_FORM_block*
0963b4bd 24549 if so return true else false. */
380bca97 24550
8e19ed76 24551static int
6e5a29e1 24552attr_form_is_block (const struct attribute *attr)
8e19ed76
PS
24553{
24554 return (attr == NULL ? 0 :
24555 attr->form == DW_FORM_block1
24556 || attr->form == DW_FORM_block2
24557 || attr->form == DW_FORM_block4
2dc7f7b3
TT
24558 || attr->form == DW_FORM_block
24559 || attr->form == DW_FORM_exprloc);
8e19ed76 24560}
4c2df51b 24561
c6a0999f
JB
24562/* Return non-zero if ATTR's value is a section offset --- classes
24563 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
24564 You may use DW_UNSND (attr) to retrieve such offsets.
24565
24566 Section 7.5.4, "Attribute Encodings", explains that no attribute
24567 may have a value that belongs to more than one of these classes; it
24568 would be ambiguous if we did, because we use the same forms for all
24569 of them. */
380bca97 24570
3690dd37 24571static int
6e5a29e1 24572attr_form_is_section_offset (const struct attribute *attr)
3690dd37
JB
24573{
24574 return (attr->form == DW_FORM_data4
2dc7f7b3
TT
24575 || attr->form == DW_FORM_data8
24576 || attr->form == DW_FORM_sec_offset);
3690dd37
JB
24577}
24578
3690dd37
JB
24579/* Return non-zero if ATTR's value falls in the 'constant' class, or
24580 zero otherwise. When this function returns true, you can apply
24581 dwarf2_get_attr_constant_value to it.
24582
24583 However, note that for some attributes you must check
24584 attr_form_is_section_offset before using this test. DW_FORM_data4
24585 and DW_FORM_data8 are members of both the constant class, and of
24586 the classes that contain offsets into other debug sections
24587 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
24588 that, if an attribute's can be either a constant or one of the
24589 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
0224619f
JK
24590 taken as section offsets, not constants.
24591
24592 DW_FORM_data16 is not considered as dwarf2_get_attr_constant_value
24593 cannot handle that. */
380bca97 24594
3690dd37 24595static int
6e5a29e1 24596attr_form_is_constant (const struct attribute *attr)
3690dd37
JB
24597{
24598 switch (attr->form)
24599 {
24600 case DW_FORM_sdata:
24601 case DW_FORM_udata:
24602 case DW_FORM_data1:
24603 case DW_FORM_data2:
24604 case DW_FORM_data4:
24605 case DW_FORM_data8:
663c44ac 24606 case DW_FORM_implicit_const:
3690dd37
JB
24607 return 1;
24608 default:
24609 return 0;
24610 }
24611}
24612
7771576e
SA
24613
24614/* DW_ADDR is always stored already as sect_offset; despite for the forms
24615 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
24616
24617static int
6e5a29e1 24618attr_form_is_ref (const struct attribute *attr)
7771576e
SA
24619{
24620 switch (attr->form)
24621 {
24622 case DW_FORM_ref_addr:
24623 case DW_FORM_ref1:
24624 case DW_FORM_ref2:
24625 case DW_FORM_ref4:
24626 case DW_FORM_ref8:
24627 case DW_FORM_ref_udata:
24628 case DW_FORM_GNU_ref_alt:
24629 return 1;
24630 default:
24631 return 0;
24632 }
24633}
24634
3019eac3
DE
24635/* Return the .debug_loc section to use for CU.
24636 For DWO files use .debug_loc.dwo. */
24637
24638static struct dwarf2_section_info *
24639cu_debug_loc_section (struct dwarf2_cu *cu)
24640{
518817b3
SM
24641 struct dwarf2_per_objfile *dwarf2_per_objfile
24642 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 24643
3019eac3 24644 if (cu->dwo_unit)
43988095
JK
24645 {
24646 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24647
24648 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24649 }
24650 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
24651 : &dwarf2_per_objfile->loc);
3019eac3
DE
24652}
24653
8cf6f0b1
TT
24654/* A helper function that fills in a dwarf2_loclist_baton. */
24655
24656static void
24657fill_in_loclist_baton (struct dwarf2_cu *cu,
24658 struct dwarf2_loclist_baton *baton,
ff39bb5e 24659 const struct attribute *attr)
8cf6f0b1 24660{
518817b3
SM
24661 struct dwarf2_per_objfile *dwarf2_per_objfile
24662 = cu->per_cu->dwarf2_per_objfile;
3019eac3
DE
24663 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24664
24665 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
8cf6f0b1
TT
24666
24667 baton->per_cu = cu->per_cu;
24668 gdb_assert (baton->per_cu);
24669 /* We don't know how long the location list is, but make sure we
24670 don't run off the edge of the section. */
3019eac3
DE
24671 baton->size = section->size - DW_UNSND (attr);
24672 baton->data = section->buffer + DW_UNSND (attr);
8cf6f0b1 24673 baton->base_address = cu->base_address;
f664829e 24674 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
24675}
24676
4c2df51b 24677static void
ff39bb5e 24678dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 24679 struct dwarf2_cu *cu, int is_block)
4c2df51b 24680{
518817b3
SM
24681 struct dwarf2_per_objfile *dwarf2_per_objfile
24682 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 24683 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 24684 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 24685
3690dd37 24686 if (attr_form_is_section_offset (attr)
3019eac3 24687 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
24688 the section. If so, fall through to the complaint in the
24689 other branch. */
3019eac3 24690 && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
4c2df51b 24691 {
0d53c4c4 24692 struct dwarf2_loclist_baton *baton;
4c2df51b 24693
8d749320 24694 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 24695
8cf6f0b1 24696 fill_in_loclist_baton (cu, baton, attr);
be391dca 24697
d00adf39 24698 if (cu->base_known == 0)
b98664d3 24699 complaint (_("Location list used without "
3e43a32a 24700 "specifying the CU base address."));
4c2df51b 24701
f1e6e072
TT
24702 SYMBOL_ACLASS_INDEX (sym) = (is_block
24703 ? dwarf2_loclist_block_index
24704 : dwarf2_loclist_index);
0d53c4c4
DJ
24705 SYMBOL_LOCATION_BATON (sym) = baton;
24706 }
24707 else
24708 {
24709 struct dwarf2_locexpr_baton *baton;
24710
8d749320 24711 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
24712 baton->per_cu = cu->per_cu;
24713 gdb_assert (baton->per_cu);
0d53c4c4
DJ
24714
24715 if (attr_form_is_block (attr))
24716 {
24717 /* Note that we're just copying the block's data pointer
24718 here, not the actual data. We're still pointing into the
6502dd73
DJ
24719 info_buffer for SYM's objfile; right now we never release
24720 that buffer, but when we do clean up properly this may
24721 need to change. */
0d53c4c4
DJ
24722 baton->size = DW_BLOCK (attr)->size;
24723 baton->data = DW_BLOCK (attr)->data;
24724 }
24725 else
24726 {
24727 dwarf2_invalid_attrib_class_complaint ("location description",
24728 SYMBOL_NATURAL_NAME (sym));
24729 baton->size = 0;
0d53c4c4 24730 }
6e70227d 24731
f1e6e072
TT
24732 SYMBOL_ACLASS_INDEX (sym) = (is_block
24733 ? dwarf2_locexpr_block_index
24734 : dwarf2_locexpr_index);
0d53c4c4
DJ
24735 SYMBOL_LOCATION_BATON (sym) = baton;
24736 }
4c2df51b 24737}
6502dd73 24738
9aa1f1e3
TT
24739/* Return the OBJFILE associated with the compilation unit CU. If CU
24740 came from a separate debuginfo file, then the master objfile is
24741 returned. */
ae0d2f24
UW
24742
24743struct objfile *
24744dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
24745{
e3b94546 24746 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
ae0d2f24
UW
24747
24748 /* Return the master objfile, so that we can report and look up the
24749 correct file containing this variable. */
24750 if (objfile->separate_debug_objfile_backlink)
24751 objfile = objfile->separate_debug_objfile_backlink;
24752
24753 return objfile;
24754}
24755
96408a79
SA
24756/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
24757 (CU_HEADERP is unused in such case) or prepare a temporary copy at
24758 CU_HEADERP first. */
24759
24760static const struct comp_unit_head *
24761per_cu_header_read_in (struct comp_unit_head *cu_headerp,
24762 struct dwarf2_per_cu_data *per_cu)
24763{
d521ce57 24764 const gdb_byte *info_ptr;
96408a79
SA
24765
24766 if (per_cu->cu)
24767 return &per_cu->cu->header;
24768
9c541725 24769 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
24770
24771 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
24772 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
24773 rcuh_kind::COMPILE);
96408a79
SA
24774
24775 return cu_headerp;
24776}
24777
ae0d2f24
UW
24778/* Return the address size given in the compilation unit header for CU. */
24779
98714339 24780int
ae0d2f24
UW
24781dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
24782{
96408a79
SA
24783 struct comp_unit_head cu_header_local;
24784 const struct comp_unit_head *cu_headerp;
c471e790 24785
96408a79
SA
24786 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24787
24788 return cu_headerp->addr_size;
ae0d2f24
UW
24789}
24790
9eae7c52
TT
24791/* Return the offset size given in the compilation unit header for CU. */
24792
24793int
24794dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
24795{
96408a79
SA
24796 struct comp_unit_head cu_header_local;
24797 const struct comp_unit_head *cu_headerp;
9c6c53f7 24798
96408a79
SA
24799 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24800
24801 return cu_headerp->offset_size;
24802}
24803
24804/* See its dwarf2loc.h declaration. */
24805
24806int
24807dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
24808{
24809 struct comp_unit_head cu_header_local;
24810 const struct comp_unit_head *cu_headerp;
24811
24812 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
24813
24814 if (cu_headerp->version == 2)
24815 return cu_headerp->addr_size;
24816 else
24817 return cu_headerp->offset_size;
181cebd4
JK
24818}
24819
9aa1f1e3
TT
24820/* Return the text offset of the CU. The returned offset comes from
24821 this CU's objfile. If this objfile came from a separate debuginfo
24822 file, then the offset may be different from the corresponding
24823 offset in the parent objfile. */
24824
24825CORE_ADDR
24826dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
24827{
e3b94546 24828 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
9aa1f1e3
TT
24829
24830 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
24831}
24832
43988095
JK
24833/* Return DWARF version number of PER_CU. */
24834
24835short
24836dwarf2_version (struct dwarf2_per_cu_data *per_cu)
24837{
24838 return per_cu->dwarf_version;
24839}
24840
348e048f
DE
24841/* Locate the .debug_info compilation unit from CU's objfile which contains
24842 the DIE at OFFSET. Raises an error on failure. */
ae038cb0
DJ
24843
24844static struct dwarf2_per_cu_data *
9c541725 24845dwarf2_find_containing_comp_unit (sect_offset sect_off,
36586728 24846 unsigned int offset_in_dwz,
ed2dc618 24847 struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
24848{
24849 struct dwarf2_per_cu_data *this_cu;
24850 int low, high;
36586728 24851 const sect_offset *cu_off;
ae038cb0 24852
ae038cb0 24853 low = 0;
b76e467d 24854 high = dwarf2_per_objfile->all_comp_units.size () - 1;
ae038cb0
DJ
24855 while (high > low)
24856 {
36586728 24857 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 24858 int mid = low + (high - low) / 2;
9a619af0 24859
36586728 24860 mid_cu = dwarf2_per_objfile->all_comp_units[mid];
9c541725 24861 cu_off = &mid_cu->sect_off;
36586728 24862 if (mid_cu->is_dwz > offset_in_dwz
9c541725 24863 || (mid_cu->is_dwz == offset_in_dwz && *cu_off >= sect_off))
ae038cb0
DJ
24864 high = mid;
24865 else
24866 low = mid + 1;
24867 }
24868 gdb_assert (low == high);
36586728 24869 this_cu = dwarf2_per_objfile->all_comp_units[low];
9c541725
PA
24870 cu_off = &this_cu->sect_off;
24871 if (this_cu->is_dwz != offset_in_dwz || *cu_off > sect_off)
ae038cb0 24872 {
36586728 24873 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 24874 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
24875 "offset %s [in module %s]"),
24876 sect_offset_str (sect_off),
ed2dc618 24877 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 24878
9c541725
PA
24879 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
24880 <= sect_off);
ae038cb0
DJ
24881 return dwarf2_per_objfile->all_comp_units[low-1];
24882 }
24883 else
24884 {
24885 this_cu = dwarf2_per_objfile->all_comp_units[low];
b76e467d 24886 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
9c541725 24887 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 24888 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 24889 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
24890 return this_cu;
24891 }
24892}
24893
23745b47 24894/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 24895
fcd3b13d
SM
24896dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
24897 : per_cu (per_cu_),
24898 mark (0),
24899 has_loclist (0),
24900 checked_producer (0),
24901 producer_is_gxx_lt_4_6 (0),
24902 producer_is_gcc_lt_4_3 (0),
24903 producer_is_icc_lt_14 (0),
24904 processing_has_namespace_info (0)
93311388 24905{
fcd3b13d
SM
24906 per_cu->cu = this;
24907}
24908
24909/* Destroy a dwarf2_cu. */
24910
24911dwarf2_cu::~dwarf2_cu ()
24912{
24913 per_cu->cu = NULL;
9816fde3
JK
24914}
24915
24916/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24917
24918static void
95554aad
TT
24919prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24920 enum language pretend_language)
9816fde3
JK
24921{
24922 struct attribute *attr;
24923
24924 /* Set the language we're debugging. */
24925 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
24926 if (attr)
24927 set_cu_language (DW_UNSND (attr), cu);
24928 else
9cded63f 24929 {
95554aad 24930 cu->language = pretend_language;
9cded63f
TT
24931 cu->language_defn = language_def (cu->language);
24932 }
dee91e82 24933
7d45c7c3 24934 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
24935}
24936
ae038cb0
DJ
24937/* Increase the age counter on each cached compilation unit, and free
24938 any that are too old. */
24939
24940static void
ed2dc618 24941age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
24942{
24943 struct dwarf2_per_cu_data *per_cu, **last_chain;
24944
24945 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
24946 per_cu = dwarf2_per_objfile->read_in_chain;
24947 while (per_cu != NULL)
24948 {
24949 per_cu->cu->last_used ++;
b4f54984 24950 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
24951 dwarf2_mark (per_cu->cu);
24952 per_cu = per_cu->cu->read_in_chain;
24953 }
24954
24955 per_cu = dwarf2_per_objfile->read_in_chain;
24956 last_chain = &dwarf2_per_objfile->read_in_chain;
24957 while (per_cu != NULL)
24958 {
24959 struct dwarf2_per_cu_data *next_cu;
24960
24961 next_cu = per_cu->cu->read_in_chain;
24962
24963 if (!per_cu->cu->mark)
24964 {
fcd3b13d 24965 delete per_cu->cu;
ae038cb0
DJ
24966 *last_chain = next_cu;
24967 }
24968 else
24969 last_chain = &per_cu->cu->read_in_chain;
24970
24971 per_cu = next_cu;
24972 }
24973}
24974
24975/* Remove a single compilation unit from the cache. */
24976
24977static void
dee91e82 24978free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
24979{
24980 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
24981 struct dwarf2_per_objfile *dwarf2_per_objfile
24982 = target_per_cu->dwarf2_per_objfile;
ae038cb0
DJ
24983
24984 per_cu = dwarf2_per_objfile->read_in_chain;
24985 last_chain = &dwarf2_per_objfile->read_in_chain;
24986 while (per_cu != NULL)
24987 {
24988 struct dwarf2_per_cu_data *next_cu;
24989
24990 next_cu = per_cu->cu->read_in_chain;
24991
dee91e82 24992 if (per_cu == target_per_cu)
ae038cb0 24993 {
fcd3b13d 24994 delete per_cu->cu;
dee91e82 24995 per_cu->cu = NULL;
ae038cb0
DJ
24996 *last_chain = next_cu;
24997 break;
24998 }
24999 else
25000 last_chain = &per_cu->cu->read_in_chain;
25001
25002 per_cu = next_cu;
25003 }
25004}
25005
fe3e1990
DJ
25006/* Release all extra memory associated with OBJFILE. */
25007
25008void
25009dwarf2_free_objfile (struct objfile *objfile)
25010{
ed2dc618
SM
25011 struct dwarf2_per_objfile *dwarf2_per_objfile
25012 = get_dwarf2_per_objfile (objfile);
fe3e1990 25013
fd90ace4 25014 delete dwarf2_per_objfile;
fe3e1990
DJ
25015}
25016
dee91e82
DE
25017/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25018 We store these in a hash table separate from the DIEs, and preserve them
25019 when the DIEs are flushed out of cache.
25020
25021 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 25022 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
25023 or the type may come from a DWO file. Furthermore, while it's more logical
25024 to use per_cu->section+offset, with Fission the section with the data is in
25025 the DWO file but we don't know that section at the point we need it.
25026 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25027 because we can enter the lookup routine, get_die_type_at_offset, from
25028 outside this file, and thus won't necessarily have PER_CU->cu.
25029 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 25030
dee91e82 25031struct dwarf2_per_cu_offset_and_type
1c379e20 25032{
dee91e82 25033 const struct dwarf2_per_cu_data *per_cu;
9c541725 25034 sect_offset sect_off;
1c379e20
DJ
25035 struct type *type;
25036};
25037
dee91e82 25038/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25039
25040static hashval_t
dee91e82 25041per_cu_offset_and_type_hash (const void *item)
1c379e20 25042{
9a3c8263
SM
25043 const struct dwarf2_per_cu_offset_and_type *ofs
25044 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 25045
9c541725 25046 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
25047}
25048
dee91e82 25049/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25050
25051static int
dee91e82 25052per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 25053{
9a3c8263
SM
25054 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25055 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25056 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25057 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 25058
dee91e82 25059 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 25060 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
25061}
25062
25063/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
25064 table if necessary. For convenience, return TYPE.
25065
25066 The DIEs reading must have careful ordering to:
25067 * Not cause infite loops trying to read in DIEs as a prerequisite for
25068 reading current DIE.
25069 * Not trying to dereference contents of still incompletely read in types
25070 while reading in other DIEs.
25071 * Enable referencing still incompletely read in types just by a pointer to
25072 the type without accessing its fields.
25073
25074 Therefore caller should follow these rules:
25075 * Try to fetch any prerequisite types we may need to build this DIE type
25076 before building the type and calling set_die_type.
e71ec853 25077 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
25078 possible before fetching more types to complete the current type.
25079 * Make the type as complete as possible before fetching more types. */
1c379e20 25080
f792889a 25081static struct type *
1c379e20
DJ
25082set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
25083{
518817b3
SM
25084 struct dwarf2_per_objfile *dwarf2_per_objfile
25085 = cu->per_cu->dwarf2_per_objfile;
dee91e82 25086 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 25087 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
25088 struct attribute *attr;
25089 struct dynamic_prop prop;
1c379e20 25090
b4ba55a1
JB
25091 /* For Ada types, make sure that the gnat-specific data is always
25092 initialized (if not already set). There are a few types where
25093 we should not be doing so, because the type-specific area is
25094 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25095 where the type-specific area is used to store the floatformat).
25096 But this is not a problem, because the gnat-specific information
25097 is actually not needed for these types. */
25098 if (need_gnat_info (cu)
25099 && TYPE_CODE (type) != TYPE_CODE_FUNC
25100 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
25101 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
25102 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
25103 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
25104 && !HAVE_GNAT_AUX_INFO (type))
25105 INIT_GNAT_SPECIFIC (type);
25106
3f2f83dd
KB
25107 /* Read DW_AT_allocated and set in type. */
25108 attr = dwarf2_attr (die, DW_AT_allocated, cu);
25109 if (attr_form_is_block (attr))
25110 {
25111 if (attr_to_dynamic_prop (attr, die, cu, &prop))
50a82047 25112 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
3f2f83dd
KB
25113 }
25114 else if (attr != NULL)
25115 {
b98664d3 25116 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 25117 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25118 sect_offset_str (die->sect_off));
3f2f83dd
KB
25119 }
25120
25121 /* Read DW_AT_associated and set in type. */
25122 attr = dwarf2_attr (die, DW_AT_associated, cu);
25123 if (attr_form_is_block (attr))
25124 {
25125 if (attr_to_dynamic_prop (attr, die, cu, &prop))
50a82047 25126 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
3f2f83dd
KB
25127 }
25128 else if (attr != NULL)
25129 {
b98664d3 25130 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 25131 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 25132 sect_offset_str (die->sect_off));
3f2f83dd
KB
25133 }
25134
3cdcd0ce
JB
25135 /* Read DW_AT_data_location and set in type. */
25136 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25137 if (attr_to_dynamic_prop (attr, die, cu, &prop))
50a82047 25138 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
3cdcd0ce 25139
dee91e82 25140 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25141 {
dee91e82
DE
25142 dwarf2_per_objfile->die_type_hash =
25143 htab_create_alloc_ex (127,
25144 per_cu_offset_and_type_hash,
25145 per_cu_offset_and_type_eq,
25146 NULL,
25147 &objfile->objfile_obstack,
25148 hashtab_obstack_allocate,
25149 dummy_obstack_deallocate);
f792889a 25150 }
1c379e20 25151
dee91e82 25152 ofs.per_cu = cu->per_cu;
9c541725 25153 ofs.sect_off = die->sect_off;
1c379e20 25154 ofs.type = type;
dee91e82
DE
25155 slot = (struct dwarf2_per_cu_offset_and_type **)
25156 htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
7e314c57 25157 if (*slot)
b98664d3 25158 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 25159 sect_offset_str (die->sect_off));
8d749320
SM
25160 *slot = XOBNEW (&objfile->objfile_obstack,
25161 struct dwarf2_per_cu_offset_and_type);
1c379e20 25162 **slot = ofs;
f792889a 25163 return type;
1c379e20
DJ
25164}
25165
9c541725 25166/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 25167 or return NULL if the die does not have a saved type. */
1c379e20
DJ
25168
25169static struct type *
9c541725 25170get_die_type_at_offset (sect_offset sect_off,
673bfd45 25171 struct dwarf2_per_cu_data *per_cu)
1c379e20 25172{
dee91e82 25173 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 25174 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 25175
dee91e82 25176 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 25177 return NULL;
1c379e20 25178
dee91e82 25179 ofs.per_cu = per_cu;
9c541725 25180 ofs.sect_off = sect_off;
9a3c8263
SM
25181 slot = ((struct dwarf2_per_cu_offset_and_type *)
25182 htab_find (dwarf2_per_objfile->die_type_hash, &ofs));
1c379e20
DJ
25183 if (slot)
25184 return slot->type;
25185 else
25186 return NULL;
25187}
25188
02142a6c 25189/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
25190 or return NULL if DIE does not have a saved type. */
25191
25192static struct type *
25193get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25194{
9c541725 25195 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
25196}
25197
10b3939b
DJ
25198/* Add a dependence relationship from CU to REF_PER_CU. */
25199
25200static void
25201dwarf2_add_dependence (struct dwarf2_cu *cu,
25202 struct dwarf2_per_cu_data *ref_per_cu)
25203{
25204 void **slot;
25205
25206 if (cu->dependencies == NULL)
25207 cu->dependencies
25208 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25209 NULL, &cu->comp_unit_obstack,
25210 hashtab_obstack_allocate,
25211 dummy_obstack_deallocate);
25212
25213 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25214 if (*slot == NULL)
25215 *slot = ref_per_cu;
25216}
1c379e20 25217
f504f079
DE
25218/* Subroutine of dwarf2_mark to pass to htab_traverse.
25219 Set the mark field in every compilation unit in the
ae038cb0
DJ
25220 cache that we must keep because we are keeping CU. */
25221
10b3939b
DJ
25222static int
25223dwarf2_mark_helper (void **slot, void *data)
25224{
25225 struct dwarf2_per_cu_data *per_cu;
25226
25227 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
25228
25229 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25230 reading of the chain. As such dependencies remain valid it is not much
25231 useful to track and undo them during QUIT cleanups. */
25232 if (per_cu->cu == NULL)
25233 return 1;
25234
10b3939b
DJ
25235 if (per_cu->cu->mark)
25236 return 1;
25237 per_cu->cu->mark = 1;
25238
25239 if (per_cu->cu->dependencies != NULL)
25240 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
25241
25242 return 1;
25243}
25244
f504f079
DE
25245/* Set the mark field in CU and in every other compilation unit in the
25246 cache that we must keep because we are keeping CU. */
25247
ae038cb0
DJ
25248static void
25249dwarf2_mark (struct dwarf2_cu *cu)
25250{
25251 if (cu->mark)
25252 return;
25253 cu->mark = 1;
10b3939b
DJ
25254 if (cu->dependencies != NULL)
25255 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
25256}
25257
25258static void
25259dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
25260{
25261 while (per_cu)
25262 {
25263 per_cu->cu->mark = 0;
25264 per_cu = per_cu->cu->read_in_chain;
25265 }
72bf9492
DJ
25266}
25267
72bf9492
DJ
25268/* Trivial hash function for partial_die_info: the hash value of a DIE
25269 is its offset in .debug_info for this objfile. */
25270
25271static hashval_t
25272partial_die_hash (const void *item)
25273{
9a3c8263
SM
25274 const struct partial_die_info *part_die
25275 = (const struct partial_die_info *) item;
9a619af0 25276
9c541725 25277 return to_underlying (part_die->sect_off);
72bf9492
DJ
25278}
25279
25280/* Trivial comparison function for partial_die_info structures: two DIEs
25281 are equal if they have the same offset. */
25282
25283static int
25284partial_die_eq (const void *item_lhs, const void *item_rhs)
25285{
9a3c8263
SM
25286 const struct partial_die_info *part_die_lhs
25287 = (const struct partial_die_info *) item_lhs;
25288 const struct partial_die_info *part_die_rhs
25289 = (const struct partial_die_info *) item_rhs;
9a619af0 25290
9c541725 25291 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
25292}
25293
b4f54984
DE
25294static struct cmd_list_element *set_dwarf_cmdlist;
25295static struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
25296
25297static void
981a3fb3 25298set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 25299{
b4f54984 25300 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 25301 gdb_stdout);
ae038cb0
DJ
25302}
25303
25304static void
981a3fb3 25305show_dwarf_cmd (const char *args, int from_tty)
6e70227d 25306{
b4f54984 25307 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
25308}
25309
cd4fb1b2 25310int dwarf_always_disassemble;
437afbb8 25311
437afbb8 25312static void
cd4fb1b2
SM
25313show_dwarf_always_disassemble (struct ui_file *file, int from_tty,
25314 struct cmd_list_element *c, const char *value)
9291a0cd 25315{
cd4fb1b2
SM
25316 fprintf_filtered (file,
25317 _("Whether to always disassemble "
25318 "DWARF expressions is %s.\n"),
25319 value);
9291a0cd
TT
25320}
25321
9291a0cd 25322static void
cd4fb1b2
SM
25323show_check_physname (struct ui_file *file, int from_tty,
25324 struct cmd_list_element *c, const char *value)
9291a0cd 25325{
cd4fb1b2
SM
25326 fprintf_filtered (file,
25327 _("Whether to check \"physname\" is %s.\n"),
25328 value);
9291a0cd
TT
25329}
25330
cd4fb1b2
SM
25331void
25332_initialize_dwarf2_read (void)
9291a0cd 25333{
9291a0cd 25334
cd4fb1b2 25335 dwarf2_objfile_data_key = register_objfile_data ();
156942c7 25336
cd4fb1b2
SM
25337 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
25338Set DWARF specific variables.\n\
25339Configure DWARF variables such as the cache size"),
25340 &set_dwarf_cmdlist, "maintenance set dwarf ",
25341 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 25342
cd4fb1b2
SM
25343 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
25344Show DWARF specific variables\n\
25345Show DWARF variables such as the cache size"),
25346 &show_dwarf_cmdlist, "maintenance show dwarf ",
25347 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 25348
cd4fb1b2
SM
25349 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25350 &dwarf_max_cache_age, _("\
25351Set the upper bound on the age of cached DWARF compilation units."), _("\
25352Show the upper bound on the age of cached DWARF compilation units."), _("\
25353A higher limit means that cached compilation units will be stored\n\
25354in memory longer, and more total memory will be used. Zero disables\n\
25355caching, which can slow down startup."),
25356 NULL,
25357 show_dwarf_max_cache_age,
25358 &set_dwarf_cmdlist,
25359 &show_dwarf_cmdlist);
156942c7 25360
cd4fb1b2
SM
25361 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
25362 &dwarf_always_disassemble, _("\
25363Set whether `info address' always disassembles DWARF expressions."), _("\
25364Show whether `info address' always disassembles DWARF expressions."), _("\
25365When enabled, DWARF expressions are always printed in an assembly-like\n\
25366syntax. When disabled, expressions will be printed in a more\n\
25367conversational style, when possible."),
25368 NULL,
25369 show_dwarf_always_disassemble,
25370 &set_dwarf_cmdlist,
25371 &show_dwarf_cmdlist);
9291a0cd 25372
cd4fb1b2
SM
25373 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25374Set debugging of the DWARF reader."), _("\
25375Show debugging of the DWARF reader."), _("\
25376When enabled (non-zero), debugging messages are printed during DWARF\n\
25377reading and symtab expansion. A value of 1 (one) provides basic\n\
25378information. A value greater than 1 provides more verbose information."),
25379 NULL,
25380 NULL,
25381 &setdebuglist, &showdebuglist);
9291a0cd 25382
cd4fb1b2
SM
25383 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25384Set debugging of the DWARF DIE reader."), _("\
25385Show debugging of the DWARF DIE reader."), _("\
25386When enabled (non-zero), DIEs are dumped after they are read in.\n\
25387The value is the maximum depth to print."),
25388 NULL,
25389 NULL,
25390 &setdebuglist, &showdebuglist);
9291a0cd 25391
cd4fb1b2
SM
25392 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25393Set debugging of the dwarf line reader."), _("\
25394Show debugging of the dwarf line reader."), _("\
25395When enabled (non-zero), line number entries are dumped as they are read in.\n\
25396A value of 1 (one) provides basic information.\n\
25397A value greater than 1 provides more verbose information."),
25398 NULL,
25399 NULL,
25400 &setdebuglist, &showdebuglist);
437afbb8 25401
cd4fb1b2
SM
25402 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25403Set cross-checking of \"physname\" code against demangler."), _("\
25404Show cross-checking of \"physname\" code against demangler."), _("\
25405When enabled, GDB's internal \"physname\" code is checked against\n\
25406the demangler."),
25407 NULL, show_check_physname,
25408 &setdebuglist, &showdebuglist);
900e11f9 25409
e615022a
DE
25410 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25411 no_class, &use_deprecated_index_sections, _("\
25412Set whether to use deprecated gdb_index sections."), _("\
25413Show whether to use deprecated gdb_index sections."), _("\
25414When enabled, deprecated .gdb_index sections are used anyway.\n\
25415Normally they are ignored either because of a missing feature or\n\
25416performance issue.\n\
25417Warning: This option must be enabled before gdb reads the file."),
25418 NULL,
25419 NULL,
25420 &setlist, &showlist);
25421
f1e6e072
TT
25422 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25423 &dwarf2_locexpr_funcs);
25424 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25425 &dwarf2_loclist_funcs);
25426
25427 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25428 &dwarf2_block_frame_base_locexpr_funcs);
25429 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25430 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
25431
25432#if GDB_SELF_TEST
25433 selftests::register_test ("dw2_expand_symtabs_matching",
25434 selftests::dw2_expand_symtabs_matching::run_test);
25435#endif
6502dd73 25436}